Nginx-manager: A tool for managing Nginx on Ubuntu/Debian Linux.

Last week we setup Nginx as a reverse proxy/load balancing/ssl endpoint. As with all of our projects, we selected Ubuntu Linux. We put this in place to replace a older server running pound. We wanted more web server features, and Nginx fit the bill perfectly. So far the server has been an overwhelming success.

Just like Ubuntu/Debian does with Apache, they chose to setup Nginx with a “sites-avaliable” and “sites-enabled” folders for enabling and disabling vhosts. However unlike Apache, there seems to be no tools to make enabling and disabling a quick single command. So we are forced to make and delete the links from the “sites-available” folder to the “sites-enabled” folder and then reload the Nginx service.

I really liked the concept of having a tool to enable and disable sites (vhosts) for me, so I took the liberty of quickly writing up a tool that does just that.

Enter Nginx-manager. This tool is just a small bash script that automates the creation and deletion of links as well as optionally will reload the server for you. On our server we have a half-dozen websites that use this single Nginx server to handle their ssl termination as well as failover to backup servers and standard down time notices (hosted on Nginx). I will write a guide on how we setup this server in detail in the next few days. But in a nut shell we have a standard Nginx setup with each backend server having it’s own vhost file in /etc/nginx/sites-available. To enable one of these vhosts, we simple use this tool as follows.

sudo nginx-manager -e www.mysite.com

and then we reload the service

sudo service nginx reload

We can also combine these into a single command.

sudo nginx-manager -R -e www.mysite.com

Disabling a site can be done just as easily.

sudo nginx-manager -d www.mysite.com

This is a very rough script. You should not use it unless you understand exactly what it is doing. It does not do anything dangerous, but you can never be too safe. It was written to meet my needs in an environment I control and is posted here only as a spring board to help you manage your systems in a more simplified fashion. To use this script, you simply need to paste the following code into a file named nginx-manager, give it executable permissions, and make sure it is in your path (I placed mine in /usr/local/bin/).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
# Only root should run this script
if [ `whoami` != 'root' ]; then
	echo 'You must be root (or use sudo) to use this tool!'
	exit;
fi
 
# Path to avaliable and enabled folders
AVAIL_PATH='/etc/nginx/sites-available/'
ENABLE_PATH='/etc/nginx/sites-enabled/'
 
function usage
{
	cat <<EOH
Usage:
	-h, --help - Displays this screen
	-e, --enabled - Enables the supplied vhost.
	-d, --disabled - Disables the supplied vhost.
	-R, --reload - Automatically restarts the nginx service.
EOH
}
 
LONG_OPTION_SPEC="help,reload,enable:,disable:"
SHORT_OPTION_SPEC="hRe:d:"
PARSED_OPTIONS=$(getopt -n "$0" -a -o $SHORT_OPTION_SPEC -l $LONG_OPTION_SPEC -- "$@")
OPTIONS_RET=$?
 
eval set -- "$PARSED_OPTIONS"
 
if [ $OPTIONS_RET -ne 0 ] || [ $# -le 0 ]; then
	usage;
	exit;
fi
 
RELOAD=false
ACTION='NONE'
VHOST='NONE'
FILENAME='NONE'
 
while [ $# -ge 1 ]; do
	case $1 in
	  --help | -h)
		usage;
		exit;;
	  --enable | -e)
		if [ $ACTION = 'NONE' ]; then
			ACTION='ENABLE';
			shift;
			FILENAME=$1;
		else
			echo 'Error! You can not supply both enable and disable!'
			usage;
			exit;
		fi;;
	  --disable | -d)
		if [ $ACTION = 'NONE' ]; then
                        ACTION='DISABLE';
			shift;
			FILENAME=$1;
                else
                        echo 'Error! You can not supply both enable and disable!'
                        usage;
                        exit;
                fi;;
	  --reload | -R)
		RELOAD=true;;
	  --) shift;;
	  * ) 
		echo "Error: unknown flag $1";
		usage;
		exit;
	esac
	shift
done
 
# First we need to make sure we have work to do.
if [ $ACTION = 'NONE' ] && [ $RELOAD = false ]; then
        echo 'Error: no action was supplied!'
        usage;
        exit;
fi
 
# Now that we know we have work to do, let's do it.
AFILE=${AVAIL_PATH}${FILENAME};
EFILE=${ENABLE_PATH}${FILENAME};
 
if [ $ACTION = 'ENABLE' ]; then
	if [ -f $AFILE ]; then
		if [ -h $EFILE ]; then
			echo "$FILENAME is already enabled.";
			exit;
		else
			ln -s $AFILE $EFILE;
			if [ $RELOAD = true ]; then
				service nginx reload;
			fi		
			echo "Site: $FILENAME is enabled!"
			if [ $RELOAD = false ]; then
				echo 'Please restart or reload nginx for your changes to take effect.'
			fi
			exit;
		fi
	else
		echo "$FILENAME is not a valid vhost file"
	fi
fi
 
if [ $ACTION = 'DISABLE' ]; then
	if [ -h $EFILE ]; then
		rm $EFILE;
        	if [ $RELOAD = true ]; then
        		service nginx reload;
        	fi
		echo "Site: $FILENAME is disabled!"
                        if [ $RELOAD = false ]; then
                                echo 'Please restart or reload nginx for your changes to t
ake effect.'
                        fi
 
        	exit;
	else
        	echo "$FILENAME is already disabled. "
	fi
fi

I hope you enjoy!

Be Sociable, Share!

10 Comments

DonOctober 12th, 2010 at 2:37 pm

I fixed a small error in the script. The help was displaying –restart when the command was really –reload.

websiteJune 29th, 2013 at 2:13 am

It’s really a great and useful piece of information. I’m glad that you just shared this helpful information with us.

Please keep us up to date like this. Thanks for sharing.

digital photography lessonsJuly 1st, 2013 at 10:04 pm

It’s very easy to find out any matter on net as compared to textbooks, as I found this piece of writing at this site.

online digital photography coursesJuly 2nd, 2013 at 11:40 am

I was suggested this web site by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my problem. You’re incredible!
Thanks!

[...] shell script called nginx-manager, when installed at /usr/local/bin, makes it easy to enable or disable virtual websites. For me, [...]

[...] With Ubuntu, virtual hosts are set up under /etc/nginx/sites-available. When you need to activate or deactivate virtual hosts, you have to create or delete symbolic links from each virtual host file to an identical file under /etc/nginx/sites-enabled. While creating or deleting a symbolic link isn’t difficult, it’s easy to make a mistake. That’s why I recommend using a script like nginx-manager. [...]

elegant wedding napkin ringJuly 23rd, 2013 at 10:18 am

Hi there! I just would like to give you a big thumbs up for your excellent info
you have got right here on this post. I am coming back to
your blog for more soon.

laptop repair in kolkataSeptember 29th, 2013 at 5:54 pm

Hey there! I’m at work surfing around your blog from
my new apple iphone! Just wanted to say I love reading through your blog and look forward to all
your posts! Keep up the excellent work!

NginX Configuration Options and WordPressNovember 7th, 2013 at 12:51 am

[...] shell script called nginx-manager, when installed at /usr/local/bin, makes it easy to enable or disable virtual hosts. For me, [...]

RodApril 22nd, 2014 at 3:49 am

It’s easy to get caught up in your individual writing and allow it to pull
you along. There is really a new variety of download site where you’ll be able to get not just movies to your Iphone but games and music too, and for free.
And whilst you cannot alter the past, we’ve got the power to switch our attitude about it, hence the actual
way it ultimately affects us.

Leave a comment

Your comment