Rachel H Kay Blog

WordPress Memory Exhausted, Increase Memory Limits?

Recently I received notification on a client’s website that the site was down. I like these notifications because I can’t keep an eye on every client’s site 24/7, I’m only human. But I was surprised to see that the site was serving a 500 internal server error. So I had to look into what could be causing the issue right away.

Redirect Visitors When There Is A Problem

But first I needed to get a maintenance page loaded so that any visitors to the site won’t see the 500 internal server error page and instead just see that the site is temporarily under maintenance. You do not want to leave a site with the 500 internal server error page visible. Since WordPress was out for the count my usual maintenance pages were a no go. Instead I had to manually create a maintenance page and force the server to show that to visitors instead.

Forcing A Redirect To An HTML Maintenance Page

In order to have a maintenance page to redirect the server to I needed a simple HTML page that just states that the site is down for maintenance. Nothing fancy. But it did need to have the client’s logo, contact information, color scheme, and clearly state that the site is under maintenance. I keep a copy for any client’s site, and all I have to do is change the logo, contact info, and background color overlay so I can quickly adapt it to any client’s site.

That’s the simple part. The next step is creating an htaccess file that forces the redirect to that HTML page. So, open your text editor, I prefer to use Sublime Text, and paste the code below into your document.

        # MAINTENANCE-PAGE REDIRECT        <ifmodule mod_rewrite.c="">      RewriteEngine on RewriteCond %{REMOTE_ADDR} !^        RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]        RewriteCond %{REQUEST_URI} !.(jpe?g?¦png¦gif¦css¦otf¦eot¦svg¦ttf¦woff) [NC]       RewriteRule .* /maintenance.html [R=302,L]       </ifmodule>

The REMOTE_ADDR needs to be set to your IP address. This will allow you to view the site as it is instead of seeing the maintenance file. That way you can keep the maintenance file visible until you are sure you have fixed the problem while also being able to check and see if any of your fixes to the issue have worked.

Then save the document as htaccess.txt, and then re-save it as .htaccess. You will have to verify that you want to save a file beginning with “.”, so just make sure you click to verify the save.

Now this will make the .htaccess file invisible unless you have hidden files visible on your computer, but no worries, if you use an FTP client you will be able to find the file without an issue. I like to keep a .txt version so that I can easily find and edit the file if needed on my computer. Then I just update the file again saving it as both htaccess.txt and .htaccess and upload that to the server.

The easiest way to do this is to use FileZilla. I just connect to the website's server, go into the public_html folder, which is the root directory, and upload the .htaccess file. If there is already a .htaccess file you can just rename the current one to something like .htaccess.current, making the new .htaccess with the redirect the file the server reads and uses.

Now when someone visits the site they will see the maintenance page rather than the 500 internal server error page.

Figuring Out The Issue

My next step was to figure out what the problem was. First I checked the error log, which can be found in the public_html folder. I wanted to see what the error was for the date that I got the notification for. What I saw was the error.

PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 4304 bytes) in /public_html/wp-content/plugins/plugin/plugin-file.php on line xxx

While it may be showing a particular plugin that could be causing the issue, the important part of that line to pay attention to is the “allowed memory size” and “exhausted”. This means that the amount of memory running on that server isn’t enough and the memory has been exhausted causing the 500 internal server error.

Increasing The PHP Memory Limit in WordPress

You can manually increase the memory limit, and it isn’t very hard to do either. You just need to edit the wp-config.php file on your WordPress website. In order to do this you will need to access the site through an FTP client, like FileZilla, or you can also use the file manager in your web hosting control panel, but I prefer FileZilla.

The wp-config.php file is located in the root directory of your website, public_html. If you are using an FTP client, like FileZilla, you just need to right click on the wp-config.php file and choose view/edit. This will open a local copy of the file on your computer that you can then edit.

Scroll down the file until you see the line “That’s all, stop editing! Happy blogging.” Then paste the code below just before that line.

        define( 'WP_MEMORY_LIMIT', '512M' );   

This will tell WordPress to increase the PHP memory limit to 512MB. You can increase the memory limit to what ever you need of course.

When you're done, save the file and close it. If using an FTP client, like FileZilla, you will get a pop up box that wants you to verify you are making changes to the file on the server. Just click yes. You can also select to remove the temporary file from your computer as well. Now you have updated the wp-config.php file on your website’s server.

To check and make sure it worked go to your web browser, delete the browser cache, and load your website. You should see a working version of your site while everyone else sees the maintenance.html file.

Now you can remove the .htaccess that forces the redirect to the maintenance.html file, change your site’s .htaccess from .htaccess.current back to .htaccess, and you are now good to go with a working, functional site once again.

Increasing Memory Limit Doesn't Work

Most of the time with these internal server errors increasing the PHP memory limit does the trick, but sometimes, on rare occasions it isn’t the root of the problem. If you run into this issue and increasing the memory limit does not fix the internal server error on your website there are two other causes to look into.

Possible Plugin Issue

It could be that one of your plugins is the culprit. Since you can’t access your website just use an FTP client, like FileZilla, navigate to the root directory, public_html, then go into the wp-content folder, and then the plugins folder.

In order to check for any plugins that could be causing the internal server error, you need to manually disable them. You can do this by renaming each one from plugin-name to plugin-name.old. If using an FTP client, just right click on the plugin and choose rename. This will disable the plugin. You will need to disable all of them. Then check your website to see if it’s working again. If it is then you will need to manually enable each plugin by removing the .old from the filename, and refresh your website after each plugin is enabled. By doing this you will be able to find out which plugin is causing the internal server error.

Once you have found the culprit, you will need to contact the plugin author who will be able to help you with the issue. If you don’t need the plugin for anything major you can just keep it disabled until the plugin author gets back to you with a solution and remove the htaccess file with the redirect to the HTML maintenance page to bring your site online, just remember to change your site’s htaccess from .htaccess.current back to .htaccess after you delete the temporary htaccess file.

HTACCESS Is Corrupted

If none of your plugins turn out to be the culprit causing the internal server error, on rare occasions the htaccess file can become corrupted, also causing an internal server error.

In order to see if your htaccess is causing the issue you will need to disable it. You can do this through an FTP client, like FileZilla, by navigating to the root directory, plublic_html, and renaming your .htaccess file to .htaccess.old, or whatever you want to rename it to. Than you need to upload a clean htaccess file.

Open your text editor, I use Sublime Text, and add this bit of code to the new htaccess file.

        # BEGIN WordPress        <IfModule mod_rewrite.c>        RewriteEngine On        RewriteBase /        RewriteRule ^index.php$ - [L]        RewriteCond %{REQUEST_FILENAME} !-f        RewriteCond %{REQUEST_FILENAME} !-d        RewriteRule . /index.php [L]       </IfModule>        # END WordPress    

Then save your file as htaccess.txt and .htaccess, open your FTP client, navigate to the root directory, public_html, and upload the new htaccess file. Then clear your browser’s cache and reload your website. You should have a functional website now.

If there was anything in your old htaccess file that you need to protect your site, or anything else you were using it for, then just copy and paste those bits of code into your new htaccess file one by one, saving the file on your server, and refreshing your site to make sure it is still up and running.

The easiest way to add those bits of code to your htaccess file is through your FTP client. You just right click on the htaccess file, choose view/edit, copy and paste the bits of code, save the file, reload your site, and if everything is still up and running then you're good to go!

Apache 2.4 Deprecated Code

One of the main reasons I like to use htaccess for my client’s sites as well as my own, is to help protect it from potential hackers. A very useful bit of code I use on every website is to block IP addresses from malicious activity, such as someone trying to hack the website.

The code below has helped me block malicious activity and prevent hack attempts from succeeding.

        #Block IP address        <Limit Get Post>        order allow,deny        deny from        allow from all        </Limit>    

Or you can simplify this code to:

        order allow,deny        deny from        allow from all   

But what happens when your site’s server runs Apache 2.4? This code is deprecated in Apache 2.4. In case you run into this issue the code below will work to block IP addresses:

        <RequireAll>        Require all granted        Require not ip        </RequireAll>   

Also, here is some documentation if you would like to read more about it in the Apache docs.

Hopefully one of those solutions helped to fix the internal server error on your website. If so please let me know below in the comments. Or if you’ve found other causes to the white screen of death and 500 internal server errors please let me know below in the comments.

Posted in:
comments powered by Disqus

About Me

Hi there. I'm Rachel Kay, a Web Developer, Illustrator, & Designer, whose hobby is to be creative and artistic, while freelancing as a Web Developer building creative, modern websites.