Linux架构之Nginx 负载均衡会话保持
Nginx负载均衡会话保持
web01、web02
(web01、web02、db01、nfs01都要优化基本源)
[root@web01 ~]# vim /etc/yum.repos.d/CentOS-Base.repo
#将[base]下的baseurl第二条、第三条源链接删除;
#将[updates]下的baseurl第二条、第三条源链接删除;
#将[extras]下的baseurl第二条、第三条源链接删除;
#将[centosplus]下的baseurl第二条、第三条源链接删除;wq保存并退出
#安装nginx
#更改nginx源
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@web02 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
#安装nginx
[root@web01 ~]# yum install -y nginx
[root@web02 ~]# yum install -y nginx
#创建nginx启动用户
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
#创建nginx启动用户
[root@web02 ~]# groupadd www -g 666
[root@web02 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
#修改nginx的启动用户(在web02上也要修改)
[root@web01 ~]# vim /etc/nginx/nginx.conf
user www;
#启动nginx并加入开机自启(在web02上也要启动nginx)
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
===================================================
#更改PHP源(web01、web02)
[root@web01 ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
#安装PHP(web01、web02)
[root@web01 ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
#修改PHP的启动用户(web01、web02)
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
user = www
group = www
#启动PHP加入开机自启(web01、web02)
[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
db01
#安装数据库
[root@db01 ~]# yum install -y mariadb-server
#启动数据库
[root@db01 ~]# systemctl start mariadb
lb01上安装nginx
#先在lb01上安装nginx
[root@lb01 /]# cd ~
[root@lb01 ~]# mkdir php
[root@lb01 ~]# rz -E
rz waiting to receive.
lb01
[root@lb01 php]# ll
total 41756
-rw-r--r-- 1 root root 42755522 Aug 20 16:26 nginx_php_mariadb.tar.gz
#解压压缩包
[root@lb01 php]# tar xf nginx_php_mariadb.tar.gz
#安装nginx
[root@lb01 php]# cd nginx_php_mariadb
[root@lb01 nginx_php_mariadb]# rpm -ivh nginx-1.16.1-1.el7.ngx.x86_64.rpm
#检查是否安装成功
[root@lb01 nginx_php_mariadb]# rpm -qa | grep nginx
nginx-1.16.1-1.el7.ngx.x86_64
#启动nginx并加入开机自启
[root@lb01 ~]# systemctl start nginx
[root@lb01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
====================================================================
1、配置nginx
[root@web01 code]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# cat php.conf
server {
listen 80;
server_name php.drz.com;
root /code/phpMyAdmin-4.8.4-all-languages;
location / {
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 conf.d]# systemctl restart nginx
2、安装phpmyadmin(web01、web02上都装)
[root@web01 ~]# mkdir /code
[root@web01 conf.d]# cd /code
#安装phpmyadmin
[root@web01 code]# wget https://files.phpmyadmin.net/phpMyAdmin/4.9.0.1/phpMyAdmin-4.9.0.1-all-languages.zip
[root@lb01 code]# yum install -y unzip
#解压
[root@web01 code]# unzip phpMyAdmin-4.9.0.1-all-languages.zip
3、配置phpmyadmin连接远程的数据库
[root@web01 conf.d]# cd /code
[root@web01 code]# cd phpMyAdmin-4.9.0.1-all-languages
[root@web01 phpMyAdmin-4.9.0.1-all-languages]# cp config.sample.inc.php config.inc.php
[root@web01 phpMyAdmin-4.9.0.1-all-languages]# vim config.inc.php
/* Server parameters */
$cfg['Servers'][$i]['host'] = '172.16.1.51';
4、配置授权
[root@web01 phpMyAdmin-4.9.0.1-all-languages]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# chown -R www.www /var/lib/php/
#重启nginx
[root@web01 conf.d]# 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 conf.d]# nginx -s reload
配置hosts文件 windows键+R——drivers——etc——10.0.0.7 php.drz.com blog.drz.com zh.drz.com
[root@web01 phpMyAdmin-4.8.4-all-languages]# ll /var/lib/php/session/
总用量 4
-rw-------. 1 www www 2424 8月 21 18:41 sess_e96b27a6a628be47745a10a36e2fcd5a
5、将web01上配置好的phpmyadmin以及nginx的配置文件推送到web02主机上
[root@web01 code]# scp -rp phpMyAdmin-4.9.0.1-all-languages/ root@172.16.1.8:/code/
[root@web01 code]# scp /etc/nginx/conf.d/php.conf root@172.16.1.8:/etc/nginx/conf.d/
6、在web02上重载nginx服务
[root@web02 code]# systemctl restart nginx
7、授权
[root@web02 code]# chown -R www.www /var/lib/php/
8、接入负载均衡
[root@lb01 ~]# cd /etc/nginx/conf.d/
[root@lb01 conf.d]# vim proxy_php.com.conf
upstream php {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name php.drz.com;
location / {
proxy_pass http://php;
proxy_set_header HOST $http_host;
}
}
[root@lb01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d]# systemctl restart nginx
配置hosts文件 windows键+R——drivers——etc——10.0.0.8 php.drz.com blog.drz.com zh.drz.com
10.0.0.8 php.drz.com blog.drz.com zh.drz.com
打开浏览器,输入php.drz.com,输入用户名:wp、密码:Zls123.com
使用负载均衡的轮询功能后,会发现两个机器上的sessionId不一致。
9、使用redis解决会话登录问题
1)安装redis内存数据库
[root@db01 ~]# yum install -y redis
2)配置redis监听在172.16.1.0网段上
[root@db01 ~]# sed -i '/^bind/c bind 127.0.0.1 172.16.1.51' /etc/redis.conf
3)启动redis
[root@db01 ~]# systemctl start redis
[root@db01 ~]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.
4)php配置session链接redis
#1.修改/etc/php.ini文件
[root@web ~]# vim /etc/php.ini
session.save_handler = redis
session.save_path = "tcp://172.16.1.51:6379"
;session.save_path = "tcp://172.16.1.51:6379?auth=123" #如果redis存在密码,则使用该方式
session.auto_start = 1
#2.注释php-fpm.d/www.conf里面的两条内容,否则session内容会一直写入/var/lib/php/session目录中(/php_value 往下看一点点)
[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-fpm
[root@web01 code]# systemctl restart php-fpm
6)将web01上配置好的文件推送到web02
[root@web01 code]# scp /etc/php.ini root@172.16.1.8:/etc/php.ini
[root@web01 code]# scp /etc/php-fpm.d/www.conf root@172.16.1.8:/etc/php-fpm.d/www.conf
7)web02上重启php-fpm
[root@web02 code]# systemctl restart php-fpm
8)redis查看数据
[root@db01 redis]# redis-cli
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:1365eaf0490be9315496cb7382965954"