如何使用Apache 2.4和PHP 5.4启用FastCGI


How to enable FastCGI with Apache 2.4 and PHP 5.4

我所做的:

从源代码安装apache2.4和php5.4。

apache-config:

./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((,我看到服务器API是FPM/FastCGI

我的问题:我将DirectoryIndex设置如下。我在目录根目录中有index.html,它是"/var/www"。当我访问localhost时,我应该看到index.html的内容。但它给出了一个404文件,但没有找到。

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

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

〔2014年5月1日星期四23:21:15.968659〕〔proxy_fcgi:error〕〔pid 3415:tid 140603093 993216〕〔client 192.168.1.157:60384〕AH01071:收到错误"主脚本未知">

当/var/www中只有index.php文件,并且像一样保留httpd.conf时

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

也得到404,访问本地主机。

我的问题:

  1. 如何解决上述问题?

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

"FastCGIE外部服务器/var/www/cgi-bin/php5.fcgi-主机127.0.0.1:9000">

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

【p.S.】谢谢regilero。我没有使用vhost,我已经检查了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:收到错误‘主脚本未知''n’

我尝试将所有以".php"结尾的请求转发到fcgi。但是上面的错误看起来像是,当我请求index.html时,它也被转发到fcgi,而fcgi无法处理它。(这是我的猜测(关于这个转发的配置,请参阅上面的代码,或者在这个页面上搜索"fcgi://"。

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

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

这是一个有点联系的答案,但我与许多遇到你现在面临的问题的人分享了这个答案,它似乎起到了作用。

签出此GIST->https://gist.github.com/diemuzi/3849349

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

也许你也可以将我的工作示例与你现有的进行比较,发现一些你目前没有使用的缺失。希望这能有所帮助!