centos7通过RPM包部署分离式LAMP+xcache (php-fpm)


要求:

rpm包部署LAMP,并且需要将LAMP环境进行分离式的部署

(1)一个虚拟主机提供wordpress

        一个虚拟主机提供phpMyadmin

(2)利用xcache来加速页面速度

环境:

此处用三台主机分别分离提供不同服务:

192.168.1.104------->提供httpd服务

192.168.1.110------->提供mariadb-server服务

192.168.1.113------->提供php-fpm  php-mysql  xcache服务



一、192.168.1.104服务器部署httpd服务:

1、安装httpd服务程序

[root@bogon ~]# yum -y install httpd
[root@bogon ~]# vim /etc/httpd/conf/httpd.conf
ServerNAme localhost:80 #修改下要不解析不出来,启动老慢了


2、建立虚拟主机

[root@bogon ~]# vim /etc/httpd/conf.d/vhosts1.conf

DirectoryIndex index.php
<VirtualHost 192.168.1.104:80>
        ServerNAme wp.magedu.com
        DocumentRoot /data/vhosts/www1
        ProxyRequests off
        ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.1.113:9000/data/vhosts/www1/$1
        ####表示包以.php开头的文件传给php-fpm来处理,此处地址是php服务器地址
        <Directory "/data/vhosts/www1">
                Options None
                AllowOverride None
                Require all granted
        </Directory>
</VirtualHost>



[root@bogon ~]# vim /etc/httpd/conf.d/vhosts2.conf

DirectoryIndex index.php
<VirtualHost 192.168.1.104:80>
        ServerNAme pam.magedu.com
        DocumentRoot /data/vhosts/www2
        ProxyRequests off
        ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.1.113:9000/data/vhosts/www2/$1

        <Directory "/data/vhosts/www2">
                Options None
                AllowOverride None
                Require all granted
        </Directory>
</VirtualHost>


3、建立网页及相关路径

[root@bogon ~]# mkdir -p /data/vhosts/www1
[root@bogon ~]# mkdir -p /data/vhosts/www2

[root@bogon ~]# echo "vhosts111" > /data/vhosts/www1/index.html 
[root@bogon ~]# echo "vhosts222" > /data/vhosts/www2/index.html


4、启动下服务我们测试下虚拟主机是否正常

[root@bogon www1]# curl http://wp.magedu.com
vhosts111
[root@bogon www1]# curl http://pam.magedu.com
vhosts222



二、在192.168.1.113服务器上部署安装php-fpm


1、安装php-fpm php-mysql php-mbstring程序

[root@pxe132 ~]# rpm -q php  ###确保之前没有php程序,如果有看情况,不符合我们需要卸载
package php is not installed
[root@pxe132 ~]# yum -y install php-fpm php-mysql php-mbstring


2、编辑/etc/php-fpm.d/www.conf

[root@pxe132 ~]# vim /etc/php-fpm.d/

listen = 192.168.1.113:9000 ###设置php服务器监听地址即监听本地能够与外部通信的地址
listen.allowed_clients = 192.168.1.104 ###监听具有httpd服务的IP地址


3、建立以下文件并且启动php-fpm服务,查看下是否已经监听

[root@pxe132 ~]# mkdir /var/lib/php/session
[root@pxe132 ~]# chown apache.apache /var/lib/php/session/
[root@pxe132 ~]# ls -ld /var/lib/php/session/
drwxr-xr-x 2 apache apache 6 Jul 18 20:37 /var/lib/php/session/

[root@pxe132 ~]# systemctl start php-fpm.service 
[root@pxe132 ~]# ss -tnl 
State       Recv-Q Send-Q                   Local Address:Port   
LISTEN      0      128                      192.168.1.113:9000 ####已经监听php地址                 
LISTEN      0      128                                  *:22     
LISTEN      0      128                          127.0.0.1:631    
LISTEN      0      100                          127.0.0.1:25                 
LISTEN      0      128                          127.0.0.1:6010  
LISTEN      0      128                                ::1:631    
LISTEN      0      100                                ::1:25                                 
LISTEN      0      128                                ::1:6010


4、在php服务器上建立与http服务器上网页DocumentRoot路径,并且编写php测试也,看看是否能够与http连接

[root@pxe132 ~]# mkdir -p /data/vhosts/www1/ 
[root@pxe132 ~]# mkdir -p /data/vhosts/www2/

[root@bogon ~]# vim /data/vhosts/www2/index.php ###虚拟主机2的php和httpd连接测试

This is vhost2
<?php
phpinfo();
?>

[root@bogon ~]# vim /data/vhosts/www1/index.php###虚拟主机1的php和httpd的连接测试

This is vhost1
<?php
phpinfo();
?>


5、加载服务访问站点测试php和httpd连接是否正常

wKiom1eM2avC5w4LAABV8Y2C1Zo967.png

wKioL1eM2cWiW7xJAABRYXhQrSo956.png

ok!此时证明了我们php服务器和http服务器已经连接成功了没有问题!



三、在192.168.1.110服务器上部署mariadb服务


1、安装mariadb服务

[root@pxe130 ~]# yum -y install mariadb-server
[root@pxe130 ~]# systemctl start mariadb.service


2、创建等会需要用到的数据库和授权等相关操作

MariaDB [(none)]> CREATE DATABASE wpdb; ##创建WordPress所用数据库
MariaDB [(none)]> GRANT ALL ON wpdb.* TO 'wpuser'@'192.168.%.%' IDENTIFIED BY 'wppass';
                        ###授权WordPress用户及操作操作
                                        
MariaDB [(none)]> CREATE DATABASE pma;  ##授权phpmyadmin所用数据库
MariaDB [(none)]> GRANT ALL ON pma.* TO 'pmauser'@'192.168.%.%' IDENTIFIED BY 'pmapass';
                       ###授权phpmyadmin的用户及操作权限
                       
MariaDB [(none)]> grant all on *.* to 'pmauser'@'192.168.%.%' identified by 'pmapass';


3、在php服务器上建立php测试页,测试php是否可以正常连接数据

[root@pxe132 ~]# vim /data/vhosts/www1/index.php 

This is vhost1
<?php
        $conn = mysql_connect('192.168.1.110','wpuser','wppass');
            if ($conn)
                   echo "ok";
            else
                   echo "NO";
phpinfo();
?>
[root@pxe132 ~]# vim /data/vhosts/www1/index.php 

This is vhost2
<?php
$conn = mysql_connect('192.168.1.110','pmauser','pmapass');
        if ($conn)
                echo "ok";
        else
                echo "NO";
phpinfo();
?>
~


4、测试

wKiom1eM4DHhR2tMAABtTQfpTys936.png

wKioL1eM4FKjMn8MAABNYgFJ3bM727.png

ok经过测试我们的mariadb数据可以同php连接了,到现在我们分离式的LAMP平台就基本构建完成了!!j_0003.gif



四、下面我们就就来部署下WordPress和phpMyadmin

注意:此处我们是把程序放置php服务器当中去部署,不原来没分离情况是不一样的,千万习惯了搞错了啊!

部署WordPress:

1、解压包,配置连接用户和密码,数据可地址

[root@pxe132 ~]# unzip wordpress-4.3.1-zh_CN.zip
[root@pxe132 ~]# mv wordpress /data/vhosts/www1/
[root@pxe132 ~]# cd /data/vhosts/www1/wordpress/
[root@pxe132 wordpress]# mv wp-config-sample.php wp-config.php
[root@pxe132 wordpress]# vim wp-config.php 

define('DB_NAME', 'wpdb');

/** MySQL数据库用户名 */
define('DB_USER', 'wpuser');

/** MySQL数据库密码 */
define('DB_PASSWORD', 'wppass');

/** MySQL主机 */
define('DB_HOST', '192.168.1.110');


2、此时我们需要把WordPress这个目录个传到http服务器主页访问的路径下

[root@pxe132 www1]# pwd
/data/vhosts/www1
[root@pxe132 www1]# ls
index.php  wordpress
[root@pxe132 www1]# scp -r wordpress/ root@192.168.1.104:/data/vhosts/www1/
                                                    ###目录传到http服务器上


部署phpMyadmin:

1、解压包配置

[root@pxe132 ~]# unzip phpMyAdmin-4.4.14.1-all-languages.zip
[root@pxe132 ~]# mv phpMyAdmin-4.4.14.1-all-languages /data/vhosts/www2/
[root@pxe132 www2]# mv phpMyAdmin-4.4.14.1-all-languages/ phpmyadmin


2、编辑配置文件

[root@pxe132 libraries]# pwd
/data/vhosts/www2/phpmyadmin/libraries
[root@pxe132 libraries]# vim config.default.php 

$cfg['blowfish_secret'] = 'V40VdxxM0rPrx8k2KYE';
$cfg['Servers'][$i]['host'] = '192.168.1.110'; ###数据库服务器地址
$cfg['Servers'][$i]['user'] = 'pmauser';
$cfg['Servers'][$i]['password'] = 'pmapass';


3、将配置好了的phpmyadmin目录传一份给httpd服务器虚拟主机对应的访问路径下

[root@pxe132 www2]# scp -r phpmyadmin/ root@192.168.1.104:/data/vhosts/www2/

测试:

wKioL1eND6jQypeXAACT3v9Vipk591.png

wKiom1eND6ijgtbVAAF1DGUq3Vg452.png


ok了下面我们对页面进行一次压力测试看看速度:

[root@bogon ~]# ab -n 10000 -c 1000 http://wp.magedu.com/wordpress
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking wp.magedu.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        Apache/2.4.6
Server Hostname:        wp.magedu.com
Server Port:            80

Document Path:          /wordpress
Document Length:        239 bytes

Concurrency Level:      1000
Time taken for tests:   3.081 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Non-2xx responses:      10002
Total transferred:      4690938 bytes
HTML transferred:       2390478 bytes
Requests per second:    3245.20 [#/sec] (mean)
Time per request:       308.147 [ms] (mean)
Time per request:       0.308 [ms] (mean, across all concurrent requests)
Transfer rate:          1486.63 [Kbytes/sec] received

从这段测试可以看出,这没加速度比我们之前的基于module+xcache的都要快。。。。



五、在php服务器安徽192.168.1.113上安装xcache进行缓存加速

1、安装php-xache

[root@pxe132 ~]# yum -y install php-xcache 

[root@pxe132 ~]# systemctl restart php-fpm.service


2、编辑配置文件,我吧缓存大小调整大写看看效果

[root@pxe132 ~]# vim /etc/php.d/xcache.ini
xcache.size  =               300M


3、压力测试:

[root@bogon ~]# ab -n 10000 -c 1000 http://wp.magedu.com/wordpress
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking wp.magedu.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        Apache/2.4.6
Server Hostname:        wp.magedu.com
Server Port:            80

Document Path:          /wordpress
Document Length:        239 bytes

Concurrency Level:      1000
Time taken for tests:   3.076 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Non-2xx responses:      10012
Total transferred:      4695628 bytes
HTML transferred:       2392868 bytes
Requests per second:    3250.70 [#/sec] (mean)
Time per request:       307.626 [ms] (mean)
Time per request:       0.308 [ms] (mean, across all concurrent requests)
Transfer rate:          1490.63 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   46 208.7      2    3011
Processing:     0   69 191.8     31    1575
Waiting:        0   68 191.7     31    1574
Total:         21  115 337.5     34    3040