天天看點

nginx反代後端動靜分離 php會話保持使用memcached

nginx排程器:

DR:172.18.11.111

]# yum -y install nginx

]# systemctl start nginx

php主機:部署phpwind程式、安裝memcached

RS1:172.18.11.112

RS2:172.18.11.113

]# yum -y install php

]# systemctl start php

]# yum -y install memcached

]# yum -y install libmemcached

]# yum -y install php-pecl-memcached

配置php将會話儲存至memcached中,確定如下兩個參數的值:

]# vim /etc/php.ini

<a href="http://s1.51cto.com/wyfs02/M00/82/10/wKiom1dJmrHTEzLjAABw6283y4Y763.png" target="_blank"></a>

<a href="http://s1.51cto.com/wyfs02/M02/82/0F/wKioL1dJm6rged9MAAB5Uw_tgGE142.png" target="_blank"></a>

測試建立php頁面setsess.php,為用戶端設定啟用session:

]# vim /var/www/html/showsess.php

<a href="http://s3.51cto.com/wyfs02/M00/82/0F/wKioL1dJm_uBQjbcAACIFYnsarY075.png" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M00/82/10/wKiom1dJmwOhAeZ2AAA5JvcQ8Pk287.png" target="_blank"></a>

web主機:

RS3:172.18.11.11

RS4:172.18.11.12

]# yum -y install httpd

]# systemctl start httpd

在php主機中,使用nfs共享存儲;

在172.18.11.112上提供共享目錄:

]# yum -y install nfs-utils

]# systemctl start nfs

]# vim /etc/exports

/var/www/html 172.18.11.0/24(rw,no_root_squash)

]# exportfs -r

把phpwind解壓在172.18.11.112

]# unzip /var/www/html/phpwind_UTF8_8.7.zip

]# cd /var/www/html/upload

]# chmod -R o+w attachment data html

]# mv install.php install.php.bak

在172.18.11.113、172.18.11.11/172.18.11.12上分别進行挂載共享目錄:

]# showmount -e 172.18.11.112 檢視可挂載共享檔案

]# yum -y install mariadb-server

]# systemctl start mariadb

]# mysql

&gt; CREATE DATABASE phpwind;

&gt; GRANT ALL ON phpwind.* TO phpwind@'172.18.11.%' IDENTIFIED BY 'phpwind';

&gt; FLUSH PRIVILEGES;

在DR上編輯nginx配置檔案:

]# vim /etc/nginx/conf.d/default.conf

<a href="http://s2.51cto.com/wyfs02/M00/82/0F/wKioL1dJnFOSd6xzAABQIS4th_o942.png" target="_blank"></a>

<a href="http://s2.51cto.com/wyfs02/M01/82/10/wKiom1dJm1qDSwCWAABvxf4GimI193.png" target="_blank"></a>

分别在後端各伺服器上啟動相應的服務;

重新整理多次:分别顯示為:會話ID交替保持

<a href="http://s3.51cto.com/wyfs02/M01/82/10/wKiom1dJm5eBHm61AAA8div67CA297.png" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M01/82/0F/wKioL1dJnJGiuMnIAABCvAqfJVY902.png" target="_blank"></a>

重新整理多次:會話ID不斷變化

<a href="http://s3.51cto.com/wyfs02/M02/82/10/wKiom1dJm7WRu7jjAABBYDgGp0c091.png" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M01/82/0F/wKioL1dJnK7wb8TCAABBI36nOaY649.png" target="_blank"></a>

測試phpwind

點選接受

<a href="http://s4.51cto.com/wyfs02/M02/82/10/wKiom1dJnA6jHHHuAALaml6SQNU908.png" target="_blank"></a>

<a href="http://s1.51cto.com/wyfs02/M02/82/0F/wKioL1dJnQijzo5yAAJpuW2rXeY783.png" target="_blank"></a>

<a href="http://s5.51cto.com/wyfs02/M02/82/0F/wKioL1dJnQmSCRevAAFB23iZOBk780.png" target="_blank"></a>

點選下一步

<a href="http://s5.51cto.com/wyfs02/M00/82/10/wKiom1dJnBGCdWJjAAE_8ovlaAo090.png" target="_blank"></a>

填入資料庫相關資訊,創始人資訊随意填寫

<a href="http://s5.51cto.com/wyfs02/M00/82/10/wKiom1dJnBLQlrW0AAI8ItOonIo080.png" target="_blank"></a>

<a href="http://s2.51cto.com/wyfs02/M00/82/0F/wKioL1dJnQziU2KLAAIA7z2GO6E446.png" target="_blank"></a>

<a href="http://s1.51cto.com/wyfs02/M01/82/10/wKiom1dJnBTyb9V6AAI9MGPuAWA576.png" target="_blank"></a>

<a href="http://s1.51cto.com/wyfs02/M00/82/0F/wKioL1dJnQ7BNqqDAALnN43UIYM829.png" target="_blank"></a>

此時,服務搭建完畢。

總結:

前端為一台nginx排程器,負責動靜分離排程到後端不同主機,後端兩台web伺服器負責響應靜态請求,兩台php伺服器負責響應動态請求,并且php會話儲存在memcached中,注意配置upstream server時location定義的通路的uri,而且要在http配置段定義,在server配置段調用,相關配置指令使用的位置不能寫錯地方,後端主機上各服務啟動配置好,在資料庫重建存放phpwind的相關資訊,php主機共享存儲使用nfs,建立共享目錄并導出,在另外所有後端伺服器挂載這個共享目錄,否則phpwind的靜态圖檔會顯示不了,以及php使用memcached都要配置好,配置檔案設定搞清楚後,就部署完成。

注意:在整個服務搭建過程中,要每做一步配置即時驗證服務配置是否成功,從單機測試,到跨主機測試,分步驟測試,不要等所有服務都配置好了再測試,那樣如果出現問題,不利于故障判斷與排除。

本文轉自 crystaleone 51CTO部落格,原文連結:http://blog.51cto.com/linsj/1784129,如需轉載請自行聯系原作者