CentOS 配置Tomcat及搭配Nginx
Posted 编程圈
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS 配置Tomcat及搭配Nginx相关的知识,希望对你有一定的参考价值。
CentOS 配置Tomcat及搭配nginx
一、准备环境
- CentOS8
- 安装 JDK1.8
- 已安装 Nginx
二、安装Tomcat过程
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
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
继续运行脚本:
sudo systemctl daemon-reload
或者用
cd bin
./startup.sh
如果要开机自动启动服务:
sudo systemctl enable tomcat --tomcat service enable
现在可以打开默认页面:
http://server_IP_address:8080
# 停止命令:
./shutdown.sh
配置tomcat web management界面:
sudo vim /opt/tomcat/conf/tomcat-users.xml
tomcat-user.xml-Admin user
<tomcat-users>
<user username="admin" password="123456" roles="manager-gui,admin-gui"/>
</tomcat-users>
重启:
sudo systemctl restart tomcat
查看日志:
cd /opt/tomcat/logs
tail -f catalina.out
三、Nginx 作前端负载均衡的设置
1、安装Nginx
vim /etc/yum.repos.d/nginx.repo
添加内容:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
完成后使用yum命令安装nginx :
yum install nginx
安装后查询Nginx位置:
whereis nginx
Nginx命令:
service nginx start/stop/restart
查检配置文件是否正确:
/usr/sbin/nginx -t
Nginx 目录: /usr/local/nginx/
sudo vi /usr/local/nginx/conf/nginx.conf
2、配置开机启动
cd /lib/systemd/system/
vim nginx.service
内容:
[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
设置 开机启动
systemctl enable nginx.service
其它命令
systemctl start nginx.service 启动nginx
systemctl stop nginx.service 结束nginx
systemctl restart nginx.service 重启nginx
3、配置负载均衡
vi /etc/nginx/nginx.conf
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
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、卸载
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的主要内容,如果未能解决你的问题,请参考以下文章
CentOS 系统配置完jdk,tomcat mysql,nginx 项目发布步骤
基于docker镜像centos:7 镜像制作自定义的centos及tomcat/php/nginx镜像
基于docker镜像centos:7 镜像制作自定义的centos及tomcat/php/nginx镜像