关于linux:新手教学Linux环境下NginxTomcat-反向代理部署简单版负载均衡springboot-20-web项目

4次阅读

共计 2920 个字符,预计需要花费 8 分钟才能阅读完成。

一. 装置 Nginx

1. 下载 Nginx 的压缩包

能够通过官网进行下载,地址点击下载。抉择最新的版本进行下载。

wget -c https://nginx.org/download/nginx-1.19.7.tar.gz

2. 装置 Nginx 所需依赖环境

1) 装置 gcc
yum install gcc-c++
2) 装置 PCRE pcre-devel
yum install -y pcre pcre-devel
3) 装置 zlib
yum install -y zlib zlib-devel
4) 装置 Open SSL
yum install -y openssl openssl-devel

3. 装置 Nginx 并运行

1) 解压 Nginx 压缩包
tar -zxvf nginx-1.19.7.tar.gz
2) 执行配置文件初始化

进入解压后 nginx 所在目录, 执行初始化

cd nginx-1.19.7/
./configure

如果须要开始 https 反对,这里请不要间接执行./configure,即不要间接执行该脚本,而是在该脚本前面加上 SSL 模块,请执行如下命令代替 ./confingure

./configure --with-http_ssl_module
3) 编译装置 Nginx

在当前目录下执行

make install

执行胜利后,Nginx 默认装置到 /usr/local/ 目录下

4) 启动 Nginx

进入 /usr/local/nginx/sbin 目录,输出./nginx 即可启动 nginx

./nginx

敞开 Nginx

./nginx -s quit 或者 ./nginx -s stop

重启 Nginx

./nginx -s reload
5) 拜访 Nginx 服务器

在浏览器申请 http://127.0.0.1 如果 Nginx 启动胜利会显示如下

如果页面无法访问,能够查看 80 端口是否被防火墙拦挡,没有放开。

firewall-cmd --list-ports #查看凋谢的端口
firewall-cmd --zone=public --add-port=80/tcp --permanent #将 80 端口放开
firewall-cmd --reload # 重启防火墙,能力失效

再次申请 http://127.0.0.1 即可显示 Nginx 启动页面

二. 装置 Tomcat

1. 下载 Tomcat 安装包

因为我部署的 springboot 2.3.5 版本的 web 我的项目, 所以应用 tomcat9 版本
同样能够通过 Tomcat 官网,找到下载地址点击下载

wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.43/bin/apache-tomcat-9.0.43.tar.gz

2. 解压 Tomcat 安装包

tar -zxvf apache-tomcat-9.0.43.tar.gz

3. 复制两个 Tomcat

因为要应用简略负载平衡,所以复制两个同样 tomcat

mv apache-tomcat-9.0.43 apache-tomcat-8081 #重命名
cp -rf  apache-tomcat-8081 apache-tomcat-8082 #复制雷同 Tomcat

4. 配置 Tomcat

因为只有一台机器,所以配置两个 Tomcat 为不同的端口。
批改 Tomcat 配置文件

cd apache-tomcat-8081/conf #进入配置文件所在目录
vi server.xml #批改配置文件

次要批改 Tomcat 的端口

    <Server port="8015" shutdown="SHUTDOWN">
    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    
    <Connector executor="tomcatThreadPool"
               port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

另外一个 Tomcat 同理,批改 servier 端口为 8016,http 端口为 8082 就行

5. 启动 Tomcat

别离进入两个 Tomcat 的 bin 目录

cd apache-tomcat-8081/bin
./startup.sh | tail -f ../logs/catalina.out #启动 Tomcat 并输入日志
ps -ef| grep 'tomcat' #查看过程

浏览器拜访 http://127.0.0.1:8081 显示 Tomcat 欢送页面就示意启动胜利。如果无奈显示回看 Nginx 启动时,凋谢端口的办法。

上传 web 我的项目到服务器 Tomcat

1. maven 编译打包

mvn clean install

2. 上传 war 包

应用 xftp 别离上传到两个 tomcat 的 /webapp 目录下

3. 去掉拜访时的我的项目名

cd apache-tomcat-8081/conf #进入配置文件所在目录
vi server.xml #批改配置文件

配置 Host 节点下的 Context 标签,path=””,docBase 指向 webapp 下的我的项目 war 的名称即可。如下

    <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
       <Context path=""docBase="nirvana-admin-0.0.1-SNAPSHOT"reloadable="true" />
    </Host>

4. 启动我的项目

cd apache-tomcat-8081/bin
./shutdown.sh #敞开 tomcat 服务
./startup.sh | tail -f ../logs/catalina.out #启动 Tomcat 并输入日志

日志输入:

org.apache.catalina.startup.Catalina.start Server startup in [32403] milliseconds

示意启动胜利。

配置 Nginx 反向代理 + 负载平衡

批改 Nginx 的 nginx.conf 配置文件

cd /usr/local/nginx/conf
vi nginx.conf
    #在 http 节点下减少 upstream,前面名称随便,留神肯定不要带“_”下滑线,否则 Tomcat 启动报错。配置两个服务地址,用于负载平衡。http {
        upstream tomcatServer{
            server localhost:8081;
            server localhost:8082;
        }
    }
    # 在 location 节点下减少反向代理 proxy_pass,指向 upstream 的 tomcatServer
    location / {
        root   html;
        index  index.html index.htm;
        proxy_pass http://tomcatServer;
    }

配置实现后,重启 Nginx

cd /usr/local/nginx/sbin
./nginx -s reload

启动实现后,即可通过 http://127.0.0.1 拜访到 Tomcat 所部署的 web 我的项目。

正文完
 0