-
Notifications
You must be signed in to change notification settings - Fork 82
/
nginx_ensite
executable file
·111 lines (96 loc) · 3.99 KB
/
nginx_ensite
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
#!/bin/bash
### nginx_ensite --- Bash script to enable or disable a site in nginx.
### Copyright (C) 2010 António P. P. Almeida <appa@perusio.net>
### Author: António P. P. Almeida <appa@perusio.net>
### Permission is hereby granted, free of charge, to any person obtaining a
### copy of this software and associated documentation files (the "Software"),
### to deal in the Software without restriction, including without limitation
### the rights to use, copy, modify, merge, publish, distribute, sublicense,
### and/or sell copies of the Software, and to permit persons to whom the
### Software is furnished to do so, subject to the following conditions:
### The above copyright notice and this permission notice shall be included in
### all copies or substantial portions of the Software.
### Except as contained in this notice, the name(s) of the above copyright
### holders shall not be used in advertising or otherwise to promote the sale,
### use or other dealings in this Software without prior written authorization.
### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
### IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
### FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
### THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
### LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
### FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
### DEALINGS IN THE SOFTWARE.
## The nginx binary.
NGINX=$(which nginx)
[ -x $NGINX ] || exit 0
## The paths for both nginx configuration files and the sites
## configuration files and symbolic link destinations.
NGINX_CONF_DIR=/etc/nginx
AVAILABLE_SITES_PATH="$NGINX_CONF_DIR/sites-available"
ENABLED_SITES_PATH="$NGINX_CONF_DIR/sites-enabled"
SCRIPTNAME=${0##*/}
## Checking the type of action we will perform. Enabling or disabling.
ACTION=$(echo $SCRIPTNAME | awk '$0 ~ /dissite/ {print "DISABLE"} $0 ~ /ensite/ {print "ENABLE"} $0 !~ /(dis|en)site/ {print "UNKNOWN"}')
if [ "$ACTION" == "UNKNOWN" ]; then
echo "$SCRIPTNAME: Unknown action!"
print_usage
fi
function print_usage() {
echo "$SCRIPTNAME <site name>"
exit 1
}
## Check the number of arguments.
if [ $# -ne 1 ]; then
print_usage
else
SITE_AVAILABLE="$AVAILABLE_SITES_PATH/$1"
# Check if the sites-enabled directory exists. If not create it.
[ -d $ENABLED_SITES_PATH ] || mkdir -p $ENABLED_SITES_PATH
# If enabling the 'default' site then make sure that it's teh
# first to be loaded.
if [ $1 == "default" ]; then
SITE_ENABLED="$ENABLED_SITES_PATH/000-default"
else
SITE_ENABLED="$ENABLED_SITES_PATH/$1"
fi
fi
## Check that the file corresponding to site exists if enabling or
## that the symbolic link exists if disabling. Perform the desired
## action if possible. If not signal an error and exit.
case $ACTION in
ENABLE)
if [ -r $SITE_AVAILABLE ]; then
if [ -h $SITE_ENABLED ]; then
## If already enabled say it and exit.
echo "$1 is already enabled."
exit 0
else # symlink if not yet enabled
ln -s $SITE_AVAILABLE $SITE_ENABLED
fi
## Test for a well formed configuration.
echo "Testing nginx configuration..."
$NGINX -t && STATUS=0
if [ $STATUS ]; then
echo -n "site $1 has been enabled."
echo "Run /etc/init.d/nginx reload to apply the changes."
exit 0
else
exit 2
fi
else
echo "Site configuration file $1 not found."
exit 3
fi
;;
DISABLE)
if [ -h $SITE_ENABLED ]; then
rm $SITE_ENABLED
echo "Site $1 has been disabled."
echo "Run /etc/init.d/nginx reload to apply the changes."
exit 0
else
echo "Site $1 doesn't exist."
exit 3
fi
;;
esac