php 设置 ini max_execution_time 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22658908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set ini max_execution_time doesn't work
提问by babak faghihian
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:
在我使用 nginx 和 php-fpm 之前,我使用了 Apache,所以当我只希望我的一个 cron 作业在没有时间执行限制的情况下运行时,我在我的 PHP 代码中使用了以下几行:
set_time_limit(0);
ini_set('max_execution_time', 0);
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.
但是在我从 Apache 迁移到 nginx 后,此代码不起作用。我知道如何更改nginx.conf
以增加最大执行时间。
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.
但我想用 php 代码来处理这个问题。有办法吗?我只想指定一个可以不受时间限制地运行 PHP 代码的文件。
回答by Ravi Delixan
Try This:
尝试这个:
Increase PHP script execution time with Nginx
使用Nginx增加 PHP 脚本执行时间
You can follow the steps given below to increase the timeout value. PHP default is 30s. :
您可以按照下面给出的步骤来增加超时值。PHP 默认为 30 秒。:
Changes in php.ini
php.ini 中的变化
If you want to change max execution time limit for php scripts from 30 seconds (default) to 300 seconds.
如果您想将 php 脚本的最大执行时间限制从 30 秒(默认)更改为 300 秒。
vim /etc/php5/fpm/php.ini
Set…
放…
max_execution_time = 300
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.
在 Apache 中,将 PHP 作为上述模块运行的应用程序就足够了。但在我们的例子中,我们需要在另外 2 个地方进行此更改。
Changes in PHP-FPM
PHP-FPM 的变化
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
仅当您之前已经取消注释 request_terminate_timeout 参数时才需要。它默认被注释,并采用在 php.ini 中找到的 max_execution_time 的值
Edit…
编辑…
vim /etc/php5/fpm/pool.d/www.conf
Set…
放…
request_terminate_timeout = 300
Changes in Nginx Config
Nginx 配置的变化
To increase the time limit for example.com by
增加 example.com 的时间限制
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;
}
If you want to increase time-limit for all-sites on your server, you can edit main nginx.conf file:
如果要增加服务器上所有站点的时间限制,可以编辑主 nginx.conf 文件:
vim /etc/nginx/nginx.conf
Add following in http{..} section
在 http{..} 部分添加以下内容
http {
#...
fastcgi_read_timeout 300;
#...
}
Reload PHP-FPM & Nginx
重新加载 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
or try this
或者试试这个
fastcgi_send_timeout 50;
fastcgi_read_timeout 50;
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.
fastcgi 有它自己的一组超时和检查,以防止它在锁定的进程中停滞不前。例如,如果您将 php 的执行时间限制设置为 0(无限制),然后不小心创建了一个无限循环,它们就会启动。或者,如果您正在运行除 PHP 之外的其他应用程序,这些应用程序没有任何自己的超时保护并且失败了。
回答by Beto Aveiga
I think that if you have php-fpm and nginx "you can't" set this time only from PHP.
我认为,如果你有 php-fpm 和 nginx,“你不能”只从 PHP 设置这个时间。
What you could do is a redirect with the parameters indicating you where to continue, but you must control the time that your script is running to avoid timeout.
您可以做的是使用指示您继续的参数的重定向,但您必须控制脚本运行的时间以避免超时。
If your process runs in a browser window, then do it with Javascript the redirect because the browser could limit the number of redirects... or do it with ajax.
如果您的进程在浏览器窗口中运行,则使用 Javascript 进行重定向,因为浏览器可能会限制重定向的数量……或者使用 ajax 进行。
Hope that helps.
希望有帮助。
回答by Berkay Y?ld?z
You can add request_terminate_timeout = 300to your server's php-fpm pool configuration if you are tried all of solutions here.
如果您在这里尝试了所有解决方案,您可以将request_terminate_timeout = 300添加到您服务器的 php-fpm 池配置中。
回答by Muhammad Tahir
ini_set('max_execution_time', 0);
do this if "Safe Mode"is off
如果“安全模式”关闭,请执行此操作
set_time_limit(0);
Place this at the top of your PHP script and let your script loose!
把它放在你的 PHP 脚本的顶部,让你的脚本松散!
Note:if your PHP setup is running in safe mode, you can only change it from the php.ini file.
注意:如果您的 PHP 设置在安全模式下运行,则只能从 php.ini 文件中更改它。