我是刚到高朗,并试图在我的服务器上主持一个简单的网站。
我在Ubuntu 18.04
我的域根目录位于/var/www/vhosts/mydomain.com/mydomain.com
中
这里有一个main.go
文件,它在浏览器中呈现一个简单的Hello。
当我从这个目录中go run main.go
时,网页是正确工作的。
现在,我正在尝试创建一个服务,以便在关闭shell
时保持网页可用。
为此,我创建了一个新的service
,名为golangweb.service
at /etc/systemd/system
。
该文件的内容是:
[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.
有人知道为什么会这样吗?
发布于 2022-05-27 22:27:21
ExecStart
的第一个参数需要是一个可执行文件。看起来你已经把它设置为一个目录了。如果您尝试在shell提示符下键入/var/www/vhosts/mydomain.com/mydomain.com
,您将看到类似的行为:无法运行目录。
你可以设置:
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
https://stackoverflow.com/questions/72411431
复制相似问题