【问题标题】:How to enable FastCGI with Apache 2.4 and PHP 5.4如何使用 Apache 2.4 和 PHP 5.4 启用 FastCGI
【发布时间】:2014-06-18 04:37:30
【问题描述】:

我做了什么:

从源代码安装 apache 2.4 和 php 5.4。

apache 配置:

./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=event

php 配置

./configure --prefix=/usr/local/php \ 
--with-mysql=mysqlnd\
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--enable-mbstring \
--with-freetype-dir \
--with-jpeg-dir\
--with-png-dir\
--with-zlib \
--with-libxml-dir=/usr/local/libxml2 \
--enable-xml \
--enable-sockets \
--enable-fpm \
--with-mcrypt \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--with-bz2\
--with-apxs2=/usr/local/apache/bin/apxs

在 httpd.conf 文件中,我启用了 mod_proxy.so 和 mod_proxy_fcgi.so 并添加以下配置:

<IfModule proxy_module>
  ProxyRequests off
  ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/$1
</IfModule>

从 phpinfo(),我看到 Server API 是 FPM/FastCGI

我的问题: 我将 DirectoryIndex 设置如下。我在 DirectoryRoot 中有 index.html,即“/var/www”。当我访问 localhost 时,我应该看到 index.html 的内容。但它给出了一个 404 文件未找到。

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

当我在上面的代码中切换 index.html 和 php 的位置时。这次html文件在php文件的前面。然后当我访问 localhost 时,我可以看到正确的内容。 我检查了 apache 错误日志。它说:

[Thu May 01 23:21:15.968659 2014] [proxy_fcgi:error] [pid 3415:tid 140603093 993216] [client 192.168.1.157:60384] AH01071: Got error 'Primary script unkn own\n'

/var/www 中只有 index.php 文件时,像 httpd.conf 一样

<IfModule dir_module>    DirectoryIndex  index.html index.php</IfModule>

同样得到404,访问本地主机。

我的问题:

  1. 如何解决上述问题?

  2. 我对fastcgi的配置做了一些研究。喜欢thisthis 他们没有提到 mod_proxy_fcgi,但都提到了这个

"FastCGIExternalServer /var/www/cgi-bin/php5.fcgi -host 127.0.0.1:9000"

所以我不确定我是否以正确的方式使用 FastCgi?这种错误的方式会导致上述问题吗?

【附注】 谢谢你。我没有使用虚拟主机,我检查了 error.log。它似乎与php-fpm有关。浏览器中的响应页面显示“找不到文件”,而不是“404”,尽管响应代码是 404。这是 error.log 中的内容

[2014 年 5 月 1 日星期四 23:21:15.968659] [proxy_fcgi:error] [pid 3415:tid 140603093 993216] [client 192.168.1.157:60384] AH01071: Got error 'Primary script unknown\n'

我尝试将所有以“.php”结尾的请求转发到 fcgi。但是上面的错误好像是我请求index.html的时候,也是转发给fcgi的,fcgi不能处理。(这是我的猜测)关于这个转发的配置,请看上面的代码,或者搜索这个“fcgi://”的页面。

这是我的目录配置。我不确定这是否是您需要的。

DocumentRoot "/var/www"
<Directory "/var/www">
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>

【问题讨论】:

  • 对于您关注的链接,他们使用旧方法将 apache 中的 php-fpm 与 libapache2-mod-fastcgi 连接。自 2.4 版以来,mod_proxy_fastcgi 是新的官方方式。现在您应该显示完整的 vhost 配置,并且 404 错误应该在 apache error.log 中添加有用信息的行。

标签: apache fastcgi php


【解决方案1】:

这在某种程度上是一个链接的答案,但是我与许多遇到您现在面临的问题的人分享了这个答案,并且似乎可以解决问题。

查看此 GIST -> https://gist.github.com/diemuzi/3849349

在那里你会找到我所有的 Apache 和 PHP-FPM 工作配置。这似乎是一个组织我的配置的更好的地方,而不是在这里粘贴它们并混淆其他人。此链接不会过期,所以不用担心。

也许您也可以将我的工作示例与您拥有的示例进行比较,并找到您当前未使用的缺失内容。希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2012-06-07
    • 2020-09-07
    • 2017-07-07
    • 2014-10-30
    • 2015-08-31
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    相关资源
    最近更新 更多