»
23. Deploy as Linux service
这是从网络的文章中总结得到的最佳Linux服务部署方案。我(张人杰)的方案:
go-search.service:
[Unit]
Description=go search server
After=network.target
[Service]
WorkingDirectory=/root/
Type=forking
ExecStart=/root/start-go-search.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -9 $MAINPID
PrivateTmp=true
Restart=always
RestartSec=15
StartLimitInterval=0
StartLimitBurst=5
[Install]
WantedBy=multi-user.target
go-nginx.service:
[Unit]
Description=go nginx server
After=network.target
[Service]
WorkingDirectory=/root/nginx-compiled/
Type=forking
ExecStart=/root/nginx-compiled/sbin/nginx
ExecReload=/root/nginx-compiled/sbin/nginx -s reload
ExecStop=/bin/killall nginx
PrivateTmp=true
Restart=always
RestartSec=10
StartLimitInterval=0
StartLimitBurst=5
[Install]
WantedBy=multi-user.target
start-go-search.sh:
#!/bin/bash
cd /root/
nohup ./go-search > /dev/null 2>&1 &
文件部署:
1、go-search.service与go-nginx.service放到/etc/systemd/system/目录里;
2、使用systemctl enable go-search、systemctl enable go-nginx和systemctl daemon-reload使能两个服务的自启动;
3、使用service go-search start和service go-nginx start启动服务;
4、判断服务运行状态service go-search status和service go-nginx status。
加一段英文:The difference between this solution and rc.local is: the process will be restarted in this solution whenever the process is down by other reasons.