这里有关memcached的详细介绍以及LNMP的环境搭建步骤就不做演示了,前面篇章有介绍到
详见:https://blog.csdn.net/weixin_42104231/article/details/83691795
https://blog.csdn.net/weixin_42104231/article/details/83589487
实验环境:四台虚拟机,一:网卡一192.168.4.5网卡二192.168.2.5作为nginx反向代理服务器兼memcache缓存服务器 ,二 :192.168.4.100 作为客户端主机,用于访问web ,第三台192.168.2.100 第四台192.168.2.200 为LNMP环境下后端web服务器 ,系统为rhel7.4
1,后端web服务器环境部署
(1) 查看LNMP环境(web1:192.168.2.100)保证nginx,php都处于监听状态:
[root@web1 ~]# ss -antulp | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=31344,fd=6),("nginx",pid=17188,fd=6))
[root@web1 ~]# ss -antulp | grep 9000
tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=18892,fd=0),("php-fpm",pid=18891,fd=0),("php-fpm",pid=18890,fd=0),("php-fpm",pid=18889,fd=0),("php-fpm",pid=18888,fd=0),("php-fpm",pid=18886,fd=6))
[root@web1 ~]# ss -antulp | grep 3306
tcp LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=5569,fd=14))
(2)安装php连接memcache的软件包
[root@web1 ~]# yum -y install php-pecl-memcache
(3)编写php动态页面
模拟登录页面
[root@web1 ~]# cat /usr/local/nginx/html/index.php
<!DOCTYPE html>
<html>
<head>
<title>Login Form in PHP with Session</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor=read>
<body>
<div id="main">
<h1>PHP Login Session Example</h1>
<div id="login">
<h2>Login Form</h2>
<form action="login.php" method="post">
<label>UserName :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password">
<input type ="submit">
</form>
</div>
</div>
</body>
</html>
登录后页面
[root@web1 ~]# cat /usr/local/nginx/html/login.php
<?php
session_start(); # Starting Session
$username=$_POST['username'];
$password=$_POST['password'];
# Assume user authenticated successfully. Store session variables
$_SESSION['login_user']= $username;
$_SESSION['logged_in'] = "true";
$_SESSION['id'] = session_id();
header("Location: home.php");
?>
(4)修改nginx配置文件
[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
……
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
……
(5 )修改php有关session的配置文件,重启php
[root@web1 ~]# vim /etc/php-fpm.d/www.conf #修改最后两行如下
…………
php_value[session.save_handler] = memcache
php_value[session.save_path] = "tcp://192.168.2.5:11211"
##指定session存储路径为192.168.2.5的memcache数据库
##本机的/var/lib/php/session/下将不会存储新的信息
[root@web1 ~]# systemctl restart php-fpm
(6 )将web2 192.168.2.200按以上步骤进行操作。
2 部署反向代理服务器和memcache缓存服务 192.168.4.5
(1)安装memcache软件包,启动服务
[root@proxy ~]# yum -y install memcached
[root@proxy ~]# systemctl start memcached
(2) 查看环境(保证nginx 和memcache数据库都处于监听状态)
[root@proxy ~]# ss -antulp | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=25549,fd=6),("nginx",pid=25094,fd=6))
[root@proxy ~]# systemctl restart memcached
[root@proxy ~]# ss -antulp | grep memcached
udp UNCONN 0 0 *:11211 *:* users:(("memcached",pid=26163,fd=28))
udp UNCONN 0 0 :::11211 :::* users:(("memcached",pid=26163,fd=29))
tcp LISTEN 0 128 *:11211 *:* users:(("memcached",pid=26163,fd=26))
tcp LISTEN 0 128 :::11211 :::* users:(("memcached",pid=26163,fd=27))
(3)修改nginx配置文件实现反向代理
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
http {
……
upstream webserver {
server 192.168.2.100:80;
server 192.168.2.200:80;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://webserver; ##转发
root html;
index index.php index.html index.htm;
}
…………
(4)重新加载nginx
[root@proxy ~]# nginx -s reload
3 客户端192.168.4.100测试,按F5刷新登录状态
[root@client ~]# firefox 192.168.4.5
点击登录,注意此时分配的为后端web1的页面(web1背景有添加颜色)
按F5刷新页面
此时已经访问到了web2 ,为什么web2不需要登录就可以进行正常访问呢,因为memcache共享了session.
再次验证:
[root@web1 ~]# ls /var/lib/php/session/
[root@web1 ~]#
### web1上查看php的session记录为空,那么session哪去了?
[root@proxy ~]# telnet 192.168.4.5 11211
Trying 192.168.4.5...
Connected to 192.168.4.5.
Escape character is '^]'.
get fh7t00o44orjtc5dk850ksnej3
VALUE fh7t00o44orjtc5dk850ksnej3 0 76
login_user|s:0:"";logged_in|s:4:"true";id|s:26:"fh7t00o44orjtc5dk850ksnej3";
END
######### 复制网页内的session ID fh7t00o44orjtc5dk850ksnej3 利用telnet 查看iD的值