本文主要记录如何实现php以fastCGI的方式与httpd结合工作的过程,其中安装httpd和mysql(所用的软件版本是httpd-2.4.16和mysql-5.6.26)的过程和之前第一篇中的过程类似,这里就不再赘述。唯一不同的地方是配置安装php这一步。

(1)创建php安装目录:

 # mkdir /usr/local/php

(2)解压php源码包(版本是php-5.6.12),配置编译安装:

 # tar xf php-5.6.12.tar.bz2

 # cd php-5.6.12

 # ./configure --prefix=/usr/local/php/ --enable-fpm --with-config-file-path=/etc/ 

--with-config-file-scan-dir=/etc/php.d --with-jpeg-dir --with-png-dir --enable-mbstring 

--with-mcrypt --with-mysql=/usr/local/mysql --with-openssl --with-freetype-dir --with-zlib 

--with-libxml-dir=/usr/ --enable-xml --enable-sockets --with-bz2 --enable-maintainer-zts

--with-mysqli=/usr/local/mysql/bin/mysql_config

 # make && make install

(3)复制php配置文件以及服务脚本到/etc目录下,并赋予服务脚本执行权限,另外可根据自己的需要配置php以fastCGI方式工作时的相关参数:

 # cp php.ini-production /etc/php.ini

 # cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

 # chmod +x /etc/init.d/php-fpm

 # chkconfig --add php-fpm

 # cd /usr/local/php/etc

 # cp php-fpm.conf.default php-fpm.conf

 # vim php-fpm.conf

可根据需要调整相关参数大小,保存退出。至此,可以启动php-fpm服务了:

 # service php-fpm start

(4)下面介绍当php与httpd以fastCGI方式结合工作时如何建立虚拟主机:

    1)、首先需要修改httpd配置文件:

     # vim /etc/httpd/httpd.conf

    启用相应模块,找到“#LoadModule proxy_module modules/mod_proxy.so”

    “#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so”这两行,将两行行首的#号去掉,启用模块;取消中心主机,将“DocumentRoot”对应行注释掉;包含相应的虚拟主机的配置文件,即找到“Include /etc/httpd/extra/httpd-vhosts.conf”行,去掉行首的#号;配置httpd支持php动态页面和主页,找到“AddType”字眼的行,在其后添加如下两行:

     AddType application/x-httpd-php .php

     AddType application/x-httpd-php-source .phps

    另外找到“DirectoryIndex index.html”所在行,修改其内容为“DirectoryIndex index.php index.html”;保存退出。

    2)、建立虚拟主机:

    编辑/etc/httpd/extra/httpd-vhosts.conf,添加以下内容:

    <VirtualHost *:80>

        DocumentRoot "/www/a.com"

        ServerName www.a.com

        ProxyRequests Off

        ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/a.com/$1

        <Directory "/www/a.com">

            Options None

            AllowOverride None

            Require all granted

        </Directory>

        ErrorLog "/var/log/httpd/error_log"

        CustomLog "/var/log/httpd/access_log" common

    </VirtualHost>

    要想虚拟主机中配置的“ErrorLog”“CustomLog”即日志存放路径生效,只需要将httpd配置文件中的“ErrorLog”“CustomLog”所在行注释掉即可。接下来就可以重启httpd服务:

 # service httpd restart

    在/www/a.com目录下编写一个很简单的测试脚本即可验证php能否正常工作,内容如下:

 <title>www.a.com</title>

 <?php

   phpinfo();

 ?>