Increase PHP script execution time with Nginx
Hatched by Felipe Soares Barbosa Silveira (Felipebros)
Sep 07, 2023
3 min read
199 views
Increase PHP script execution time with Nginx
When it comes to running PHP scripts, it is crucial to ensure that they have enough execution time to complete their tasks successfully. In this article, we will explore how to increase the PHP script execution time by making some adjustments in Nginx, php.ini, and PHP-FPM.
- Changes in php.ini
The first step to increasing the PHP script execution time is to modify the php.ini file. This file contains various configuration settings for PHP, including the maximum execution time. By default, the maximum execution time is set to 30 seconds, which may not be sufficient for some scripts.
To change this value, locate the php.ini file on your server and open it using a text editor. Look for the following line:
"max_execution_time = 30"
Change the value to the desired execution time, such as 60 for 1 minute or 120 for 2 minutes. Save the changes and restart your PHP server for the modifications to take effect.
- Changes in PHP-FPM
PHP-FPM (FastCGI Process Manager) is a highly efficient alternative PHP FastCGI implementation. To increase the PHP script execution time in PHP-FPM, you will need to make changes in its configuration file.
Locate the PHP-FPM configuration file on your server, which is usually named php-fpm.conf or www.conf. Open the file in a text editor and look for the following line:
"request_terminate_timeout = 30s"
Change the value to the desired execution time, similar to what you did in the php.ini file. Once again, save the changes and restart PHP-FPM to apply the new settings.
- Changes in Nginx Config
Now that you have adjusted the execution time in both php.ini and PHP-FPM, it's time to make some changes in the Nginx configuration file. This will ensure that Nginx allows enough time for PHP scripts to complete their execution without triggering a 504 Gateway timeout error.
Open the Nginx configuration file, which is typically named nginx.conf or default.conf, in a text editor. Look for the following line:
"proxy_read_timeout 300;"
This line sets the timeout value for Nginx when proxying requests to PHP-FPM. Increase the value to match or exceed the maximum execution time you set in php.ini and PHP-FPM. For example, if you set the maximum execution time to 120 seconds, change the line to:
"proxy_read_timeout 120s;"
Save the changes and restart Nginx to apply the new configuration.
Prevent Nginx 504 Gateway timeout using PHP set_time_limit()
In addition to the above methods, you can also use the PHP function set_time_limit() within your scripts to prevent Nginx's 504 Gateway timeout error. By calling this function at the beginning of your script, you can extend the execution time for that particular script only.
For example, if you want to increase the execution time to 120 seconds, add the following line at the beginning of your PHP script:
"set_time_limit(120);"
This will ensure that the script has enough time to run, even if the default execution time set in php.ini and PHP-FPM is shorter.
Conclusion
By making the necessary changes in php.ini, PHP-FPM, and Nginx configuration, you can effectively increase the PHP script execution time and prevent Nginx's 504 Gateway timeout error. Remember to always adjust the execution time according to the specific requirements of your scripts.
Actionable Advice:
- Regularly monitor the execution time of your PHP scripts to identify any potential performance bottlenecks.
- Consider implementing caching mechanisms or optimizing your code to reduce the overall execution time and improve the efficiency of your PHP scripts.
- Keep an eye on server resources, such as CPU and memory usage, as increasing the PHP script execution time may put additional strain on your server. Consider upgrading your server or optimizing resource allocation if necessary.
Sources
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 🐣