我是靠谱客的博主 老迟到毛豆,这篇文章主要介绍CentOS 配置Tomcat及搭配Nginx一、准备环境二、安装Tomcat过程三、Nginx 作前端负载均衡的设置,现在分享给大家,希望可以做个参考。

CentOS 配置Tomcat及搭配Nginx

  • 一、准备环境
  • 二、安装Tomcat过程
      • /etc/systemd/system/tomcat.service
  • 三、Nginx 作前端负载均衡的设置
    • 1、安装Nginx
    • 2、配置开机启动
    • 3、配置负载均衡
    • 4、卸载

一、准备环境

  • CentOS8
  • 安装 JDK1.8
  • 已安装 Nginx

二、安装Tomcat过程

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.46/bin/apache-tomcat-9.0.46.tar.gz tar -zxvf apache-tomcat-9.0.46.tar.gz cp -R apache-tomcat-9.0.46.tar.gz /opt/tomcat sudo groupadd tomcat sudo useradd -M -s /bin/nologin -g tomcat -d /opt/tomcat tomcat cd ~ cd /opt/tomcat sudo chgrp -R tomcat conf sudo chmod g+rwx conf sudo chmod g+r conf/* sudo chown -R tomcat work/ temp/ logs/

/etc/systemd/system/tomcat.service

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
sudo vim /etc/systemd/system/tomcat.service # Systemd unit file for tomcat [Unit] Description=Apache Tomcat Web Application Container After=syslog.target network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib/jvm/jre Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid Environment=CATALINA_HOME=/opt/tomcat Environment=CATALINA_BASE=/opt/tomcat Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC' Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom' ExecStart=/opt/tomcat/bin/startup.sh ExecStop=/bin/kill -15 $MAINPID User=tomcat Group=tomcat [Install] WantedBy=multi-user.target

继续运行脚本:

复制代码
1
2
3
4
5
6
7
sudo systemctl daemon-reload 或者用 cd bin ./startup.sh 如果要开机自动启动服务: sudo systemctl enable tomcat --tomcat service enable

现在可以打开默认页面:

http://server_IP_address:8080

复制代码
1
2
3
# 停止命令: ./shutdown.sh

配置tomcat web management界面:

复制代码
1
2
sudo vim /opt/tomcat/conf/tomcat-users.xml

tomcat-user.xml-Admin user

复制代码
1
2
3
4
<tomcat-users> <user username="admin" password="123456" roles="manager-gui,admin-gui"/> </tomcat-users>

重启:

复制代码
1
2
sudo systemctl restart tomcat

查看日志:

复制代码
1
2
3
cd /opt/tomcat/logs tail -f catalina.out

三、Nginx 作前端负载均衡的设置

1、安装Nginx

复制代码
1
2
vim /etc/yum.repos.d/nginx.repo

添加内容:

复制代码
1
2
3
4
5
6
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1

完成后使用yum命令安装nginx :

复制代码
1
2
yum install nginx

安装后查询Nginx位置:

复制代码
1
2
whereis nginx

Nginx命令:

复制代码
1
2
service nginx start/stop/restart

查检配置文件是否正确:

复制代码
1
2
/usr/sbin/nginx -t

Nginx 目录: /usr/local/nginx/
sudo vi /usr/local/nginx/conf/nginx.conf

2、配置开机启动

复制代码
1
2
3
cd /lib/systemd/system/ vim nginx.service

内容:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target

设置 开机启动

复制代码
1
2
systemctl enable nginx.service

其它命令

复制代码
1
2
3
4
systemctl start nginx.service 启动nginx systemctl stop nginx.service 结束nginx systemctl restart nginx.service 重启nginx

3、配置负载均衡

vi /etc/nginx/nginx.conf

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
user nginx; worker_processes 2; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; 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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; upstream backend { server 内部ip1:8080 weight=1; server 内部ip2:8080 weight=1; } include /etc/nginx/conf.d/*.conf; }

vi /etc/nginx/conf.d/domain.com.conf

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server { listen 8080; server_name domain.com; location / { proxy_pass http://backend; proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }

4、卸载

复制代码
1
2
yum remove nginx

参考:
安装Tomcat
https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-centos-7
如果yum安装参考这里
https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-7-on-centos-7-via-yum

最后

以上就是老迟到毛豆最近收集整理的关于CentOS 配置Tomcat及搭配Nginx一、准备环境二、安装Tomcat过程三、Nginx 作前端负载均衡的设置的全部内容,更多相关CentOS内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部