Increase PHP script execution time with Nginx

3 min read

0

Increase PHP script execution time with Nginx
Prevent nginx 504 Gateway timeout using PHP set_time_limit()

When it comes to running PHP scripts, one of the most common challenges developers face is dealing with script execution time. By default, PHP has a maximum execution time set, and if a script takes longer than that time to run, it will result in a timeout error. This can be particularly problematic when using Nginx as the web server, as it has its own timeout settings that can lead to a 504 Gateway timeout error.

To overcome this issue and increase the PHP script execution time with Nginx, we need to make changes in three different places: php.ini, PHP-FPM, and the Nginx configuration.

First, let's start with the php.ini file. This file contains various configuration settings for PHP, including the maximum execution time. Locate the php.ini file on your server and open it with a text editor. Look for the following line:

max_execution_time = 30  

The default value is usually set to 30 seconds. Increase this value to a higher number, depending on the requirements of your application. For example, if you need to allow scripts to run for up to 60 seconds, change the line to:

max_execution_time = 60  

Save the php.ini file and restart your PHP service for the changes to take effect.

Next, we need to make changes in the PHP-FPM configuration. PHP-FPM (FastCGI Process Manager) is a separate service that handles PHP requests. Locate the PHP-FPM configuration file, usually named php-fpm.conf or www.conf, and open it with a text editor.

Look for the following line:

;request_terminate_timeout = 0  

Remove the semicolon (;) at the beginning of the line to uncomment it, and change the value to the desired maximum execution time in seconds. For example:

request_terminate_timeout = 60  

Save the PHP-FPM configuration file and restart the PHP-FPM service for the changes to take effect.

Finally, we need to make changes in the Nginx configuration file. Nginx has its own timeout settings that can override the PHP settings if not adjusted accordingly. Locate the Nginx configuration file, usually named nginx.conf or default.conf, and open it with a text editor.

Inside the server block, add or modify the following lines:

location ~ \.php$ {  
    fastcgi_read_timeout 60;  
    fastcgi_send_timeout 60;  
}  

These lines set the read and send timeouts for PHP scripts to 60 seconds. Adjust the values according to your requirements.

Save the Nginx configuration file and restart the Nginx service for the changes to take effect.

By making these changes in php.ini, PHP-FPM, and the Nginx configuration, you can increase the PHP script execution time with Nginx and prevent the 504 Gateway timeout error.

In addition to these steps, there is another PHP function that can be used to prevent script timeouts - set_time_limit(). This function allows you to set the maximum execution time for a specific script. For example, if you have a long-running script that needs more time to complete, you can use set_time_limit() to extend the execution time.

To use set_time_limit(), simply add the following line at the beginning of your PHP script:

set_time_limit(60);  

This will set the maximum execution time for the script to 60 seconds. Make sure to adjust the value accordingly.

In conclusion, increasing the PHP script execution time with Nginx involves making changes in php.ini, PHP-FPM, and the Nginx configuration. By adjusting the maximum execution time settings in these three places, you can prevent the 504 Gateway timeout error and ensure that your PHP scripts have enough time to complete. Additionally, using the set_time_limit() function in specific scripts can provide more flexibility in controlling the execution time. Remember to always consider the requirements of your application and adjust the values accordingly for optimal performance.

Sources

← Back to Library

Hatch New Ideas with Glasp AI 🐣

Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)

Start Hatching 🐣