【发布时间】:2016-07-21 15:22:43
【问题描述】:
我想通过使用 PHP 的 ini_set 函数来测试 max_execution_time 设置。所以我设置了一个简单的测试来创建一个无限循环以确保它会超时。
这是我的代码
Test.php
<?php
ini_set('max_execution_time', 300);
echo "Hello World";
//for(;;); Commented, see history
sleep(100);
?>
这段代码在我运行在 XAMPP 上的 Windows 机器上运行良好。 5 分钟后,会显示 Hello World 和致命错误超出超时。
然后我将代码移至运行 ubuntu 和 Apache 的 VPS。然后我从浏览器打开网页。但是,仅 1 分钟后,我的浏览器显示
Error Code: 503 Service Unavailable
Network Error
A communication error occurred: "Operation timed out"
The Web Server may be down, too busy, or experiencing other problems preventing it from responding to requests. You may wish to try again at a later time.
它甚至不等待 5 分钟。并且在 1 分钟后一直出错。但是在我的错误日志中,Apache 在 5 分钟后写入了超过 time 的致命错误。
所以,在我看来,请求在 PHP 脚本实际完成之前就被取消了。
我在 apache2.conf 上使用默认设置和额外的虚拟主机
apache2.conf (additional only)
<Directory /var/www/mysite.com/public>
Options FollowSymLinks
AllowOverride All
</Directory>
mysite.virtualhost.conf
<VirtualHost virtualhost.mysite.com:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerAdmin admin@mysite.com
ServerName virtualhost.mysite.com
ServerAlias virtualhost.mysite.com
DocumentRoot /var/www/mysite.com/public
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
php.ini 文件
Php.ini
safe_mode = off
max_execution_time = 1200
那么,我在这里错过了什么配置?我认为我的 apache 配置有问题,但我不知道是哪一部分。
更新
查看进程运行后,运行无限循环的 apache 进程正在消耗 99% 的 CPU。为什么会这样?使用 strace 查看返回 this 的进程
write(10, "36.72.135.58 - - [22/Jul/2016:03:15:35 +0700] \"GET /test11.php HTTP/1.1\" 500 211 \"-\" \"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36\"\n", 198) = 198
times({tms_utime=29996, tms_stime=4, tms_cutime=0, tms_cstime=0}) = 4967918020
gettimeofday({1469132442, 496002}, NULL) = 0
gettimeofday({1469132442, 496046}, NULL) = 0
shutdown(17, SHUT_WR) = 0
poll([{fd=17, events=POLLIN}], 1, 2000) = 1 ([{fd=17, revents=POLLIN|POLLHUP}])
read(17, "", 512) = 0
close(17) = 0
read(5, 0x7fff82f5cf67, 1) = -1 EAGAIN (Resource temporarily unavailable)
gettimeofday({1469132442, 496411}, NULL) = 0
accept4(4, <detached ...>
【问题讨论】:
-
什么 php 配置?并非所有配置都允许通过 ini_set 覆盖 ini 设置
-
我添加了 php safe_mode off,它实际上覆盖了 php.ini max_execution_time,因为它更长,即 1200。将使用 php.ini 配置更新我的问题
-
503响应代码看起来真的可能在您的 VPS 和您之间存在某种代理或其他网关,并且配置了较低的超时时间。 503 响应来自与您的浏览器中止请求不同的服务器(不会有 http 响应),因为它花费了太长时间或中间的东西只是轰炸并关闭连接。您对托管平台了解更多吗?他们是否提供任何类型的地理负载平衡或 DDOS 保护?像这样的服务可能意味着其他东西在起作用。 -
如果 PHP 作为 CGI 或 Apache 模块运行?你确定是 Apache 在 5 分钟后写入了致命错误,而不是像 php-fpm 那样写入它自己的日志吗?
-
PHP 作为 Apache 模块运行。我正在使用数字海洋作为 VPS 提供商。知道可能有什么样的代理吗?还有一个 apache tomcat 实例也在该机器上运行。