环境:centos7.1
结合模式: linux+apache+php-fpm+mariadb
服务器:三台
host1:172.16.52.58 作httpd服务器
host2:172.16.52.61 作php-fpm服务器
host3:172.16.52.60 作数据库服务器,这里安装的是mariadb数据库
注意事项:确保防火墙和selinux都关闭
host1:
1.yum install httpd
2. systemctl start httpd
3.测试:
浏览器:http://172.16.52.58
host2:
4.yum install php-fpm
5.mkdir -pv /var/www/html
host1:
6.
vim /etc/php-fpm.d/www.conf,修改如下:
listen = 172.16.52.60:9000
listen.allowed_clients = 172.16.52.58
vim /etc/httpd/conf.d/fcgi.conf
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$fcgi://172.16.52.60:9000/var/www/html/$1
7.vim phpinfo.php
phpinfo();
?>
host2:
8.systemctl start php-fpm
host1:
9.systemctl reload httpd.service
10.测试:
http://172.16.52.58/phpinfo.php
这个页面是交由host2处理的
host3:
11.yum install mariadb-server
12.systemctl start mariadb.service
13.
~]#msyql
MariaDB [(none)]> GRANT ALL ON *.* TO 'php'@'172.16.%.%' IDENTIFIED BY 'dyl@1234';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
host2:
14.vim /var/www/html/mysql.php
$conn = mysql_connect('172.16.52.61','php','dyl@1234');
if ($conn)
echo "mysql connect correct." ; ///语法规定后面要有分号,忘了写于是报错。。。。
else
echo "mysql connect wrong." ;
?>
15.systemctl reload php-fpm
16.测试
http://172.16.52.58/mysql.php
总结:如果以php-fpm模式访问php文件,静态页面将交由host1处理,php页面将交由host2处理,这就实现了反向代理。
host1:
1.注释掉主配置文件:
vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"
2.配置虚拟主机
cd /etc/httpd/conf.d/
vim virfcgi.conf
ServerName "www.a.com"
DocumentRoot "/web/vhosts/htdocs"
ProxyRequests Off
ProxyPassMatch^/(.*\.php)$ fcgi://172.16.52.60:9000/web/vhosts/htdocs/$1
Options None
AllowOverride None
Require all granted
3.mkdir -pv /web/vhosts/htdocs
host2:
4.mkdir -pv /web/vhosts/htdocs
cp /var/www/htm/{mysql,phpinfo}.php /web/vhosts/htdocs
5.测试
通过FQDN访问,记得修改hosts文件
打开浏览器,输入:
http://www.a.com/phpinfo.php
http://www.a.com/mysql.php