/
a2vhost16
164 lines (139 loc) · 3.75 KB
/
a2vhost16
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
#!/bin/bash
TEXTDOMAIN=virtualhost
### waw ubuntu 16.04 apache2 php7
# global? mv this to /usr/local/bin, chmod +x
# else rename to anything.sh , ./anything.sh
### Set default parameters - vhost [create|delete] [example.com] [/path/to/hell] [admin email]
action=$1
domain=$2
root=$3
email=$4
# apache2 user dir ubuntu 16.04
a2=/var/www/
# Put owner/group that apache2 must run over for your system.
owner=$(who am i | awk '{print $1}')
group=www-data
#apache2 default
sitesEnable='/etc/apache2/sites-enabled/'
sitesAvailable='/etc/apache2/sites-available/'
#apache2 restart
restart='/etc/init.d/apache2 reload'
#check for root
if [ "$(whoami)" != 'root' ]; then
echo $"You don't have the permission to run $0 as non-root user. Sudo maybe?"
exit 1;
fi
if [ "$action" != 'create' ] && [ "$action" != 'delete' ]
then
echo $"You need to prompt for action (create or delete) -- Lower-case only"
exit 1;
fi
while [ "$domain" == "" ]
do
echo -e $"Please provide domain. e.g. example.com"
read domain
done
sitesAvailabledomain=$sitesAvailable$domain.conf
if [ "$action" == 'create' ]
then
while [ "$root" == "" ]
do
example=example
echo -e $"Enter directory name e.g 'example' for $a2$example"
read root
done
while [ "$email" == "" ]
do
echo -e $"Please provide administrator email"
read email
done
### check if domain already exists
if [ -e $sitesAvailabledomain ]; then
echo -e $"This domain already exists. Exit."
exit;
fi
### check if directory exists or not
if ! [ -d $a2$root ]; then
### create the directory
mkdir $a2$root
### give permission to root dir
chmod 755 $a2$root
### write test file in the new domain dir
if ! echo $domain > $a2$root/index.html
then
echo $"ERROR: Unable to write in $a2$root/. Please check permissions."
exit;
else
echo $"Added index.html into $a2$root/"
fi
if ! echo "<?php phpinfo(); ?>" > $a2$root/info.php
then
echo $"ERROR: Unable to write in $a2$root/. Please check permissions."
exit;
else
echo $"Added info.php into $a2$root/"
fi
fi
### create virtual host rules file
if ! echo "<VirtualHost *:80>
ServerAdmin $email
ServerName $domain
ServerAlias $domain
DocumentRoot $a2$root
<Directory />
AllowOverride All
</Directory>
<Directory $a2$root>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Require all granted
</Directory>
ErrorLog /var/log/apache2/$domain-error.log
LogLevel error
CustomLog /var/log/apache2/$domain-access.log combined
</VirtualHost>" > $sitesAvailabledomain
then
echo -e $"There is an ERROR create $domain file"
exit;
else
echo -e $"New Virtual Host Created."
fi
### Add domain in /etc/hosts
if ! echo "127.0.0.1 $domain" >> /etc/hosts
then
echo $"ERROR: Not able write in /etc/hosts"
exit;
else
echo -e $"Host added to /etc/hosts file."
fi
chown -R $owner:$group $a2$root
### enable website
a2ensite $domain
### restart apache2
$restart
### show the finished message
echo -e $"Done! \nYour new host is: http://$domain located at $a2$root"
exit;
else
### check whether domain already exists
if ! [ -e $sitesAvailabledomain ]; then
echo -e $"This domain does not exist.\nPlease try another one"
exit;
else
olddir=$(head -5 /etc/apache2/sites-enabled/$domain.conf|tail -1| awk -F " |;" '{print $2}')
### Delete domain in /etc/hosts
newhost=${domain//./\\.}
sed -i "/$newhost/d" /etc/hosts
### disable website
a2dissite $domain.conf
### delete user data
rm -rf $olddir
### restart Apache
/etc/init.d/apache2 reload
### Delete virtual host rules files
rm $sitesAvailabledomain
fi
### show the finished message
echo -e $"Complete!\nYou just removed Virtual Host $domain"
exit 0;
fi