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:

[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

Test it:

service nginx restart

If you see this, then it’s working:

[[email protected] ~]# service nginx restart
Redirecting to /bin/systemctl restart  nginx.service
[[email protected] ~]# 


Leave a Reply