Start pkgsrc's nginx with systemd

Not so long ago, I wrote about using pkgsrc on Debian GNU/Linux, and assumed you’d start an installed service using rc.d. When I setup the new iMil.net server, I decided to give a try to kvm as it is easier to maintain, has good performances (sometimes better than Xen), nice administration tools, plus NetBSD now has a good VirtIO driver but no PVHVM support yet.

The first thing I do when setting up a Debian Jessie server is getting rid of systemd, whose philosophy and quality don’t match my personnal taste; but in that case, I wanted to use libvirtd so I could manage my virtual machines with virt-manager, and as a matter of fact, libvirtd has a hard dependency on systemd. There was no escape this time, I had to learn and use it.

Once nginx installed through pkgsrc, I wrote a unit file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ cat /etc/systemd/system/nginx.service 
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target

[Service]
Type=forking
ExecStartPre=/usr/pkg/sbin/nginx -t
ExecStart=/usr/pkg/sbin/nginx
ExecReload=/usr/pkg/sbin/nginx -s reload
ExecStop=/usr/pkg/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target

Then enabled the nginx service:

1
$ sudo systemctl enable nginx

And finally started it:

1
$ sudo systemctl start nginx

Witness everything went as expected:

1
2
3
4
5
6
7
8
9
10
11
$ sudo systemctl status nginx -l
● nginx.service - The NGINX HTTP and reverse proxy server
Loaded: loaded (/etc/systemd/system/nginx.service; enabled)
Active: active (running) since Mon 2016-02-08 11:54:48 CET; 2 weeks 5 days ago
Main PID: 23512 (nginx)
CGroup: /system.slice/nginx.service
├─11453 nginx: worker proces
└─23512 nginx: master process /usr/pkg/sbin/ngin

Feb 08 11:54:48 starkiller nginx[23508]: nginx: the configuration file /usr/pkg/etc/nginx/nginx.conf syntax is ok
Feb 08 11:54:48 starkiller nginx[23508]: nginx: configuration file /usr/pkg/etc/nginx/nginx.conf test is successful

Note that this does not prevent from using nginx‘s -t or -s flags.