Publisher does not support the Fluid field type. Please do not contact asking when support will be available.

If you purchased an add-on from expressionengine.com, be sure to visit boldminded.com/claim to add the license to your account here on boldminded.com.

Ticket: 500 error when trying to access cached page on front-end

Status Resolved
Add-on / Version Speedy 1.9.1
Severity
EE Version 7.4.5

DSite

Mar 27, 2024

Hi Brian,

I am back with another puzzling 1.9.0/1.9.1 issue… I’m getting a 500 error on the front end when trying to access cached versions of a page. I know those are the worst, but maybe you can see what I’m missing.

I have a site that is working properly with 1.8.1. To generate the error, I do the following: 1. Replace the 1.8.1 add-on files with 1.9.0/1.9.1 (I’ve tried both) 2. Update the add-on in the add-ons section 3. Delete the /html/static directory 4. Regenerate it from the Speedy control panel page

The first time I visit a page, it appears properly. When I refresh the page, I get the 500 error.

Speedy does generate the static version of the page. I can see it in the CP and in the static directory. As far as I can tell, those cached versions seem to be as expected. But something is not connecting to the static version on the second page load.

I’ve turned on debugging, but that obviously doesn’t help much with a 500 error.

When I revert to 1.8.1, everything works as expected.

Again, I know that this is likely not enough information. Aside from turning off other add-ons to look for conflicts, is there a troubleshooting step that I’m missing? I’ve seen one or two 500-error-related tickets in the last couple of months, but I can’t tell if you’ve made suggestions in the private messages that might help.

#1

BoldMinded (Brian)

Check your php error log on the server, or try this https://gist.github.com/litzinger/5598641

#2

DSite

Thanks, Brian.

Here is what I see in the PHP logs:

[Thu Mar 28 13:55:55.613596 2024] 
[proxy_fcgi:error] [pid 788089:tid 140276114912832] [client xxx.xxx.xxx.xxx:0] 
AH01071: Got error '
PHP message: PHP Warning:  Undefined array key "DB_HOSTNAME" in /home/my_directory/webapps/mysite/system/user/config/config.php on line 14; 
PHP message: PHP Warning:  Undefined array key "DB_DATABASE" in /home/my_directory/webapps/mysite/system/user/config/config.php on line 15; 
PHP message: PHP Warning:  Undefined array key "DB_USERNAME" in /home/my_directory/webapps/mysite/system/user/config/config.php on line 16; 
PHP message: PHP Warning:  Undefined array key "DB_PASSWORD" in /home/my_directory/webapps/mysite/system/user/config/config.php on line 17; 

PHP message: PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /home/my_directory/webapps/mysite/system/ee/legacy/database/drivers/mysqli/mysqli_connection.php:111
Stack trace:
#0 /home/my_directory/webapps/mysite/system/ee/legacy/database/drivers/mysqli/mysqli_connection.php(111): PDO->__construct()
#1 /home/my_directory/webapps/mysite/html/static/utilities/Csrf/SpeedyDatabase.php(74): CI_DB_mysqli_connection->open()
#2 /home/my_directory/webapps/mysite/html/static/utilities/Csrf/SpeedyCsrf.php(32): SpeedyDatabase->__construct()
#3 /home/my_directory/webapps/mysite/html/static/my_site/static/my_channel/my_entry/index.php(32): SpeedyCsrf->__construct()
#4 /home/my_directory/webapps/mysite/html/static/my_site/static/my_channel/my_entry/index.php(135): Speedy_be14828973acffa96639003961ed9059->__construct()
#5 {main}
  thrown in /home/my_directory/webapps/mysite/system/ee/legacy/database/drivers/mysqli/mysqli_connection.php on line 111', referer: https://mysite.com/referring_page

I’m not super fluent in stack trace, but it looks like Speedy is trying to connect to the database sever and failing because EE can’t load my environment variables in .env.php?

If that is roughly correct, it’s puzzling to me in that EE has no issues connecting to the database for the initial page load or for the admin.

Do you have a clearer idea of what might be happening here and how I might go about fixing or troubleshooting this further?

#3

BoldMinded (Brian)

When using static caching, and making it support the CSRF token it needs to connect to the database directly and not to through EE, but it looks like I didn’t account for loading config values from env vars…. something EE does when it boots up. I’ll have to take a look at this and find a fix. For now though you’ll need to get your db connection info into the config directly for it to work :(

#4

DSite

Thanks. I just verified that adding the database connection info to config.php resolves the issue.

For what it’s worth, I’ve used Speedy successfully with env vars for a while now. 1.8.1 seems to handle them successfully. It’s only the newer builds that trigger the error for me.

If there’s anything I can do to help you troubleshoot or test further, I’m happy to help.

#5

BoldMinded (Brian)

Right, the newer build is what fixes the CRSF issue by making the DB connection. Try this. Open the SpeedyDatabase.php file in the speedy/Service/Csrf folder (this is what gets copied to the utilities folder). On line 55 after the include $configPath line add this:

try {
            if (file_exists(SYSPATH .'../.env.php')) {
                $dotenv = ExpressionEngine\Dependency\Dotenv\Dotenv::createImmutable(SYSPATH .'../', '.env.php');
                $dotenv->load();
            }
        } catch (\Exception $e) {

        }

I haven’t tested this so I’m just speculating, but it might fix it. You’ll need to regenerate your utilities folder.

#6

BoldMinded (Brian)

Disregard this comment

#7

DSite

I’m still getting the same error after adding the code and regenerating the utilities. I’m assuming I applied it correctly, but just in case:

include_once $systemPath . 'ee/ExpressionEngine/Service/Database/Connection.php';
        include $configPath;

        try {
            if (file_exists(SYSPATH .'../.env.php')) {
                $dotenv = ExpressionEngine\Dependency\Dotenv\Dotenv::createImmutable(SYSPATH .'../', '.env.php');
                $dotenv->load();
            }
        } catch (\Exception $e) {

        }

        // Take defaults from EE core, merge the site's db config into it, and create a db connection.
        $dbConfig = array_merge([
            'port' => 3306,
            'hostname' => '127.0.0.1',
            'username' => 'root',
            'password' => '',
            'database' => '',
            'dbdriver' => 'mysqli',
            'pconnect' => false,
            'dbprefix' => 'exp_',
            'swap_pre' => 'exp_',
            'db_debug' => true,
            'cache_on' => false,
            'autoinit' => false,
            'char_set' => 'utf8',
            'dbcollat' => 'utf8_unicode_ci',
            'cachedir' => rtrim($appPath, '/') . '/user/cache/db_cache/',
        ], $config['database']['expressionengine'] ?? []);

        $this->connection = new Connection($dbConfig);
        $this->connection->open();

        // Unset what we loaded, just in-case.
        unset($config);
#8

BoldMinded (Brian)

Comment has been marked private.

#9

DSite

Comment has been marked private.

#10

BoldMinded (Brian)

Comment has been marked private.

#11

DSite

Comment has been marked private.

#12

BoldMinded (Brian)

Cool! I’ll get that fix into the next release.

Login to reply