-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage-vhost.sh
executable file
·350 lines (312 loc) · 8.68 KB
/
manage-vhost.sh
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/bash
HERE=$(pwd)
PORT=""
DIR=""
NAME=""
ARGS_OK=0
HOSTS="/etc/hosts"
VHOSTS="/etc/apache2/extra/httpd-vhosts.conf"
UNAME=$(uname)
TS=0
echo "Virtual Host Setup."
echo ""
# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# make sure we are running on mac osx :(
if [ $UNAME != "Darwin" ]; then
echo "This script only works on Mac OS X"
exit 1
fi
##
# Get a timestamp
# usage:
# TS=$(timestamp)
# echo $TS
function timestamp() {
local __result=`date +"%Y%m%d-%H%M%S"`
echo "$__result"
}
##
# Display help for tool
function usage {
echo "usage: manage-vhost -n NAME -p PORT -d DIRECTORY"
echo "-h : this usage message"
echo "-l : list virtual hosts"
echo "-n <name> : name of site, i.e. product name"
echo "-p <port> : port number to access site by"
echo "-d <path> : top-level directory of website"
echo "-r <port> : remove virtual host on this port"
echo "-R : restore config files to defaults"
echo "NB: arguments can be given in any order."
echo ""
echo "Run with no arguments for interactive mode"
echo "example: manage-vhost -n mysite -p 9999 -d ~/path/to/site/"
echo "On completion, the site will be available at localhost:9999"
}
##
# Clean up vhosts file, remove dummy entries
function cleanupVhosts() {
cp $VHOSTS $VHOSTS.backup
sed -i '' "/NameVirtualHost \*:80/d" $VHOSTS
sed -i '' "/\<VirtualHost \*:80\>/,/\<\/VirtualHost\>/d" $VHOSTS
}
##
# Add virtual host details
# Modifies apache to enable virtual hosts
# Adds details to http-vhosts.conf
# Add details to /etc/hosts
# restarts apache
function addHost {
# quick check we have everything we need.
if [ -z $NAME ]; then
echo "Site name missing!"
usage
exit -1;
else
NAME="$NAME.site"
fi
if [ -z $PORT ]; then
echo "Site port missing!"
usage
exit -1;
fi
if [ -z $DIR ]; then
echo "Site directory missing!"
usage
exit -1;
fi
#have all parameters, let's make a virtual host!
echo "Build vhost for $DIR:$PORT as $NAME"
# enable virtual hosts if not already done so
if [ ! -f /etc/apache2/httpd.conf.abacus ]; then
echo "Back up httpd.conf and enable virtual hosts"
cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.abacus
sed -i '' "s/#Include \/private\/etc\/apache2\/extra\/httpd-vhosts.conf/Include \/private\/etc\/apache2\/extra\/httpd-vhosts.conf/" /etc/apache2/httpd.conf
fi
#Create httpd-vhosts.conf(if necessary)
if [ ! -f $VHOSTS ]; then
echo "Create $VHOSTS"
touch $VHOSTS
fi
# backup original vhost file, remove dummy entries
cleanupVhosts
echo "Append site details to $VHOSTS"
echo "# Virtual host start $NAME $PORT" >> $VHOSTS
echo "Listen $PORT" >> $VHOSTS
echo "NameVirtualHost *:$PORT" >> $VHOSTS
echo "" >> $VHOSTS
echo "<Directory \"$DIR\">" >> $VHOSTS
echo "Allow From All" >> $VHOSTS
echo "AllowOverride All" >> $VHOSTS
echo "Options +Indexes" >> $VHOSTS
echo "</Directory>" >> $VHOSTS
echo "<VirtualHost *:$PORT>" >> $VHOSTS
echo " ServerName \"$NAME\"" >> $VHOSTS
echo " DocumentRoot \"$DIR\"" >> $VHOSTS
echo "</VirtualHost>" >> $VHOSTS
echo "# Virtual host end $NAME" >> $VHOSTS
echo "" >> $VHOSTS
# append details to /etc/hosts
echo "Append site details to $HOSTS"
echo "# Virtual host start $NAME $PORT" >> $HOSTS
echo "127.0.0.1 $NAME" >> $HOSTS
echo "fe80::1%lo0 $NAME" >> $HOSTS
echo "# Virtual host end $NAME" >> $HOSTS
echo "" >> $HOSTS
#restart apache
apachectl restart
echo "Configuration complete, test site link: http://localhost:$PORT"
}
##
# List all virtual hosts created by this tool
function listHosts {
declare -a ARRAY
let count=0
let i=0
while read LINE
do
if [[ $LINE == *Virtual\ host\ start* ]]
then
ARRAY[$count]=$LINE
((count++))
fi
done < $HOSTS
echo "Available hosts:"
for (( i=0;i<$count;i++)); do
echo "${ARRAY[${i}]}"
done
}
##
# Remove host details from specified file
# Host host is defined between 'start' and 'end' marker strings
# @param file to modify
function removeHost {
declare -a ARRAY
let count=0
let i=0
local CAPTURE=1
local FILE=$1
echo "Remove host by port: $PORT from $FILE"
# backup file
echo "cp $FILE $FILE.backup"
cp $FILE $FILE.backup
while read LINE
do
if [[ $LINE == *Virtual\ host\ start* ]]
then
if [[ ${LINE:(-4)} == $PORT ]]
then
CAPTURE=0
fi
fi
if [[ $LINE == *Virtual\ host\ end* ]]
then
if [ $CAPTURE -eq 0 ]; then
CAPTURE=2
fi
fi
if [ $CAPTURE -eq 1 ]; then
ARRAY[$count]=$LINE
((count++))
fi
if [ $CAPTURE -eq 2 ]; then
CAPTURE=1
fi
done < $FILE
# empty $FILE
> $FILE
for (( i=0;i<$count;i++)); do
echo "${ARRAY[${i}]}" >> $FILE
done
}
function restore() {
echo "Restore $VHOSTS and $HOSTS to defaults."
if [ ! -f $VHOSTS.modified ]; then
cp $VHOSTS $VHOSTS.modified
fi
if [ ! -f $HOSTS.modified ]; then
cp $HOSTS $HOSTS.modified
fi
cp /etc/apache2/httpd.conf.abacus /etc/apache2/httpd.conf
echo '#' > $VHOSTS
echo '# Virtual Hosts' >> $VHOSTS
echo '#' >> $VHOSTS
echo '# If you want to maintain multiple domains/hostnames on your' >> $VHOSTS
echo '# machine you can setup VirtualHost containers for them. Most configurations' >> $VHOSTS
echo "# use only name-based virtual hosts so the server doesn't need to worry about" >> $VHOSTS
echo '# IP addresses. This is indicated by the asterisks in the directives below.' >> $VHOSTS
echo '#' >> $VHOSTS
echo '# Please see the documentation at ' >> $VHOSTS
echo '# <URL:http://httpd.apache.org/docs/2.2/vhosts/>' >> $VHOSTS
echo '# for further details before you try to setup virtual hosts.' >> $VHOSTS
echo '#' >> $VHOSTS
echo '# You may use the command line option '-S' to verify your virtual host' >> $VHOSTS
echo '# configuration.' >> $VHOSTS
echo '' >> $VHOSTS
echo '#' >> $VHOSTS
echo '# Use name-based virtual hosting.' >> $VHOSTS
echo '#' >> $VHOSTS
echo 'NameVirtualHost *:80' >> $VHOSTS
echo '' >> $VHOSTS
echo '#' >> $VHOSTS
echo '# VirtualHost example:' >> $VHOSTS
echo '# Almost any Apache directive may go into a VirtualHost container.' >> $VHOSTS
echo '# The first VirtualHost section is used for all requests that do not' >> $VHOSTS
echo '# match a ServerName or ServerAlias in any <VirtualHost> block.' >> $VHOSTS
echo '#' >> $VHOSTS
echo '<VirtualHost *:80>' >> $VHOSTS
echo ' ServerAdmin [email protected]' >> $VHOSTS
echo ' DocumentRoot "/usr/docs/dummy-host.example.com"' >> $VHOSTS
echo ' ServerName dummy-host.example.com' >> $VHOSTS
echo ' ServerAlias www.dummy-host.example.com' >> $VHOSTS
echo ' ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"' >> $VHOSTS
echo ' CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common' >> $VHOSTS
echo '</VirtualHost>' >> $VHOSTS
echo '' >> $VHOSTS
echo '<VirtualHost *:80>' >> $VHOSTS
echo ' ServerAdmin [email protected]' >> $VHOSTS
echo ' DocumentRoot "/usr/docs/dummy-host2.example.com"' >> $VHOSTS
echo ' ServerName dummy-host2.example.com' >> $VHOSTS
echo ' ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"' >> $VHOSTS
echo ' CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common' >> $VHOSTS
echo '</VirtualHost>' >> $VHOSTS
echo '' >> $VHOSTS
echo "##" > $HOSTS
echo "# Host Database" >> $HOSTS
echo "#" >> $HOSTS
echo "# localhost is used to configure the loopback interface" >> $HOSTS
echo "# when the system is booting. Do not change this entry." >> $HOSTS
echo "##" >> $HOSTS
echo "127.0.0.1 localhost" >> $HOSTS
echo "255.255.255.255 broadcasthost" >> $HOSTS
echo "::1 localhost" >> $HOSTS
echo "fe80::1%lo0 localhost" >> $HOSTS
echo "" >> $HOSTS
}
#if no args passed, whinge and exit
if [ $# == 0 ]; then
echo "This script will update your apache configuration to allow websites to be addressed by port number."
echo -e "site nickname, i.e. mysite: "
read NAME
echo -e "port number: "
read PORT
echo -e "path to project, i.e. /Users/<user>/path/to/site: "
read DIR
ARGS_OK=1
fi
# if there are cmd line args, parse them
if [ $ARGS_OK == 0 ]; then
#parse arguments
while getopts "lhRp:d:n:r:" opt; do
case $opt in
r)
#remove host by port number
PORT=$OPTARG
if [ -z $PORT ]; then
echo "No port number given!"
usage
exit 1
fi
removeHost $HOSTS
removeHost $VHOSTS
apachectl restart
exit
;;
p)
PORT=$OPTARG
;;
n)
NAME=$OPTARG
;;
d)
DIR=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
exit 1
;;
l)
listHosts
exit 0
;;
R)
restore
apachectl restart
exit 0
;;
h)
usage
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
fi
addHost