You are here: Home » Website Maintenance » Dealing With PHP Memory Issues For Self-Hosted WordPress Blogs

Dealing With PHP Memory Issues For Self-Hosted WordPress Blogs

by Dave Doolin on July 29, 2009 · 8 comments

(Reading time: 5 – 8 minutes)

Once you’ve installed a few blogs, you are going to run up against a PHP “out-of-memory” error.

They look something like this:

[02-Jul-2009 22:41:54] PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 7680 bytes) in /home2/equalit6/public_html/wp-includes/kses.php(1005) : runtime-created function on line 1

This php memory error is usually not too difficult to fix…

Fixing your locally hosted WordPress installation

If you’re running your own, personal PHP installation, it’s really simple: open up your php.ini file, increase the allowable memory limit. For example, here’s what it looks like in my localhost php.ini:

300
301
302
303
304
305
306
307
;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;
 
max_execution_time = 30     ; Maximum execution time of each script, in seconds
max_input_time = 60	; Maximum amount of time each script may spend parsing request data
;max_input_nesting_level = 64 ; Maximum input variable nesting level
memory_limit = 128M      ; Maximum amount of memory a script may consume (128MB)

Line 307 is the one you want to change. Because I’m running locally, I set it to a high level, 128M of memory. If you’re on a hosted server, you probably won’t be able to get away with setting it this high.

Investigating your PHP configuration

WP Security plugin shows current PHP memory limit

WP Security plugin shows current PHP memory limit

There’s several ways to find your PHP memory limit. One way is to use the phpinfo(); function in a file, then load that file. This technique is extremely easy to find on Google, so I won’t waste our time here writing a superfluous explanation.

Another way, which should be much easier for you since you have installed the Website In A Weekend plugin security suite is to open the main page to the WP Security plugin. On the right side of the main page, several relevant parameters for your WordPress installation are listed out. The one we care about is in the green box. You can see it’s set to 64M. So far, that’s been enough for this blog.

If you have PHP memory trouble with your WordPress blog, I bet that number is 32M, or maybe even 16M. Too low. You need to increase your memory.

Increasing Bluehost’s PHP memory limits

Bluehost sets the PHP memory limits to a default value, 32M. When this isn’t enough, you have to increase it. I’m going to show you how to do that.

I’m using Bluehost as an example, but these instructions should work with any host that uses cPanel for administration. If your host doesn’t use cPanel, with a little thought, you should be able to figure out a procedure leading to the same result.

Your first step is firing up cPanel, and clicking on the PHP configuration icon, as shown in the screenshot below:

cPanel menu item for configuring PHP

cPanel menu item for configuring PHP

Now, you should be at a page that gives you a choice of several PHP configuration options. By default, the top radio button should be checked. You want the second radio button instead, “PHP5 (Single php.ini),” as denoted by item [1]. Then click item [2] to save the change in configuration. After the page reloads, click item [3] to write your php.ini in your public_html directory.

Change PHP configuration for your website

Change PHP configuration for your website

You’re not done yet!

Follow the instructions at the beginning of this article to change the value of the memory. You can do this by downloading the php.ini.default file, renaming it to php.ini, changing the memory value using a text editor on your computer, then uploading and overwriting the file written by the host. Or you can use the built-in cPanel file editor. Doesn’t really matter. Make a backup before you change anything!

A few notes to keep in mind:

  • When you change your parameters in your php.ini file, you may need to restart the webserver to force a reload. You may not… it depends on how Apache is configured. I restart Apache anyway on localhost.
  • On a hosted server, you may need to do a little research to figure what’s necessary for WordPress to use the increased value in it’s PHP scripts. According to years old threads in Bluehost’s forums, Bluehost’s servers are “smart” enough to figure out that php.ini files have been changed, and to reload without requiring an Apache restart. My experience is mixed… sometimes the changes work, but only after a while… cause and effect is difficult to establish.

Directing WordPress to use more memory

This tip is straight out of the WordPress Codex: use a WordPress directive to increase PHP memory. It looks like this in your wp-config.php file:

1
define('WP_MEMORY_LIMIT', '64M');

I’ve included this tip in this post because it’s simple and will save you time. Read the Codex discussion on wp-config.php here when you get a moment or two.

What if this isn’t enough?

In rare cases, this may not be enough. You may need to dig deeper into your WordPress installation and change individual files. Jeff Starr of Perishable Press has an excellent overview of how to accomplish this more advanced technique, when necessary. Here’s two articles you can take a look at:

  1. Improve Site Performance by Increasing PHP Memory for WordPress
  2. WordPress Error Fix(?): Increase PHP Memory for cache.php

>>>NOTE: Read these article carefully to ensure you know what you’re doing! Meddling with the .htaccess files can get you a 500 Server Error, so watch your logs at the same time.

“I’m still running out of memory!”

Ok, leave a comment explaining the problem as best you can. I may be able to help.




Would you like more? Send me a letter...
"Hi Dave,
Website In A Weekend seems pretty cool. I'm serious about this WordPress and web stuff, and I'd like to keep up with it. My name is and my email address is . I'm comfortable with email newsletters. I know you will protect my privacy, and that I can unsubscribe at any time. "

{ 8 comments }

Josh July 29, 2009 at 4:38 pm

Hehe, I could have used this timely post about 2 weeks ago just before I attempted to update Wordpress to version 2.8.2. I got stuck on the upgrade step essentially stranding me on 2.8.1 until I fixed the memory issue.

I’m a programmer so it wasn’t too hard for me to figure out what the problem was and how to fix it, but for someone new to Wordpress this could be quite worrying for them.

Nice article, and well written.

Dr Wordpress! July 29, 2009 at 5:31 pm

Thanks!

This is another one of those issues where I still cross my fingers every time I tackle it. Sometimes the usual works, sometimes I have to go rooting around in individual php files.

ZoSo November 18, 2009 at 2:04 pm

Wow! I am NOT a coder/programmer at all, but your instructions (which I followed to a tee) worked on my first attempt! I still have no idea what I just finished changing, but I just finished upgrading WP to 2.8.6 successfully on BlueHost (no more `memory error` messages!). Anyway, I really appreciate your effort with this simple tutorial. Thank you very much for saving me a LOT of trouble! Cheers!!

Dr Wordpress! November 18, 2009 at 2:09 pm

I just released the Whitepaper for this article:
http://website-in-a-weekend.net/wordpress-whitepapers/ww5-handling-wordpress-php-memory-problems-wordpress/

Glad it worked out for you. These issues can be really, really tricky sometimes.
Dr Wordpress!´s last blog ..Website In A Weekend Whitepapers – Series I My ComLuv Profile

Bill December 23, 2009 at 1:37 pm

Thanks for the tutorial. I had been stuck with an older version of wp on bluehost for a while.

It took a bit of searching to discover that php.ini.default is placed in the public_html folder. Once I figured that out, it was easy to open php.ini in a text editor. Thanks for the help!
Bill´s last blog ..WordPress Social Media Plugins That I Love My ComLuv Profile

Dr Wordpress! December 23, 2009 at 1:40 pm

Bill, glad you found it useful. This kind of stuff just frustrates me incredibly when I can’t figure it out. Thanks for the RT.
Dr Wordpress!´s last blog ..Be a Guest Author on Website In A Weekend My ComLuv Profile

Valerie Cudnik April 23, 2010 at 6:49 pm

Nice simple explanation. Wish I knew how to restart the Apache server on localhost. BlueHost is horribly inconsistent on whether those changes take effect immediately. Makes me a bit crazy since I develop WordPress blogs for a living.
Valerie Cudnik´s last blog ..Computer Design Graphics on the Web My ComLuv Profile

Dave Doolin April 24, 2010 at 8:49 am

Valeria, if Bluehost continues on their current trajectory, I wouldn’t be surprised if they start offering more advanced capabilities such as being able to have Apache reload configuration files on the fly.

Thanks for your encouragement.
Dave Doolin´s last blog ..3 Ways Daily Blog Posting Benefits You (and your readers) My ComLuv Profile

Comments on this entry are closed.

Previous post:

Next post: