Debian – start nginx on boot with systemd

debiannginxsystemd

I just installed nginx 1.9 on a Debian 8 server.
nginx is working fine, when I tell it to run, but it won't seem to load nginx automatically on boot.

I have tried numerous init scripts recommended on the internet, but nothing has worked yet. So now I am trying to figure it out with systemctl.

~$ systemctl status nginx
● nginx.service
   Loaded: masked (/dev/null)
   Active: inactive (dead)
~$ sudo systemctl try-restart nginx
Failed to try-restart nginx.service: Unit nginx.service is masked.
~$ sudo systemctl reload nginx
Failed to reload nginx.service: Unit nginx.service is masked.
~$ sudo systemctl reload nginx
Failed to reload nginx.service: Unit nginx.service is masked.

Unfortunately, I do not know what "service is masked" means, and I don't know why it is masked.

when I run

sudo nginx

the server runs just fine. So then, I looked into unmasking the nginx service.

~$ sudo systemctl unmask nginx.service
Removed symlink /etc/systemd/system/nginx.service.

ok cool, now I can start nginx using systemctl. So I checked to see if rebooting would load nginx automatically. But it fails to do so, and I have no idea where to go from here.

Can someone help me get nginx running automatically on boot?

Best Answer

You seem to confuse enable, start and mask operations.

  • systemctl start, systemctl stop: starts (stops) the unit in question immediately;
  • systemctl enable, systemctl disable: marks (unmarks) the unit for autostart at boot time (in a unit-specific manner, described in its [Install] section);
  • systemctl mask, systemctl unmask: disallows (allows) all and any attempts to start the unit in question (either manually or as a dependency of any other unit, including the dependencies of the default boot target). Note that marking for autostart in systemd is implemented by adding an artificial dependency from the default boot target to the unit in question, so "mask" also disallows autostarting.

So, these all are distinct operations. Of these, you want systemctl enable.

Ref.: systemctl(1).

More: Lennart Poettering (2011-03-02). "The Three Levels of Off". systemd for Administrators. 0pointer.de.

Related Question