-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvhost
executable file
·57 lines (42 loc) · 1.27 KB
/
vhost
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
#!/bin/bash
# Validation
# Check if ran with root privileges
if [[ $EUID > 0 ]]; then # we can compare directly with this syntax.
echo "You must run script with root privileges!"
exit 1
fi
# Check for supplied action
if [[ $1 != 'create' ]] && [[ $1 != 'delete' ]]; then
echo 'You must choose operation! Either create or delete...'
exit 1;
fi
# Check for supplied vhost name
if [[ $2 = '' ]]; then
echo 'You must enter vhost name!'
exit 1;
fi
if [[ $1 = 'create' ]]; then
echo "<VirtualHost *:80>
ServerAdmin admin@$2.local
ServerName $2.local
ServerAlias www.$2.local
DocumentRoot /var/www/html/$2
ErrorLog /var/log/apache2/$2-error.log
CustomLog /var/log/apache2/$2-access.log combined
</VirtualHost>" > /etc/apache2/sites-available/$2.local.conf
echo -e "127.0.0.1\t$2.local www.$2.local" >> /etc/hosts
chown -R www-data:www-data /var/www/html/$2
chmod -R 775 /var/www/html/$2
a2ensite -q $2.local.conf > /dev/null
apache2ctl graceful
echo "$2.local vhost created."
exit 0
fi
if [[ $1 = 'delete' ]]; then
a2dissite $2.local.conf > /dev/null
sed "/$2\.local/d" -i /etc/hosts
rm /etc/apache2/sites-available/$2.local.conf
apache2ctl graceful
echo "$2.local vhost deleted."
exit 0
fi