List of content you will read in this article:
Scripts are some of the most convenient pieces of code that one could write. The lack of need for compilation and the fact that they are much faster to develop than conventional programs makes them very appealing to use.
They do, however, have a big downside. When talking in the context of server-side scripting, a poorly written script can easily eat up all of the available server resources and might even cause other parts to malfunction. Thus, in PHP, a script programming language, there is a default cap to how much of the server’s memory a single script can use.
While being much needed in most applications, certain cases require this limit to be increased. Today we will be showing you how to do exactly that, as well as give you a few other useful tips on increasing different limits within the PHP configuration. First, however, let us dive into a short explanation about PHP memory limits.
What is PHP memory_limit setting?
Unlike what many might believe, the memory_limit setting in PHP is on a per-script basis, meaning it is the maximum amount of server memory a single PHP script is allowed to consume. The official PHP documentation puts it this way:
This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts from eating up all available memory on a server.
Let’s further break it down with an example. If you have not changed the default memory limit setting in PHP, then it will be set to 128M. Let’s say you are running seven scripts, each one requiring 80M. Despite your overall memory usage being 560M, it still does not hit the 128M limit as each of the scripts uses less than that. Now if one of the scripts suddenly decides it needs 130M, that’s where an issue will occur. An error like this will appear:
Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in /path/to/php/script
PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /path/to/php/script
Let us move on onto how to get rid of these errors.
How to increase PHP memory limit in DirectAdmin
Follow these simple steps in order to increase your PHP memory limit in DirectAdmin:
Step 1: To change the PHP memory limit in DirectAdmin, you need to edit php.ini file but first, you need to find it. A new and easy trick to find your php.ini would be to ask PHP itself what it's using, e.g.:
/usr/local/bin/php --ini | grep 'Loaded Configuration File'
If you're running the default version of CLI PHP, your php.ini will be located at:
Step 2: Now you will need to edit the ”memory_limit” line to your desired values. If no "memory_limit" found, add the following line at the end of php.ini
memory_limit = 128M; /* Change the 128M to your needs */Save file.
Step 3: Now, restart Apache.
Additional Notes: Also consider that the following settings may also be related:
If you want to increase the maximum post data size:
post_max_size = 8M
If you wish to change the maximum allowed size for uploaded files:
upload_max_filesize = 2M
Lastly, some RoundCube versions have limits set in the following file:
Thus, be sure to increase them there as well if they're set.
The PHP memory limit exists for a good reason. Without it, a poorly written script can easily eat up all of your server resources and cause many issues. If you have a beefier system, however, and wish to utilize more of your resources towards PHP scripts, you can change the maximum allocated memory for each script by simply editing a configuration file. Today short tutorial showed you how to do exactly that. If you have any questions or suggestions, please leave them in the comment section below.