繁体   English   中英

使用 php7 和 apache 设置 php-fpm 状态页面

[英]Set up php-fpm status page with php7 and apache

我正在尝试通过 http 调用设置和获取 php-fpm 统计信息。 我知道可以使用service status命令,但我想从我的浏览器中获取它。

我正在运行 php7 和 apache,这就是我在服务器配置中所做的。

在 apache 端,我用这个创建了一个虚拟主机:

<LocationMatch "/fpm-status">
             Order Allow,Deny
             Allow from 127.0.0.1
             ProxyPass fcgi://127.0.0.1:9000
</LocationMatch>

在 php 池配置( /etc/php/7.0/fpm/pool.d/www.conf )我有这个:

[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000
listen.owner = www-data
listen.group = www-data

pm = ondemand

pm.max_children = 1000

pm.start_servers = 150
pm.min_spare_servers = 50
pm.max_spare_servers = 400
pm.max_requests = 200
pm.process_idle_timeout = 5s
pm.status_path = /fpm-status

但是在重新启动 apache 和 php-fpm 进程后,当我尝试使用 curl 时,我得到了这个输出:

admin@ip-10-3-23-78:~$curl http://localhost/fpm-status
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /fpm-status
on this server.<br />
</p>
</body></html>
admin@ip-10-3-23-78:~$ 

在 apache 日志文件中,我有这个:

==> /var/log/apache2/error.log <==
[Thu Aug 25 13:36:10.776665 2016] [access_compat:error] [pid 12608] [client ::1:23142] AH01797: client denied by server configuration: proxy:fcgi://127.0.0.1:9000

我想知道如何真正设置它。 我在谷歌上搜索了很长时间并没有得到确切的答案,每个人都在尝试自己的方式。 谁负责创建状态页面(在我的例子中是fpm-status )? 何时以及如何生成此页面(我猜是 php-fpm)? 设置页面并从浏览器访问的正确方法是什么?

现在可能有点晚了,但我想用 php-fpm(7.1+)/apache(2.4) 发布一个直接简单的答案,因为我在网上找到的大多数答案都有些复杂。 这是使用需要 unix 套接字与端口映射的默认 php-fpm 设置。

1)在/etc/php-fpm.d/www.conf ,我为下面的监听袜子设置了以下配置选项并取消注释:

listen = /var/run/php-fpm.sock

pm.status_path = /fpm-status

2) 使用我的 apache 配置php-latest.conf (或类似的),我添加了一个匹配来查找 fpm-status 并将其设置为proxypass到 unix 套接字并从 fcgi 运行 fpm-status。 它还限制它,因此只有本地主机可以调用它:

<LocationMatch "/fpm-status">
    Order Allow,Deny
    Allow from 127.0.0.1
    ProxyPass unix:/var/run/php-fpm.sock|fcgi://localhost/fpm-status
</LocationMatch>

3) 只需在本地运行curl命令:

$ curl http://localhost/fpm-status
pool:                 www
process manager:      dynamic
start time:           16/Oct/2019:11:33:25 -0400
start since:          14
accepted conn:        12
listen queue:         0
max listen queue:     0
listen queue len:     0
idle processes:       38
active processes:     2
total processes:      40
max active processes: 5
max children reached: 0
slow requests:        0

我遇到了同样的问题,并在那里花了几个小时来解决我们的安装问题。 不幸的是,我无法回答您在那里提出的所有问题,这主要是“使用 php7 和 apache 设置 php-fpm 状态页面”磁贴的有效解决方案

我们开始吧(Ubuntu 16.04):

第 1 步:需要的东西只需检查您是否安装了类似的东西:

apt-get -y install apache2
apt-get -y install libapache2-mod-fastcgi php7.0-fpm php7.0
a2enmod actions fastcgi alias
systemctl restart apache2.service

第 2 步:设置 fastcgi在 /etc/apache2/mods-available/fastcgi.conf(或类似的)中输入以下内容:

<IfModule mod_fastcgi.c>
        # Define a named handler
        AddHandler php7-fcgi .php
        # Generate an alias pointing to /usr/lib/cgi-bin/php[VersionNumber]-fcgi
        Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
        # Configure an external server handling your upcoming requests (note where the alias is pointing towards)
        FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php/php7.0-fpm.sock -pass-header Authorization

         # only on if fpm-status is match. You might want to put this into your concrete vhost.conf file. For the testing, fastcgi.conf should work.
         <LocationMatch "/fpm-status">
             # set the before defined handler here
             SetHandler php7-fcgi
             # use the handler for the action handling virtual requests
             Action php7-fcgi /php7-fcgi virtual
         </LocationMatch>
</IfModule>

第 3 步:检查您的 /etc/php/7.0/fpm/pool.d/www.conf确保取消注释状态路径:

pm.status_path = /fpm-status

第 4 步:保护页面(可选)在投入生产之前,以某种方式保护它当然是明智的,例如:

 Order deny,allow
 Deny from all
 Allow from [Some-IP]

希望这有帮助,干杯。

如果您在 apache-server 上运行其他 Web 应用程序,则其中一个可能附带一个.htaccess文件,该文件会干扰处理/staus页面(或您在 php-fpm 池配置中为该页面命名的任何内容)。

我最近在 nextcloud 实例中遇到了这个问题。 在 nextcloud-(apache)-configuration 中,将 URL 列入白名单并禁用此路径的.htaccess -overrides ( RewriteEngine Off ) 使该页面在我的情况下可访问。 确保用正确的路径替换套接字的路径(这是 Ubuntu 16.04 的示例)。

<FilesMatch "^ping|status$">                                                       
  RewriteEngine Off
  SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"               
</FilesMatch>

注意正如评论中所指出的,正确的指令可能是<Location "^ping|status$">而不是<FilesMatch>

socket-path 在默认 ubuntu 版本中定义在/etc/php/7.2/fpm/pool.d/www.conf ( listen = /run/php/php7.2-fpm.sock )。

如果您收到权限错误,请尝试添加

listen.mode = 0666

/etc/php/7.0/fpm/pool.d/www.conf

尽管我仍然无法查看状态页面并且在我尝试时收到 404 错误,但这对我使整个 fastcgi 堆栈与 php-fpm 正常运行是必要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM