[toc]

会话保持的概述

简介

将用户登陆的状态记录下来,并且保持用户登陆状态

会话保持的方式

  • 运维:nginx ip_hash

  • 开发

    • cookie

      • 前端开发用户将用户登陆的信息,保存到浏览器中(开发者工具->application->cookie)
      • 如果仅仅将用户的登陆信息保存在cookie中,随时可以修改的
    • session

      • 后端开发人员,将用户的登录信息记录在 服务器上 (共享存储,某一个问及那家下的某个文件,数据库中,缓存数据库..)
      • session是对cookie做的一个加密,保存在服务器中

image-20230104134700681

部署phpMyadmin

环境准备

主机名 WanIP LanIP 角色 应用
lb01 10.0.0.5 172.16.1.5 负载均衡 nginx
web01 10.0.0.7 172.16.1.7 phpmyadmin网站 nginx php
web02 10.0.0.8 172.16.1.8 phpmyadmin网站 nginx php
db01 10.0.0.51 172.16.1.51 数据库 Mariadb

部署(web01 web02)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 安装nginx和php
yum install -y nginx
rpm -ivh *.rpm

# 创建code目录并进入
mkdir /code && cd /code

# 下载phpmyadmin代码
wget http://test.driverzeng.com/Nginx_Code/phpMyAdmin-4.9.0.1-all-languages.zip

# 解压代码
unzip phpMyAdmin-4.9.0.1-all-languages.zip

# 改名
mv phpMyAdmin-4.9.0.1-all-languages phpmyadmin

# 编辑配置文件
vim /etc/nginx/conf.d/php.conf
server {
listen 80;
server_name php.xxx.com;
root /code/phpmyadmin;
index index.php index.html;

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

# 修改代码连接数据库的配置文件
mv config.sample.inc.php config.inc.php
vim config.inc.php

## 31行 localhost 改成 172.16.1.51
31 $cfg['Servers'][$i]['host'] = '172.16.1.51';

# 创建www组和用户
groupadd www -g 666 && useradd www -g 666 -u 666 -s /sbin/nologin/ -M

# 修改nginx和php的配置文件
vim /etc/nginx/nginx.conf
user www;

vim /etc/php-fpm.d/www.conf
[www]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = www
; RPM: Keep a group allowed to write in log dir.
group = www

# 修改代码文件属主和属组
chown -R www:www /code/phpmyadmin/

# 修改session文件属主和属组
chown www:www /var/lib/php/session/

# 启动nginx和php并加入开机自启
systemctl start nginx && systemctl start php-fpm
systemctl enable nginx && systemctl enable php-fpm

# 域名解析
10.0.0.7 php.xxx.com
10.0.0.8 php.xxx.com

image-20230104144056681

数据库操作(db01)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 安装数据库
yum install -y mariadb-server

# 启动数据库
systemctl start mariadb

# 设置密码用户
mysqladmin -u root password '123'

# 登陆
mysql -uroot -p123

# 创建数据库账户
grant all on *.* to phpadmin@'%' identified by '123';
用户名 phpadmin
密码 123

添加phpmyadmin的负载均衡(lb01)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 在负载均衡上添加nginx配置文件
vim /etc/nginx/conf.d/lb.conf

upstream php.xxx.com{
server 172.16.1.7;
server 172.16.1.8;
}
server {
listen 80;
server_name php.xxx.com;

location /{
proxy_pass http://php.xxx.com;
include proxy_params;
}
}

# 启动nginx
systemctl start nginx

# 本地域名解析
10.0.0.5 php.xxx.com

故障解析

用户登录系统报错

image-20230105225342784

1
用户的登录系统,session没有做共享

制作session共享(web01 web02)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
## redis端口 6379
# 安装redis到数据库服务器
yum install -y redis

# 修改redis配置文件
vim /etc/redis.conf

## 修改61行内容
61 bind 0.0.0.0

# 启动服务
systemctl start redis

# 修改php程序的配置文件
vim /etc/php.ini
## 修改1231行内容
1231 session.save_handler = redis
## 在1265行添加内容
session.save_path = "tcp://172.16.1.51:6379"

# 修改php启动程序配置文件
vim /etc/php-fpm.d/www.conf
## 注释修改395-396行内容
395 ;php_value[session.save_handler] = files
396 ;php_value[session.save_path] = /var/lib/php/session

#重启php
systemctl restart php-fpm