Trying to enable Nginx on Ubuntu 18.04 throws an error Default-Start contains no runlevels, aborting?

Don40 asked:

I have an nginx service and it works perfectly, the only problem is I can’t enable it. I followed this thread Not a native service systemd – can't run on boot and did what was told in the accepted answer and I still have a problem.

When I run:

sudo systemctl enable nginx.service

I get:

Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nginx
update-rc.d: error: nginx Default-Start contains no runlevels, aborting.

When I start it with sudo systemctl start nginx.service and run:

sudo systemctl status nginx.service

I am getting:

nginx.service - The NGINX HTTP and reverse proxy server
   Loaded: loaded (/etc/systemd/system/nginx.service; disabled; vendor preset: enabled)
   Active: active (running) since Wed 2020-10-28 08:26:05 UTC; 57min ago
    Tasks: 3 (limit: 2303)
   CGroup: /system.slice/nginx.service
           ├─1281 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           ├─2094 nginx: worker process
           └─2095 nginx: cache manager process

Oct 28 08:26:04 vps-cef86568 systemd[1]: Starting nginx.service...
Oct 28 08:26:05 vps-cef86568 nginx[1262]:  * Starting Nginx Server
Oct 28 08:26:05 vps-cef86568 nginx[1262]:    ...done.
Oct 28 08:26:05 vps-cef86568 systemd[1]: Started nginx.service.

My /etc/systemd/system/nginx.service file looks like this:

Description=The NGINX HTTP and reverse proxy server

ExecStartPre=/usr/sbin/nginx -t
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID


Any suggestions how to enable it?

My answer:

For some reason Ubuntu’s build of nginx includes both a systemd unit and the obsolete SysV init script that it was meant to replace. This also is present in 20.04 LTS, so upgrading will not help. I would guess some packager just forgot about it; consider opening a report on Launchpad so the maintainers can remove the obsolete script.

For the immediate problem, you probably should use the systemd unit that Ubuntu shipped, rather than your own. Delete the file you created, run systemctl daemon-reload, and then try enabling the service again.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.