项目搭建:博客、知乎、PHP(MySQL数据库管理工具)

在这里插入图片描述

1.项目搭建:
          博客、知乎、PHP(MySQL数据库管理工具)
2.搭建流程:
          web端搭建服务==》通过nfs服务实现所有web端共享==》数据同步3.到数据库db01==》nfs服务在实时备份到backup==》挂载七层负载均衡集群==》挂载四层负载均衡 
3.准备环境:

在这里插入图片描述

4.操作流程:
1.LNMP架构搭建 (web01与web02相同)

(1)配置nginx官方源rpm包

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true                  

(2)yum安装nginx

[root@web01 ~]# yum install nginx -y

(3)修改nginx配置文件

[root@web01 ~]# vim /etc/nginx/nginx.conf  
user www;  #用户改为www
http {
    access_log  /var/log/nginx/access.log  main;
    client_max_body_size 200M; #在此处加入此段
}

(4)根据配置文件配置用户

#创建www用户与组 指定gid、uid为666
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

(5)启动nginx

[root@web01 ~]# systemctl start nginx  #启动nginx
[root@web01 ~]# systemctl enable nginx  #开机自启动nginx
[root@web01 ~]# ps -ef | grep nginx  #验证启动
root      10439      1  0 00:57 ?        00:00:00 nginx: master process /usr/sbin/ngin -c /etc/nginx/nginx.conf
www       10440  10439  0 00:57 ?        00:00:00 nginx: worker process
root      10481    891  0 00:59 pts/0    00:00:00 grep --color=auto nginx

(6)删除nginx的default.conf默认配置文件

[root@web01 ~]# ll /etc/nginx/conf.d/
总用量 4
-rw-r--r-- 1 root root 1093 10月 29 23:21 default.conf
[root@web01 ~]# rm -rf /etc/nginx/conf.d/default.conf 

(7)安装PHP

#上传php压缩包
[root@web01 ~]# rz -E
rz waiting to receive.
[root@web01 ~]# ll
总用量 19432
-rw-r--r--  1 root root 19889622 12月  6 17:15 php.tar.gz
#解压包
[root@web01 ~]# tar xf php.tar.gz 
#本地安装php的rpm包
[root@web01 ~]# yum localinstall -y *.rpm

(8)配置PHP

[root@web01 ~]# vim /etc/php-fpm.d/www.conf 
user = www
group = www
[root@web01 ~]# vim /etc/php.ini
upload_max_filesize = 200M
post_max_size = 200M

(9)启动PHP服务

[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.
[root@web01 ~]# ps -ef |grep php-fpm   #验证启动
root      10663      1  0 01:26 ?        00:00:00 php-fpm: master process (/etc/php-fp.conf)
www       10664  10663  0 01:26 ?        00:00:00 php-fpm: pool www
www       10665  10663  0 01:26 ?        00:00:00 php-fpm: pool www
www       10666  10663  0 01:26 ?        00:00:00 php-fpm: pool www
www       10667  10663  0 01:26 ?        00:00:00 php-fpm: pool www
www       10668  10663  0 01:26 ?        00:00:00 php-fpm: pool www
root      10725    891  0 +01:36 pts/0    00:00:00 grep --color=auto php-fpm

2.搭建Wprdpress(博客)、Zhihu(知乎)、PHP(MySQL数据库管理工具)(web01与web02相同)

(1)创建站点目录,上传代码包

[root@web01 ~]# mkdir /code
[root@web01 ~]# cd /code
[root@web01 code]# rz -E
rz waiting to receive.
[root@web01 code]# rz -E
rz waiting to receive.
[root@web01 code]# rz -E
rz waiting to receive.
[root@web01 code]# ll
总用量 29900
-rw-r--r-- 1 root root 11060845 12月  3 14:57 phpMyAdmin-4.9.0.1-all-languages.zip
-rw-r--r-- 1 root root  8451194 12月  6 18:01 WeCenter_3-2-1.zip
-rw-r--r-- 1 root root 11098483 12月  6 18:01 wordpress-5.0.3-zh_CN.tar.gz

(2)解压代码包,修改权限

[root@web01 code]# unzip WeCenter_3-2-1.zip
[root@web01 code]# tar xf wordpress-5.0.3-zh_CN.tar.gz
[root@web01 code]# unzip phpMyAdmin-4.9.0.1-all-languages.zip
[root@web01 code]# mv WeCenter_3-2-1 zhihu  #改名字
[root@web01 code]#  mv phpMyAdmin-4.9.0.1-all-languages php  #改名字
[root@web01 code]# cp /code/php/{config.sample.inc.php,config.inc.php}  #改名字
[root@web01 code]# vim /code/php/config.inc.php  #修改连接数据库代码
$cfg['Servers'][$i]['host'] = '172.16.1.51';
[root@web01 code]# ll
总用量 29912
drwxrwxr-x  3 root root       52 6月   4 2018 __MACOSX
drwxr-xr-x 12 root root     4096 12月  7 02:11 php
drwxr-xr-x  5 1006 1006     4096 1月  11 2019 wordpress
drwx------ 14 root root      296 6月   4 2018 zhihu
#修改站点目录及一下的权限
[root@web01 code]# chown -R www.www /code

(3)配置博客的nginx

[root@web01 ~]# vim /etc/nginx/conf.d/linux.wp.com.conf
server {
    listen 80;
    server_name linux.wp.com;

    location / {
        root /code/wordpress;
        index index.php;
    }

    location ~* \.php$ {
          #告诉fastcgi协议去解析哪个目录下的php文件
         fastcgi_pass 127.0.0.1:9000; 
        #表示具体去解析php文件的位置,$fastcgi_script_name是一个变量,意思是去 /code/wordpress下读取所有的php文件
        fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
        #读取的所有文件内容存放在fastcgi_params这里,因为启用php会调用include里所有文件
        include fastcgi_params;
    }
}

(4)配置知乎的nginx

[root@web01 ~]# vim /etc/nginx/conf.d/linux.zhihu.com.conf
server {
    listen 80;
    server_name linux.zhihu.com;
    root /code/zhihu;

    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)配置phpmyadmin的nginx

[root@web01 ~]# 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;
    }
}
 重启nginx访问会报错:Error during session start; #session会话无权限
 #修改session权限
 [root@web01 ~]# chown -R www.www /var/lib/php/session #授权后访问

PS:fastcgi_pass==》nginx连接php的代理协议

关于web服务器 --下载’php-fpm’,因为’nginx’没办法处理’php代码程序’,只能识别’html,jpg,gif,mp3,mp4(静态请求)‘等静态文件,。所以’nginx’需要用 ‘fastcgi’模块关联’php-fpm’,处理’*.php(动态请求)'的文件。
(6)验证nginx服务,重启

[root@web01 ~]# 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 ~]# systemctl restart nginx

(7)配置本地hosts文件 访问测试

10.0.0.7 linux.wp.com linux.zh.com linux.php.com
3.搭建mariadb数据库

(1)安装

[root@web01 ~]# yum install -y mariadb-server

(2)启动服务

[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb
[root@web01 ~]# ps -ef |grep mariab   #验证启动
root      11373    891  0 02:58 pts/0    00:00:00 grep --color=auto mariab

(3)创建数据库

[root@web01 ~]# mysql  #进入数据库
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;  #查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
#设置数据库密码
[root@web01 ~]# mysqladmin -uroot password '123'
#使用密码进入数据库
[root@web01 ~]#  mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

(4)数据库建库

MariaDB [(none)]> create database wordpress;  #创建博客数据库
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create database zhihu;   #创建知乎数据库
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;  #查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |   #博客数据库
| zhihu              |   #知乎数据库
+--------------------+
6 rows in set (0.00 sec)

(5)创建数据库用户并授权

MariaDB [(none)]> grant all on wordpress.* to wp@'172.16.1.%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all on zhihu.* to zh@'172.16.1.%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host from mysql.user;  #查看用户
+------+------------+
| user | host       |
+------+------------+
| root | 127.0.0.1  |
| wp   | 172.16.1.% |  #博客用户
| zh   | 172.16.1.% |  #知乎用户
| root | ::1        |
|      | localhost  |
| root | localhost  |
|      | web01      |
| root | web01      |
+------+------------+
8 rows in set (0.00 sec)
PS:授权时,以上步骤要对应好如下的配置文件,改为数据库IP的目的,是为了“迁移新的数据库”,在本机数据库停用的情况下,保证让用户继续能访问数据库的信息!
        1>想要通过本机访问则指定为如下:
            localhost(本地服务器)
        2>想要通过数据库访问指定为入下:
            172.16.1.51(数据库IP)
4.web端拆分数据库到db01 (目的为了1.缓解web网站的压力;2.增强数据库读写性能;3.提高用户访问速度)

(1)在新的服务器(db01)上数据库建库

[root@db01 ~]# yum install mariadb-server -y

(2)开启数据库,设置密码

[root@db01 ~]# systemctl start mariadb  #开启数据库
[root@db01 ~]# systemctl enable mariadb  #设置开机自启
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@db01 ~]# mysqladmin -uroot password  #给数据库设置密码
New password: 123
Confirm new password: 123

(3)创建所需数据库,授权用户

#创建博客数据库
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)
#创建知乎数据库
MariaDB [(none)]> create database zhihu;
Query OK, 1 row affected (0.00 sec)
#授权博客
MariaDB [(none)]> grant all on wordpress.* to wp@'172.16.1.%' identified by'123';
Query OK, 0 rows affected (0.00 sec)
#授权知乎
MariaDB [(none)]> grant all on zhihu.* to zh@'172.16.1.%' identified by'123';
Query OK, 0 rows affected (0.00 sec)
#查看是否创建成功
MariaDB [(none)]> select user,host from mysql.user;
+------+------------+
| user | host       |
+------+------------+
| root | 127.0.0.1  |
| wp   | 172.16.1.% |  #博客创建成功并授权
| zh   | 172.16.1.% |  #知乎创建成功并授权
| root | ::1        |
|      | db01       |
| root | db01       |
|      | localhost  |
| root | localhost  |
+------+------------+
8 rows in set (0.00 sec)

(4)web01客户端远程连接db01数据库

[root@web01 ~]# mysql -uwp -p -h 172.16.1.51
Enter password: 123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> 

(5)旧数据库(web01)导出数据到本机的/tmp/wp.sql里

[root@web01 ~]# mysqldump -uroot -p -B wordpress > /tmp/wp.sql
Enter password: 123

(6)将数据推送到新数据库(db01)

#打包博客数据到/tmp/wp.sql然后推送到db01的/tmp下
[root@web01 ~]# mysqldump -uroot -p -B wordpress > /tmp/wp.sql
Enter password: 123
[root@web01 ~]# scp /tmp/wp.sql  172.16.1.51:/tmp
root@172.16.1.51's password: 100
wp.sql                                               100% 1420     1.5MB/s   00:00
#打包知乎数据到/tmp/wp.sql然后推送到db01的/tmp下
[root@web01 ~]# mysqldump -uroot -p -B zhihu > /tmp/zh.sql
Enter password: 
[root@web01 ~]# scp /tmp/zh.sql  172.16.1.51:/tmp
root@172.16.1.51's password: 
zh.sql                                               100% 1404     1.9MB/s   00:00 

(7)数据导入方法扩展

#方法一:库外导入
 [root@db01 ~]# mysql -uroot -p < /tmp/wp.sql   #把db01下的/tmp/wp.sql 的博客数据从库外导入进数据库内
 Enter password: 123
 [root@db01 ~]# mysql -uroot -p < /tmp/zh.sql   #把db01下的/tmp/zh.sql 的博客知乎从库外导入进数据库内
 Enter password: 123
 #方法二:库内导入 
 [root@db01 ~]# mysql -uroot -p1
 MariaDB [wordpress]> source /tmp/wp.sql;  #站在数据库里拽库外的博客数据
 MariaDB [zhihu]> source /tmp/zh.sql;    #站在数据库里拽库外的知乎数据
 #方法三:使用任意门直接传送
 [root@web01 code]# mysql -uwp -p -h 172.16.1.51 < /tmp/wp.sql  #直接在web01将博客数据导入db01数据库
 Enter password: 123
 [root@web01 code]# mysql -uwp -p -h 172.16.1.51 < /tmp/zh.sql  #直接在web01将知乎数据导入db01数据库
 Enter password: 123

(8)查看新数据库里的数据 (db01)

MariaDB [zhihu]> use wordpress;   #切换到博客数据表
Database changed
MariaDB [wordpress]> show tables; #查看博客数据表
+-----------------------+
| Tables_in_wordpress   |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
12 rows in set (0.00 sec)
MariaDB [zhihu]> use zhihu;   #切换到知乎数据表
Database changed
MariaDB [zhihu]> show tables; #查看到知乎数据表
+--------------------------------+
| Tables_in_zhihu                |
+--------------------------------+
| aws_active_data                |
| aws_column_focus               |
| aws_draft                      |
| aws_edm_task                   |
| aws_edm_taskdata               |
| aws_edm_unsubscription         |
| aws_edm_userdata               |
| aws_edm_usergroup              |
| aws_education_experience       |
| aws_favorite                   |
| aws_favorite_tag               |
| aws_feature                    |
| aws_feature_topic              |
| aws_work_experience            |
+-----------------------------

(9)修改项目中数据库地址(告知亲戚新家地址)

[root@web01 ~]# vim /code/wordpress/wp-config.php
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');

/** MySQL数据库用户名 */
define('DB_USER', 'wp');

/** MySQL数据库密码 */
define('DB_PASSWORD', '123');

/** MySQL主机 */
define('DB_HOST', '172.16.1.51');
[root@web01 ~]# vim /code/zhihu/system/config/database.php

(10)停掉旧数据库(拆迁旧家)

[root@web01 ~]# systemctl stop mariadb

5.web端做扩展

(1)web02安装nginx

#配置官方源
[root@web01 ~]# scp /etc/yum.repos.d/nginx.repo  172.16.1.8:/etc/yum.repos.d/
#安装nginx
[root@web02 ~]# yum install nginx -y

(2)创建用户与组

[root@web02 ~]# groupadd www -g 666
[root@web02 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

(3)上传php包,解压,安装php

[root@web02 ~]# rz
[root@web02 ~]# tar xf php.tar.gz 
[root@web02 ~]# yum localinstall -y *.rpm

(4)同步web01的配置文件到web02

#同步nginx配置文件
[root@web01 ~]# scp /etc/nginx/nginx.conf 172.16.1.8:/etc/nginx/ 
[root@web01 ~]# scp /etc/nginx/conf.d/* 172.16.1.8:/etc/nginx/conf.d/
root@172.16.1.8's password: 
linux.php.com.conf                                                      100%  301   158.6KB/s   00:00    
linux.wp.com.conf                                                       100%  312   243.0KB/s   00:00    
linux.zhihu.com.conf                                                    100%  302   161.3KB/s   00:00 

#同步php的配置
[root@web01 ~]# scp /etc/php-fpm.d/www.conf 172.16.1.8:/etc/php-fpm.d/ 
[root@web01 ~]# scp /etc/php.ini 172.16.1.8:/etc/

(5)启动nginx和php服务

[root@web02 ~]# systemctl start nginx
[root@web02 ~]# systemctl enable nginx
[root@web02 ~]# systemctl start php-fpm
[root@web02 ~]# systemctl enable php-fpm

(6)推送web01站点目录到web02,授权站点目录

[root@web01 ~]# scp -r /code 172.16.1.8:/
[root@web02 ~]# chown -R www.www /code/

(7)修改本地hosts访问测试

#10.0.0.7 linux.php.com linux.zh.com linux.wp.com 
10.0.0.8 linux.php.com linux.wp.com linux.zh.com
6.web端挂载到nfs实现共享

(1)nfs端安装NFS共享服务

#首先要保证实验环境的安全==》关闭防火墙和selinux
#安装NFS
[root@nfs ~]# yum install -y nfs-utils

(2)创建挂载目录

[root@nfs ~]# mkdir /data/wp -p
[root@nfs ~]# mkdir /data/zh -p

(3)配置NFS

[root@nfs ~]# vim /etc/exports
/data/wp 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/zh 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

(4)创建用户

[root@nfs ~]# groupadd www -g 666
[root@nfs ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

(5)授权与启动nfs

[root@nfs ~]# chown -R www.www /data
[root@nfs ~]# systemctl start nfs

(6)检查配置

[root@nfs ~]# cat /var/lib/nfs/etab
/data/zh	172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash)
/data/wp	172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash)

(7)web01和web02客户端安装nfs和rpcbind

[root@web01 ~]# yum install -y nfs-utils rpcbind
[root@web02 ~]# yum install -y nfs-utils rpcbind

(8)启动nfs、rpcbind

[root@web01 ~]# systemctl start nfs rpcbind
[root@web02 ~]# systemctl start nfs rpcbind

(9)查看挂载点

[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/zh 172.16.1.0/24
/data/wp 172.16.1.0/24
[root@web02 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/zh 172.16.1.0/24
/data/wp 172.16.1.0/24

(10)先推送挂载目录下文件内容

[root@web01 ~]# rsync -avz /code/wordpress/wp-content/uploads/ 172.16.1.31:/data/wp/
[root@web01 ~]#rsync -avz /code/zhihu/uploads/ 172.16.1.31:/data/zh/
[root@web02 ~]# rsync -avz /code/wordpress/wp-content/uploads/ 172.16.1.31:/data/wp/
[root@web02 ~]#rsync -avz /code/zhihu/uploads/ 172.16.1.31:/data/zh/

(11)挂载

[root@web01 ~]# mount -t nfs 172.16.1.31:/data/wp /code/wordpress/wp-content/uploads
[root@web01 ~]#  mount -t nfs 172.16.1.31:/data/zh /code/zhihu/uploads
[root@web02 ~]# mount -t nfs 172.16.1.31:/data/wp /code/wordpress/wp-content/uploads
[root@web02 ~]#  mount -t nfs 172.16.1.31:/data/zh /code/zhihu/uploads

7.backup服务端配置rsync实时备份

(1)backup端安装inotify-tools、rsync

[root@backup ~]# yum install -y rsync

(2)修改rsync配置文件

[root@backup ~]# vim /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = true
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[web_data]
comment = "该备份文件是web端挂载到nfs服务器的文件"
path = /data

(3)创建用户与组,配置密码文件

[root@backup ~]# groupadd www -g 666
[root@backup ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
#配置密码文件
[root@backup ~] echo "rsync_backup:123456" > /etc/rsync.passwd
[root@backup ~] chmod 600 /etc/rsync.passwd 
[root@backup ~] systemctl start rsyncd

(4)创建共享目录并授权

[root@backup ~]# mkdir /data
[root@backup ~]# chown -R www.www /data
8.nfs配置rsync实时备份

(1)nfs下载inotify-tools

[root@nfs ~]# yum install -y inotify-tools
[root@nfs ~]# yum install -y rsync
[root@nfs ~]#systemctl start rsyncd  #开启rsync服务

(2)编写实时备份脚本

[root@nfs ~]# vim rsync-inotify.sh
#!/bin/bash
export RSYNC_PASSWORD=123456
dir=/data
    /usr/bin/inotifywait -mrq --format '%w %f' -e create,delete,attrib,close_write $dir | while read line;do
    cd $dir && rsync -az -R --delete . rsync_backup@172.16.1.41::web_data
>/dev/null 2>&1
done &
[root@nfs ~]#sh rsync-inotify.sh  #运行脚本

(3)编写密码文件

[root@nfs ~]# echo "123456" > /etc/rsync.passwd
[root@nfs ~]#  chmod 600 /etc/rsync.passwd 
9.挂载七层负载均衡 lb01与lb02

(1)配置nginx官方源,下载nginx,配置nginx配置文件,创建用户,启动nginx
详解见上web端操作
(2)lb01上配置博客的配置文件

[root@lb01 ~]# vim /etc/nginx/conf.d/linux.wp.com.conf
upstream web_group {
    server 172.16.1.7;
    server 172.16.1.8;
}
server {
    listen 80;
    server_name linux.wp.com;

    location / {
        proxy_pass http://web_group;
        include proxy_params;
    }
}

(3)lb01上配置知乎的配置文件

[root@lb01 ~]# vim /etc/nginx/conf.d/linux.zh.com.conf
upstream zhihu {
    server 172.16.1.7;
    server 172.16.1.8;
}
server {
    listen 80;
    server_name linux.zh.com;

    location / {
        proxy_pass http://zhihu;
        include proxy_params;
    }
}

(4)配置优化文件

[root@lb01 ~]# vim /etc/nginx/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_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
[root@lb01 ~]# systemctl restart nginx   #重启nginx

(5)配置本地hosts文件,访问测试

10.0.0.4 linux.wp.com linux.zh.com

(6)lb02同步lb01操作(从下载nginx开始)

#推送nginx官方源配置文件
[root@lb01 ~]# scp -r /etc/yum.repos.d/nginx.repo 172.16.1.5:/etc/yum.repos.d/
#下载nginx
[root@lb02 ~]# yum install nginx -y
#配置nginx配置文件
[root@lb02 ~]# vim /etc/nginx/nginx.conf 
user www;  #用户改为www
http {
    access_log  /var/log/nginx/access.log  main;
    client_max_body_size 200M; #在此处加入此段
}
#创建用户
[root@lb02 ~]#  groupadd www -g 666
[root@lb02 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
#开启nginx并开机自启
[root@lb02 ~]# systemctl start nginx
[root@lb02 ~]# systemctl enable nginx
#删除default。conf文件防止第一个读取它
[root@lb02 ~]# rm -rf /etc/nginx/conf.d/default.conf
#推送博客配置文件
[root@lb01 ~]# scp -r /etc/nginx/conf.d/linux.wp.com.conf 172.16.1.5:/etc/nginx/conf.d/
#推送知乎配置文件
[root@lb01 ~]# scp -r /etc/nginx/conf.d/linux.zh.com.conf 172.16.1.5:/etc/nginx/conf.d/
#推送nginx优化配置文件
[root@lb01 ~]# scp -r /etc/nginx/proxy_params 172.16.1.5:/etc/nginx/
#重启nginx
[root@lb02 ~]# systemctl restart nginx
10.Nginx负载均衡会话保持(session共享的方法)

(1)web端上传phpmyadmin压缩包并解压改名(web端所有服务步骤相同)
(2)配置php的nginx文件(以上两步讲解见开头web端操作)
(3)登录报错:如下图
在这里插入图片描述

#报错解决方法
[root@web01 ~]# chown -R www.www /var/lib/php/session   #授权session会话
#进入数据库给用户授权
[root@db01 ~]# mysql -uroot -p123   #先进入数据库
MariaDB [(none)]> grant all on *.* to root@'172.16.1.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)    #授权

(4)配置php负载均衡文件 (lb01和lb02同步操作)

[root@lb01 ~]# vim /etc/nginx/conf.d/linux.php.com.conf
upstream phpmyadmin {
    server 172.16.1.7;
    server 172.16.1.8;
}

server {
    listen 80;
    server_name linux.php.com;

    location / {
        proxy_pass http://phpmyadmin;
        include proxy_params;
    }
}

[root@lb01 ~]# 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 ~]# systemctl restart nginx

(5)重新配置hosts,10.0.0.4 linux.php.com,访问报错:如下图
注意:若使用redis方法需要同步所有虚拟机时间,因为session值是有有效时间的
在这里插入图片描述

#解决报错
方法一:挂载session文件
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/wp /var/lib/php/session/
[root@web02 ~]# mount -t nfs 172.16.1.31:/data/wp /var/lib/php/session/
方法二:使用redis实现session共享
[root@db01 ~]# yum install -y redis   #安装redis
[root@db01 ~]# vim /etc/redis.conf   #配置服务
bind 172.16.1.51
[root@db01 ~]# systemctl start redis   #启动服务
#修改PHP服务将session存储到redis
[root@web01 ~]# vim /etc/php.ini   
#原配置
#session.save_handler = files   #原配置
session.save_handler = redis   #修改后
#;session.save_path = "/tmp"    #原配置
session.save_path = "tcp://172.16.1.51:6379"    #修改后

[root@web01 ~]# vim /etc/php-fpm.d/www.conf
#注释以下两行
;php_value[session.save_handler] = files
;php_value[session.save_path]    = /var/lib/php/session
#重启
[root@web01 ~]# systemctl restart php-fpm
#再次访问
linux.php.com
11.搭建四层负载均衡 (lb4)

(1)lb4下载nginx,创建目录 (下载nginx详见web端)

1.1.配置nginx主配置文件
[root@lb4 ~]# vim /etc/nginx/nginx.conf
#注释http层所有内容
#添加一个包含文件
user  www;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
include /etc/nginx/conf.c/*.conf; #添加到此
#http {
#    include       /etc/nginx/mime.types;
#    default_type  application/octet-stream;
#    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                      '$status $body_bytes_sent "$http_referer" '
#                      '"$http_user_agent" "$http_x_forwarded_for"';
#    access_log  /var/log/nginx/access.log  main;
#    sendfile        on;
#    #tcp_nopush     on;
#    keepalive_timeout  65;
#    #gzip  on;
#    include /etc/nginx/conf.d/*.conf;
#}

2.创建目录
[root@lb4 ~]# mkdir /etc/nginx/conf.c

(2)配置四层负载均衡

1.编辑配置文件
#四层负载均衡是没有access的日志的,因为在nginx.conf的配置中,access的日志格式是配置在http下的,而四层负载均衡配置是在http以外的;

#如果需要日志则需要配置在stream下面
[root@lb4 ~]# vim /etc/nginx/conf.c/linux.lb4.com.conf
stream {
	log_format  proxy '$remote_addr $remote_port - [$time_local] $status $protocol '
                  '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"';
    access_log /var/log/nginx/proxy.log proxy;

    upstream lbserver {
        server 10.0.0.4:80;
        server 10.0.0.5:80;
        
    }

    server {
        listen 80;
        proxy_pass lbserver;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
    }
}

#查看所有web服务器日志
[root@web01 ~]# tail -f /var/log/nginx/access.log
[root@web02 ~]# tail -f /var/log/nginx/access.log
2.检查配置并启动访问
[root@lb4 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb4 ~]# systemctl start nginx

#配置hosts,浏览器访问测试,其他IP主机先注释掉!
10.0.0.6 linux.zh.com linux.wp.com linux.php.com linux.1.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值