forked from RoverWire/virtualhost
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvirtualhost.sh
executable file
·252 lines (221 loc) · 6.96 KB
/
virtualhost.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
#!/bin/bash
### Set Language
TEXTDOMAIN=virtualhost
### Set default parameters
action=$1
domain=$2
rootDir=$3
owner=$(who am i | awk '{print $1}')
email=$4
https=$5
sitesEnable='/etc/apache2/sites-enabled/'
sitesAvailable='/etc/apache2/sites-available/'
userDir='/var/www/'
sitesAvailabledomain=$sitesAvailable$domain.conf
### don't modify from here unless you know what you are doing ####
if [ "$(whoami)" != 'root' ]; then
echo $"You have no permission to run $0 as non-root user. Use sudo"
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. example: test.com"
read domain
done
if [ "$rootDir" == "" ]; then
rootDir=${domain//./}
fi
### if root dir starts with '/', don't use /var/www as default starting point
if [[ "$rootDir" =~ ^/ ]]; then
userDir=''
fi
rootDir=$userDir$rootDir
if [ "$action" == 'create' ]
then
### check if domain already exists
if [ -e $sitesAvailabledomain ]; then
echo -e $"This domain already exists.\nPlease Try Another one"
exit;
fi
while [ "$https" != 'n' ] && [ "$https" != 'y' ]
do
echo -e $"Force domain to HTTPS? (Openssl self-signed cert) (y/n)"
read https
done
while [ "$email" == "" ]
do
echo -e $"Please provide email. example: webmaster@localhost"
read email
done
### check if directory exists or not
if ! [ -d $rootDir ]; then
### create the directory
mkdir $rootDir
### give permission to root dir
chmod 755 $rootDir
### write test file in the new domain dir
if ! echo "<?php echo phpinfo(); ?>" > $rootDir/index.php
then
echo $"ERROR: Not able to write in file $rootDir/index.php. Please check permissions"
exit;
else
echo $"Added content to $rootDir/index.php"
fi
fi
if [ "$https" == 'n' ]
then
### create virtual host rules file
if ! echo "
<VirtualHost $domain:80>
ServerAdmin $email
ServerName $domain
ServerAlias www.$domain
DocumentRoot $rootDir
<Directory />
AllowOverride all
</Directory>
<Directory $rootDir>
Options FollowSymLinks
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 creating $domain file"
exit;
else
clear
echo -e $"\nNew Virtual Host Created\nYour new host is: http://$domain \nAnd its located at $rootDir"
fi
fi
if [ "$https" == 'y' ]
then
### enable required modules for apache2 with https
a2enmod headers
a2enmod rewrite
a2enmod ssl
### create ssl self-signed cert
mkdir /etc/apache2/ssl
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt
### create virtual host rules file with https
if ! echo "
<VirtualHost $domain:80>
ServerAdmin $email
ServerName $domain
ServerAlias www.$domain
DocumentRoot $rootDir
<Directory />
AllowOverride all
</Directory>
<Directory $rootDir>
Options FollowSymLinks
AllowOverride all
Require all granted
</Directory>
ErrorLog /var/log/apache2/$domain-error.log
LogLevel error
CustomLog /var/log/apache2/$domain-access.log combined
Header always set Strict-Transport-Security \"max-age=63072000; includeSubDomains\"
# Force redirect to https(port 443) and non-wwww.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\\.(.*)\$ [NC]
RewriteRule ^(.*)\$ https://%1/\$1 [R=301,L]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost $domain:443>
ServerAdmin $email
ServerName $domain
ServerAlias www.$domain
DocumentRoot $rootDir
Header always set Strict-Transport-Security \"max-age=63072000; includeSubDomains\"
# Force redirect to https(port 443) and non-wwww.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\\.(.*)\$ [NC]
RewriteRule ^(.*)\$ https://%1/\$1 [R=301,L]
ErrorLog /var/log/apache2/$domain-error.log
LogLevel error
CustomLog /var/log/apache2/$domain-access.log combined
# Enable/Disable SSL for this virtual host.
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key
<FilesMatch \"\\.(cgi|shtml|phtml|php)$\">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
BrowserMatch \"MSIE [2-6]\" \\
nokeepalive ssl-unclean-shutdown \\
downgrade-1.0 force-response-1.0
# MSIE 7 and newer should be able to use keepalive
BrowserMatch \"MSIE [17-9]\" ssl-unclean-shutdown
</VirtualHost>
</IfModule>" > $sitesAvailabledomain
then
echo -e $"There is an ERROR creating $domain file"
exit;
else
clear
echo -e $"\nNew Virtual Host Created\nYour new host is: https://$domain \nAnd its located at $rootDir"
fi
fi
### Add domain in /etc/hosts
if ! echo "127.0.0.1 $domain" >> /etc/hosts
then
echo $"ERROR: Not able to write in /etc/hosts"
exit;
else
echo -e $"Host added to /etc/hosts file \n"
fi
if [ "$owner" == "" ]; then
chown -R $(whoami):$(whoami) $rootDir
else
chown -R $owner:$owner $rootDir
fi
### enable website
a2ensite $domain
### restart Apache
/etc/init.d/apache2 restart
else
### check whether domain already exists
if ! [ -e $sitesAvailabledomain ]; then
echo -e $"This domain does not exist.\nPlease try another one"
exit;
else
### Delete domain in /etc/hosts
newhost=${domain//./\\.}
sed -i "/$newhost/d" /etc/hosts
### disable website
a2dissite $domain
### restart Apache
/etc/init.d/apache2 restart
### Delete virtual host rules files
rm $sitesAvailabledomain
fi
### check if directory exists or not
if [ -d $rootDir ]; then
echo -e $"Delete host root directory ? (y/n)"
read deldir
if [ "$deldir" == 'y' -o "$deldir" == 'Y' ]; then
### Delete the directory
rm -rf $rootDir
echo -e $"Directory deleted"
else
echo -e $"Host directory conserved"
fi
else
echo -e $"Host directory not found. Ignored"
fi
### show the finished message
echo -e $"Complete!\nYou just removed Virtual Host $domain"
exit 0;
fi