16_构建memcached服务、LNMP+memcached、PHP的本地Session信息、PHP实现session共享

proxy   10.10.11.10
client  10.10.11.11
web1    10.10.11.12
web2    10.10.11.13

proxy:
1.构建memcached服务
]# yum -y install memcached
]# cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
]# systemctl start memcached
]# systemctl enable memcached
]# netstat -anptu | grep memcached
 
2.使用telnet访问memcached服务器
]# yum -y install telnet
]# telnet 10.10.11.10 11211
##提示:0表示不压缩,180为数据缓存时间,3为需要存储的数据字节数量。
set name 0 180 3 # 定义变量,变量名称为name
aaa              # 输入变量的值,值为aaa               
STORED
get name         # 获取变量的值
VALUE name 0 3   # 输出结果
aaa
END
 
add myname 0 180 10      # 新建,myname不存在则添加,存在则报错
wwwwwwwwww
STORED

set myname 0 180 5       # 添加或替换变量
wwwww
STORED

replace myname 0 180 3   # 替换,如果myname不存在则报错
ttt
STORED

get myname               # 读取变量
ttt
END

append myname 0 180 5   # 向变量中追加数据
ooooo
STORED
get myname
VALUE myname 0 8
tttooooo

delete myname  # 删除变量
DELETED

stats          # 查看状态
flush_all      # 清空所有
OK

quit           # 退出登录                                  
 
3.LNMP+memcached(web1 web2)
3.1 部署nginx(前面有)
3.2 部署mariadb
]# yum -y install mariadb mariadb-server mariadb-devel
]# systemctl start  mariadb
]# systemctl enable mariadb
]# mysqladmin -uroot -p password "123456"
]# mysql -uroot -p123456

3.3 部署PHP
]# yum -y install php php-mysql php-fpm php-pecl-memcache
]# systemctl start php-fpm
]# systemctl enable php-fpm

nginx开启php
]# vim /etc/nginx/conf.d/default.conf
...
location / {
        root   /usr/share/nginx/html;
        index index.php  index.html index.htm;
    }
...
location ~ \.php$ {
        root           /usr/share/nginx/html;   #绝对路径
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
]# nginx -s reload

连接memcache数据库
]# vim /usr/share/nginx/html/test.php
<?php
$memcache=new Memcache;             # 创建memcache对象
$memcache->connect(‘10.10.11.10‘,11211) or die (‘could not connect!!‘);
$memcache->set(‘key‘,‘test‘);       # 定义变量
$get_values=$memcache->get(‘key‘);  # 获取变量值
echo $get_values;
?>

client检测:
]# firefox http://10.10.11.12/test.php
 
4.PHP的本地Session信息
通过Nginx调度器负载后端两台Web服务器
部署Nginx为前台调度服务器
调度算法设置为轮询
后端为两台LNMP服务器
部署测试页面,查看PHP本地的Session信息
 
4.1 proxy搭建nginx(前面有)
web1
]# echo "web1" > /usr/share/nginx/html/index.html
web2
]# echo "web2" > /usr/share/nginx/html/index.html
4.2 7层调度
]# vim /etc/nginx/nginx.conf
.. ..
http {
.. ..
#使用upstream定义后端服务器集群,集群名称任意(如webserver)
#使用server定义集群中的具体服务器和端口
upstream webserver {
       server 10.10.11.12:80;
       server 10.10.11.13:80;
        }
.. ..

]# vim /etc/nginx/conf.d/default.conf
server {
        listen     80;
        server_name  www.a.com;
#通过proxy_pass将用户的请求转发给webserver集群
        location / {
            proxy_pass http://webserver;
        }
...
]# nginx -s reload

client测试:(默认轮询)
]# curl http://10.10.11.10
web1
]# curl http://10.10.11.10
web2
]# curl http://10.10.11.10
web1
]# curl http://10.10.11.10
web2
 
4.3 部署测试页面(session)
web1 web2
]# cd php-memcached-demo
]# cp -a * /usr/share/nginx/html/
]# ls
50x.html  images      index.php  README.md  
home.php  index.html  login.php  style.css
web1:(proxy轮询,client方便查看是哪台服务器)
]# vim index.php  --> <body bgcolor="red">
]# vim home.php   --> <body bgcolor="red">
web2:
]# vim index.php  --> <body bgcolor="blue">
]# vim home.php   --> <body bgcolor="blue">
真机goole chrome查看:
http://10.10.11.10/index.php(F5刷新查看登陆页面轮询)
输入账户、密码(2次,以便调度器在两台web服务器都存储session信息)
登陆后:(F5刷新查看登陆页面轮询)
 
5. PHP实现session共享
沿用4,通过修改PHP-FPM配置文件,实现session会话共享.
配置PHP使用memcached服务器共享Session信息.
客户端访问两台不同的后端Web服务器时,Session 信息一致.
在练习三拓扑的基础上,Nginx服务器除了承担调度器外,还需要担任memcached数据库的角色,并在两台后端LNMP服务器上实现PHP的session会话共享。
5.1 部署memcache
]# yum -y install memcached
]# systemctl start memcached
]# systemctl enable memcached
]# netstat -anptu | grep memcached
5.2 在后端LNMP服务器上部署Session共享
web1 web2
]# vim /etc/php-fpm.d/www.conf  //文件的最后2行
 
修改前效果如下:
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
//原始文件,默认定义Sessoin会话信息本地计算机(默认在/var/lib/php/session)
 
修改后效果如下:
php_value[session.save_handler] = memcache
php_value[session.save_path] = "tcp://10.10.11.10:11211"
//定义Session信息存储在公共的memcached服务器上,主机参数中为memcache(没有d)
//通过path参数定义公共的memcached服务器在哪(服务器的IP和端口)
]# systemctl restart php-fpm
 
真机goole chrome查看:
(F5刷新查看登陆页面轮询)
http://10.10.11.10/index.php
如果出现错误,查看php日志:
]# ls /var/log/php-fpm/
error.log  www-error.log