增加 php 的最大执行时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7739870/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:16:51  来源:igfitidea点击:

Increase max execution time for php

php.htaccessscriptingphp-ini

提问by Bajrang

I have added set_time_limit(0);function to increase execution time but its executing only 2-3 minutes maximum.

我添加set_time_limit(0);了增加执行时间的功能,但最多只能执行 2-3 分钟。

error_reporting(E_ALL);
error_reporting(1);
set_time_limit(0);

I want to search links from a site wich taking a long lime .

我想从一个需要很长时间的网站上搜索链接。

Any help would be greatly appreciated.

任何帮助将不胜感激。

Thanks a lot.

非常感谢。

回答by Sumith Harshan

PHP file (for example, my_lengthy_script.php)

PHP 文件(例如,my_lengthy_script.php)

ini_set('max_execution_time', 300); //300 seconds = 5 minutes

.htaccess file

.htaccess 文件

<IfModule mod_php5.c>
php_value max_execution_time 300
</IfModule>

More configuration options

更多配置选项

<IfModule mod_php5.c>
php_value post_max_size 5M
php_value upload_max_filesize 5M
php_value memory_limit 128M
php_value max_execution_time 300
php_value max_input_time 300
php_value session.gc_maxlifetime 1200
</IfModule>

If wordpress, set this in the config.php file,

如果是 wordpress,请在 config.php 文件中进行设置,

define('WP_MEMORY_LIMIT', '128M');

If drupal, sites/default/settings.php

如果是drupal,sites/default/settings.php

ini_set('memory_limit', '128M');

If you are using other frameworks,

如果您正在使用其他框架,

ini_set('memory_limit', '128M');

You can increase memory as gigabyte.

您可以将内存增加为千兆字节

ini_set('memory_limit', '3G'); // 3 Gigabytes

259200 means:-

259200 意味着:-

( 259200/(60x60 minutes) ) / 24 hours ===> 3 Days

More details on my blog

在我的博客上有更多详细信息

回答by Salman Aslam

Add these lines of code in your htaccess file. I hope it will solve your problem.

在您的 htaccess 文件中添加这些代码行。我希望它能解决你的问题。

<IfModule mod_php5.c>
php_value max_execution_time 259200
</IfModule>

回答by Mob

Well, since your on a shared server, you can't do anything about it. They usually set the max execution time so that you can't override it. I suggest you contact them.

好吧,由于您在共享服务器上,因此您无能为力。他们通常设置最大执行时间,以便您无法覆盖它。我建议你联系他们。

回答by Nishu Tayal

well, there are two way to change max_execution_time.
1. You can directly set it in php.ini file.
2. Secondly, you can add following line in your code.

好吧,有两种方法可以更改 max_execution_time。
1.可以直接在php.ini文件中设置。
2. 其次,您可以在代码中添加以下行。

ini_set('max_execution_time', '100')

回答by PrestaShark

Use mod_php7.cinstead of mod_php5.cfor PHP 7

在 PHP 7 中使用mod_php7.c而不是mod_php5.c

Example

例子

<IfModule mod_php7.c>
   php_value max_execution_time 500
</IfModule>

回答by Jefferson Schiavetto

Try to set a longer max_execution_time:

尝试设置更长的时间max_execution_time

<IfModule mod_php5.c>
    php_value max_execution_time 300
</IfModule>

<IfModule mod_php7.c>
    php_value max_execution_time 300
</IfModule>

回答by seven

This is old question, but if somebody finds it today chances are php will be run via php-fpm and mod_fastcgi. In that case nothing here will help with extending execution time because Apache will terminate connection to a process which does not output anything for 30 seconds. Only way to extend it is to change -idle-timeout in apache (module/site/vhost) config.

这是个老问题,但如果今天有人发现它,很可能 php 将通过 php-fpm 和 mod_fastcgi 运行。在这种情况下,这里没有任何东西可以帮助延长执行时间,因为 Apache 将终止与 30 秒内不输出任何内容的进程的连接。扩展它的唯一方法是更改​​ apache(模块/站点/虚拟主机)配置中的 -idle-timeout。

FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /run/php/php7.0-fpm.sock -idle-timeout 900 -pass-header Authorization

More details - Increase PHP-FPM idle timeout setting

更多细节 -增加 PHP-FPM 空闲超时设置

回答by symcbean

There's probably a limit set in your webserver. Some browsers/proxies will also implement a timeout. Invoking long running processes via an HTTP request is just plain silly. The right way to solve the problem (assuming you can't make the processing any faster) is to use the HTTP request to trigger processing outside of the webserver session group then poll the status via HTTP until you've got a result set.

您的网络服务器中可能设置了限制。一些浏览器/代理也会实现超时。通过 HTTP 请求调用长时间运行的进程是很愚蠢的。解决问题的正确方法(假设您不能使处理速度更快)是使用 HTTP 请求触发 Web 服务器会话组之外的处理,然后通过 HTTP 轮询状态,直到您获得结果集。