Increase PHP script execution time with Nginx
Hatched by Felipe Soares Barbosa Silveira (Felipebros)
Jun 24, 2024
3 min read
13 views
Increase PHP script execution time with Nginx
In the world of web development, PHP is a popular programming language for building dynamic websites and web applications. However, if you're working with complex or resource-intensive scripts, you may run into issues with the default PHP script execution time. Fortunately, with the help of Nginx, you can easily increase the execution time and ensure that your scripts have enough time to complete their tasks.
By default, Apache allows for longer script execution times when PHP is running as a module above it. However, when using Nginx as the web server, you need to make a few changes in order to increase the PHP script execution time. Let's take a look at the necessary steps to achieve this.
-
Changes in php.ini:
The first step is to locate and modify the php.ini file. This file contains various configuration settings for PHP. Look for the "max_execution_time" directive and increase its value to the desired execution time in seconds. For example, if you want to set the execution time to 120 seconds, you would change the value to "max_execution_time = 120". Save the changes and restart the PHP service to apply the new settings. -
Changes in PHP-FPM:
PHP-FPM (FastCGI Process Manager) is a popular alternative to running PHP as an Apache module. If you're using PHP-FPM with Nginx, you'll need to make changes in the PHP-FPM configuration file. Locate the "request_terminate_timeout" directive and set its value to the desired execution time in seconds. Save the changes and restart the PHP-FPM service. -
Changes in Nginx Config:
Finally, you need to make changes in the Nginx configuration file to reflect the increased PHP script execution time. Locate the "fastcgi_read_timeout" directive and set its value to the desired execution time in seconds. Additionally, you may also need to increase the "proxy_read_timeout" directive if your Nginx server acts as a reverse proxy. Save the changes and restart the Nginx service.
By following these steps, you can effectively increase the PHP script execution time with Nginx. However, it's important to note that increasing the execution time too much can have adverse effects on server performance and may result in slow response times for other scripts. It's recommended to find a balance and set a reasonable execution time based on the specific requirements of your application.
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 🐣