问题描述
在我使用nginx和php-fpm之前,我使用了Apache,因此,当我只希望运行一个cron作业而没有时间执行限制时,我在PHP代码中使用了以下几行:
Before I use nginx and php-fpm, I used Apache, so when I wanted only one of my cron jobs to run without time execution limitation, I used these lines in my PHP code:
set_time_limit(0);
ini_set('max_execution_time', 0);
但是当我从Apache迁移到nginx之后,此代码不起作用.我知道更改nginx.conf
来增加最长执行时间的方法.
but after I migrated from Apache to nginx, this code doesn't work. I know ways to change nginx.conf
to increase maximum execution time.
但是我想用php代码来处理这个问题.有办法吗?我只想指定一个可以运行PHP代码而没有时间限制的文件.
But I want to handle this with php code. Is there a way?I want to specify only one file that can run PHP code without time limitation.
推荐答案
尝试一下:
使用 Nginx
您可以按照以下步骤增加超时值. PHP的默认值为30s. :
如果要将php脚本的最大执行时间限制从30秒(默认)更改为300秒.
If you want to change max execution time limit for php scripts from 30 seconds (default) to 300 seconds.
vim /etc/php5/fpm/php.ini
设置...
max_execution_time = 300
在Apache中,将PHP作为上述模块运行的应用程序就足够了.但是在我们的情况下,我们需要在另外2个地方进行此更改.
In Apache, applications running PHP as a module above would have suffice. But in our case we need to make this change at 2 more places.
PHP-FPM中的更改
仅当您之前没有对request_terminate_timeout参数进行注释时,才需要使用此命令.默认情况下,它带有注释,并采用php.ini中的max_execution_time值
This is only needed if you have already un-commented request_terminate_timeout parameter before. It is commented by default, and takes value of max_execution_time found in php.ini
编辑...
vim /etc/php5/fpm/pool.d/www.conf
设置...
request_terminate_timeout = 300
Nginx Config中的更改
将example.com的时间限制增加
To increase the time limit for example.com by
vim /etc/nginx/sites-available/example.com
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_read_timeout 300;
}
如果要增加服务器上所有站点的时限,可以编辑nginx.conf主文件:
If you want to increase time-limit for all-sites on your server, you can edit main nginx.conf file:
vim /etc/nginx/nginx.conf
在http {..}部分中添加以下内容
Add following in http{..} section
http {
#...
fastcgi_read_timeout 300;
#...
}
重新加载PHP-FPM& Nginx
别忘了这样做,这样所做的更改才会生效:
Don’t forget to do this so that changes you have made will come into effect:
service php5-fpm reload
service nginx reload
或尝试
fastcgi_send_timeout 50;
fastcgi_read_timeout 50;
fastcgi有它自己的一组超时,并进行检查以防止其在锁定的进程中停滞.例如,如果您将php的执行时间限制设置为0(无限制),然后不小心创建了一个无限循环,它们就会加入.或者,如果您运行的是除PHP之外的其他应用程序,则该应用程序没有任何自己的超时保护,并且它失败了.
fastcgi has it's own set of timeouts and checks to prevent it from stalling out on a locked up process. They would kick in if you for instance set php's execuction time limit to 0 (unlimited) then accidentally created an infinite loop. Or if you were running some other application besides PHP which didn't have any of it's own timeout protections and it failed.
这篇关于设置ini max_execution_time不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!