start nginx on boot with systemd

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?

Asked By: j0h

||

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.

Answered By: intelfx

Heres what worked for me:
https://web.archive.org/web/20150328063215/https://longhandpixels.net/blog/2014/02/install-nginx-debian-ubuntu

I ignored most of the document, which was specific to compiling other versions, of nginx, and went over to “Make it Autostart”.

I followed the directions there, and now when I reboot, nginx 1.9 is running.

I definately appreciate everyone’s help, and insight. Thank you all!

Answered By: j0h

Fixed the link in the accepted answer so it redirects to the right page. But here’s a relevant bit:

sudo systemctl enable nginx.service
sudo systemctl start nginx.service
sudo systemctl status nginx.service

where /lib/systemd/system/nginx.service looks something like:

# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

`
Answered By: luxagraf

from nginx resources https://www.nginx.com/resources/wiki/start/topics/examples/systemd/

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

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

[Install]
WantedBy=multi-user.target
" > /lib/systemd/system/nginx.service
Answered By: Simone Sanfratello

Faced this same issue and here is what worked for me.

sudo systemctl unmask nginx.service

then

sudo systemctl enable nginx.service

finally

service nginx start

from https://mkyong.com/nginx/nginx-unit-nginx-service-is-masked/

Answered By: Andrew Chamamme
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.