nginx+tomcat+nfs集群部署

 架构解析:nginx代理外部的web访问请求,由两台到多台tomcat服务器提供真实的web服务,为了保证所有用户访问网站的统一性,所有tomcat服务器的网站根目录指向NFS(图中Database服务器)共享目录

实验部署环境顺序

NFS→Tomcat→Nginx

实验部署环境

四台CentOS7.9 第一台NFS 第二台Tomcat 第三台Tomacat 第四台Nginx

废话不多说,脚本如下:

Tomcat安装包如下

链接: https://pan.baidu.com/s/14A7R3miWLwHQ4HFK2PbYjQ

提取码: xks1

链接: https://pan.baidu.com/s/1Y9WHLF7WR8mclE0IknQ1Vw

提取码: xks1

NFS脚本如下:

#!/bin/bash
#function: nfs一键部署服务
#author: aliang  20230905
#####root判断#####
if
  [  "$USER"  != "root"   ]
then
   echo "错误:非root用户,权限不足!"
  exit  0
fi
############防火墙与高级权限##########
systemctl stop firewalld && systemctl disable firewalld  && echo "防火墙已经关闭"
sed -i 's/SELINUX=.*/SELINUX=disabled/g'  /etc/selinux/config  && echo "关闭selinux"

###############网络############
ping -c 3   www.baidu.com
if
   [ $? -eq 0 ]
   then
   echo -e "\n\033[32m-----------------------------------------------\033[0m"
                	echo -e "\033[32m网络畅通,即将安装依赖包\033[0m"
   else 
     echo -e "\n\033[32m-----------------------------------------------\033[0m"
                	echo -e "\033[32m即将退出,请检查外网通讯 !\033[0m"
        exit  0
fi

###安装nfs-utils###
yum -y install rpcbind nfs-utils

###设置共享目录###
mkdir -p /opt/share  && chmod -R 777 /opt/share 

##编辑配置文件###
cat >> /etc/exports << EOF

/opt/share 192.168.75.0/24(rw,sync,no_root_squash)

EOF
###########网页文件#####
cat > /opt/share/index.jsp <<EOF
1233573827
EOF
####开启服务####
systemctl start rpcbind && systemctl enable rpcbind         #设置为开机自启rpcbind
systemctl start nfs  && systemctl enable nfs               #设置为开机自启
#####共享nfs####
exportfs -r

###########测试#############
showmount -e localhost

Tomcat脚本如下:

#!/bin/bash
#function: tomcat一键部署服务
#author: aliang  20230905
#####root判断#####
if
  [  "$USER"  != "root"   ]
then
   echo "错误:非root用户,权限不足!"
  exit  0
fi
############防火墙与高级权限##########
systemctl stop firewalld && systemctl disable firewalld  && echo "防火墙已经关闭"
sed -i 's/SELINUX=.*/SELINUX=disabled/g'  /etc/selinux/config  && echo "关闭selinux"
datalj=/usr/local/
echo "安装之前请先确保是否安装了NFS文件服务器,是否符合条件"
read -ep "是否符合以上条件请输入(yes/no):" niuma
case $niuma in
    yes)
    echo  "符合条件,即将开始安装tomcat服务"
    ;;
    no)
    echo  "不符合条件,请先部署nfs服务"
    exit 0
    ;;
    *)
     echo "您的输入有错误,即将退出脚本"
    exit 1
esac
#########清理jkd相关插件#######
rpm -qa | grep java-* &> /dev/null
rpm  -e java-* &> /dev/null
sleep 3
#####上传.解压包#########
cd $datalj
echo "上传安装包jdk-8u161-linux-x64.tar.gz"
wen="jdk-8u161-linux-x64.tar.gz"
if [ -e "$wen" ]; then
  echo "软件包已存在,继续执行"
else
  echo "软件包不存在,安装个毛线!!!"
fi
###解压包###
cd $datalj
tar -zxf jdk-8u161-linux-x64.tar.gz &> /dev/null
####修改解压缩###
mv jdk1.8.0_161/ jdk1.8

###配置环境变量####
cat >> /etc/profile << EOF
export JAVA_HOME=/usr/local/jdk1.8/

export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export PATH=$PATH:$JAVA_HOME/bin

EOF
###刷新环境变量####
source /etc/profile

###配置web服务###
yum -y install httpd &> /dev/null   #安装httpd服务

##启动服务###
systemctl restart httpd

if [ $? -eq 0 ]; then
   echo "服务开启成功,老铁不错哟!!!"
else
   echo "服务开启失败,慢慢排查吧!!!"
exit 1
fi

####配置tomcat服务####
#上传包
cd $datalj
wen_path="apache-tomcat-9.0.80.tar.gz"
if [ -e "$wen_path" ]; then
   echo "文件包已存在,继续执行"
else
   echo "文件不存在,即将退出!!!"
exit 1
fi
tar -zxvf apache-tomcat-9.0.80.tar.gz  &> /dev/null

###修改解压包名称###
mv apache-tomcat-9.0.80/ tomcat9

#####插入内容#####
sed  '2aJAVA_HOME=/usr/local/jdk1.8/'   /usr/local/tomcat9/bin/catalina.sh

####启动服务###
cd tomcat9/bin     #tomcat9是我tomcat解压后的目录
./startup.sh &> /dev/null     #启动tomcat服务
if [ $? -eq 0 ]; then
   echo "老铁服务已成功启动,不错哟!!!"

else
   echo "老铁服务开启失败,自己慢慢排查吧!!!"
exit 1

fi
###############NFS################
 read  -ep  "请输入NFS服务器的IP:" nfsip
 ###默认首页目录/usr/local/tomcat9/webapps/ROOT#########
####默认安装目录/usr/local/tomcat9#######
###永久挂载nfs####
cat >> /etc/fstab << EOF
192.168.75.137:/opt/share /usr/local/tomcat9/webapps/ROOT   nfs  defaults    0 0
EOF
mount -a


Nginx部署脚本如下:

#!/bin/bash
#function:nginx+tomcat一键部署
#author:niumaliang  20230830
#####root判断#####
if
  [  "$USER"  != "root"   ]
then
   echo "错误:非root用户,权限不足!"
  exit  0
fi
##########防火墙及SElinux############
systemctl stop firewalld && systemctl disable firewalld  && echo "防火墙已经关闭"
sed -i 's/SELINUX=.*/SELINUX=disabled/g'  /etc/selinux/config  && echo "关闭selinux"
datalj=/usr/src/
######nginx_install#####
cd $datalj && wget http://nginx.org/download/nginx-1.22.1.tar.gz
ruanjian_path="nginx-1.22.1.tar.gz"
if [ -e "$ruanjian_path" ]; then
   
    echo "帅哥通行卡存在继续执行"
else
   
    echo "帅哥通行卡不存在,安装个毛线"
  if [ $? -eq 0 ]; then
      echo "兄弟请上传安装包到/usr/loacal"
exit 1
fi
fi

###############安装依赖包###########
yum -y install gcc-c++
yum -y install pcre pcre-devel 
yum -y install zlib zlib-devel 
yum -y install openssl openssl-devel
###########解包########
cd $datalj
tar zxf nginx-1.22.1.tar.gz
####编译安装######
cd nginx-1.22.1/
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module
 
if [ $? -eq 0 ]; then
 make && make install
  if [ $? -eq 0 ]; then
   echo "老铁编译安装完成,不错哟(*^_^*)"
fi
else
   echo "老铁编译安装失败,即将退出喽!!!"
exit 1
fi

####编辑文件#########
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

#########赋予权限######
chmod 755 /usr/lib/systemd/system/nginx.service
##########重载配置文件######
systemctl daemon-reload
####编辑网页####
groupadd nginx && useradd -M -s /sbin/nologin nginx -g nginx
mkdir -p /var/www/html
chown -R nginx.nginx /var/www/html

#######编辑nginx配置文件
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
read -ep  "请输入第一台tomcat服务器的IP:"   tomcata
read -ep  "请输入第一台tomcat服务器的IP:"   tomcatb
cat > /usr/local/nginx/conf/nginx.conf <<EOF
#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
    upstream backend{
                                                             server  $tomcata:8080; 
                                                             server  $tomcatb:8080;      
                                                              }
 
    #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  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
 
    server {
        listen       80;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://backend;
        }
 
        #error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
 
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
 
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
 
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
 
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
 
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
}

EOF
#开启服务#######
/usr/local/nginx/sbin/nginx -t
if [ $? -eq 0 ]; then
   echo "老铁,配置成功,不错不错(●ˇ∀ˇ●)"
else
   echo "老铁配置失败,即将退出喽!!!)"
exit 1
fi
#######服务是否开启#########
/usr/local/nginx/sbin/nginx
sleep 3
if [ $? -eq 0 ]; then
echo "nginx服务启动成功,服务部署完成"
else
 echo "哦豁,服务启动失败啦,自己慢慢排查吧!!!"
exit 1
fi

测试结果如下:

实验也就完成了,觉得不错的点赞加关注,后续还会有新的文章发出!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陪小余久¹点⁸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值