Home  >  Article  >  Backend Development  >  How to change the timeout in php

How to change the timeout in php

PHPz
PHPzOriginal
2023-04-03 15:14:501673browse

As our business develops and the number of website visits increases, we often need to execute PHP scripts that take a long time. However, PHP's default timeout is only 30 seconds, which is far from meeting business needs. In order to solve this problem, we need to modify the timeout period of PHP.

Here are some ways to change the PHP timeout:

Modify php.ini

This is the most common way, by modifying the max_execution_time parameter in the php.ini file , you can change PHP's timeout. Find the following content in the php.ini file:

; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 30

Change the max_execution_time parameter to a value greater than 30 seconds.

It is worth mentioning that if modifying the php.ini file does not work, you need to check which php.ini file is currently used by PHP. This can be viewed in PHP code using the phpinfo() function.

Use set_time_limit() in code

The set_time_limit() function is a built-in function in PHP that can be used to change the maximum time for script execution. For example, add the following code at the beginning of the code:

set_time_limit(600);

The maximum execution time of the PHP script will be set to 600 seconds, which is 10 minutes.

It is worth mentioning that if the parameter is passed in 0, it means that the execution time limit is cancelled, and the script can continue to execute.

Set in Apache

If PHP runs as an Apache module, you can change the PHP timeout by modifying php_value in the .htaccess file.

Add the following code in the .htaccess file:

php_value max_execution_time 600

At this time, the maximum execution time of PHP will be set to 600 seconds, which is 10 minutes.

Set in Nginx or FastCGI

If you are using Nginx or FastCGI, you can add the following code in the configuration file:

fastcgi_read_timeout 600;

At this time, the maximum execution of PHP The time will be set to 600 seconds, which is 10 minutes.

Set in PHP-FPM

If you are using PHP-FPM, you can set the PHP timeout by modifying the php-fpm.conf or www.conf file. Take the php-fpm.conf file as an example here:

Find the following content:

; Process timeouts in seconds. Negative values disable timeouts.
; Default Value:
;     process.timeout = 0s
process.timeout = 30s

Change the process.timeout parameter to a value greater than 30 seconds.

Summary

The above is the method to change the PHP timeout. According to different environments and uses, choose different methods to modify the PHP timeout. In addition, we also need to pay attention to adjusting parameters such as PHP memory limit (memory_limit) and Apache or Nginx server request response time to improve the performance and stability of the website.

The above is the detailed content of How to change the timeout in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn