nfs共享和nginx负载均衡
实验前置:
Nginx1:192.168.8.5
Nginx2:192.168.8.6
NFS:192.168.8.7
Mysql:192.168.8.8
Nginx_lb:192.168.8.9
实验步骤:
(1)在192.168.8.5和192.168.8.6上安装Nginx、博客和PHP
(2)在192.168.8.8上安装mysql
(3)在192.168.8.7上安装NFS
(4)在192.168.8.9上安装负载均衡器
实验前置环境:每台虚拟机还原虚拟机初始快照,网卡桥接,关闭防火墙和selinux
查看IP地址:
[root@nginx ~]#ifconfig ens33|grep netmask
//192.168.8.5为服务器的IP地址
关防火墙和selinux:
[root@nginx ~]# systemctl stop firewalld //关闭防火墙
[root@nginx ~]# setenforce 0 //关闭selinux
实验步骤:
开启五台虚拟机,用xshell连接就可以开始做实验了
(1)在192.168.8.5和192.168.8.6上安装Nginx、博客和PHP:
Nginx1:
配置Nginx镜像文件(192.168.8.5、192.168.8.6、192.168.8.9):
[root@node13 yum.repos.d]# vim /etc/yum.repos.d/nginx.repo
添加:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
保存退出
使用yum安装Nginx服务:
[root@node13 yum.repos.d]# yum -y install nginx
//Nginx服务已经安装完成
开启服务,并设置开机自启动:
[root@node13 yum.repos.d]# systemctl start nginx
[root@node13 yum.repos.d]# systemctl enable nginx
将PHP软件包拖拽到/目录下:(192.168.8.5、192.168.8.6)
//PHP软件包
将php-common.x86_64这个软件包删除掉,因为要安装的软件包和这个会发生冲突:
[root@node13 php]# yum -y remove php-common.x86_64
使用本地安装php软件包:
[root@node13 php]# yum -y localinstall *.rpm
启动php服务和设置自启动:
[root@node13 php]# systemctl start php-fpm
[root@node13 php]# systemctl enable php-fpm
将wordpress软件包拖拽到/目录下:(192.168.8.5、192.168.8.6)
//博客软件包
解压软件包:
[root@node13 /]# unzip wordpress-4.9.4-zh_CN.zip
给一个所有权限:
[root@node13 /]# chmod -R 777 wordpress
创建虚拟主机配置文件:
[root@node13 /]# 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服务:
[root@node13 /]# nginx -s reload
(2)在192.168.8.8上安装mysql:
Mysql:
创建一个/mysql目录,将mysql软件包拖拽到这个文件中:
[root@node13 ~]# mkdir /mysql
[root@node13 ~]# cd /mysql
//将mysql5.6-rpm中的所有软件包拖拽到/mysql目录中
查看一下:
[root@node13 mysql]# ls
将mariadb-libs-5.5.68-1.el7.x86_64这个软件包删除掉,因为要安装的软件包和这个会发生冲突:
[root@node13 mysql]# yum -y remove mariadb-libs-5.5.68-1.el7.x86_64
使用本地安装mysql软件包:
[root@node13 mysql]# yum -y localinstall *.rpm
启动mysql,并设置自启动:
[root@node13 mysql]# systemctl start mysqld
[root@node13 mysql]# systemctl enable mysqld
[root@node13 mysql]# mysql_secure_installation
密码:123
登录mysql:
[root@node13 mysql]# mysql -uroot –p
密码:123
创建数据库:
mysql> create database blog;
授权认证并创建本地用户,配置密码:
mysql> grant all on blog.* to lisi@'192.168.8.%' identified by '123';
编辑/etc/hosts文件:
[root@node13 mysql]# echo "192.168.8.5 blog.benet.com" >> /etc/hosts
(3)在192.168.8.7上安装NFS:
NFS:
查看是否安装了nfs-utils、rpcbind这两个软件包:
nfs-utils 负责文件共享
rpcbind 负责网络通信
[root@nfs ~]# rpm -q nfs-utils rpcbind
//已安装
创建挂载点:
[root@nfs ]# mkdir -p /nfs
发布共享目录:
[root@nfs ~]# vim /etc/exports
添加:
/nfs 192.168.8.0/24(rw,sync,no_root_squash)
重启nfs服务:
[root@nfs ~]# systemctl restart rpcbind
[root@nfs ~]# systemctl restart nfs
在两台Nginx服务器(192.168.8.5、192.168.8.6)上查看nfs共享目录:
[root@node13 /]# showmount -e 192.168.8.7
把wordpress的内容目录挂载到nfs:
[root@node13 wordpress]# cp -rp wp-content/ wp-contentbak
[root@node13 wordpress]# mount -t nfs 192.168.8.7:/nfs/ wp-content
[root@node13 wordpress]# cp -rp wp-contentbak/* wp-content/
切换目录:
[root@node13 wordpress]# cd wp-content
在NFS(192.168.8.7)上,切换到nfs目录,创建一个文件:
[root@nfs /]# cd /nfs
[root@nfs nfs]# ls
[root@nfs nfs]# touch gjm
在Nginx1上查看:
[root@node13 wp-content]# ls
两台nginx(192.168.8.5、192.168.8.6)上设置永久挂载:
[root@nfs nfs]# vim /etc/fstab
在最下面添加:
192.168.8.7:/nfs /wordpress/wp-content nfs defaults 0 0
//第一台Nginx
//第二台Nginx
(4)在192.168.8.9上安装负载均衡器:
在前面就要安装Nginx环境!
配置负载均衡:
[root@nginx_lb ~]# vim /etc/nginx/conf.d/lb1.conf
添加:
upstream web {
server 192.168.8.10:80;
server 192.168.8.20: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服务:
[root@nginx_lb ~]# nginx -s reload
在NFS(192.168.8.7)上配置域名解析:
[root@nfs nfs]# vim /etc/hosts
添加:
192.168.8.9 blog.benet.com
验证:在NFS上打开火狐访问
站点标题:admin
用户名:gjm
密码:123.com
电子邮件:123123@456456.com
在第一台和第二台上查看日志:
[root@nginx1 wp-content]# tail -f /var/log/nginx/access.log
[root@nginx2 wp-content]# tail -f /var/log/nginx/access.log
刷新网页内容,两台日志都在更新
实验结果:实验负载均衡!!!