How to increase or reduce the PHP memory limit in cPanel
  John Mwaniki /   20 Jan 2022

How to increase or reduce the PHP memory limit in cPanel

In this article, you will learn what PHP memory limit is, and how you can increase or decrease it for your website in the hosting cPanel account.

Any time a request is sent to a PHP script on your web server, the PHP interpreter starts a PHP process and allocates memory for the script.

The job of this PHP process is to generate HTML pages to send back to the client or to processes the request if it requires backend processing. This PHP process provides a runtime to the PHP script that should be doing that.

This memory is allocated to the PHP script in small chunks of a fixed amount. When all the allocated memory has been used by the script, the interpreter allocates another chunk.

If for instance there is an error in your PHP script, like an infinite loop that keeps asking for more and more RAM, the entire server may get out of memory and go down.

To avoid such a scenario, PHP has a limit on how much memory a PHP process can ask the system for before it gets killed. Of course, the server RAM is much, much more than the PHP memory limit.

The memory limit can be defined as the maximum amount of memory that a process can use.

The memory limit is per-process setting. So for instance, if every PHP process is set to take up to 120MB of memory, if there are 5 concurrent requests, they can use up to 600MB memory.

The memory limit can be increased or decreased in your website hosting cPanel (which we will cover below).

Though it may seem like a good solution to just increase the memory limit to the maximum, it's not a good decision. Memory can easily be misused due to bugs, poorly written code such as infinite loops which can consume gigabytes of memory per single request.

That is why it is advisable to always set the memory limit to as low as possible to help prevent poorly written scripts from eating up all available memory on a server.

This ensures that if you have a bug in one of your pages/scripts in your website, it won’t affect the performance of your entire site as much as if you have the memory limit maximized.

The default memory limit value is 128MB.

If your PHP script exceeds the set limit, the process fails and you get the error message below:

Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in /path/to/file/filename.php on line x

Or like this

PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /path/to/file/filename.php on line x

In such a case you may need to increase your PHP memory limit or fix your code if it has bugs that are causing it to consume a lot of memory.

Below is how you can check the current memory limit via one line of PHP code.

echo phpinfo();

And you will be able to see the set memory limit from the PHP info output as shown below:

PHP info memory limit

How to increase or decrease PHP memory limit in cPanel

The memory limit can be edited via the php.ini or the .htaccess files. All you need to do is to locate these files in the website's root directory and edit them as below.

In the .htaccess file, locate the line below and update the value after memory_limit with your preferred value. The "128M" below stands for 128MB memory, you can replace it with something like 256M, etc. Do not write it as MB but as M.

php_value memory_limit 128M

For the php.ini file, find the line below and edit it in a similar way.

memory_limit = 256M

However, instead of having to edit these files directly, it is more advisable to use the MultiPHP Ini Editor. The MultiPHP INI editor makes changes to the .htaccess, .user.ini, and php.ini files all at once via a web form interface.

Below are the steps on how to change the memory limit via the MultiPHP INI Editor:

Step 1: Log in to the website cPanel account.

Step 2: Open the "MultiPHP INI Editor" under the Software section.

cPanel MultiPHP INI Editor

Step 3: On the MultiPHP Ini Editor page, you will see two tabs with two modes, Basic mode, and Editor Mode. Click on the "Basic Mode" and select the domain name whose website you want to edit the PHP memory limit configuration under the "Configure PHP INI basic settings" dropdown options.

MultiPHP INI Editor page

Step 4: You will see a table with a list of PHP Directives, their descriptions, and settings in a form interface. Scroll down to the "memory_limit" directive and enter your preferred maximum limit value for your site. This value should be set to at least as big as post_max_size. Increase the limit to only the memory amount required.

Changing the memory limit in MultiPHP INI Editor

Step 5: Click on the "Apply" button at the bottom left to save the changes.

If the change was successful, you will see a confirmation success message in a green box as shown below.

Success: Successfully applied the settings.

That’s all!

Now you can comfortably increase or decrease the PHP memory limit in your web hosting cPanel account.