PHP Errors
When your application is running, you may notice PHP errors within your application logs, or you may want to ensure your PHP scripts are not running indefinitely due to incomplete or stalled requests. This article explains how to configure appropriate timeout settings and memory limits to ensure the stability, security, and optimal performance of your application.
Memory limit
The default PHP memory limit on Application Hosting is 128MB for all PHP versions. If your application exceeds this limit, you may see an error similar to the following in your application logs:
Detected 1047468200 Bytes of RAM
PHP memory_limit is 128M Bytes
To increase your application’s memory limit, create a .user.ini file in the application’s root directory and add the memory limit setting to the file. For example, if you want to increase the PHP memory limit to 256MB, add the following:
memory_limit = 256M
Increase request terminate timeout
The request_terminate_timeout
directive for FastCGI Process Manager (FPM) determines the maximum time a PHP script can run before the web server forcefully terminates it, regardless of whether the script has completed or not.
This is useful for preventing PHP scripts from running indefinitely, which can happen due to coding errors, infinite loops, or excessive processing times. By setting an appropriate request_terminate_timeout
, you can prevent PHP processes from consuming excessive server resources and potentially affecting the overall performance and stability of the server.
To change the value of request_terminate_timeout:
- Create a file named fpm_custom.conf within the root directory of your repository and add the required value within it, for example (default unit is seconds):
request_terminate_timeout = 120
- Push the changes to your Git repository.
- Within MyKinsta, select your Application > Processes > edit the Web process > update the Start command to include the fpm_custom.conf file, for example:
heroku-php-apache2 -F fpm_custom.conf
Increase max execution time
max_execution_time
defines the maximum amount of time, in seconds, that a PHP script is allowed to run before it is terminated by the server.
To increase your application’s max execution time, create a .user.ini file in the application’s root directory and add the max execution time setting to the file. For example, if you want to increase the max execution time to 60 seconds, add the following:
max_execution_time = 60
Note: Cloudflare’s 185-second limit restricts this value to a maximum of 185 seconds and cannot be changed.
When you change the max_execution_time
you should also update the request_terminate_timeout
value to ensure the script is not terminated before the execution time.