Centos 7.3部署LAMP架構動靜分離

博文大綱:

  • 一、LAMP動靜分離的概念
  • 二、LAMP的安裝與配置
  • 三、web網站壓力測試
  • 四、部署PHP加速軟件Xcache
  • 五、部署bbs論壇

一、LAMP動靜分離的概念

當需要搭建一個高效的web架構時,採用動靜分離無疑是最好的選擇,這篇博文將寫下來LAMP的動靜分離部署方式。

所謂LAMP?LAMP架構是目前最成熟的企業網站應用模式之一,指的是協同工作的一整套系統和相關軟件,能夠提供動態web站點服務及其應用開發環境。與之並肩的還有LNMP、LTMP等,LAMP說白了就是Linux操作系統上安裝Apache網站服務,構建php/perl/Python運行環境來連接mysql數據庫,四個組件合起來就簡稱“LAMP”。 LNMP只不過是用Nginx來搭建了這個httpd服務。

PHP在LAMP環境下共有三種工作模式:CGI 模式、apache 模塊、FastCGI (FCGI)模式。CGI 模式下運行 PHP,性能不是很好。FastCGI 的方式和 apache 模塊的不同點在於:FastCGI 方式 PHP 是一處獨立的進程,所有 PHP 子進程都由 PHP 的一個叫做php-fpm 的組件負責管理;而 apache 模塊化方式運行的 PHP,則是 apache 負責調用 PHP 完成工作。PHP 的 FastCGI 方式性能要比 apache模塊化方式強很多。

這裏將以 FastCGI 方式編譯安裝 LAMP架構。

FastCGI 的工作機制:
客戶端發起請求,請求分爲 2 種,一種是靜態請求它可以直接由 Apache 直接響應返回;另一種是動態的請求,如其中包含中 php或者 Perl 這種腳本解釋性語言,則由 Apache 服務器通過fastcgi協議調用php服務器執行並返回給Apache由Apache返回解釋執行後的結果,如果這個過程中涉及到對數據的操作,此時 php 服務器還會還會通過 mysql 協議調用 mysql服務器。

示意圖如下:
Centos 7.3部署LAMP架構動靜分離

二、LAMP的安裝與配置

1、環境準備

我這裏環境如下:
Centos 7.3部署LAMP架構動靜分離
我這裏已經存在了Apache服務器和MySQL服務器,自行部署即可,可參考博文:基於centos7搭建MySQL數據庫;’Apache安裝部署及工作模式詳解

2、對PHP服務器進行部署
這裏直接開始部署PHP服務器
下載我提供的源碼包,然後上傳至PHP服務器


#以下爲安裝PHP及其依賴
[root@php ~ ]# tar zxf libmcrypt-2.5.7.tar.gz -C /usr/src
[root@php ~]# tar zxf php-5.6.27.tar.gz -C /usr/src
[root@php ~]# yum -y install libxml2-devel openssl-devel bzip2-devel
[root@php ~]# cd /usr/src/libmcrypt-2.5.7
[root@php libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt && make && make install
[root@php libmcrypt-2.5.7]# cd ../php-5.6.27/
[root@php php-5.6.27]# ./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts && make && make install

上面編譯安裝PHP的選項作用解釋如下:

  • --prefix=/usr/local/php5.6 #安裝位置
  • --with-mysql=mysqlnd #支持 mysql
  • --with-pdo-mysql=mysqlnd #支持 pdo 模塊
  • --with-mysqli=mysqlnd #支持 mysqli 模塊
    #上面的三選項的作用:數據庫與 php 不在一個服務器上,指定此種方式,安裝數據庫連接驅動。
  • --with-openssl #支持 openssl 模塊
  • --enable-fpm #支持 fpm 模式
  • --enable-sockets #啓用 socket 支持
  • --enable-sysvshm #啓用系統共享內存支持
  • --enable-mbstring #多字節字串、像我們的中文就是多字節字串
  • --with-freetype-dir #支持 freetype、就要裝 freetype-devel、跟字體相關的、字體解析工具
  • --with-jpeg-dir
  • --with-png-dir
    #上面的二選項的作用:處理 jpeg、png 圖片的、php 可以動態生成 jpeg 圖片
  • --with-zlib #是個壓縮庫、在互聯網傳輸時用來壓縮傳輸的
  • --with-libxml-dir=/usr #這個 libxml 是用來解析 xml 的、指定/usr 下
  • --enable-xml #支持 xml 的
  • --with-mhash #支持 mhash
  • --with-mcrypt=/usr/local/libmcrypt #libmcrypt-devel 這個程序包所指定的
  • --with-config-file-path=/etc #指定配置文件的存放路徑的
  • --with-config-file-scan-dir=/etc/php.d #配置文件掃描路徑
  • --with-bz2 #支持 BZip2

爲了支持 apache 的 worker 或 event 這兩個 MPM,編譯時使用了--enable-maintainer-zts 選項。


#以下爲調整PHP的配置文件及控制服務的啓停
[root@php php-5.6.27]# cp php.ini-production /etc/php.ini    #複製源碼中中提供的PHP配置文件
[root@php php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
#複製其服務控制腳本文件
[root@php php-5.6.27]# chmod +x /etc/init.d/php-fpm    #賦予執行權限
[root@php php-5.6.27]# chkconfig --add php-fpm    #添加爲系統服務,以便支持systemctl管理
[root@php php-5.6.27]# chkconfig php-fpm on   #開啓
#複製php-fpm提供的默認配置文件並編輯它
[root@php php-5.6.27]# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
[root@php php-5.6.27]# vim /usr/local/php5.6/etc/php-fpm.conf
listen = 192.168.20.5:9000    #監聽地址是本機的IP9000端口
pm.max_children = 50         #最大啓動的進程數
pm.start_servers = 5          #初始啓動進程數
pm.min_spare_servers = 5     #最小空閒進程
pm.max_spare_servers = 35     #最大空閒進程
#修改完成後,保存退出即可
[root@php php-5.6.27]# systemctl start php-fpm    #啓動PHP服務
[root@php php-5.6.27]# netstat -anput | grep php-fpm     #確認其9000端口以啓動
[root@php ~]# mkdir -p /var/www/html        #創建其網頁存放目錄,須和apache服務器的網頁存放路徑一樣
[root@php ~]# firewall-cmd --permanent --add-port=9000/tcp   #防火牆放行9000端口的流量
[root@php ~]# firewall-cmd --reload    #重載以便生效

3、配置apache服務器(切換到apache服務器進行以下操作)


[root@apache ~]# cd /usr/local/http-2.4.23/conf/
[root@apache conf]# vim httpd.conf     #編輯apache服務的主配置文件
#啓用以下兩個模塊(將下面兩行前面的“#”號去掉即可)
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
Include conf/extra/httpd-vhosts.conf        #啓用虛擬主機的配置文件
               ..............#省略部分內容
        AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
#定位到上面兩行(注意,這兩行一定是沒有被註釋的),然後寫入下面兩行配置,讓apache可以識別php格式的頁面
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
<IfModule dir_module>      #定位到該標籤
    DirectoryIndex index.php index.html   #在index.html前添加index.php
</IfModule>
#至此保存退出即可
[root@apache conf]# vim extra/httpd-vhosts.conf     #編輯虛擬主機配置文件
#虛擬主機的配置文件如下
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html"
    ServerName www.test.com
    ErrorLog "logs/test-error_log"
    CustomLog "logs/test-access_log" common
    ProxyRequests Off
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://192.168.20.5:9000/var/www/html/$1
<Directory "/var/www/html">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
#編輯完成後,保存退出
[root@apache conf]# mkdir -p /var/www/html/    #創建網頁根目錄
[root@apache conf]# echo this is a test file >> /var/www/html/index.html
[root@apache conf]# apachectl restart    #重啓apache服務

以上虛擬主機的配置文件解釋如下:

  • ProxyRequests off #關閉正向代理
  • ProxyPassMatch #把以.php 結尾的文件請求發送到 php-fpm 進程,php-fpm 至少需要知道運行的目錄和 URI,所以這裏直接在 fcgi://192.168.20.5:9000 後指明瞭這兩個參數,其它的參數的傳遞已經被 mod_proxy_fcgi.so 進行了封裝,不需要手動指定。
  • 特別注意的是,/var/www/html/需要與<VirtualHost >中的 DocumentRoot 後的路徑一致
  • ProxyPassMatch #只有滿足特定正則模式的內容纔會匹配並執行此規則,這裏的模式是,^/(..php(/.)?)$ ,從網站(虛擬主機<VirtualHost >的根目錄開始,匹配任何以 .php 結尾,或者在 .php 之後緊跟一個 / 再跟別的內容的路徑。
  • ^ (caret) 和 $ (dollar)標誌要匹配的路徑的開始和結束
  • ( )括號裏的內容可以用 $1 來表示,以方便後面引用它。
  • fcgi://192.168.20.5:9000 通過 mod_proxy_fcgi 來轉發的代理,使用 fastCGI 協議,轉到PHP-FPM 監聽的端口。
  • /var/www/html #非常重要!必須與虛擬主機的路徑匹配,且必須是對應 php 文件在操作系統中的絕對路徑,否則會找不到文件。

4、回到PHP服務器,測試LAMP環境

#編寫以下兩個網頁文件
[root@php ~]# cat /var/www/html/index.php   #用來顯示PHP的版本信息
<?php
phpinfo();
?>
[root@php ~]# cat /var/www/html/test.php    #用來測試連接數據庫
<?php
$link=mysqli_connect('192.168.20.6','lvjz','pwd@123');
if($link) echo "恭喜你,數據庫連接成功!!!"; else echo "connect shibai";
mysqli_close($link);
?>

5、在MySQL服務器上添加用戶,並賦予遠程登錄的權限

[root@localhost src]# mysql -uroot -p    
Enter password:            #驗證數據庫用戶密碼
mysql> create database bbs;        #創建專用的數據庫
Query OK, 1 row affected (0.00 sec)
mysql> grant all on bbs.* to [email protected] identified by 'pwd@123';  #授權用戶爲lvjz
Query OK, 0 rows affected (0.01 sec)

Centos 7.3部署LAMP架構動靜分離

客戶端訪問web服務器的www.test.com/test.php 進行測試:

Centos 7.3部署LAMP架構動靜分離
看到上面兩個測試頁說明 apache、php、mysql 之間可以協同工作了

三、web網站壓力測試

網站性能壓力測試是服務器網站性能調優過程中必不可缺少的一環。只有讓服務器處在高壓情況下,才能真正體現出軟件、硬件等各種設置不當所暴露出的問題。

性能測試工具目前最常見的有以下幾種:ab、http_load、webbench、siege。我比較習慣用apahce自帶的ab工具。

ab 非常實用,它不僅可以對 apache 服務器進行網站訪問壓力測試,也可以對或其它類型的服務器進行壓力測試。比如 nginx、tomcat、IIS 等。

1、ab 的原理

ab 命令會創建多個併發訪問線程,模擬多個訪問者同時對某一 URL 地址進行訪問。它的測試目標是基於 URL 的,因此,它既可以用來測試 apache 的負載壓力,也可以測試 nginx、lighthttp、tomcat、IIS 等其它 Web 服務器的壓力。
ab 命令對發出負載的計算機要求很低,它既不會佔用很高 CPU,也不會佔用很多內存。但卻會給目標服務器造成巨大的負載,其原理類似 CC gong擊。自己測試使用也需要注意,否則一次上太多的負載。可能造成目標服務器資源耗完,嚴重時甚至導致死機。

2、ab的安裝

ab 的安裝非常簡單,如果是源碼安裝 apache 的話,那就更簡單了。apache 安裝完畢後 ab命令存放在 apache 安裝目錄的 bin 目錄下。如下:
/usr/local/http2.4.23/bin/ab。
如果 apache 是通過 yum 的 RPM 包方式安裝的話,ab 命令默認存放在/usr/bin 目錄下。如下:
which ab
注意:如果不想安裝 apache 但是又想使用 ab 命令的話,我們可以直接安裝 apache 的工具包 httpd-tools。如下:
yum -y install httpd-tools
查看 ab 是否安裝成功,可以切換到上述目錄下,使用 ab –V 命令進行檢測。


[root@apache conf]# ab -V     #好像報錯了
ab: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory
[root@apache conf]# export    LD_LIBRARY_PATH="/usr/local/openssl/lib/"  
#執行該命令,設置環境變量即可
[root@apache conf]# ab -V
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
               .................... #省略部分內容

3、ab壓力測試


[root@apache ~]# ab -c 500 -n 1000 127.0.0.1/index.html   #對本機的靜態網頁進行測試
#-n:在測試會話中所執行的請求個數(即總請求數)。
#-c:一次產生的請求個數(即 併發用戶數 )。
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software:        Apache/2.4.23
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /index.html     #請求的資源
Document Length:        20 bytes     #HTTP 響應數據的正文長度

Concurrency Level:      500    #併發個數(併發用戶數)
Time taken for tests:   2.093 seconds   #所有這些請求處理完成所花費的時間
Complete requests:      1000    #完成請求數
Failed requests:        0        #失敗的請求數
Total transferred:      264000 bytes    
#表示所有請求的響應數據長度總和,包括每個 HTTP響應數據的頭信息和正文數據的長度。
HTML transferred:       20000 bytes  #網頁文件的大小(就是去除響應頭部後的大小) 
Requests per second:    477.87 [#/sec] (mean)  
#吞吐量,計算方式:請求的次數/用戶等待時間(Complete requests/Time taken for tests)
Time per request:       1046.320 [ms] (mean)   
#用戶平均等待一個頁面的時間,計算方式:用戶等待時間/完成請求的次數(Complete requests/Concurrency Level)
Time per request:       2.093 [ms] (mean, across all concurrent requests)
#服務器處理一個請求花費的時間,計算方式:用戶等待時間/完成請求的次數(Time taken for tests/Complete requests)
Transfer rate:          123.20 [Kbytes/sec] received
#用戶請求的數據大小,計算方式:數據的總長度/用戶等待時間(Total trnasferred/ Time taken for tests)
#這個統計很好的說明服務器的處理能力達到極限時,其出口寬帶的需求量。(即平均每秒網絡上的流量)

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1  252 401.1    109    1262
Processing:    13  245 224.7    230     817
Waiting:        2  225 208.0    230     816
Total:         20  498 559.7    243    1812

Percentage of the requests served within a certain time (ms)
  50%    243
  66%    496
  75%    691
  80%    834
  90%   1760
  95%   1760
  98%   1762
  99%   1762
 100%   1812 (longest request)
以上部分的數據用於描述每個請求處理時間的分佈情況,比如以上測試,80%的請求處理時間都不超過 834ms,這個處理時間是指前面的 Time per request,即對於單個用戶而言,平均每個請求的處理時間。

4、ab性能指標

在進行性能測試過程中有幾個指標比較重要:
1)吞吐率(Requests per second)
服務器併發處理能力的量化描述,單位是 reqs/s,指的是在某個併發用戶數下單位時間內處理的請求數。某個併發用戶數下單位時間內能處理的最大請求數,稱之爲最大吞吐率。
注:吞吐率是基於併發用戶數的。這句話代表了兩個含義:

  • 吞吐率和併發用戶數相關;
  • 不同的併發用戶數下,吞吐率一般是不同的。

計算公式:總請求數/處理完成這些請求數所花費的時間,即
Request per second=Complete requests/Time taken for tests
必須要說明的是,這個數值表示當前機器的整體性能,值越大越好。
2)併發連接數(The number of concurrent connections)
併發連接數指的是某個時刻服務器所接受的請求數目,簡單的講,就是一個會話。
3)併發用戶數(Concurrency Level)
要注意區分這個概念和併發連接數之間的區別,一個用戶可能同時會產生多個會話,也即連接數。
4)用戶平均請求等待時間(Time per request)
計算公式:處理完成所有請求數所花費的時間/(總請求數/併發用戶數),即:
Time per request=Time taken for tests/(Complete requests/Concurrency Level)
5)服務器平均請求等待時間(Time per request:across all concurrent requests)
計算公式:處理完成所有請求數所花費的時間/總請求數,即:
Time taken for/testsComplete requests
可以看到,它是吞吐率的倒數。
同時,它也等於用戶平均請求等待時間/併發用戶數,即
Time per request/Concurrency Level

四、部署PHP加速軟件Xcache

Xcache是一款用來爲PHP頁面做緩存的工具。當然,實際工作環境中,是不會對PHP動態頁面來做緩存的,意義不大(動態頁面嘛,數據更新較快)又佔用內存空間。這裏只是爲了表現出,PHP動態頁面,也是可以做緩存的。

1、安裝Xcache

在對PHP部署時,我提供了一個下載的鏈接,其中包含了這個xcache這個源碼包,上傳至PHP服務器即可。


[root@php ~]# tar zxf xcache-3.2.0.tar.gz -C /usr/src
[root@php ~]# cd /usr/src/xcache-3.2.0/
[root@php xcache-3.2.0]# /usr/local/php5.6/bin/phpize   #使用PHP中的phpize生成configure文件
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226

[root@php xcache-3.2.0]# ./configure  --enable-xcache  --enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/php5.6/bin/php-config && make && make install
#安裝完成後,記住結尾提示的路徑,以後會用到,如下:
/usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/

2、創建Xcache緩存文件

[root@php ~]# touch /tmp/xcache
[root@php ~]# chmod 777 /tmp/xcache    #保證有寫入權限

3、複製Xcache後臺管理程序到網站根目錄


[root@php xcache-3.2.0]# cp -r htdocs/ /var/www/html/xcache

4、配置PHP支持Xcache

[root@php xcache-3.2.0]# vim /etc/php.ini  #編輯PHP的主配置文件
#在文件末尾添加以下內容
[xcache-common]
extension = /usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/xcache.so
#這就是指定安裝Xcache後返回的目錄
[xcache.admin]
xcache.admin.enable_auth = Off
[xcache]
xcache.shm_scheme ="mmap"
xcache.size=60M
xcache.count =1
xcache.slots =8K
xcache.ttl=0
xcache.gc_interval =0
xcache.var_size=64M
xcache.var_count =1
xcache.var_slots =8K
xcache.var_ttl=0
xcache.var_maxttl=0
xcache.var_gc_interval =300
xcache.test =Off
xcache.readonly_protection = Off
xcache.mmap_path ="/tmp/xcache"
xcache.coredump_directory =""
xcache.cacher =On
xcache.stat=On
xcache.optimizer =Off
[xcache.coverager]
xcache.coverager =On
xcache.coveragedump_directory ="" 
[root@php xcache-3.2.0]# systemctl restart php-fpm   #重啓PHP
[root@php xcache-3.2.0]# vim /etc/exports   #設置nfs

/var/www/html/       192.168.20.0/24(rw,sec=sys,sync,no_root_squash)

5、回到apache服務器,掛載nfs共享目錄,以便同步網頁根目錄下的文件

[root@apache ~]# showmount -e 192.168.20.5  #確認可以查看到PHP共享的目錄
Export list for 192.168.20.5:
/var/www/html 192.168.20.0/24
[root@apache ~]# mount -t nfs 192.168.20.5:/var/www/html/ /var/www/html/
#掛載
[root@apache ~]# df -hT /var/www/html/    #確認已掛載
文件系統                   類型  容量  已用  可用 已用% 掛載點
192.168.20.5:/var/www/html nfs4   39G  5.0G   34G   13% /var/www/html

6、測試

訪問www.test.com/xcache ,即可看到如下頁面:

Centos 7.3部署LAMP架構動靜分離

至此,Linux 下安裝 php 加速軟件 Xcache 完成。

7、對apache的動態頁面進行測試

[root@apache ~]# export LD_LIBRARY_PATH="/usr/local/openssl/lib/" 
[root@apache ~]# ab -c 100 -n 1000 http://192.168.20.4/index.php        #進行第一次測試
          ......#省略部分內容
Time taken for tests:   4.292 seconds
         ......#省略部分內容
Requests per second:    232.99 [#/sec] (mean)
          ......#省略部分內容
[root@apache ~]# ab -c 100 -n 1000 http://192.168.20.4/index.php        #進行第二次測試
         ......#省略部分內容
Time taken for tests:   1.631 seconds
          ......#省略部分內容
Requests per second:    613.29 [#/sec] (mean)
         ......#省略部分內容

查看Xcache的命中率:

Centos 7.3部署LAMP架構動靜分離

五、部署bbs論壇

1、在PHP服務器上進行以下操作


bbs論壇的源碼包爲Discuz_7.0.0_FULL_SC_UTF8.zip,同樣在我文章開頭的鏈接中可以提取,並上傳至PHP服務器。

[root@php ~]# unzip Discuz_7.0.0_FULL_SC_UTF8.zip    #解壓
[root@php Discuz_7.0.0_FULL_SC_UTF8]# cp -r upload/ /var/www/html/bbs
[root@php Discuz_7.0.0_FULL_SC_UTF8]# chmod -R 777 /var/www/html/bbs/
[root@php ~]# vim /etc/php.ini     #修改PHP配置文件
short_open_tag = On   #將原來的Off修改爲On
[root@php xcache-3.2.0]# systemctl restart php-fpm   #重啓PHP

2、配置bbs(使用之前用來測試鏈接數據庫的用戶給bbs論壇使用即可)

瀏覽器訪問www.test.com/bbs/install :

Centos 7.3部署LAMP架構動靜分離
Centos 7.3部署LAMP架構動靜分離
Centos 7.3部署LAMP架構動靜分離
Centos 7.3部署LAMP架構動靜分離
Centos 7.3部署LAMP架構動靜分離

——————感謝閱讀~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章