Apache "This page isn't working" with a HTTP 500 status.....


I was using an Apache web server with some healthy PHP installed to run a website module, however with the base build I was upload certain attachments of lists of data as I was prevented by this error:


Edge provided me this error as that at the time was my weapon of choice at the time (apologies Chrome is such a better browser, Chrome for life!) - so they only evidence I had was when uploading or interacting with large amounts of data, once the event has failed the server seemed to be very unreliable randomly in the future, and I was trying to figure out why.

The first port of call here is the Apache error log which you can get with this command:

tail /var/log/apache2/error.log

That log showed the below which looks very much like memory exhaustion to me from the section highlighted in bold:

[Wed Jul 03 18:21:19.940028 2024] [php7:error] [pid 967] [client 10.245.161.136:43608] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/Cachet/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 389, referer: https://status.bear.local

This would mean we seem to in PHP we are trying to allocate more memory that is allowed, so I think we need to increase this, I am using PHP 7.4 in this example, and it would appear that the default memory allocation limit for that version is 128MB however we are trying to allocate 134MB so this is output normal operating parameters.

Right so we need more allocation in the php.ini so lets open that up with this:

sudo nano /etc/php/7.4/apache2/php.ini

Then we need to from within Nano use Ctrl+W to search and look for "memory" and find this section:

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 128M

We need to update the last line to this:

memory_limit = 256M
 
Now we have update the PHP file we need to restart Apache to ensure we get the updated version in the running configuration with this command:

sudo systemctl restart apache2

If you check the error log again you should see this, which signals Apache is back online:

[Wed Jul 03 19:51:28.367471 2024] [mpm_prefork:notice] [pid 1945] AH00163: Apache/2.4.41 (Ubuntu) OpenSSL/1.1.1f configured -- resuming normal operations

This means we are ready to try the request again and upon a retry the request now works, however I wonder if I use a larger file will I get the error back with the HTTP 500 and be back to where I was before.

Yes, it would appear so, when the size of the data increases the limit it still not high enough, however this time it has moved to the dashboard login this time, not the root site as you can see below:

[Thu Jul 04 08:24:56.031238 2024] [php7:error] [pid 4443] [client 10.245.186.191:59401] PHP Fatal error:  Allowed memory size of 805306368 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/Cachet/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php on line 199, referer: https://status.bear.local/auth/login

This therefore means we need to head back to the PHP file and amend it to look like this and restart Apache one again:

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 2048M

sudo systemctl restart apache2

If  you check the error log again you will see the restart event first, then the event the indicate that startup is complete, shown below:

[Thu Jul 04 08:29:01.642016 2024] [mpm_prefork:notice] [pid 1974] AH00169: caught SIGTERM, shutting down
[Thu Jul 04 08:29:02.146238 2024] [mpm_prefork:notice] [pid 4467] AH00163: Apache/2.4.41 (Ubuntu) OpenSSL/1.1.1f configured -- resuming normal operations

Previous Post Next Post

نموذج الاتصال