一、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-reloadsudo 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"