PHP: Increase memory limit

Tue, Aug 5, 2008

PHP

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
, , , , , , , , , , , , ,
468x60gif

This post was written by:

Antonie Potgieter - who has written 57 posts on Lost-In-Code.

I (Antonie Potgieter) am a software engineer/web developer located in South Africa. My full-time work is the management of Tribulant Software and the development of its software packages.

Contact the author

5 Comments For This Post

  1. David Harris Says:

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

  2. Austin Cook Says:

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

  3. Timothy Heisserer Says:

    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!

  4. Aimee Chapman Says:

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

  5. Sebastian Sanders Says:

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

1 Trackbacks For This Post

  1. Parse This! » Blog Archive » The mysterious “blank” page in Wordpress admin Says:

    [...] So short story shorter, check your memory limit! I would just like to add that to the long list of things to check.  Here’s a good article on changing memory limit. [...]

Leave a Reply