Increase PHP script execution time with Nginx
Hatched by Felipe Soares Barbosa Silveira (Felipebros)
Feb 25, 2024
3 min read
11 views
Increase PHP script execution time with Nginx
In today's fast-paced digital world, optimizing the performance of our web applications is crucial. One aspect that often needs attention is the execution time of PHP scripts. By default, PHP has a maximum execution time limit set, but there may be scenarios where we need to increase this limit to ensure our scripts can run without interruption. In this article, we will explore how we can achieve this using Nginx, a popular web server.
Firstly, let's understand the different components involved in increasing PHP script execution time with Nginx. Typically, when using Apache, applications running PHP as a module above would have sufficed. However, with Nginx, we need to make changes at three different places - php.ini, PHP-FPM, and Nginx Config.
The first step is to modify the php.ini file. This file contains various configuration settings for PHP. To increase the script execution time, we need to locate the "max_execution_time" directive in the php.ini file and set it to the desired value. The default value is often set to 30 seconds, but depending on our application's requirements, we can increase it accordingly. For example, if we want to set the maximum execution time to 60 seconds, we would update the directive as follows:
max_execution_time = 60
Once we have made the necessary changes in the php.ini file, the next step is to modify the PHP-FPM configuration. PHP-FPM (FastCGI Process Manager) is a PHP FastCGI implementation that provides a high-performance alternative to PHP running as an Apache module. In the PHP-FPM configuration file, we need to locate the "request_terminate_timeout" directive and set it to the same value as the "max_execution_time" set in the php.ini file. This ensures that PHP-FPM allows scripts to run for the desired duration without terminating them prematurely.
Finally, we need to make changes in the Nginx configuration to reflect the increased PHP script execution time. In the Nginx configuration file, we need to locate the "fastcgi_read_timeout" directive and set it to the same value as the "max_execution_time" set in the php.ini file. This tells Nginx to wait for the PHP script to finish executing before timing out. Additionally, we can also set the "proxy_read_timeout" directive to the same value as an added measure.
By making these changes in the php.ini, PHP-FPM, and Nginx configuration files, we can effectively increase the PHP script execution time with Nginx. This ensures that our scripts have sufficient time to complete their tasks without being interrupted.
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 🐣