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/ 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 “” value to either the IP address of the server or the domain.

ssh -lroot

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 :


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


  1. […] 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. […]

  2. David Harris on May 19, 2010

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

  3. Austin Cook on May 24, 2010

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

  4. Timothy Heisserer on May 30, 2010

    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!

  5. Aimee Chapman on July 15, 2010

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

  6. Sebastian Sanders on August 2, 2010

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

  7. Leo King on September 29, 2010

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

  8. Molly Moore on October 11, 2010

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

  9. Kitchen Canisters  on October 14, 2010

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

  10. Air Purifiers : on October 25, 2010

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

  11. L-Methionine Side Effects on December 12, 2010

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

  12. Karen on February 6, 2011

    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!

  13. Karen on February 6, 2011

    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/ on line 310

    How do I address this please?

  14. Karen on February 7, 2011

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

    • Antonie Potgieter on February 14, 2011


      The line to add to .htaccess is:

      php_value memory_limit 96M

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

      All the best,

  15. Karen on February 8, 2011

    OK, my problem just got solved!!!

    Got it from this, thought I’d share it with you guys :

define(‘ABSPATH’, dirname(__FILE__).’/’);

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


    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!

  16. […] hours trying to get it to work. Finally after searching the forums and stumbling across this post: I was able to get it […]

  17. […] into php script.  The best practice would be embed in script, followed by .htaccess. Refrences […]

  18. Asch on September 12, 2011

    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?


  19. […] PHP: Increase memory limit – Lost In … – PHP: Increase memory limit Posted in PHP By Antonie Potgieter On August 5, 2008. … Dedicated or VPS Optimized. In case you have a dedicated or virtual private … […]


Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: