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 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:

$ cat /etc/systemd/system/nginx.service 
Description=The NGINX HTTP and reverse proxy server

ExecStartPre=/usr/pkg/sbin/nginx -t
ExecReload=/usr/pkg/sbin/nginx -s reload
ExecStop=/usr/pkg/sbin/nginx -s quit


Then enabled the nginx service:

$ sudo systemctl enable nginx

And finally started it:

$ sudo systemctl start nginx

Witness everything went as expected:

$ 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.