A photo of Mitesh Shah

Mitesh Shah

Linux Expert | Automation Enthusiast | Security Consultant

Email Skype Github Twitter Resume Hire Me Keybase LinkedIn Stackoverflow


Auto Reload NGINX

Overview

Requirements

  • inotify-tools
  • nginx

Auto Reload NGINX

NGINX Virtual Host

[mitesh@Matrix ~]$  cat /etc/nginx/nginx.conf
##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
  • As you noticed all the Virtual Host Configuration is stored at /etc/nginx/sites-enabled/ location.
  • The /etc/nginx/sites-enabled/ location contains only symbolic link of /etc/nginx/sites-available/.

For Example:

[mitesh@Matrix ~]$ ls -l /etc/nginx/sites-available/example.com
-rw-r--r-- 1 root root 418 Jun 23 18:14 /etc/nginx/sites-available/example.com
[mitesh@Matrix ~]$ ls -l /etc/nginx/sites-enabled/example.com
lrwxrwxrwx 1 root root 38 Jun 23 18:14 /etc/nginx/sites-enabled/example.com -> /etc/nginx/sites-available/example.com

Shell Script To Auto Reload NGINX

#!/bin/bash
# Check inotify-tools is installed or not
dpkg --get-selections | grep -v deinstall | grep inotify-tools &> /dev/null
if [ $? -ne 0 ]
then
        echo "Installing inotify-tools, please wait..."
        apt-get -y install inotify-tools
fi
while true
do
        inotifywait --exclude .swp -e create -e modify -e delete -e move  /etc/nginx/sites-enabled
        # Check NGINX Configuration Test
        # Only Reload NGINX If NGINX Configuration Test Pass
        nginx -t
        if [ $? -eq 0 ]
        then
                echo "Reloading Nginx Configuration"
                service nginx reload
        fi
done




Post Navigation