PHP Memory_Limit
The PHP Memory_Limit is the amount of memory a given process can use on your server. Any time a client makes a request to your webserver and PHP handles it, it begins a PHP process. If you exceed the set memory limit, the process will fail and Apache will report it as an HTTP 500 error.
Why Set the PHP Memory_Limit?
The most common reasons why you would change the memory limit are listed below.
- Looking into lowering the limits due to the server swapping to disk or crashing.
- Raising the limit because a page or php script is giving the memory_limit error due to not enough memory
- If you are raising the limit, you will need to determine whether to raise the “Global” memory limit for the entire server or the local limit by adjusting the PHP handler for a site or folder. The first step is to adjust the memory limit at the highest level, or “global” limit and test the broken page until it works.
- Once you know how much memory is required, you can troubleshoot to decide if it is safe to leave on the global level or if it needs to be set locally or by folder.
Warning: |
| There are many security/stability considerations that should be researched before changing any of the settings in the php.ini or .user.ini file. These files contain all the default PHP settings for your server. Many times, when adjusting the memory_limit, other PHP configurations will need to be modified in order to increase the limit. Additionally, you will want to ensure that the server has enough resources to accommodate the higher memory limits. |
Setting a “Global” memory_limit
- The memory_limit setting here effects all php scripts, but can be overridden in many circumstances.
- This will set a global default for the entire server (per PHP version).
Note:
EasyApache 4 supports multiple PHP versions. This means in order to set a “global” PHP memory_limit, you must update each version of PHP separately in order to increase the memory_limit for all scripts and users. For servers with cPanel, this can be set within WHM under: Home -> Software -> MultiPHP INI Editor
You can quickly check the current handler from the command line using:
/usr/local/cpanel/bin/rebuild_phpconf --current
Setting a Local memory_limit
memory_limit Locations by PHP Handler
If you are setting the memory_limit for a specific website default or folder level default, there are several places where the PHP memory_limit will be set. It all depends on the PHP handler, below is a listing of the handler and the file path.
EasyApache 3
- suPHP – /home/$websitename/public_html/php.ini
- CGI/FCGI – /home/$websitename/public_html/.user.ini
- DSO – /home/$websitename/public_html/.htaccess
EasyApache 4 (For servers with cPanel, this can be set within WHM under: Home -> Software -> MultiPHP INI Editor)
- CGI – /home/$websitename/public_html/.user.ini
- FPM-PHP – /home/$websitename/public_html/.user.ini
- suPHP – /home/$websitename/public_html/php.ini
- DSO – /home/$websitename/public_html/.htaccess
Why set the memory_limit at the local level?
- This is useful for setting a website level default or folder level default that is higher than you want to set globally.
- Because the .htaccess is a configuration override for Apache, if you are using mod_php (aka DSO to cPanel), then PHP is running inside of Apache so PHP can take configuration options. You can find these configuration changes in the PHP Manual.
- If you are using SuPHP/FCGI/CGI/PHP-FPM, then you are running PHP as an external program to Apache and it will not understand what to do with the pvp_value settings. Instead, you will use a local php.ini file. For example: /home/user/public_html/php.ini
Note:
Keep in mind that the local php.ini only effects PHP scripts in its folder and not in subfolders. - The local php.ini only affects PHP scripts in its folder, not in subfolders. SuPHP does have a way around that by creating a custom php.ini file which makes the change recursive and will allow the memory_limit to be set once for an entire website.
Setting a memory_limit in the Script
You will use ini_set to set a memory_limit for an individual script, not affecting any other limits on the site.
- This is useful for single scripts that require a lot of memory where you don’t want to raise it that high globally or on a folder level.
- You may want to refer to the PHP Manual for more information on using this functionality in a script. Since scripting and coding are outside our scope of support, you’ll want to work with your developer on implementing this setting in your script.