Nginx负载均衡综合实验
一、实验环境
二、实验步骤
1.配置IP地址
2.安装Nginx和PHP(Nginx1和Nginx2)
yum -y remove php*
yum -y install nginx
将PHP的rpm包拖入
yum -y localinstall *.rpm
systemctl start php-fpm
3.搭建wordpress(Nginx1)
(一)将wordpress-4.9.4-zh_CN包拖入根目录下
(二)解压并赋予权限
unzip wordpress-4.9.4-zh_CN.zip
chmod -R 777 wordpress
(三)创建虚拟主机配置文件
vim /etc/nginx/conf.d/blog.conf
添加:
server {
listen 80;
server_name blog.benet.com;
root /wordpress;
index index.php index.html;
location ~ \.php$ {
root /wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
(四)启动nginx
systemctl start nginx
4.安装MySQL(MySQL)
yum -y remove mariadb*
将mysql的rpm包拖入
yum -y localinstall *.rpm
systemctl start mysqld
mysqladmin -uroot -p password 123.com
5.创建blog数据库和管理用户(MySQL)
mysql -uroot -p123.com
create database blog;
grant all on blog.* to lkk@'%' identified by '123.com';
6.搭建NFS服务器(NFS)
(一)安装NFS
yum -y install nfs-utils rpcbind
nfs-utils负责文件共享
rpcbind负责网络通信
(二)创建挂载目录
mkdir -p /nfs
(三)发布共享目录
vim /etc/exports
添加:
/nfs 192.168.8.0/24(rw,sync,no_root_squash)
(四)重启NFS
systemctl restart rpcbind
systemctl restart nfs
(五)在nginx1上查看nfs共享目录
showmount -e 192.168.8.7
(六)把wordpress的网页目录挂载到nfs
cd /wordpress
cp -rp wp-content/ wp-contentbak
mount -t nfs 192.168.8.7:/nfs wp-content
cp -rp wp-contentbak/* wp-content
(七)设置永久挂载
vim /etc/fstab
添加:
192.168.8.7:/nfs /wordpress/wp-content nfs defaults 0 0
7.搭建Nginx2的网页
(一)把nginx1的配置文件复制到nginx2
scp -rp /etc/nginx/* root@192.168.8.6:/etc/nginx
(二)把nginx1的网页复制到nginx2
scp -rp /wordpress root@192.168.8.6:/
(三)启动nginx2
systemctl start nginx
(四)在nginx2上查看nfs共享目录
showmount -e 192.168.8.7
(五)把wordpress的网页目录挂载到nfs
cd /wordpress
cp -rp wp-content/ wp-contentbak
mount -t nfs 192.168.8.7:/nfs wp-content
cp -rp wp-contentbak/* wp-content
(六)设置永久挂载
vim /etc/fstab
添加:
192.168.8.7:/nfs /wordpress/wp-content nfs defaults 0 0
验证:
验证Nginx1网页服务
win10:
修改hosts文件
浏览器访问blog.benet.com
访问主页blog.benet.com
验证Nginx2网页服务
win10:
修改hosts文件
访问主页blog.benet.com
8.搭建Nginx负载均衡(Nginx-lb)
(一)安装nginx
yum -y install nginx
(二)创建负载均衡配置文件
vim /etc/nginx/conf.d/lb1.conf
添加:
upstream web {
server 192.168.8.5:80;
server 192.168.8.6:80;
}
server {
listen 80;
server_name blog.benet.com;
location / {
proxy_pass http://web;
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;
}
}
(三)启动nginx
systemctl start nginx
验证:
win10:
修改hosts文件
浏览器访问blog.benet.com
Nginx1和Nginx2输入tail -f /var/log/nginx/access.log查看日志,刷新一次浏览页面后会生成一条日志内容