go-platform/docs/服务端启动命令.md
2026-04-09 18:07:59 +08:00

5.1 KiB
Raw Permalink Blame History

方式一:使用 systemd 服务(推荐)

自动安装(推荐)

使用安装脚本自动配置 systemd 服务:

# 进入脚本目录
cd /www/wwwroot/api.yunzer.cn/scripts

# 添加执行权限
chmod +x install-systemd-service.sh

# 运行安装脚本
sudo bash install-systemd-service.sh

或者

sudo env PATH=$PATH:/usr/local/btgo/bin bash install-systemd-service.sh


脚本会自动:

  • 停止现有服务和进程
  • 创建正确的 systemd 配置文件
  • 启动服务
  • 启用开机自启
  • 显示服务状态和日志

手动安装

如果需要手动配置:

# 1. 停止现有服务
systemctl stop go-api
pkill -f "go run main.go"

# 2. 复制服务文件
sudo cp /www/wwwroot/api.yunzer.cn/scripts/go-api.service /etc/systemd/system/

# 3. 重载 systemd
sudo systemctl daemon-reload

# 4. 启动服务
sudo systemctl start go-api

# 5. 启用开机自启
sudo systemctl enable go-api

# 6. 查看状态
sudo systemctl status go-api

启动服务

systemctl start go-api

查看状态

systemctl status go-api

常用命令

# 启动
systemctl start go-api

# 停止
systemctl stop go-api

# 重启
systemctl restart go-api

# 查看状态
systemctl status go-api

# 查看日志systemd 日志)
journalctl -u go-api -f

# 查看日志(文件日志)
tail -f /www/wwwroot/api.yunzer.cn/go.log

# 开机自启
systemctl enable go-api

# 禁用开机自启
systemctl disable go-api

方式二:使用管理脚本(推荐)

脚本位置

/www/wwwroot/api.yunzer.cn/scripts/service.sh

添加执行权限

chmod +x /www/wwwroot/api.yunzer.cn/scripts/service.sh

常用命令

# 启动服务
bash /www/wwwroot/api.yunzer.cn/scripts/service.sh start

# 停止服务
bash /www/wwwroot/api.yunzer.cn/scripts/service.sh stop

# 重启服务
bash /www/wwwroot/api.yunzer.cn/scripts/service.sh restart

# 查看状态
bash /www/wwwroot/api.yunzer.cn/scripts/service.sh status

# 查看日志(最后 50 行)
bash /www/wwwroot/api.yunzer.cn/scripts/service.sh logs

# 实时查看日志
bash /www/wwwroot/api.yunzer.cn/scripts/service.sh logs -f

# 查看最后 100 行日志
bash /www/wwwroot/api.yunzer.cn/scripts/service.sh logs 100

创建快捷命令(可选)

# 添加到 ~/.bashrc
echo 'alias go-service="bash /www/wwwroot/api.yunzer.cn/scripts/service.sh"' >> ~/.bashrc
source ~/.bashrc

# 使用快捷命令
go-service start
go-service restart
go-service status
go-service logs -f

方式三:后台直接启动

启动服务

cd /www/wwwroot/api.yunzer.cn
nohup go run main.go > go.log 2>&1 &

查看是否运行成功

tail -f go.log

查看进程

ps aux | grep "go run main.go" | grep -v grep

重启服务

pkill -f "go run main.go" && cd /www/wwwroot/api.yunzer.cn && nohup go run main.go > go.log 2>&1 &

停止服务

pkill -f "go run main.go"

日志查看

查看实时日志

# systemd 方式
journalctl -u go-api -f

# 直接启动方式
tail -f /www/wwwroot/api.yunzer.cn/go.log

查看最近日志

# systemd 方式
journalctl -u go-api -n 100

# 直接启动方式
tail -n 100 /www/wwwroot/api.yunzer.cn/go.log

查看错误日志

# systemd 方式
journalctl -u go-api -p err

# 直接启动方式
grep -i error /www/wwwroot/api.yunzer.cn/go.log

常见问题

1. 服务启动失败

检查日志

# systemd
journalctl -u go-api -n 50

# 直接启动
tail -n 50 /www/wwwroot/api.yunzer.cn/go.log

常见原因

  • 端口被占用8081
  • 数据库连接失败
  • 配置文件错误

2. 端口被占用

查看端口占用

netstat -tlnp | grep 8081
# 或
lsof -i :8081

停止占用进程

# 找到 PID
lsof -i :8081

# 停止进程
kill -9 <PID>

3. 进程残留

查找残留进程

ps aux | grep "go run main.go" | grep -v grep

清理残留进程

pkill -9 -f "go run main.go"

4. 日志文件不存在

原因:启动命令没有重定向输出

解决:使用正确的启动命令

nohup go run main.go > go.log 2>&1 &

性能监控

查看资源占用

# CPU 和内存
top -p $(pgrep -f "go run main.go")

# 详细信息
ps aux | grep "go run main.go" | grep -v grep

查看连接数

netstat -an | grep 8081 | wc -l

查看文件描述符

lsof -p $(pgrep -f "go run main.go") | wc -l

生产环境建议

  1. 使用 systemd 服务:更稳定,支持自动重启
  2. 配置日志轮转:防止日志文件过大
  3. 监控服务状态:使用监控工具(如 Prometheus
  4. 定期备份:备份数据库和配置文件
  5. 使用编译后的二进制:比 go run 更高效

编译并运行(推荐生产环境)

# 编译
cd /www/wwwroot/api.yunzer.cn
go build -o server main.go

# 运行
nohup ./server > go.log 2>&1 &

# 或使用 systemd修改 ExecStart
# ExecStart=/www/wwwroot/api.yunzer.cn/server

更新日期

2026-04-09