Nginx Tomcat Jar包开机启动自动配置

发布于:2025-09-07 ⋅ 阅读:(32) ⋅ 点赞:(0)

一、Nginx配置

1、创建systemd nginx 服务文件

vi /usr/lib/systemd/system/nginx.service

### 内容

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target

[Service]
Type=forking
ExecStartPre=/mnt/nginx/sbin/nginx -t
ExecStart=/mnt/nginx/sbin/nginx -c /mnt/nginx/conf/nginx.conf
ExecReload=/mnt/nginx/sbin/nginx -s reload
ExecStop=/mnt/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

2、重载systemd配置

sudo systemctl daemon-reload

3、启动服务,配置开机启动

# 启动nginx服务
sudo systemctl start nginx
# 设置开机自启   
sudo systemctl enable nginx  
# 检查服务状态
sudo systemctl status nginx
# 验证开机时候开机启动命令
sudo systemctl is-enabled nginx

4、重载,日志等命令

#停止 Nginx
sudo systemctl stop nginx
#重启 Nginx
sudo systemctl restart nginx
#重新加载配置:
sudo systemctl reload nginx
#禁用开机自启:
sudo systemctl disable nginx
# 实时日志命令
journalctl -u nginx -f

二、Jar包脚本配置

1、创建systemd 服务文件

sudo vi /etc/systemd/system/auth-center.service

2、增加内容

[Unit]
Description=auth_center Service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/data/production/auth_center
ExecStart=/data/java/jdk1.8.0_281/bin/java -jar firebirds-plume-oauth2-server-template-2.0.0-SNAPSHOT.jar  --spring.config.location=/data/production/auth_center/application.yml
Restart=on-failure
RestartSec=5s
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

3、重载systemd 配置,配置开机启动

### 重载
sudo systemctl daemon-reload
### 开机启动
sudo systemctl enable auth-center

4、服务管理命令

命令 说明
sudo systemctl start auth_center 启动服务
sudo systemctl stop auth_center 停止服务
sudo systemctl restart auth_center 重启服务
sudo systemctl status auth_center 查看状态
journalctl -u auth_center -f 查看实时日志

三、 Tomcat 开机启动

1、 创建服务

sudo vi /etc/systemd/system/tomcat.service

2、编辑内容

[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking

# 路径
Environment=CATALINA_PID=/data/production/apache-tomcat-8.5.82/temp/tomcat.pid
Environment=CATALINA_HOME=/data/production/apache-tomcat-8.5.82
Environment=CATALINA_BASE=/data/production/apache-tomcat-8.5.82
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'

#JAVA_HOME路径
Environment=JAVA_HOME=/data/java/jdk1.8.0_281/

ExecStart=/data/production/apache-tomcat-8.5.82/bin/startup.sh
ExecStop=/data/production/apache-tomcat-8.5.82/bin/shutdown.sh

# 使用tomcat用户运行
Group=tomcat
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

3、重载服务,启动命令

sudo systemctl daemon-reload

sudo systemctl start tomcat
sudo systemctl enable tomcat

4、可以使用journalctl查看日志

# 查看tomcat服务的所有日志
sudo journalctl -u tomcat.service

# 实时查看日志
sudo journalctl -u tomcat.service -f

# 查看最近100行日志
sudo journalctl -u tomcat.service -n 100

# 查看指定时间段的日志
sudo journalctl -u tomcat.service --since "2023-10-01" --until "2023-10-02"


网站公告

今日签到

点亮在社区的每一天
去签到