CentOS7部署LAMP+xcache (php-fpm模式)

  這次實驗準備3臺CentOS7服務器,版本號:CentOS Linux release 7.2.1511。php

搭建Apache服務器

  • 經過 yum -y install httpd 安裝Apache:

  • 訪問正常:

  • 修改配置文件,添加2個虛擬主機,一個提供phpMyAdmin,一個提供wordpress:
[root@happiness ~]# vim /etc/httpd/conf/httpd.conf
#啓用解析,加速httpd服務的啓動
    ServerName localhost:80
#註銷默認主目錄
    #DocumentRoot "/var/www/html"

[root@happiness ~]# vim /etc/httpd/conf.d/vhost.conf
#默認主頁
    DirectoryIndex index.php
#虛擬主機a,後期提供phpMyAdmin
    <VirtualHost 192.168.4.119:80>
        ServerName www.aaa.net
        DocumentRoot /var/www/aaa
        ProxyRequests off 
        ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.4.118:9000/var/www/php_aaa/$1
        <Directory "/var/www/aaa">
            Options None
            Allowoverride None
            Require all granted
        </Directory>
    </VirtualHost>
#虛擬主機b,後期提供wordpress
    <VirtualHost 192.168.4.119:80>
        ServerName www.bbb.net
        DocumentRoot /var/www/bbb
        ProxyRequests off 
        ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.4.118:9000/var/www/php_bbb/$1
        <Directory "/var/www/bbb">
            Options None
            Allowoverride None
            Require all granted
        </Directory>
    </VirtualHost>
  • 測試2個虛擬主機靜態資源是否可用:
#在虛擬主機a中新建測試頁aaa.html
[root@happiness ~]# vim /var/www/aaa/aaa.html
    <h1>test virtualhost aaa</h1>
#在虛擬主機b中新建測試頁bbb.html [root@happiness ~]# vim /var/www/bbb/bbb.html <h1>test virtualhost bbb</h1>

訪問結果:(實驗中沒搭建dns服務,修改要訪問Apache的主機的hosts文件,添加 192.168.4.119  www.aaa.net 和 192.168.4.119  www.bbb.net,來完成訪問)
   

搭建php服務器

  •  經過 yum -y install php-fpm php-mysql php-mbstring 搭建php:

  • 修改php配置文件:
[root@happiness ~]# vim /etc/php-fpm.d/www.conf
#監聽本地能與外部通訊的地址
    listen = 192.168.4.118:9000
#容許哪些主機訪問
    listen.allowed_clients = 192.168.4.119
#確保user、group跟Apache服務器httpd進程的user、group一致
    user = apache
    group = apache
#session目錄不存在,須要手動建立
    php_value[session.save_path] = /var/lib/php/session
[root@happiness ~]# mkdir /var/lib/php/session [root@happiness ~]# chown apache:apache /var/lib/php/session [root@happiness ~]# ll -hd /var/lib/php/session/ drwxr-xr-x. 2 apache apache 4.0K Jun 21 10:28 /var/lib/php/session/
#虛擬機a新建php測試頁 [root@happiness ~]# cat /var/www/php_aaa/index.php <h1>a</h1> <?php phpinfo(); ?> #虛擬機b新建php測試頁 [root@happiness ~]# cat /var/www/php_bbb/testbbb.php <h1>b</h1> <?php phpinfo(); ?> 訪問結果:(注意,若是訪問結果出現:File not found相似的提示時,須要關閉php服務器的selinux,臨時關閉經過命令 setenforce Permissive 進行,永久關閉修改配置文件 vim /etc/sysconfig/selinux)
  

 搭建Mariadb服務器

  • 經過 yum -y install mariadb-server 安裝mariadb:

  • 建立mariadb用戶訪問權限:
MariaDB [(none)]> CREATE USER 'wpuser'@'192.168.4.118' IDENTIFIED BY 'wppasswd';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON wordpressdb.* TO 'wpuser'@'192.168.4.118';
MariaDB [(none)]> CREATE USER 'pmauser'@'192.168.4.118' IDENTIFIED BY 'pmapasswd';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON pmadb.* TO 'pmauser'@'192.168.4.118';
MariaDB [(none)]> FULSH PRIVILEGES;
MariaDB [(none)]> CREATE DATABASE wordpressdb DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
MariaDB [(none)]> CREATE DATABASE pmadb DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
  • 分別在虛擬機a和b中創建測試頁,看可否成功鏈接mariadb服務器:
[root@happiness ~]# cat /var/www/php_aaa/mysql_conn.php
    <h1>aaa</h1>
    <?php
        $conn = mysql_connect('192.168.4.117','pmauser','pmapasswd');
        if($conn)
            echo "aaa connects successfully";
        else
            echo "aaa fails to connect.";
    ?>
[root@happiness ~]# cat /var/www/php_bbb/mysql_conn.php
    <h1>bbb</h1>
    <?php
        $conn = mysql_connect('192.168.4.117','wpuser','wppasswd');
        if($conn)
            echo "bbb connects successfully.";
        else
            echo "bbb fails to connect.";
    ?>

訪問結果:

搭建phpMyAdmin和WordPress

  • 分別從官網下載 phpMyAdmin 和 WordPress 源碼包,分別解壓於對應的虛擬機主目錄下:(此時最新的 phpMyAdmin-4.8.1版本僅支持 PHP 5.5 to 7.2 and MySQL 5.5,而實驗中PHP版本爲5.4,故下載了 phpMyAdmin-4.4.15.10版本)

  • 搭建phpMyAdmin:
#修改phpMyAdmin配置文件
[root@happiness ~]# vim /var/www/php_aaa/phpmyadmin/libraries/config.default.php #可經過 openssl rand -base64 32 隨機字符串   $cfg['blowfish_secret'] = 'w14hLCZeyOtg4+9izK9O3oW8BLjTpJVAzF1GCAtJ7U4=';   $cfg['Servers'][$i]['host'] = '192.168.4.117';   $cfg['Servers'][$i]['user'] = 'pmauser';   $cfg['Servers'][$i]['password'] = 'pmapasswd'; #修改完配置後複製一整份 phpmyadmin文件到Apache服務器 [root@happiness ~]# scp -r /var/www/php_aaa/phpmyadmin/ root@192.168.4.119:/var/www/aaa/ 訪問結果:
  •  搭建WordPress:
#修改WordPress配置文件
[root@happiness ~]# cp /var/www/php_bbb/wordpress/wp-config-sample.php /var/www/php_bbb/wordpress/wp-config.php
[root@happiness ~]# vim /var/www/php_bbb/wordpress/wp-config.php
  define('DB_NAME', 'wordpressdb');
  define('DB_USER', 'wpuser');
  define('DB_PASSWORD', 'wppasswd');
  define('DB_HOST', '192.168.4.117');

#修改完配置後複製一整份wordpress到Apache服務器:
[root@happiness ~]# scp -r /var/www/php_bbb/wordpress root@192.168.4.119:/var/www/bbb/

訪問結果:

 藉助php-xcache加速訪問

  •  沒安裝php-xcache進行的壓力測試:
[root@happiness ~]# ab -n 10000 -c 1000 http://www.bbb.net/wordpress
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.bbb.net (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:        www.bbb.net
Server Port:            80

Document Path:          /wordpress
Document Length:        237 bytes

Concurrency Level:      1000
Time taken for tests:   12.966 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Non-2xx responses:      10000
Total transferred:      4650000 bytes
HTML transferred:       2370000 bytes
Requests per second:    771.26 [#/sec] (mean)
Time per request:       1296.588 [ms] (mean)
Time per request:       1.297 [ms] (mean, across all concurrent requests)
Transfer rate:          350.23 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   47 206.0      0    1004
Processing:     5  136 1056.6     14   12899
Waiting:        1  136 1056.6     14   12899
Total:         10  183 1085.0     15   12947

Percentage of the requests served within a certain time (ms)
  50%     15
  66%     15
  75%     16
  80%     17
  90%     25
  95%   1208
  98%   1676
  99%   1679
 100%  12947 (longest request)
  • 在php服務器上經過 yum -y install php-cache 安裝cache:

  • systemctl reload php-fpm.service,並進行壓力測試,能夠看到訪問速度有了很大提高:
[root@happiness ~]# ab -n 10000 -c 1000 http://www.bbb.net/wordpress
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.bbb.net (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:        www.bbb.net
Server Port:            80

Document Path:          /wordpress
Document Length:        237 bytes

Concurrency Level:      1000
Time taken for tests:   1.973 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Non-2xx responses:      10000
Total transferred:      4650000 bytes
HTML transferred:       2370000 bytes
Requests per second:    5068.89 [#/sec] (mean)
Time per request:       197.282 [ms] (mean)
Time per request:       0.197 [ms] (mean, across all concurrent requests)
Transfer rate:          2301.79 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   56 220.6      4    1011
Processing:     5   39  90.4     13    1102
Waiting:        1   36  90.5     10    1101
Total:         10   95 245.2     16    1222

Percentage of the requests served within a certain time (ms)
  50%     16
  66%     17
  75%     19
  80%     20
  90%    220
  95%   1019
  98%   1029
  99%   1213
 100%   1222 (longest request)
  • 修改xcache配置文件,修改緩存大小爲100M,再進行壓力測試,能夠看到訪問速度略有提升:
[root@happiness ~]# vim /etc/php.d/xcache.ini
    xcache.size  =  100M

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

Benchmarking www.bbb.net (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:        www.bbb.net
Server Port:            80

Document Path:          /wordpress
Document Length:        237 bytes

Concurrency Level:      1000
Time taken for tests:   1.642 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Non-2xx responses:      10000
Total transferred:      4650000 bytes
HTML transferred:       2370000 bytes
Requests per second:    6088.82 [#/sec] (mean)
Time per request:       164.235 [ms] (mean)
Time per request:       0.164 [ms] (mean, across all concurrent requests)
Transfer rate:          2764.94 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   57 226.4      3    1011
Processing:     6   49 171.5     11    1613
Waiting:        1   47 171.7      8    1612
Total:          8  106 310.0     14    1636

Percentage of the requests served within a certain time (ms)
  50%     14
  66%     16
  75%     22
  80%     23
  90%    219
  95%   1028
  98%   1409
  99%   1418
 100%   1636 (longest request)
相關文章
相關標籤/搜索