forked from darkwhispering/Newvhost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newvhost
361 lines (292 loc) · 7.85 KB
/
newvhost
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
351
352
353
354
355
356
357
358
359
360
361
#!/bin/bash
#
# title : newvhost
# description : adds a new apache virtual host and create database if wanted
# usage : No parameters. Just invoke script-name.
# version : 1.1.0
#==================================================================================
#-----------------------------------------------
# Edit these variables after your needs
#-----------------------------------------------
# User to user for permissions
tSERVERUSER="www-data"
# Location to host all virtual hosts (recommended is the home folder of the user specified above)
# Do not include a traling slash
tLOCATION="/var/www/html"
# Safe ocation path, must be same as tLOCATION!
# Do not include a traling slash
tLOCATIONSAFE="\/var\/www/html"
# If left empty, the script will use root as account for MySQL
tDBU=""
# If left empty, the script will ask for MySQL root account password
tDBPW=""
#-----------------------------------------------
# DO NOT EDIT BELOW THIS LINE!
#-----------------------------------------------
## Check so root is running the script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Set up needed variables
tVHOST=""
tVHOSTDIR=""
tVHOSTCONFIG=""
tDBUSER=""
tDIR="/usr/local/newvhost"
tGENPASSWD=12
tGENPW=""
# Get proper paths to binaries used
tSED=$(which sed)
tA2ENSITE=$(which a2ensite)
tCHOWN=$(which chown)
tMKDIR=$(which mkdir)
tMYSQL=$(which mysql)
tTR=$(which tr)
tHEAD=$(which head)
tCP=$(which cp)
tRM=$(which rm)
tAPACHE2CTL=$(which apache2ctl)
#-----------------------------------------------
# Functions
#-----------------------------------------------
#
# Perform an action quietly
#
function quietly() {
"$@" >/dev/null 2>&1
}
#
# Generat a password
#
function genpasswd() {
tGENPW=$($tTR -dc A-Za-z0-9_ </dev/urandom | $tHEAD -c ${tGENPASSWD} | xargs)
}
#
# Get server name for the virtual host
#
function set_vhost() {
clear
echo
echo "-----------------------------------------------------------------------------"
echo
echo "Please type in the domain name to be used for the virtual host."
echo
echo "It must be a fully qualified domain name, including .com/.net or what else"
echo "you have as domain ending. However, DO NOT include http(s)!!"
echo
echo "-----------------------------------------------------------------------------"
echo
read -r -p "Domain name ? " _domain_
if [ -z "$_domain_" ]; then
echo
echo "No domain name given, exiting."
echo
exit 1
else
tVHOST=$_domain_
fi
read -r -p "Folder name ? " _folder_
if [ -z "$_folder_" ]; then
tVHOSTDIR=$tVHOST
else
tVHOSTDIR=$_folder_
fi
}
#
# Ask for the mysql root password if it is not already specified in the config
#
function get_rootpw() {
if [ -z "$tDBU" ]; then
echo
read -r -s -p "Database admin's user ? " _mydbu_
if [ -z "$_mydbu_" ]; then
tDBU=root
else
tDBU=$_mydbu_
fi
fi
if [ -z "$tDBPW" ]; then
echo
read -r -s -p "Database admin's password ? " _mydbpw_
if [ -z "$_mydbpw_" ]; then
echo
echo "No password given, exiting."
echo
exit 1
else
tDBPW=$_mydbpw_
fi
fi
}
#
# Create the new virtual host
#
function create_vhost() {
tVHOSTLOG="$tVHOSTDIR/log/"
tVHOSTDIRLOCATION="$tLOCATION/$tVHOSTDIR"
echo
echo
echo -n "Creating new virtual host for domain $tVHOST... "
# Check if the dir for the new vhost already exists
if [ -d "$tVHOSTDIRLOCATION" ]; then
echo "$tVHOSTDIRLOCATION already exists, aborting."
exit 1
fi
# Create the new dir for the vhost
$tMKDIR -p ${tVHOSTDIRLOCATION}/
# $tMKDIR -p $tVHOSTDIR
# $tMKDIR -p $tVHOSTLOG
# Copy the index.html template to the new vhost-dir
$tCP $tDIR/index.template $tVHOSTDIRLOCATION/index.html
# Set the proper web server permissions
$tCHOWN -R $tSERVERUSER:$tSERVERUSER $tVHOSTDIRLOCATION
# Put vhost config-file into place
tVHOSTCONFIG="/etc/apache2/sites-available/${tVHOST}.conf"
# Check if config file already exists
if [ -f "$tVHOSTCONFIG" ]; then
echo "$tVHOSTCONFIG already exists, aborting."
exit 1
fi
# Create apache config file to /etc/apache2/site-available
if ! echo "
<VirtualHost *:80>
ServerName $tVHOST
ServerAlias www.$tVHOST
DocumentRoot $tVHOSTDIRLOCATION
<Directory />
AllowOverride All
</Directory>
<Directory $tVHOSTDIRLOCATION>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Require all granted
</Directory>
ErrorLog /var/log/apache2/$tVHOST-error.log
LogLevel error
CustomLog /var/log/apache2/$tVHOST-access.log combined
</VirtualHost>" >$tVHOSTCONFIG; then
echo -e $"There is an ERROR creating $domain file"
exit
else
echo -e $"\nNew Virtual Host Created\n"
fi
printf "Done\n"
# Activate the new virtual host?
echo
read -r -p "Do you want to enable the newly added virtual host ($tVHOST) ? [y/N] " tCHOICE
case $tCHOICE in
[yY][eE][sS] | [yY])
echo
echo -n "Enabling vhost... "
$tA2ENSITE ${tVHOST}.conf | quietly
$tAPACHE2CTL graceful | quietly
printf "Done\n"
;;
*)
echo
echo "Skipping vhost enabling procedure by user choice"
return
;;
esac
}
#
# Ask if user wants to create a database too
#
function add_db() {
# Ask if we want to create a database to the new virtual host
echo
echo "-----------------------------------------------------------------------------"
echo
read -r -p "Do you want to create a database to your new virtual host ($tVHOST) ? [y/N] " tDBCHOICE
case $tDBCHOICE in
[yY][eE][sS] | [yY])
get_rootpw
create_db
;;
*)
echo
echo "Skipping creating a database by user choice"
return
;;
esac
}
#
# Create a new database and database user
#
function create_db() {
echo
echo "-----------------------------------------------------------------------------"
echo
echo "Type in the desired username. It will also be used as the table name."
echo
echo "Remember no to use a to long name. MySQL only support up to 16 character in"
echo "the username. Anything above will break this script when trying to create"
echo "the database user."
echo
echo "-----------------------------------------------------------------------------"
echo
read -r -p "Database username ? " _dbuser_
if [ -z "$_dbuser_" ]; then
echo
echo "No domain name given, exiting."
echo
exit 1
else
tDBUSER=$_dbuser_
echo
echo -n "Creating new database... "
# Create a random password
genpasswd
# Check if the database already exists
if [ -d "/var/lib/mysql/$tDBUSER" ]; then
echo "Database $tDBUSER already exists, aborting."
exit 1
fi
# Create temporary db and db-user config files from template
$tSED -e "s/CHANGEMEDB/$tDBUSER/g" $tDIR/mysql.template >$tDIR/mysql.template.1
$tSED -e "s/CHANGEMEUSER/$tDBUSER/g" $tDIR/mysql.template.1 >$tDIR/mysql.template.2
$tSED -e "s/CHANGEMEPW/$tGENPW/g" $tDIR/mysql.template.2 >$tDIR/mysql.template.3
# Create database table and user
$tMYSQL --user=$tDBU --password=$tDBPW <$tDIR/mysql.template.3
# Delete temporary config files
$tRM $tDIR/mysql.template.1
$tRM $tDIR/mysql.template.2
$tRM $tDIR/mysql.template.3
printf "Done\n"
fi
}
#
# We are done, print final message
#
function finished() {
echo
echo "-----------------------------------------------------------------------------"
echo
echo "# FINISHED"
echo "Everything is done and ready for your. Please save this information securely"
echo "This is the only time you will see it!"
echo
echo "# VIRTUAL HOST"
echo "Host location: $tVHOSTDIR"
echo "Log location: $tVHOSTLOG"
if [ -n "$tDBUSER" ]; then
echo
echo "# DATABASE"
echo "Database username: $tDBUSER"
echo "Database tablename: $tDBUSER"
echo "Database password: $tGENPW"
echo
echo $tGENPW >/root/{$tDBUSER}_dbpass.txt
fi
echo
echo "-----------------------------------------------------------------------------"
echo
}
#-----------------------------------------------
# Main
#-----------------------------------------------
set_vhost
create_vhost
add_db
finished