无法启动 nginx.service:未找到单位 nginx.service

Failed to start nginx.service: Unit nginx.service not found

我已经使用以下选项从源二进制文件安装 Nginx:

/configure --sbin-path=/usr/bin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --with-pcre --with-http_ssl_module

一切正常,

$ sudo nginx -V
nginx version: nginx/1.21.0
built by gcc 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) 
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --sbin-path=/usr/bin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --with-pcre --with-http_ssl_module

$ ps aux | grep nginx
amookhs+    1793  1.0  0.0   8160   736 pts/0    S+   03:58   0:00 grep --color=auto nginx

但是,当我 运行 sudo systemctl reload nginx 命令时,我得到这个错误:

Failed to reload nginx.service: Unit nginx.service not found.

我该如何解决?

我将此文件另存为 /lib/systemd/system/nginx.service

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/bin/nginx -t
ExecStart=/usr/bin/nginx
ExecReload=/usr/bin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

注意。PIDFile 和 NGINX 二进制文件的位置可能会有所不同,具体取决于 NGINX 的编译方式。