PHP: Increase memory limit

Have you ever received a fatal error on your server similar to the one below when you tried to execute a script?

The fatal error shown below, indicates that your PHP configuration has a memory limit set and the script which you are executing is trying to allocate more memory than what is available for use.

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 8192 bytes) in /var/www/vhosts/domain.com/httpdocs/script.php on line 23

There is a solution to this though. You can increase the memory limit of your PHP configuration. There are two approaches though. One which is for shared hosting customers without shell or php.ini configuration file access and the second approach is for VPS or dedicated server customers who have the ability to edit their PHP configuration file.

Shared Hosting

Shared hosting customers need to insert a command into a .htaccess file, telling your web server that a PHP configuration value needs to be modified when PHP is loaded. Place this .htaccess file into the root directory of the specified domain. If you already have a .htaccess file, you can open it and place the command below the existent content of the file. Here is the command :

php_value memory_limit 64M

The command above will change your PHP memory limit configuration value to 64 megabytes of RAM available of PHP scripts being executed on that domain. You can change the 64 to any other value you wish to use. The default on a shared hosting account is usually about 8M in most cases.

Dedicated or VPS Optimized

In case you have a dedicated or virtual private server, you should have shell access and the ability to manually edit/change your PHP configuration file to set the values according to your needs. Follow the steps below to log in to your server via SSH, access your php.ini configuration file, change the memory_limit value, then save the file and restart your web server (Apache) for the changes to take place.

First off, log into your server via SSH. Change the “root” value to your SSH username and the “domain.com” value to either the IP address of the server or the domain.

ssh -lroot domain.com

Locate your PHP.ini configuration file. It will show you the location of your PHP.ini configuration file. In my case, it is “/usr/local/php/etc/php.ini” but it might be different on your box.

locate php.ini

With the location of your PHP.ini configuration file, open the file with VI

vi /usr/local/php/etc/php.ini

In VI, you need to find the text “memory_limit” inside the file. You can do that by hitting forward slash (/) and then typing “memory_limit”. Once the text has been found and it is highlighted, press the “i” key on your keyboard to go into INSERT mode. Use the keyboard arrows to navigate and change the line so it looks like this :

memory_limit=64M;

Again, as explained above in the “Shared Hosting” explanation, you can change the “64″ to any other preferred integer. If you need 128 megabytes of memory for scripts to allocate, change the value to “128M” and so on.

With the value changed, hit the “Esc” button on your keyboard to exit the INSERT mode. Then press “Shift” + “Q” + “W” to write the changes to the file and quit. With the file successfully saved, the last thing left to do is to restart your web server (Apache). You can do that with the following command :

httpd restart

On my Plesk dedicated server, it is :

/sbin/service httpd restart
  • Pingback: Parse This! » Blog Archive » The mysterious “blank” page in Wordpress admin

  • http://www.thermalimaginginfo.com David Harris

    Can anyone suggest a reliable Dedicated Server hosting that is not very expensive?’~:

  • http://www.light-sensors.com Austin Cook

    Does anyone know of a cheap but reliable web hosting company?’`.

  • http://personal-injury-insurance.info Timothy Heisserer

    Hello,just observed your web-site when i google something and wonder what webhosting do you use for your wordpress,the speed is more faster than my blog, i really need to know it.will back to check it out,thank you!

  • http://www.phtest.net Aimee Chapman

    Dedicated servers are the best when you want a stable webhost. ~.’

  • http://www.dryscalptreatment.info Sebastian Sanders

    there are many webhosting companies these days and most of them are overselling their products:;”

  • http://www.mirroredfurniturelab.com Leo King

    i always look for reviews about my webhosting server, if they have great review then i choose them~:”

  • http://www.carpetshampooerinfo.com Molly Moore

    dedicated servers are the best stuff that can host your traffic hungry websites:,;

  • http://www.kitchen-canisters.net Kitchen Canisters 

    the best webhosting provider should also provide 24 hour support and 99.99% uptime;,*

  • http://www.airpurifierfaq.com Air Purifiers :

    when it comes to webhosting, try selecting a webhost with Directadmin rather than Webmin control panel,~,

  • http://www.l-methionine.net L-Methionine Side Effects

    when you want to do some heavy content and traffic on your website, you will really need a dedicated server ,`;

  • http://healthydinnerrecipesreview.com Karen

    This is how my .htaccess file looked after adding the line at the end: (Is this correct?)

    # BEGIN WordPress
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]

    # uploaded files
    RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ – [L]
    RewriteRule . index.php [L]
    # END WordPress
    php_value memory_limit 64M

    I saved this then refreshed. But when I went back to my wp-admin, I got this error message: Internal Server Error :(

    Now, I can’t access my sites (both actual sites and wp-admins) with the domain name in question, all of them showing this internal error message. (Panic mode!!!)

    Would greatly appreciate help.please….desperately need help.

    Thanks in advance!

  • http://healthydinnerrecipesreview.com Karen

    Ok, I contacted my host and he told me that my memory limit is already 64M. I was also able to check it myself via phpinfo. I was also told that since I’m on shared hosting, I can’t go beyond 64M anymore.

    Now I am already able to access my sites and wp-admin. However, I still see this message whenever I add a new site using a new subdomain:

    fatal error: out of memory (allocated 66846720) (tried to allocate 35 bytes) in /home/maxlife/public_html/healthydinnerrecipesreview.com/wp-includes/meta.php on line 310

    How do I address this please?

  • http://healthydinnerrecipesreview.com/thedspreview Karen

    What’s the correct line to add in the .htaccess file to increase the memory limit?

  • http://healthydinnerrecipesreview.com/thedspreview Karen

    OK, my problem just got solved!!!

    Got it from this http://wordpress.org/support/topic/fatal-error-out-of-memory-messages?replies=24#post-1929111, thought I’d share it with you guys :

    
THIS TO BE ENTERED IN THE WP-CONFIG FILE WHICH IS IN THE ROOT OF THE WORDPRESS SETUP:
    ENTER AFTER:
define(‘ABSPATH’, dirname(__FILE__).’/');
    THE FOLLOWING:

    define(‘WP_MEMORY_LIMIT’, ’64M’);

    I ALSO CREATED A PHP.INI WHICH I UPLOADED TO THE PLUGIN FOLDER IN WP-CONTENT:

    memory_limit = 128M; Maximum amount of memory a script may consume (64MB)

    max_execution_time = 45;

    upload_max_filesize = 10M;

    post_max_size = 20M;

    Hope this helps!

  • http://tribulant.com Antonie Potgieter

    @Karen,

    The line to add to .htaccess is:

    php_value memory_limit 96M

    Replace “96M” with the memory limit you need.

    All the best,
    Antonie

  • Pingback: Steedan Crowe | Blog | Installing Jetpack with BuddyPress on Wordpress [not working]

  • Pingback: How and Why use set PHP Memory Limit – Thunder Chrome

  • http://www.animestatic.com/ Asch

    I tried this, but a while ago my host placed individual php.ini files in all my folders, do I have to edit each and every php.ini or is there a way to set one that will be used for everything?

    Thanks,
    ~asch.