Tips / Nginx

Nginx Init Script for Fedora Linux

Sometimes, you need to compile Nginx from source. One of the disadvantages of this method is the fact that the source code doesn’t provide any init script for a fast service interaction with the user (such as service nginx start, stop, reload, restart, etc).

However, you can install your own Nginx init script, and avoid running Nginx manually from the command line. Simply call the binary each time you need to start, stop, or reload the Nginx configuration.

Some time ago, you may have noticed the Nginx init script for RHEL and CentOS. Today, it’s time to share the Nginx init script version for Fedora. This is a little bit different, since it uses Systemd.

This init script should work on Fedora, OpenSUSE and Arch Linux. It is tested on Fedora 22.

Important: You may need to change the paths of your pid file and Nginx binary file, depending on what parameters you pass to the ./configure command while compiling.

Create the systemd daemon service:

nano -w /lib/systemd/system/nginx.service

Paste this inside:

Description=The nginx HTTP and reverse proxy server
ExecStartPre=/usr/sbin/nginx -t
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID

Test it:

service nginx restart

If you see this, then it’s working:

[root@server ~]# service nginx restart
Redirecting to /bin/systemctl restart  nginx.service
[root@server ~]# 

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking