CentOS 7 Apache 2.4与PHP-FPM

PHP-FPM确实有一些依赖于解决方案的优点,通常的方法是将Nginx与PHP-FPM一起使用。但是,如果您想要利用Apache的常规特性(如.htaccess文件等基础特性),但又要保留PHP-FPM附带的调优选项,会发生什么情况呢?嗯,这里有一个模块!

本指南将假设一个新的CentOS 7服务器将从头到尾说明所有内容,并假设该服务器上的所有站点将使用相同的php-fpm池。

1.首先,为您的web服务器安装所需的包:

[root@web01 ~]# yum install httpd httpd-tools mod_ssl php-fpm

2.禁用PHP的全局Apache配置:

[root@web01 ~]# mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.bak

 

3.为这个特定的站点创建一个新的PHP-FPM池,并相应地更新它:

[root@web01 ~]# cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/example.com.conf
[root@web01 ~]# vim /etc/php-fpm.d/example.com.conf
; listen = 127.0.0.1:9000
listen = /var/run/php-fpm/example.com.sock
...
listen.allowed_clients = 127.0.0.1
listen.owner = apache
listen.group = apache
listen.mode = 0660
user = apache
group = apache

 

4.然后更新站点的Apache vhost,指向80节和443节中的一个新的PHP-FPM池。请确保在下面2节中更新您的站点的套接字!(即:unix:/ var / run / php-fpm / example.com.sock)

[root@web01 ~]# vim /etc/httpd/vhost.d/example.com.conf
<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/vhosts/example.com

        # Proxy declaration
        <Proxy "unix:/var/run/php-fpm/example.com.sock|fcgi://php-fpm">
                # we must declare a parameter in here (doesn't matter which) or it'll not register the proxy ahead of time
                ProxySet disablereuse=off
                # Note: If you configure php-fpm to use the "ondemand" process manager, then use "ProxySet disablereuse=on"
        </Proxy>

        # Redirect to the proxy
        <FilesMatch \.php$>
                SetHandler proxy:fcgi://php-fpm
        </FilesMatch>
...
<VirtualHost *:443>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/vhosts/example.com

        # Proxy declaration
        <Proxy "unix:/var/run/php-fpm/example.com.sock|fcgi://php-fpm">
                # we must declare a parameter in here (doesn't matter which) or it'll not register the proxy ahead of time
                ProxySet disablereuse=off
                # Note: If you configure php-fpm to use the "ondemand" process manager, then use "ProxySet disablereuse=on"
        </Proxy>

        # Redirect to the proxy
        <FilesMatch \.php$>
                SetHandler proxy:fcgi://php-fpm
        </FilesMatch>
...

 

 

5.然后重启服务:

[root@web01 ~]# systemctl restart php-fpm
[root@web01 ~]# systemctl restart httpd

 

6.最后,通过创建下面的文件来测试站点以确保PHP能够正常工作并使用PHP- fpm,然后访问example.com/info.php:)的页面:

[root@web01 ~]# vim /var/www/vhosts/example.com/info.php
<?php phpinfo(); ?>

 

 

 

 

 

 

 

 

 

登录 评论
  订阅  
提醒