golang gin web 服务器部署
前言
go-gin开发web服务端时,通常使用go run main.go
进行直接启动,但是在上线部署后,一旦ssh断开,服务就停止了。
基本流程
1、build 项目
2、创建一个 service 配置文件
3、启动
golang gin 服务器部署
下面具体叙述
1、build 项目
项目中,主文件为main.go
执行:
go build -o server main.go
则生成了一个新文件server
对其设置权限,这里用了777
chmod 777 server
2、创建一个 service 配置文件
输入命令创建:
vim /usr/lib/systemd/system/pilotgo_server.service
其中PilotGo Server为服务名称,以后启动都是这个名称。
里面写这样的内容:
[Unit]
Description=PilotGo Server
[Service]
Type=simple
Restart=always
RestartSec=3s
WorkingDirectory=/opt/PilotGo
ExecStart=/opt/PilotGo/server --config.file=/opt/PilotGo/config_server.yaml
[Install]
WantedBy=multi-user.target
路径需要自行替换
说明如下:
Description是对这个服务的描述
Restart=always服务异常退出时会重启
RestartSec=3s设置重启间隔为3秒
WorkingDirectory 项目的完整目录
ExecStart 启动文件的完整路径及配置文件路径等等,启动服务时会识别此字段
WantedBy=multi-user.target所有用户都可以执行
3、启动
重载service文件
systemctl daemon-reload
systemctl start pilotgo_server #启动
systemctl stop pilotgo_server #停止
systemctl restart pilotgo_server #重启
systemctl status pilotgo_server #查看状态