18

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?

  • 6
    systemctl enable nginx... – jasonwryan May 19 '15 at 23:04
21

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.

| improve this answer | |
  • I just want nginx to load on boot. I thought systemctl could help me figure out why its not happening. – j0h May 20 '15 at 10:58
  • @j0h: Re-read my answer again. I have described why it doesn't autostart and what to do to make it autostart. Hint: the last sentence. – intelfx May 20 '15 at 11:00
  • The link redirects to a 404 page, and I used systemctl to enable nginx. When I rebooted, it still wasnt't running. perhaps I should try systemctl enable nginx.service – j0h May 20 '15 at 11:05
  • 1
    @j0h: I've meant sentence, not reference. (BTW, sorry for broken link — fixed now.) And yes, re-read it once again, you need systemctl enable (not systemctl start) to make something start on boot. – intelfx May 20 '15 at 11:08
2

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

`
| improve this answer | |
2

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!

| improve this answer | |
1

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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.