Increasing the PHP memory_limit Setting
If you've built PHP applications for even a short period of time, chances are you've encountered this dreaded error:
PHP Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y bytes)
This error occurs when the executing PHP script consumes more memory than what has been allocated using PHP's memory_limit
setting. Keep in mind this setting imposes a cap on each PHP process, meaning if the memory_limit
setting was 128MB and there were four simultaneously executing PHP scripts with each requiring 100MB, then none would produce the above PHP fatal error because each was under the 128MB maximum memory allocation.
The memory_limit
value is often set quite low, for instance to 128MB:
memory_limit = 128M
Because many web application servers are configured with much higher amounts of RAM than in years past, you can almost certainly increase the setting to 512 MB or even higher. This is typically done by changing the memory_limit
setting found in your server's php.ini
file.
Finding Your php.ini File
It would seem trivial to find your server's php.ini
configuration file, however there is a catch. Modern PHP installations come with two php.ini
files: one for use in conjunction with CLI-based PHP applications, and another for web-based PHP applications. If you SSH into the server and run the following command you'll be presented with the location of the php.ini
file used for the former:
$ php --ini | grep Configuration
Configuration File (php.ini) Path: /etc/php/7.3/cli
Loaded Configuration File: /etc/php/7.3/cli/php.ini
However if you instead use the find
command you'll locate two php.ini
files:
$ find . -name php.ini
./etc/php/7.3/fpm/php.ini
./etc/php/7.3/cli/php.ini
The php.ini
file found in the fpm
directory is used by PHP-FPM, so that's the version you'll want to modify. After making the change, restart your PHP-FPM daemon:
$ sudo service php7.3-fpm restart
See description for details.