watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=myphpadmin Project understanding cookie and session When we usually surf the Internet , After refreshing or quitting the browser, you don't need to log in to the web page to open the browser again , That's the use cookie and session; Environment configuration hostname wanIP lanIP role web01 10.0.0.7 172.16.1.7 Web server



myphpadmin Project understanding cookie and session

When we usually surf the Internet , After refreshing or quitting the browser, you don't need to log in to the web page to open the browser again , That's the use cookie and session;


Environment configuration

hostname wanIP lanIP role
web01 10.0.0.7 172.16.1.7 Web server 1
web02 10.0.0.8 172.16.1.8 Web server 2
db01 10.0.0.51 172.16.1.51 database server
lb01 10.0.0.5 172.16.1.5 Load Balancer


The configuration process


1、web01 and web02 install nginx and php

# 1、 Upload files
# 2、 Unpack the installation
[[email protected] ~]# tar -xf nginx_and_php.tar.gz 
[[email protected] ~]# cd nginx_php/
[[email protected] ~/nginx_php]# yum -y localinstall *.rpm
# 3、 Changing configuration files
[[email protected] ~]# vi /etc/nginx/conf.d/phpadmin.conf
server {
        listen 80;
        server_name php.gong.com;
        root /website/phpadmin;
        index index.php;
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}
# 4、 Create administrative users
[[email protected] ~]# groupadd www -g 666
[[email protected] ~]# useradd www -g666 -u666
# 5、 Change the startup user
[[email protected] ~]# vi /etc/nginx/nginx.conf 
user  www;
[[email protected] ~]# vi /etc/php-fpm.d/www.conf 
; Start a new pool named 'www'.
[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
# 6、 Create a site directory
[[email protected] ~]# mkdir -p /website/phpadmin/
[[email protected] ~]# cd /website/phpadmin/
##  Upload php Source code , decompression
[[email protected] ~]# unzip phpMyAdmin-4.9.0.1-all-languages.zip
[[email protected] /website/phpadmin]# mv phpMyAdmin-4.9.0.1-all-languages/* ./
# 6、 to grant authorization
[[email protected] ~]# chown -R www.www /website/phpadmin/
# 7、 start-up ; Boot from boot
[[email protected] ~]# nginx
[[email protected] ~]# systemctl start php-fpm
[[email protected] ~]# systemctl enable nginx php-fpm
# 7、 to grant authorization session Catalog
[[email protected] ~]# chown -R www.www  /var/lib/php/session
#  Access test 

2、 mount this database

# 1、 mount this database
[[email protected] ~]# yum -y install mariadb-server
# 7、 start-up ; Boot from boot
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# systemctl enable mariadb
# 7、 establish root password
[[email protected] ~]# mysqladmin -uroot password '123'
# 8、 land
[[email protected] ~]# mysql -uroot -p123
# 7、 Create database
MariaDB [(none)]> create database php_db;
Query OK, 1 row affected (0.00 sec)
# 7、 Create management database users and passwords
MariaDB [(none)]> grant all on php_db.* to [email protected]'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
# 1、 change php Code , Database connection address
[[email protected] ~]# cd /website/phpadmin/
[[email protected] /website/phpadmin]# cp config.sample.inc.php config.inc.php
[[email protected] /website/phpadmin]# vi config.inc.php
/* Server parameters */
$cfg['Servers'][$i]['host'] = '172.16.1.51';

After landing, the local cookie And the server side session

Put the server-side session After deleting , Refresh the page ; Will be asked to log back in .

3、 hold web01 Copy the configuration on to web02 Up

# 1、 Use rsync Send to web02
[[email protected] ~]# rsync -avz /etc/nginx/conf.d/phpadmin.conf [email protected]:/etc/nginx/conf.d/
[[email protected] ~]# rsync -avz /website [email protected]:/
# 2、 start-up
[[email protected] ~]# systemctl start nginx php-fpm
[[email protected] ~]# systemctl enable nginx php-fpm
# 3、 to grant authorization
[[email protected] ~]# chown -R www.www  /var/lib/php/session

3、 Join load balancing

#  Edit profile
[[email protected] ~]# vi /etc/nginx/conf.d/upstream.conf
upstream phpadmin {
        server 172.16.1.7;
        server 172.16.1.8;
}
  
server {
        listen 80;
        server_name php.gong.com;
        location / {
                proxy_pass http://phpadmin;
                include proxy_params;
        }
}
[[email protected] /etc/nginx]# vi proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
 
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
# 2、 start-up
[[email protected] /etc/nginx]# nginx
[[email protected] /etc/nginx]# systemctl enable nginx

After load balancing , The following will happen , Refresh request to login again , Or make up for it , That's because , If in web01 It's stored on session Then the client accesses to web01 When cookie Of key and session Of value Is able to match, the user access is legal , But the poll found that wen02 When , No, session Or stored different session, This will cause the refresh to be re authenticated or , The situation of not landing .

To configure redis

4、 Join in redis

[[email protected] ~]# yum -y install redis
[[email protected] ~]# sed  -i '/^bind/c bind 127.0.0.1 172.16.1.51' /etc/redis.conf
[[email protected] ~]# systemctl start redis
[[email protected] ~]# systemctl enable redis
[[email protected] ~]# vim /etc/php.ini
session.save_handler = redis
session.save_path = "tcp://172.16.1.51:6379"
#  You don't have to try
session.auto_start = 1
[[email protected] ~]# vi /etc/php-fpm.d/www.conf
;php_value[session.save_handler] = files
;php_value[session.save_path] = /var/lib/php/session
[[email protected] ~]# systemctl restart php-fpm
[[email protected] ~]# scp /etc/php.ini [email protected]:/etc/php.ini
[[email protected] ~]# scp /etc/php-fpm.d/www.conf [email protected]:/etc/php-fpm.d/www.conf 
[[email protected] ~]# systemctl restart php-fpm

When there's shared storage , The website under the same domain name cookie It's all the same , So no matter how many servers , No matter which server the request is assigned to the same user's cookie It is the same. . in other words cookie Corresponding session It's the only one . therefore , This ensures that multiple business servers can visit the same website , And refresh will not appear to re login or page error situation .

Cookie It is the information about some websites stored locally after visiting some websites , Next time I visit you, I will reduce some steps . Another more accurate way to say it is :Cookies It is a small piece of text stored by the server on the local machine and sent to the same server with each request , It's a solution to keep state on the client side .



FBI WARNING


QQ:1402122292 Be original sheldon Others call me Xiaodong