Centos8.2安装Nginx连接上tomcat项目

1、使用yum安装nginx变异工具及安装环境。

[root@zhanchenglong ~]# yum -y install lrzsz make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

2、安装pcre,让nginx支持rewrite功能

[root@zhanchenglong ~]# cd /usr/local/pcre/
[root@zhanchenglong pcre]# wget https://sourceforge.net/projects/pcre/files/pcre2/10.36/pcre2-10.36.tar.gz
[root@zhanchenglong pcre]# tar xf pcre2-10.36.tar.gz 
[root@zhanchenglong pcre]# cd pcre2-10.36/
[root@zhanchenglong pcre2-10.36]# ./configure
[root@zhanchenglong pcre2-10.36]# echo $?
0
[root@zhanchenglong pcre2-10.36]# make && make install

检查安装结果
[root@iZbp1779uwgmvan37a08rzZ pcre2-10.36]# pcre2-config --version
10.36

3.编译安装nginx

[root@iZbp1779uwgmvan37a08rzZ nginx]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@iZbp1779uwgmvan37a08rzZ nginx]# tar xf nginx-1.18.0.tar.gz 
[root@iZbp1779uwgmvan37a08rzZ nginx]# cd nginx-1.18.0/
[root@iZbp1779uwgmvan37a08rzZ nginx-1.18.0]# ./configure --prefix=/usr/local/nginx
[root@iZbp1779uwgmvan37a08rzZ nginx-1.18.0]#./configure --prefix=/usr/local/nginx
[root@iZbp1779uwgmvan37a08rzZ nginx-1.18.0]#make && make install
[root@iZbp1779uwgmvan37a08rzZ nginx-1.18.0]# cd /usr/local/nginx/
[root@iZbp1779uwgmvan37a08rzZ nginx]# ./sbin/nginx
[root@iZbp1779uwgmvan37a08rzZ nginx]# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

设置快捷自启动脚本,只需要将以上内容保存到 /lib/systemd/system/nginx.service,然后 systemctl enable nginx 即可

vim /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

也可以参照其它文档进行安装,更详细,转载https://segmentfault.com/a/1190000020839997

4.安装jdk

软件包官网进行下载,此处不解释
[root@iZbp1779uwgmvan37a08rzZ jdk]# tar xf jdk-16_linux-x64_bin.tar.gz
[root@iZbp1779uwgmvan37a08rzZ jdk-16]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk/jdk-16
export CLASSPATH=$:CLASSPATH:$JAVA_HOME/lib/
export PATH=$PATH:$JAVA_HOME/bin


编辑完之后,保存并退出,然后输入以下指令,刷新环境配置使其生效:
[root@iZbp1779uwgmvan37a08rzZ jdk]# source /etc/profile
[root@iZbp1779uwgmvan37a08rzZ jdk-16]# java -version
java version "16" 2021-03-16
Java(TM) SE Runtime Environment (build 16+36-2231)
Java HotSpot(TM) 64-Bit Server VM (build 16+36-2231, mixed mode, sharing)

5.安装tomcat

[root@iZbp1779uwgmvan37a08rzZ tomcat]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.44/bin/apache-tomcat-9.0.44.tar.gz

启动tomcat
[root@iZbp1779uwgmvan37a08rzZ apache-tomcat-9.0.44]# ./bin/startup.sh
[root@iZbp1779uwgmvan37a08rzZ apache-tomcat-9.0.44]# netstat -lntp
tcp6       0      0 :::8080          :::*             LISTEN      18141/java    

检查相关防火墙安全组    用ip加上端口方式就可以访问了

6.编辑nginx配置文件,实现转接tomcat

[root@iZbp1779uwgmvan37a08rzZ conf]# vim nginx.conf
[root@iZbp1779uwgmvan37a08rzZ conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@iZbp1779uwgmvan37a08rzZ conf]# /usr/local/nginx/sbin/nginx -s reload

注释:至此已经实现用nginx代理tomcat的8080端口,此实验结束。

打赏

Leave a Comment

电子邮件地址不会被公开。 必填项已用*标注