天天看點

建構memcached服務 LNMP+memcached PHP實作session共享

1 案例1:建構memcached服務

1.1 問題

本案例要求先快速搭建好一台memcached伺服器,并對memcached進行簡單的增、删、改、查操作:

  • 安裝memcached軟體,并啟動服務
  • 使用telnet測試memcached服務
  • 對memcached進行增、删、改、查等操作

1.2 方案

memcached是高性能的分布式緩存伺服器,用來集中緩存資料庫查詢結果,減少資料庫通路次數,以提高動态Web應用的響應速度。通路拓撲如圖-1所示。

建構memcached服務 LNMP+memcached PHP實作session共享

圖-1

使用1台RHEL7虛拟機作為memcached伺服器(192.168.4.5)。

在RHEL7系統CD光牒中包含有memcached,是以需要提前配置yum源,即可直接使用yum安裝,用戶端測試時需要提前安裝telnet遠端工具。

驗證時需要用戶端主機安裝telnet,遠端memcached來驗證伺服器的功能:

  • add name 0 180 10 //變量不存在則添加
  • set name 0 180 10 //添加或替換變量
  • replace name 0 180 10 //替換
  • get name //讀取變量
  • append name 0 180 10 //向變量中追加資料
  • delete name //删除變量
  • stats //檢視狀态
  • flush_all //清空所有
  • 提示:0表示不壓縮,180為資料緩存時間,10為需要存儲的資料位元組數量。

1.3 步驟

實作此案例需要按照如下步驟進行。

步驟一:建構memcached服務

1)使用yum安裝軟體包memcached

  1. [[email protected] ~]# yum -y install memcached

  2. [[email protected] ~]# rpm -qa memcached

  3. memcached-1.4.15-10.el7_3.1.x86_64

2) memcached配置檔案(檢視即可,不需要修改)

  1. [[email protected] ~]# vim /usr/lib/systemd/system/memcached.service

  2. ExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS

  3. [[email protected] ~]# vim /etc/sysconfig/memcached

  4. PORT="11211"

  5. USER="memcached"

  6. MAXCONN="1024"

  7. CACHESIZE="64"

  8. OPTIONS=""

3)啟動服務并檢視網絡連接配接狀态驗證是否開啟成功:

netstat指令可以檢視系統中啟動的端口資訊,該指令常用選項如下:

-a顯示所有端口的資訊

-n以數字格式顯示端口号

-t顯示TCP連接配接的端口

-u顯示UDP連接配接的端口

-l顯示服務正在監聽的端口資訊,如httpd啟動後,會一直監聽80端口

-p顯示監聽端口的服務名稱是什麼(也就是程式名稱)

注意:在RHEL7系統中,使用ss指令可以替代netstat,功能與選項一樣。

  1. [[email protected] ~]# systemctl start memcached

  2. [[email protected] ~]# systemctl status memcached

  3. [[email protected] ~]# netstat -anptu | grep memcached

  4. tcp    0    0 0.0.0.0:11211        0.0.0.0:*        LISTEN        2839/memcached

  5. tcp    0    0 :::11211            :::*                LISTEN        2839/memcached

  6. udp    0    0 0.0.0.0:11211        0.0.0.0:*                    2839/memcached

  7. udp    0    0 :::11211            :::*                            2839/memcached

  8. [[email protected] ~]# setenforce 0

  9. [[email protected] ~]# firewall-cmd --set-default-zone=trusted

步驟二:使用telnet通路memcached伺服器 1)使用yum安裝telnet

  1. [[email protected] ~]# yum -y install telnet

2)使用telnet連接配接伺服器測試memcached伺服器功能,包括增、删、改、查等操作。

  1. [[email protected] ~]# telnet 192.168.4.5 11211

  2. Trying 192.168.4.5...

  3. ……

  4. ##提示:0表示不壓縮,180為資料緩存時間,3為需要存儲的資料位元組數量。

  5. set name 0 180 3                //定義變量,變量名稱為name

  6. plj                            //輸入變量的值,值為plj

  7. STORED

  8. get name                        //擷取變量的值

  9. VALUE name 0 3             //輸出結果

  10. plj

  11. END

  12. ##提示:0表示不壓縮,180為資料緩存時間,3為需要存儲的資料位元組數量。

  13. add myname 0 180 10            //建立,myname不存在則添加,存在則報錯

  14. set myname 0 180 10            //添加或替換變量

  15. replace myname 0 180 10        //替換,如果myname不存在則報錯

  16. get myname                    //讀取變量

  17. append myname 0 180 10        //向變量中追加資料

  18. delete myname                    //删除變量

  19. stats                        //檢視狀态

  20. flush_all                        //清空所有

  21. quit                            //登出                                 

2 案例2:LNMP+memcached

2.1 問題

沿用練習一,部署LNMP+memcached網站平台,通過PHP頁面實作對memcached伺服器的資料操作,實作以下目标:

  1. 部署LNMP實作PHP動态網站架構

  2. 為PHP安裝memcache擴充

  3. 建立PHP頁面,并編寫PHP代碼,實作對memcached的資料操作

2.2 方案

使用2台RHEL7虛拟機,其中一台作為memcached及LNMP伺服器(192.168.4.5)、另外一台作為測試用的Linux客戶機(192.168.4.100),如圖-1所示。

建構memcached服務 LNMP+memcached PHP實作session共享

圖-1 在RHEL7系統CD光牒中包含有我們需要的MariaDB、PHP,我們需要使用源碼安裝Nginx,使用RPM包安裝FPM。另外如果希望使用PHP來操作memcached,注意必須要為PHP安裝memcache擴充(php-pecl-memcache),否則PHP無法解析連接配接memcached的指令。用戶端測試時需要提前安裝telnet遠端工具。

2.3 步驟

實作此案例需要按照如下步驟進行。 步驟一:部署LNMP環境(如果環境中已經存在LNMP環境本步驟可以忽略) 1)使用yum安裝基礎依賴包

  1. [[email protected] ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel

  2. .. ..

2)源碼安裝Nginx

  1. [[email protected] ~]# tar -xf nginx-1.12.2.tar.gz

  2. [[email protected] ~]# cd nginx-1.12.2

  3. [[email protected] nginx-1.12.2]# ./configure \

  4. > --with-http_ssl_module

  5. [[email protected] nginx-1.12.2]# make && make install

3)安裝MariaDB資料庫

  1. [[email protected] ~]# yum -y install mariadb mariadb-server mariadb-devel

4)安裝PHP

  1. [[email protected] ~]# yum -y install php php-mysql

  2. [[email protected] ~]# yum -y install php-fpm-5.4.16-42.el7.x86_64.rpm

5)修改Nginx配置檔案

  1. [[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

  2. location / {

  3. root html;

  4. index index.php index.html index.htm;

  5. }

  6. location ~ \.php$ {

  7. root html;

  8. fastcgi_pass 127.0.0.1:9000;

  9. fastcgi_index index.php;

  10. # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  11. include fastcgi.conf;

  12. }

步驟二:啟動服務(如果所有服務已經啟動,也可以忽略這一步驟)

1)啟動Nginx服務

這裡需要注意的是,如果伺服器上已經啟動了其他監聽80端口的服務軟體(如httpd),則需要先關閉該服務,否則會出現沖突。

  1. [[email protected] ~]# systemctl stop httpd                //如果該服務存在,則關閉該服務

  2. [[email protected] ~]# /usr/local/nginx/sbin/nginx

  3. [[email protected] ~]# netstat -utnlp | grep :80

  4. tcp    0    0 0.0.0.0:80        0.0.0.0:*        LISTEN        32428/nginx

2)啟動MySQL服務

  1. [[email protected] ~]# systemctl start mariadb

  2. [[email protected] ~]# systemctl status mariadb

3)啟動PHP-FPM服務

  1. [[email protected] ~]# systemctl start php-fpm

  2. [[email protected] ~]# systemctl status php-fpm

4)關閉SELinux、防火牆

  1. [[email protected] ~]# setenforce 0

  2. [[email protected] ~]# firewall-cmd --set-default-zone=trusted

步驟三:建立PHP頁面,使用PHP語言測試memcached服務

1)部署測試頁面

建立PHP首頁文檔/usr/local/nginx/html/index.php,測試頁面可以參考lnmp_soft/php_scripts/mem.php:

  1. [[email protected] ~]# vim /usr/local/nginx/html/test.php

  2. <?php

  3. $memcache=new Memcache;                //建立memcache對象

  4. $memcache->connect('localhost',11211) or die ('could not connect!!');

  5. $memcache->set('key','test');          //定義變量

  6. $get_values=$memcache->get('key');     //擷取變量值

  7. echo $get_values;

  8. ?>

2)用戶端測試(結果會失敗) 用戶端使用浏覽器通路伺服器PHP首頁文檔,檢驗對memcached的操作是否成功:

  1. [[email protected] ~]# firefox http://192.168.4.5/test.php

注意:這裡因為沒有給PHP安裝擴充包,預設PHP無法連接配接memcached資料庫,需要給PHP安裝擴充子產品才可以連接配接memcached資料庫。

3)為PHP添加memcache擴充

  1. [[email protected] ~]# yum -y install php-pecl-memcache

  2. [[email protected] ~]# systemctl restart php-fpm

4)用戶端再次測試(結果會成功顯示資料結果)

  1. [[email protected] ~]# firefox http://192.168.4.5/test.php

3 案例3:PHP的本地Session資訊

3.1 問題

通過Nginx排程器負載後端兩台Web伺服器,實作以下目标:

  1. 部署Nginx為前台排程伺服器
  2. 排程算法設定為輪詢
  3. 後端為兩台LNMP伺服器
  4. 部署測試頁面,檢視PHP本地的Session資訊

3.2 方案

使用4台RHEL7虛拟機,其中一台作為Nginx前端排程器伺服器(eth0:192.168.4.5,eth1:192.168.2.5)、兩台虛拟機部署為LNMP伺服器,分别為Web1伺服器(192.168.2.100)和Web2伺服器(192.168.2.200),另外一台作為測試用的Linux客戶機(192.168.4.100),拓撲如圖-2所示。

建構memcached服務 LNMP+memcached PHP實作session共享

圖-2

3.3 步驟

實作此案例需要按照如下步驟進行。

步驟一:部署後端LNMP伺服器相關軟體

注意:以下部署LNMP伺服器的操作,需要在兩台後端伺服器做相同的操作,下面我們以一台Web1伺服器(192.168.2.100)為例,對Web2伺服器執行相同操作即可。

1)使用yum安裝基礎依賴包

  1. [[email protected] ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel

  2. .. ..

2)源碼安裝Nginx

  1. [[email protected] ~]# tar -xf nginx-1.12.2.tar.gz

  2. [[email protected] ~]# cd nginx-1.12.2

  3. [[email protected] nginx-1.12.2]# ./configure \

  4. > --with-http_ssl_module

  5. [[email protected] nginx-1.12.2]# make && make install

3)安裝MariaDB資料庫

  1. [[email protected] ~]# yum -y install mariadb mariadb-server mariadb-devel

4)安裝PHP(php-fpm軟體包在lnmp_soft中有提供)

  1. [[email protected] ~]# yum -y install php php-mysql

  2. [[email protected] ~]# yum -y install php-fpm-5.4.16-42.el7.x86_64.rpm

5)修改Nginx配置檔案(修改預設首頁與動靜分離)

  1. [[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

  2. location / {

  3. root html;

  4. index index.php index.html index.htm;

  5. }

  6. location ~ \.php$ {

  7. root html;

  8. fastcgi_pass 127.0.0.1:9000;

  9. fastcgi_index index.php;

  10. # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  11. include fastcgi.conf;

  12. }

步驟二:啟動LNMP伺服器相關的服務 1)啟動Nginx服務 這裡需要注意的是,如果伺服器上已經啟動了其他監聽80端口的服務軟體(如httpd),則需要先關閉該服務,否則會出現沖突。

  1. [[email protected] ~]# systemctl stop httpd                //如果該服務存在,則關閉該服務

  2. [[email protected] ~]# /usr/local/nginx/sbin/nginx

  3. [[email protected] ~]# netstat -utnlp | grep :80

  4. tcp    0    0 0.0.0.0:80        0.0.0.0:*        LISTEN        32428/nginx

2)啟動MySQL服務

  1. [[email protected] ~]# systemctl start mariadb

  2. [[email protected] ~]# systemctl status mariadb

3)啟動PHP-FPM服務

  1. [[email protected] ~]# systemctl start php-fpm

  2. [[email protected] ~]# systemctl status php-fpm

4)關閉SELinux、防火牆

  1. [[email protected] ~]# setenforce 0

  2. [[email protected] ~]# firewall-cmd --set-default-zone=trusted

步驟三:部署前端Nginx排程伺服器 1)使用源碼安裝nginx軟體(如果Nginx軟體包已存在可以忽略此步驟)

  1. [[email protected] ~]# yum -y install pcre-devel openssl-devel

  2. [[email protected] ~]# tar -xf nginx-1.12.2.tar.gz

  3. [[email protected] ~]# cd nginx-1.12.2

  4. [[email protected] nginx-1.12.2]# ./configure

  5. [[email protected] nginx-1.12.2]# make && make install

2)修改Nginx配置檔案

Nginx配置檔案中,通過upstream定義後端伺服器位址池,預設排程政策為輪詢,使用proxy_pass調用upstream定義的伺服器位址池:

  1. [[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

  2. .. ..

  3. upstream webs {

  4. server 192.168.2.100:80;

  5. server 192.168.2.200:80;

  6. }

  7. server {

  8. listen 80;

  9. server_name localhost;

  10. location / {

  11.              proxy_pass http://webs;

  12. root html;

  13. index index.php index.html index.htm;

  14. }

  15. }

3)重新加載配置檔案

  1. [[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

  2. #請先確定nginx是啟動狀态才可以執行指令成功,否則報錯,報錯資訊如下:

  3. #[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

4)關閉SELinux、防火牆

  1. [[email protected] ~]# setenforce 0

  2. [[email protected] ~]# firewall-cmd --set-default-zone=trusted

步驟四:測試環境是否配置成功 1)浏覽器通路測試頁面驗證。

  1. [[email protected] ~]# curl http://192.168.4.5/index.html        //檢視是否有資料

步驟五:部署測試頁面

1)部署測試頁面(Web1伺服器)。

測試頁面可以參考lnmp_soft/php_scripts/php-memcached-demo.tar.gz。

  1. [[email protected] ~]# cd lnmp_soft/php_scripts/

  2. [[email protected] php_scripts]# tar -xf php-memcached-demo.tar.gz

  3. [[email protected] php_scripts]# cd php-memcached-demo

  4. [[email protected] php-memcached-demo]# cp -a * /usr/local/nginx/html/

2)浏覽器直接通路後端伺服器的測試頁面(Web1伺服器)。

  1. [[email protected] ~]# firefox http://192.168.2.100            //填寫賬戶資訊

  2. [[email protected] ~]# cd /var/lib/php/session/            //檢視伺服器本地的Session資訊

  3. [[email protected] ~]# ls

  4. sess_ahilcq9bguot0vqsjtd84k7244                        //注意這裡的ID是随機的

  5. [[email protected] ~]# cat sess_ahilcq9bguot0vqsjtd84k7244

注意:可用修改index.php和home.php兩個檔案的内容,添加頁面顔色屬性,以差別後端兩台不同的伺服器:<body bgcolor=blue>。

3)部署測試頁面(Web2伺服器)。

測試頁面可以參考lnmp_soft/php_scripts/php-memcached-demo.tar.gz。

  1. [[email protected] ~]# cd lnmp_soft/php_scripts/

  2. [[email protected] php_scripts]# tar -xf php-memcached-demo.tar.gz

  3. [[email protected] php_scripts]# cd php-memcached-demo

  4. [[email protected] php-memcached-demo]# cp -a * /usr/local/nginx/html/

4)浏覽器直接通路後端伺服器的測試頁面(Web2伺服器)。

  1. [[email protected] ~]# firefox http://192.168.2.100         //填寫賬戶資訊

  2. [[email protected] ~]# cd /var/lib/php/session/            //檢視伺服器本地的Session資訊

  3. [[email protected] ~]# ls

  4. sess_qqek1tmel07br8f63d6v9ch401                        //注意這裡的ID是随機的

  5. [[email protected] ~]# cat sess_qqek1tmel07br8f63d6v9ch401    

注意:可用修改index.php和home.php兩個檔案的内容,添加頁面顔色屬性,以差別後端兩台不同的伺服器:<body bgcolor=green>。

5)浏覽器通路前端排程器測試(不同後端伺服器Session不一緻)。

推薦使用google浏覽器測試。

  1. [[email protected] ~]# google-chrome http://192.168.4.5

  2. //填寫注冊資訊後,重新整理,還需要再次注冊,說明兩台計算機使用的是本地Session

  3. //第二台主機并不知道你再第一台主機已經登入,第一台主機的登入資訊也沒有傳遞給第二台主機

4 案例4:PHP實作session共享

4.1 問題

沿用練習三,通過修改PHP-FPM配置檔案,實作session會話共享,本案例需要在練習三的基礎上實作:

  • 配置PHP使用memcached伺服器共享Session資訊

  • 用戶端通路兩台不同的後端Web伺服器時,Session 資訊一緻

4.2 方案

在練習三拓撲的基礎上,Nginx伺服器除了承擔排程器外,還需要擔任memcached資料庫的角色,并在兩台後端LNMP伺服器上實作PHP的session會話共享。拓撲結構如圖-4所示。

建構memcached服務 LNMP+memcached PHP實作session共享

圖-4

4.3 步驟

實作此案例需要按照如下步驟進行。 步驟一:建構memcached服務 1)安裝Memcached服務(如果192.168.4.5上已經有本軟體包,此步驟可以忽略)

  1. [[email protected] ~]# yum -y install memcached

2)啟動服務并檢視網絡連接配接狀态驗證是否開啟成功:

  1. [[email protected] ~]# systemctl restart memcached

  2. [[email protected] ~]# netstat -anptu | grep memcached

  3. tcp    0    0 0.0.0.0:11211        0.0.0.0:*        LISTEN        2839/memcached

  4. tcp    0    0 :::11211            :::*                LISTEN        2839/memcached

  5. udp    0    0 0.0.0.0:11211        0.0.0.0:*                    2839/memcached

  6. udp    0    0 :::11211            :::*                            2839/memcached

3)關閉SELinux、防火牆

  1. [[email protected] ~]# setenforce 0

  2. [[email protected] ~]# firewall-cmd --set-default-zone=trusted

步驟二:在後端LNMP伺服器上部署Session共享

注意:這些操作在兩台後端Web伺服器上均需要執行,以下操作以Web1(192.168.2.100)伺服器為例。

1)為PHP添加memcache擴充

注意,因為後端兩台web伺服器(web1,web2)都需要連接配接memcached資料庫,是以兩台主機都需要安裝PHP擴充子產品(下面也web為例)。

  1. [[email protected] ~]# yum -y install php-pecl-memcache

2)修改PHP-FPM配置檔案,并重新開機服務 注意,因為後端兩台web伺服器(web1,web2)都需要修改配置檔案(下面也web為例)。

  1. [[email protected] ~]# vim /etc/php-fpm.d/www.conf            //修改該配置檔案的兩個參數

  2. //檔案的最後2行

  3. 修改前效果如下:

  4. php_value[session.save_handler] = files

  5. php_value[session.save_path] = /var/lib/php/session

  6. //原始檔案,預設定義Sessoin會話資訊本地計算機(預設在/var/lib/php/session)

  7. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  8. 修改後效果如下:

  9. php_value[session.save_handler] = memcache

  10. php_value[session.save_path] = "tcp://192.168.2.5:11211"

  11. //定義Session資訊存儲在公共的memcached伺服器上,主機參數中為memcache(沒有d)

  12. //通過path參數定義公共的memcached伺服器在哪(伺服器的IP和端口)

  13. [[email protected] ~]# systemctl restart php-fpm

步驟三:用戶端測試

用戶端使用浏覽器通路兩台不同的Web伺服器。

操作步驟與練習三一緻,最終可以獲得相關的Session ID資訊。

繼續閱讀