Ubuntu 16.04 Server: increase the PHP memory limit
To increase the memory limit of PHP running as an Apache module on Ubuntu 16.04, change the memory_limit value in the corresponding PHP php.ini file, then restart Apache and check the applied value in a web request. Because PHP-FPM or CLI PHP may use different configuration files, you need to check the execution method first.
Scope of application and pre-change verification
This article is an example of inspecting an existing server based on PHP 7.0 and the Apache module on Ubuntu 16.04. General security support for Ubuntu 16.04 has ended, and after extended security maintenance, separate Legacy support may apply. Check the scope of application in Ubuntu security maintenance schedule and plan to migrate to a supported environment.
memory_limit is a setting that limits the memory a PHP script can allocate. It is not the maximum memory of the physical memory installed on the server or the total memory shared by multiple requests. Increasing the limit may increase the server's memory burden depending on the number of simultaneous requests, so check both available memory and actual errors. The default value may vary depending on PHP settings and the application.
Finding PHP settings used on the web
In the basic installation of PHP 7.0 Apache module on Ubuntu 16.04, /etc/php/7.0/apache2/php.ini is used. However, the path may vary depending on the PHP version, installation method, and separate configurations. PHP-FPM usually uses a separate fpm/php.ini, and you should not assume that the php --ini result from the command line matches the settings for a web request.
In a restricted test environment, create a temporary PHP file in the web document directory and insert the code below. Open the file in a browser to check the execution method, configuration file path, and current limit. Do not place it in a public environment as server information such as file paths will be exposed, and delete it immediately after checking.
<?php
header('Content-Type: text/plain; charset=UTF-8');
echo 'SAPI: ' . PHP_SAPI . "\n";
echo 'Loaded ini: ' . php_ini_loaded_file() . "\n";
echo 'memory_limit: ' . ini_get('memory_limit') . "\n";
If it is apache2handler, it uses the Apache module method, and if it is fpm-fcgi, it uses the PHP-FPM method. If the configuration file does not appear or is different from what you expect, check the installation and the connection to the web server. PHP may read additional .ini files besides the default file or override values in the application/server settings. For more details, refer to PHP configuration file documentation.
Modifying memory_limit
The following is an example when the path checked on the web is /etc/php/7.0/apache2/php.ini. If it is a different path, apply it to the file you actually checked. Before editing, make sure there is no backup file yet, and if there is, change the backup name in the command below to a new name.
sudo cp -a -n /etc/php/7.0/apache2/php.ini /etc/php/7.0/apache2/php.ini.before-memory-limit
sudo nano /etc/php/7.0/apache2/php.ini
Find the existing memory_limit settings in the editor, change them to the required values, and save. For example, if the existing value is 128M and you find that more is needed for the task, you can try 256M. The following values are examples and are not recommended values suitable for all servers.
memory_limit = 256M
The value -1 removes the limit; do not use it indiscriminately to solve problems. The cause of memory shortage may be large input, inefficient code, or concurrent requests. The meaning of the values can be found in PHP memory_limit description.
Apply to Apache and check
In the Apache module method, first run a configuration check. Syntax OK should appear to proceed to the next step. If the check fails, resolve the errors and then check again. This check verifies the Apache configuration and does not guarantee that PHP limits are properly applied in web requests.
sudo apache2ctl configtest
If the check succeeds, restart Apache at an appropriate time. Restarting may affect connections and ongoing requests, so review the impact in the production environment.
sudo systemctl restart apache2
sudo systemctl status apache2
Verify that the service is operating normally and request the restricted-access PHP check file created earlier to see if memory_limit shows the intended value. After the check, delete the temporary file. Also, ensure that errors do not recur in the actual application.
In the case of PHP-FPM, simply updating the above Apache module files and restarting may not apply the FPM settings. Check the actually loaded FPM configuration file and the service name in use, and follow the configuration check and restart procedure for that service.
When it is not applied or problems occur
If the value checked on the web is different, check whether the edited path, PHP version, and execution method are correct. The value may also differ in additional .ini files, Apache settings, FPM pool settings, and the application's ini_set(). If memory errors occur even after changes, you need to look at the error log, work details, and server memory usage together.
If you need to revert the changes, check the contents of the backed-up configuration files and restore them to the original files, then perform an Apache configuration check and service restart again. If the backup path is different from the example, use the actual backup path. You can also separately keep the current configuration before restoration to compare the changes.




