session共享的方法
1 2 3 |
1.把多台机器的session文件挂载到NFS 2.通过程序将session存储到MySQL数据库 3.通过程序将session存储到redis |
搭建第一台phpmyadmin
1)上传包
1 2 |
[root@web01 ~]# cd /code/ [root@web01 /code]# rz phpMyAdmin-4.9.0.1-all-languages.zip |
2)解压
1 2 |
[root@web01 /code]# unzip phpMyAdmin-4.9.0.1-all-languages.zip [root@web01 /code]# mv phpMyAdmin-4.9.0.1-all-languages php |
3)配置代码
1 2 3 |
[root@web01 /code]# cp php/config.sample.inc.php php/config.inc.php [root@web01 /code]# vim php/config.inc.php $cfg['Servers'][$i]['host'] = '172.16.1.51'; |
4)配置nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[root@web01 /code]# vim /etc/nginx/conf.d/linux.php.com.conf server { listen 80; server_name linux.php.com; root /code/php; location / { index index.php; } location ~* \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } |
5)重启
1 2 3 4 5 |
[root@web01 /code]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 /code]# systemctl restart nginx |
同步配置到第二台服务器
1)推送配置和站点
1 2 3 4 5 |
#推送站点目录 [root@web01 /code]# scp -r /code/php 172.16.1.9:/code/ #推送nginx配置 [root@web01 /code]# scp /etc/nginx/conf.d/linux.php.com.conf 172.16.1.9:/etc/nginx/conf.d/ |
2)重启访问
1 2 3 4 |
[root@web03 ~]# systemctl restart nginx #配置hosts 10.0.0.9 linux.php.com |
3)授权目录
1 2 3 4 5 6 |
#报错 session_start(): open(SESSION_FILE, O_RDWR) failed: Permission denied (13) session_start(): Failed to read session data: files (path: /var/lib/php/session) #解决: [root@web03 /code]# chown -R www.www /var/lib/php/session |
配置负载均衡
1)配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[root@lb01 ~]# cp /etc/nginx/conf.d/blog.conf /etc/nginx/conf.d/php.conf [root@lb01 ~]# vim /etc/nginx/conf.d/php.conf upstream php { server 172.16.1.7:80; server 172.16.1.9:80; } server { listen 80; server_name linux.php.com; location / { proxy_pass http://php; include /etc/nginx/proxy_params; } } |
2)重启并访问
1 2 3 4 |
[root@lb01 ~]# systemctl restart nginx #配置hosts 10.0.0.4 linux.php.com |
使用redis实现session共享
1)安装redis
1 |
[root@db01 ~]# yum install -y redis |
2)配置redis
1 2 |
[root@db01 ~]# vim /etc/redis.conf bind 127.0.0.1 172.16.1.51 |
3)启动redis
1 2 3 4 5 6 |
[root@db01 ~]# systemctl start redis #检查启动 [root@db01 ~]# netstat -lntp tcp 0 0 172.16.1.51:6379 0.0.0.0:* LISTEN 29104/redis-server tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 29104/redis-server |
4)配置PHP服务将session存储到redis
1 2 3 4 5 6 7 8 9 |
[root@web01 /code]# vim /etc/php.ini #原配置 session.save_handler = files session.save_handler = redis session.save_path = "tcp://172.16.1.51:6379" [root@web01 /code]# vim /etc/php-fpm.d/www.conf #最下面几行注释 ;php_value[session.save_handler] = files ;php_value[session.save_path] = /var/lib/php/session |
5)重启php
1 2 |
[root@web01 /code]# systemctl restart php-fpm [root@web03 /code]# systemctl restart php-fpm |
6)访问测试
7)redis查看seesion
1 2 3 4 5 6 |
[root@db01 ~]# redis-cli 127.0.0.1:6379> keys * 1) "PHPREDIS_SESSION:8b8721df0b5736149ea0c716f05773e9" 2) "PHPREDIS_SESSION:b59336d7a1a053c6d26c2550032c1609 127.0.0.1:6379> TTL PHPREDIS_SESSION:b59336d7a1a053c6d26c2550032c1609 (integer) 1199 |