NFS 共享 Nginx 网页根目录(自动部署)

简介: NFS 共享 Nginx 网页根目录(自动部署)
IP HOSTNAME SERVICE SYSTEM
192.168.131.132 proxy-nfs nginx+nfs-server CentOS 7.6
192.168.131.131 nginx01 nginx+nfs-client CentOS 7.6
192.168.131.130 nginx02 nginx+nfs-client CentOS 7.6

环境准备

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@localhost ~]# sestatus
SELinux status:                 disabled
[root@localhost ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@localhost ~]# hostnamectl --static set-hostname porxy-nfs
[root@localhost ~]# hostnamectl --static set-hostname nginx01
[root@localhost ~]# hostnamectl --static set-hostname nginx02

部署nginx-proxy

[root@porxy-nfs ~]# vim install_nginx_proxy.sh
#!/usr/bin/bash
if [ -e /etc/nginx/nginx.conf ]
    then
        echo 'Already installed'
        exit 0
    else
        /usr/bin/yum -y install epel* && /usr/bin/yum -y install nginx
fi

if [ -f /etc/nginx/nginx.conf ];then
    proxy="upstream nfs-share { server 192.168.131.131 weight=8;server 192.168.131.130 weight=6;}"
    /usr/bin/sed -ri "/^http/a $proxy" /etc/nginx/nginx.conf
    /usr/bin/sed -ri "/^ *location \/ \{$/a proxy_pass http://nfs-share\;" /etc/nginx/nginx.conf
fi

/usr/sbin/nginx -t
if [ $? -ne 0 ]
    then
        echo "config error"
        exit 2
    else
        echo "config success "
fi

/usr/bin/systemctl enable nginx --now
/usr/bin/ps -ef | /usr/bin/grep [n]ginx

if [ $? -eq 0 ]
    then
    echo 'Start nginx successful'
else
    echo 'Start nginx faild please check again'
fi
[root@porxy-nfs ~]# sh install_nginx_proxy.sh
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
config success
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
root      13283  10234  0 09:07 pts/0    00:00:00 sh install_nginx_proxy.sh
root      13589      1  0 09:07 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     13590  13589  0 09:07 ?        00:00:00 nginx: worker process
Start nginx successful

部署nfs-server

[root@porxy-nfs ~]# vim install_nfs_server.sh
#!/usr/bin/bash
test -s /etc/exports
if [ $? -eq 0 ]
  then
    /usr/bin/yum -y install rpcbind nfs-utils
    echo "/share 192.168.131.132/24 (rw,sync,root_squash,sid=0)" > /etc/exports
    mkdir -p /share
    chmod -R o+w /share
    systemctl enable rpcbind.service --now && systemctl enable nfs-server.service --now
    iptables -F && iptables -X
    share=`showmount -e`
    access_IP=`awk '{print $2}' /etc/exports`
    echo "NFS was successfully installed. The directory you shared with us is: $share $access_IP"
  exit
else
    echo "NFS was installed"
    exit
fi
[root@porxy-nfs ~]# sh install_nfs_server.sh
NFS was successfully installed. The directory you shared with us is:  192.168.131.132/24

部署nginx01&nginx02

[root@nginx01 ~]# vim install_nginx.sh
#!/usr/bin/bash
if [ -e /etc/nginx/nginx.conf ]
    then
        echo 'Already installed'
        exit 0
    else
        /usr/bin/yum -y install epel* && /usr/bin/yum -y install nginx
fi

/usr/sbin/nginx -t
if [ $? -ne 0 ]
    then
        echo "config error"
        exit 2
    else
        echo "config success "
fi

/usr/bin/systemctl enable nginx --now
/usr/bin/ps -ef | /usr/bin/grep [n]ginx

if [ $? -eq 0 ]
    then
    echo 'Start nginx successful'
else
    echo 'Start nginx faild please check again'
fi
[root@nginx01 ~]# sh install_nginx.sh
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
config success
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
root      22300   7245  0 09:28 pts/0    00:00:00 sh install_nginx.sh
root      23036      1  0 09:30 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     23037  23036  0 09:30 ?        00:00:00 nginx: worker process
Start nginx successful

部署nfs-client

[root@nginx01 ~]# vim install_nfs_client.sh
#!/usr/bin/bash
iptables -F && iptables -F
yum -y install rpcbind nfs-utils
systemctl enable rpcbind.service --now && systemctl enable nfs-server.service --now
showmount -e 192.168.131.132
  if [ $? -eq 0 ]
      then
          mount -t nfs 192.168.131.132:/share /usr/share/nginx/html
          echo "mount -t nfs 192.168.131.132:/share /usr/share/nginx/html" > ~/.bashrc
      else
          mount fiald !
          exit 1
  fi
[root@nginx01 ~]# sh install_nfs_client.sh
Redirecting to /bin/systemctl start rpcbind.service
Redirecting to /bin/systemctl start nfs.service
Export list for 192.168.131.132:
/share (everyone)

测试nfs共享nginx网站目录

# 只要更新了nfs的共享目录,nginx的web页面也会马上更新
[root@porxy-nfs ~]# echo "hello world" > /share/test.html
[root@porxy-nfs ~]# curl 192.168.131.132/test.html
hello world
[root@porxy-nfs ~]# echo "linux" > /share/test.html
[root@porxy-nfs ~]# curl 192.168.131.132/test.html
linux
# 基于负载均衡,查看后端nginx日志,都有访问日志,轮询也没有问题
[root@nginx01 ~]# tail /var/log/nginx/access.log
192.168.131.132 - - [10/Aug/2020:10:02:45 +0800] "GET /test.html HTTP/1.0" 200 12 "-" "curl/7.29.0" "-"
[root@nginx02 ~]# tail /var/log/nginx/access.log
192.168.131.132 - - [10/Aug/2020:10:02:39 +0800] "GET /test.html HTTP/1.1" 200 12 "-" "curl/7.29.0" "-"
目录
相关文章
|
8天前
|
编解码 前端开发 JavaScript
摄像头web网页播放功能: ffmeg和nginx实现
摄像头web网页播放功能: ffmeg和nginx实现
126 0
|
应用服务中间件 Linux 网络安全
部署一个网页Docker+Nginx
在docker中运行一个nginx容器,部署一个网页
651 0
部署一个网页Docker+Nginx
|
9月前
|
负载均衡 应用服务中间件 Shell
共享nfs和nginx负载均衡
共享nfs和nginx负载均衡
109 0
|
9月前
|
负载均衡 关系型数据库 应用服务中间件
共享nfs和nginx负载均衡
共享nfs和nginx负载均衡
共享nfs和nginx负载均衡
|
9月前
|
域名解析 运维 负载均衡
【运维知识进阶篇】Tomcat集群实战之部署zrlog博客(Tomcat服务安装+静态资源挂载NFS+Nginx负载均衡+HTTPS证书+Redis会话保持)
【运维知识进阶篇】Tomcat集群实战之部署zrlog博客(Tomcat服务安装+静态资源挂载NFS+Nginx负载均衡+HTTPS证书+Redis会话保持)
267 1
|
存储 负载均衡 应用服务中间件
【web项目】keepalived高可用+LVS负载均衡+nginx动静分离+nfs共享存储
【web项目】keepalived高可用+LVS负载均衡+nginx动静分离+nfs共享存储
225 0
【web项目】keepalived高可用+LVS负载均衡+nginx动静分离+nfs共享存储
|
应用服务中间件 Apache nginx
nginx配置网页密码
nginx配置网页密码
167 0
nginx配置网页密码
|
Ubuntu 应用服务中间件 网络安全
Ubuntu下 Nginx静态代理部署网页常见报错
Ubuntu下 Nginx静态代理部署网页常见报错
Ubuntu下 Nginx静态代理部署网页常见报错
|
存储 负载均衡 NoSQL
Nginx模拟CLB搭建nginx+nfs同步web服务器搭建
Nginx模拟CLB搭建nginx+nfs同步web服务器搭建
239 0
Nginx模拟CLB搭建nginx+nfs同步web服务器搭建
|
存储 负载均衡 NoSQL
nginx反向代理做负载均衡以及使用redis实现session共享配置详解
nginx反向代理做负载均衡以及使用redis实现session共享配置详解
462 0
nginx反向代理做负载均衡以及使用redis实现session共享配置详解