我需要运行在我的ubuntu server.how中使用gin框架和mongodb作为服务构建的golang服务器可以做到这一点吗?(每次运行go脚本时,我都需要这样做,gvm也使用go1.4)
发布于 2020-10-09 13:13:02
go build
(或应用程序的类似命令)为应用程序创建二进制文件。构建应该针对Linux (export GOOS=Linux
)。/var/www/webapp
或/opt/webapp
),并确保将root
作为二进制文件的所有者(或者可以为应用程序设置专用的webappuser
用户),并为二进制文件(chmod +x /var/www/webapp
)提供可执行权限。supervisor
),要么使用Ubuntu的本机systemd
为应用程序编写服务文件。apt-get install supervisor
。一旦安装好它,您就可以为应用程序编写一个管理脚本,如下所示:[program:webapp]
command=/var/www/webapp
autostart=true
autorestart=true
stderr_logfile=/var/log/webapp.err.log
stdout_logfile=/var/log/webapp.out.log
您需要以webapp.conf
的形式在/etc/supervisor/conf.d/webapp.conf
中编写这个文件。写入该文件后,您需要通过运行supervisorctl reread
和supervisorctl update
来重新加载监控器。然后,您的应用程序将开始通过Supervisor作为服务运行。
systemd
的S本地功能。您可以简单地在位置/lib/systemd/system/webapp.service
中编写一个文件:[Unit]
Description=Golang Web application
[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart=/var/www/webapp
[Install]
WantedBy=multi-user.target
写完之后,您将运行systemctl daemon-reload
;systemctl start webapp.service
和systemctl enable webapp.service
。然后,您的应用程序将开始通过Systemd作为服务运行。
我通常倾向于通过主管编写服务文件,因为它只允许您控制由您部署的应用程序。此外,您可能已经注意到,我已经在我的示例配置中编写了一些关于日志文件的内容。这基本上允许我控制日志位置和如何编写应用程序的日志(即错误在哪里,应用程序的日志在哪里)。这也有助于将特定文件流到外部日志管理服务(例如AWS Cloudwatch)。
https://askubuntu.com/questions/735183
复制相似问题