Golang webapp的systemd服务ExecStart失败



我是golang的新手,我想在我的服务器上托管一个简单的网站。

我在Ubuntu 18.04

我的域根目录在/var/www/vhosts/mydomain.com/mydomain.com

我有一个main.go文件,它在浏览器中呈现一个简单的Hello World。

当我从这个目录go run main.go时,网页工作正常。

现在我试图创建一个服务,以保持网页可用时,我关闭我的shell

为此,我在/etc/systemd/system处创建了一个新的service,名为golangweb.service

该文件的内容为:

[Unit]
Description = Go Server
[Service]
ExecStart=/var/www/vhosts/mydomain.com/mydomain.com
Type=simple
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target

保存文件后,按顺序插入以下命令:

sudo systemctl daemon-reload
sudo systemctl enable golangweb.service
sudo systemctl start golangweb.service
sudo systemctl status golangweb.service

当我试图获得状态时,我得到以下错误(我在那里剔除了一些数据):

golangweb.service - Go Server
Loaded: loaded (/etc/systemd/system/golangweb.service; enabled; vendor preset: enabl
Active: activating (auto-restart) (Result: exit-code) since Fri xx-xx-xx 23:43:52
**Process: xx ExecStart=/var/www/vhosts/mydomain.com/mydomain.com (code=exited, sta
Main PID: xx (code=exited, status=203/EXEC)**
Mai xx xx:xx:xx xxxxx.xxxx.xxxx.systemd[1]: golangweb.service: Failed with re
Warning: Journal has been rotated since unit was started. Log output is incomplete or u
lines 1-8/8 (END)
● golangweb.service - Go Server
Loaded: loaded (/etc/systemd/system/golangweb.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Fri xx-xx-xx xx:xx:xx CEST; 3s ago
Process: xx ExecStart=/var/www/vhosts/mydomain.com/mydomain.com (code=exited, status=203/EXEC)
Main PID: xx (code=exited, status=203/EXEC)
Mai xx xx:xx:xx xxxx.xx.xx xx[1]: golangweb.service: Failed with result 'exit-code'.
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

有人知道为什么会这样吗?

ExecStart的第一个参数需要是一个可执行文件。看起来您已经将它设置为一个目录。如果您尝试在shell提示符中输入/var/www/vhosts/mydomain.com/mydomain.com,您将看到类似的行为:您无法运行目录。

Youcouldset:

WorkingDirectory=/var/www/vhosts/mydomain.com/mydomain.com
ExecStart=/usr/bin/go run main.go
或者,您可以编译代码(go build),然后将ExecStart设置为编译后二进制文件的完整路径:
ExecStart=/var/www/vhosts/mydomain.com/mydomain.com/compiledprogramname

最新更新