How to change your php memory_limit
ok, what happened?
If you build websites, at one point or another you will install or run a script and get a blank white screen. Chances are, you will have this tell-tale look on your face.
Never fear, you have most likely just hit the memory limit on your server. Memory limits are very important to have. They help to keep scripts from running out of control or using up all of your free memory. On the other hand, sometimes you are adding a rather fancy or elaborate feature to your site, that simply requires slightly more memory then the default 8mb that Apache
gives you. Let’s show you how to fix that.
First, before we get too far, these instructions assume your server is running Apache and you either are on a VPS, or a dedicated server. If you are on a shared host, this might still be possible, but it is dependent upon how your host has your account setup. It may or may not be easy to change. If you are unsure, submit a ticket to your webhost. You are paying for their service, there is nothing wrong with asking them.
Now, let’s get rolling. You will want to be able to see what your memory limit is easily. If you are on Joomla, you can login to the backend and go to:
Help>System Info>Php Information
Search the page for “memory_limit”. You will see something similar to this. (yours will possibly say 8m)
If you do not have a Joomla install ready, you can also create a file, put it in your public_html directory and insert this code:
Name the file something like phpinfo.php, (it must end in .php) and access it. You will now see your servers php info. It is very important that you do not leave this file and delete it when done. Leaving this file in place is a huge security hole.
Now that we know what our php memory limit is, let’s get to changing it.
Many guides for changing your memory limit on the internet are written for people with a dedicated server and presume you want your entire server, and all it’s enclosed websites to have a higher memory limit. This is less then ideal, but I will explain who to do it anyhow.
Edit your php.ini file.
Open your php.ini file. YMMV
, but it will most likely be located at:
Search for memory_limit and change the value from 8m, to 16mb or 32mb. (don’t go much higher)
Save the file, then restart Apache via ssh using this commend or whatever method you prefer.
Check your phpinfo now, and you should be fixed.
Do as I say, not as I foo.
Now, if you have more then one website on your server you most likely do not want to raise your memory limit globally. You would want to do it on a per-site basis. To do it this way, you will make a change in your websites public_html directory. This is called a override.
Not all webhosting companies are the same. There are two ways you can do this.
A: .htaccess override.
Open up, (or create) a file in your public_html directory called .htaccess. Inside this file, add the line
php_value memory_limit 16M
Save it, and check your phpinfo again. No restart is required for this.
B: php.ini method
If .htaccess didn’t work, some webhosts will have you put a file called php.ini in your public_html directory instead. Inside that file, insert the line:
memory_limit = 16M
Save it, and check your phpinfo again.
Use one or the other of these methods. Typically one of these methods will make your site 500 error if your webhost does not allow it. Make sure you check your site after every change you make. If your site 500′s, then your host does not permit the method you tried, and revert your changes.
500 error. No bueno!
There. Now you have raised your php memory limit to be able to handle heavier scripts. Do a little dance, have a beer. Celebrate.
Tags: hosting, technology