Friday, July 2, 2010

PHP: Increase memory limit

SkyHi @ Friday, July 02, 2010

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 :