So, Your Host Didn’t Give You Enough Memory?

“Allowed memory size of 33554433 bytes exhausted”

Are you getting this error? It seems to be pretty standard for most hosting services to set their default php settings to 32M when it comes to the memory limit for php scripts. For some users of the most recent versions of popular CMS software like Drupal and WordPress, 32 megs is cutting it quite close, especially if many plug-ins are activated. This post is a solution directed to the problem of NGG users running WordPress 2.8 +, but of course, fixes any php memory issue.

Luckily, most hosts allow you to define your own php.ini configuration that can bypass the default settings by editing the value for memory_limit. However, your custom php.ini file must be placed in every single directory where php files are executed (this includes plug-in sub-directories). One could tediously copy the php.ini file to every single directory, but this will have to be done every time a new directory is added, or changes are made to the php configuration. If you have the ability to define cron jobs, here’s an easy way to automate the process:

  1. Create your own php.ini file (here’s mine as an example with memory_limit set to 128M)
  2. Place it in the root directory of your server.
  3. Define a cron job (this can be done through cPanel) to run once a day (or however frequently you want) with the following command (one line):
    ls -R | grep ‘/public_html’ | cat | sed -e ‘s/.///’ -e ‘s/:///’ -e ‘s/ /\ /g’ -e ‘s/^/cp php.ini /’ | sh

And, that’s it! The cron command will copy the php.ini file located in your root directory to every single subdirectory located in your public_html folder. If you store your website elsewhere, simply change the ‘/public_html’ string to the path where your website is stored.

Note: Ensure your php.ini works by testing it in just your website root directory before running the cron command.

Hope this helps. If you have another suggestion, post it in a comment below!

Leave a Reply