Centos 6 上的 Apache 2.4 和 PHP-FPM 5.6

Centos 6 上的 Apache 2.4 和 PHP-FPM 5.6

我正在尝试让 PHP 在 Centos 上通过 FPM 运行。

在 FPM 方面,/etc/php-fpm.d/www.conf几乎都是默认值。我使用 TCP 端口,如下所示(我可能稍后想要更改为文件端口以提高性能,但在运行之前让我们先了解一下):

listen = 127.0.0.1:9000

服务php-fpm启动顺利。

所以我认为我的问题出在 Apache 方面。我添加了一个conf.d/php-fpm.conf包含以下内容的配置文件:

# Defining a worker will improve performance
# And in this case, re-use the worker (dependent on support from the fcgi application)
# If you have enough idle workers, this would only improve the performance marginally
<Proxy "fcgi://localhost:9000/" enablereuse=on max=10>
</Proxy>
<FilesMatch "\.php$">
    <If "-f %{REQUEST_FILENAME}">
        # Pick one of the following approaches
        # Use the standard TCP socket
        SetHandler "proxy:fcgi://localhost/:9000"
        # If your version of httpd is 2.4.9 or newer (or has the back-ported feature), you can use the unix domain socket
        #SetHandler "proxy:unix:/path/to/app.sock|fcgi://localhost/"
    </If>
</FilesMatch>

我已经建立了一个测试站点,如下所示:

<VirtualHost 192.168.1.35:80>
    ServerAdmin [email protected]
    ServerName centos.local
    ServerAlias www.centos.local

    #ProxyPassMatch "^/(.*\.php)$" "unix:/var/run/myappname.sock|fcgi://localhost/webroot"

    DirectoryIndex index.html

    ErrorLog /var/log/httpd24/centos.error.log
    CustomLog /var/log/httpd24/centos.access.log combined

    <Directory /var/www/vhosts/centos.local/httpdocs>
        DocumentRoot /var/www/vhosts/centos.local/httpdocs
        # Allow .htaccess
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

(我不确定是否需要 ProxyPassMatch,无论是否有它,我似乎都会得到相同的结果)。

httpd 服务启动后,html 页面运行正常,但如果我尝试加载 PHP 文件,浏览器中就会出现“服务不可用”的提示,错误日志中会写入以下内容:

[Sun Jul 22 21:13:21.813760 2018] [proxy:error] [pid 14621] (2)No such file or directory: AH02454: FCGI: attempt to connect to Unix domain socket /var/run/myappname.sock (localhost) failed
[Sun Jul 22 21:13:21.814003 2018] [proxy_fcgi:error] [pid 14621] [client 192.168.1.41:54578] AH01079: failed to make connection to backend: httpd-UDS

希望有人能告诉我哪里错了!

编辑:

根据建议,我已删除 ProxyPassMatch。现在我收到以下错误:

[Sun Jul 22 23:25:08.066467 2018] [proxy:error] [pid 16319] (13)Permission denied: AH00957: FCGI: attempt to connect to 127.0.0.1:8000 (*) failed
[Sun Jul 22 23:25:08.066548 2018] [proxy_fcgi:error] [pid 16319] [client 192.168.1.41:56396] AH01079: failed to make connection to backend: localhost

似乎无法在端口 8000 上找到 FPM,但我在任何配置文件中都看不到任何内容。我遗漏了什么?

谢谢,

詹姆士

答案1

由于您使用的是 TCP 套接字代理<Proxy "fcgi://localhost:9000/" enablereuse=on max=10> </Proxy>

该问题是由于SetHandler "proxy:fcgi://localhost/:9000"

由于某种原因,Apache 将斜杠解释为目录分隔符,并默认为8000代理的端口。

要解决这个问题,只需删除/之前的:9000

SetHandler "proxy:fcgi://localhost:9000"

确保您的 php-fpm 配置匹配:

[www]
listen = localhost:9000
listen.allowed_clients=localhost

我强烈建议您声明一个 IP 地址。localhost 是一种万能的地址。

由于localhost可以应用于 Web 服务器上的多个 IP 地址,因此我建议明确指定一个 IP,以避免代理被分配给多个私有 IP 或 DNS 解析。
示例:

#/etc/hosts
127.0.0.1 localhost
192.168.1.2 localhost fqdn.example.com

以下是在不使用 Unix 域套接字 (UDS) 时建议的 Apache 虚拟主机和 php-fpm 配置。

9001用于不与默认的[www]php-fpm 池冲突,并允许您创建单独的池配置。

确保您的 MPM 配置与您当前的配置不冲突。

#/etc/httpd/conf.d/01-mpm.conf
LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule cgid_module modules/mod_cgid.so
<IfModule mpm_event_module>
    ServerLimit              100
    StartServers             4
    ThreadLimit              64
    MaxRequestWorkers        100
    MinSpareThreads          25
    MaxSpareThreads          75
    ThreadsPerChild          25
    MaxConnectionsPerChild   1000
    ListenBacklog       511
</IfModule>
#/etc/httpd/conf.d/999-centos.local.conf
<VirtualHost 192.168.1.35:80>
    ServerAdmin [email protected]
    ServerName centos.local
    ServerAlias www.centos.local
    DocumentRoot /var/www/vhosts/centos.local/httpdocs

    ErrorLog /var/log/httpd24/centos.error.log
    CustomLog /var/log/httpd24/centos.access.log combined

    <Directory /var/www/vhosts/centos.local/httpdocs>
        # Allow .htaccess
        AllowOverride All
        Require all granted
        DirectoryIndex index.php index.html
        <IfModule mod_setenvif.c>
            SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
        </IfModule>
    </Directory>

    <IfModule proxy_fcgi_module>
        <Proxy "fcgi://127.0.0.1:9001/" enablereuse=on max=10>
            ProxySet timeout=1800
        </Proxy>
        <FilesMatch "\.php$">
            <If "-f %{REQUEST_FILENAME}">
                SetHandler "proxy:fcgi://127.0.0.1:9001"
            </If>
        </FilesMatch>
    </IfModule>
</VirtualHost>
;/etc/php-fpm.d/centos.local.conf
[centos.local]
user=apache
group=apache

listen=127.0.0.1:9001
listen.allowed_clients=127.0.0.1

pm=dynamic
pm.max_children=10
pm.start_servers=2
pm.min_spare_servers=2
pm.max_spare_servers=5

security.limit_extensions=.php

您可以选择替换 TCP 套接字配置以使用 UDS。

<VirtualHost>
    #...
    <IfModule proxy_fcgi_module>
        <Proxy "fcgi://127.0.0.1/" enablereuse=on max=10>
            ProxySet timeout=1800
        </Proxy>
        <FilesMatch "\.php$">
            <If "-f %{REQUEST_FILENAME}">
                SetHandler proxy:unix:/var/run/unique-domain.sock|fcgi://127.0.0.1/
            </If>
        </FilesMatch>
    </IfModule>
</VirtualHost>
[centos.local]
;...

;listen=127.0.0.1:9001
;listen.allowed_clients=127.0.0.1
listen=/var/run/unique-domain.sock
listen.mode=0660
listen.owner=nobody
listen.group=nobody

;...

注意:mod_proxy_fcgi UDS 支持是在 Apache 2.4.9 中引入的[原文如此]

当使用 UDS 和 Apache 2.4 < 2.4.11 时,您不应该使用enablereuse=on2.4.11 之前的 Apache 版本存在与 UDS 和连接重用不兼容的问题。[原文如此]

如果您在使用 UDS 时遇到奇怪的响应或 503 服务不可用错误,请enablereuse=on max=10<Proxy "fcgi://127.0.0.1:9001/">

答案2

教程你关注过节目将 PHP 代理到 php-fpm 的不同方法。它们都是互斥的,您只能选择其中一种。

我建议你坚持通过处理程序进行代理按照第一个代码块中的方法,然后删除TCP套接字(IP和端口)方法从第二个代码块中删除以下内容:

    ProxyPassMatch "^/(.*\.php)$" "unix:/var/run/myappname.sock|fcgi://localhost/webroot"

无论如何,它没有引用 php-fpm 正在监听的 TCP 套接字。

顺便说一句,你也应该修复这个问题,DirectoryIndex但那是另一个问题。

相关内容