-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnvhost.sh
659 lines (470 loc) · 17.6 KB
/
cnvhost.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#!/bin/bash
# Created: Sun August 03 21:04:12 2014 by Nader Nabil @Nader_N2012
#
## sudo get the permissions first
LRED="\033[01;31m"
LGREEN="\033[01;32m"
## Web Site Dirs and Logs
## user can edit this section if it need:
## this configration for ubuntu servers
webDir="/var/www/htdocs/"
vhostsDir="vhosts/"
## port for the vhost configration file 80 the stander
port="80"
## vhost file for site
configFile=".conf"
injectedComment="# Include vhosts"
injectedCommand="Include vhosts/*.conf"
checkInjectedCommand="Include vhosts/\*.conf"
## for CentOS can use configration like
## find system type
## CentOS or Ubuntu
ubuntu=$(cat /proc/version | grep -o ubuntu)
centOS=$(cat /proc/version | grep -o centos)
if [[ -n $ubuntu && "$ubuntu" == "ubuntu" ]]; then
#echo -e "${LGREEN}[+]\e[0m System detacted: " $ubuntu
configFilePath="/etc/apache2/"
configFileName="apache2.conf"
backupConfigFile="apache2.conf.bak"
logDir="/var/www/log/"
elif [[ -n $centOS && "$centOS" == "centos" ]]; then
#echo -e "${LGREEN}[+]\e[0m System detacted: " $centOS
## for CentOS can use configration like
configFilePath="/etc/httpd/conf/"
configFileName="httpd.conf"
backupConfigFile="httpd.conf.bak"
logDir="/etc/httpd/logs/"
injectedCommand="Include conf/vhosts/*.conf"
checkInjectedCommand="Include conf/vhosts/\*.conf"
else
echo -e "${LRED}[-]\e[0m error!, can't detact system"
echo -e "${LGREEN}[+]\e[0m using defualt setting"
## this the defualt configration for the script
configFilePath="/etc/apache2/conf"
configFileName="apache2.conf"
backupConfigFile="apache2.conf.bak"
logDir="/var/www/log/"
fi;
## Logs Files Access Log And Error Log
errorLog="_error.log"
accessLog="_access.log"
## user argement and options
opt=$1
siteName=$2
delOpt=$3
## full configration path
mainConfigFile=$configFilePath$configFileName
mainConfigBackupFile=$configFilePath$backupConfigFile
mainvHostDir=$configFilePath$vhostsDir
## vhost paths
## all site will be vhosts/sitename.conf
## later we can make it: vhosts/sitenameFolder/sitename.conf
## for avoiding nasted files and subdomains configrations files
nSiteVHostConfigFile=$mainvHostDir$siteName$configFile
## web site paths
nSiteDIR=$webDir$siteName"/"
nSitePubHTM=$webDir$siteName"/public_html/"
nSiteIndex=$webDir$siteName"/public_html/index.html"
nSiteLogsDIR=$logDir$siteName"/"
nSiteLogErrorFile=$logDir$siteName"/"$siteName$errorLog
nSiteLogAccessFile=$logDir$siteName"/"$siteName$accessLog
## MySQL data
mysqlUser="scriptAccess"
mysqlPass="yoursHere"
dbString=$(echo "${siteName}" | sed 's/\./_/g')
mysqlDBName="db_"${dbString}
dbNewUser=$dbString
## generating random password for new users
## importnet for database user authentication
function randPass {
[ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-32}
echo
}
## generate random password
randPassword=$(randPass 8)
function editApache2Config {
## open and edit apache2 configration file
## and adding vhost configration files into
## find and add Include path of the vhost folder and file
## first search for the defination in the configration file
## # Include vhosts
## if it exitst the vhosts already added
## else add the vhosts configration
## Include vhosts/*.conf
## Used Variables: configFile, injectedComment, injectedCommand
## mainConfigBackupFile, mainConfigFile, checkInjectedCommand
## find comment of our script
## backup main configration file
if [ ! -e ${mainConfigBackupFile} ]; then
echo "Configration file Backup is running..."
cp ${mainConfigFile} ${mainConfigBackupFile}
if [ -e ${mainConfigBackupFile} ]; then
echo -e "${LGREEN}[+]\e[0m Configration file is Backed up."
else
echo -e "${LRED}[-]\e[0m Error!, Configration file Backup error!"
fi;
else
echo -e "${LRED}[-]\e[0m Configration file is already exist!"
fi;
check=$(cat ${mainConfigFile} | grep -n "${injectedComment}" | head -1 | cut -d: -f2)
if [ "$injectedComment" = "$check" ]; then
echo -e "${LRED}[-]\e[0m comment already exist!"
else
echo -e "${LGREEN}[+]\e[0m adding script comment!"
echo ${injectedComment} >> ${mainConfigFile}
fi;
## find vhost config folder to apache2 configration
check2=$(cat ${mainConfigFile} | grep -n "${checkInjectedCommand}" | head -1 | cut -d: -f2)
if [ "$injectedCommand" = "$check2" ]; then
echo -e "${LRED}[-]\e[0m command already exist!"
else
echo -e "${LGREEN}[+]\e[0m adding script command!"
echo ${injectedCommand} >> ${mainConfigFile}
fi;
}
function createAllFolders {
## check the vhost folder if it exist
## =================================
## Main vhosts Dir
if [ ! -d $mainvHostDir ]; then
mkdir -p $mainvHostDir;
echo -e "${LGREEN}[+]\e[0m Creating Virtual Host Folder."
else
echo -e "${LRED}[-]\e[0m '$mainvHostDir' Folder already exist!"
fi;
## =================================
## check and create logs folders
## main Logs Dir
if [ ! -d ${nSiteLogsDIR} ]; then
mkdir -p ${nSiteLogsDIR}
echo -e "${LGREEN}[+]\e[0m Creating Log Folder."
else
echo -e "${LRED}[-]\e[0m '${nSiteLogsDIR}' Folder already exist!"
fi;
## =================================
## make the web site folders
if [ ! -d ${nSiteDIR} ]; then
mkdir -p ${nSiteDIR}
mkdir -p ${nSitePubHTM}
echo -e "${LGREEN}[+]\e[0m Creating web site folder: " ${nSiteDIR};
echo -e "${LGREEN}[+]\e[0m Creating web site public_html folder: " ${nSitePubHTM};
else
echo -e "${LRED}[-]\e[0m '${nSiteDIR}' Folder already exist!"
echo -e "${LRED}[-]\e[0m '${nSitePubHTM}' Folder already exist!"
fi;
}
function createLogsFiles {
## ==================================
## create the log files
## this created with -c option
if [ ! -e ${nSiteLogErrorFile} ]; then
echo '' > ${nSiteLogErrorFile}
echo -e "${LGREEN}[+]\e[0m creating file: " ${nSiteLogErrorFile}
else
echo -e "${LRED}[-]\e[0m '${nSiteLogErrorFile}' File dos not exist!"
fi;
if [ ! -e ${nSiteLogAccessFile} ]; then
echo '' > ${nSiteLogAccessFile}
echo -e "${LGREEN}[+]\e[0m creating file: " ${nSiteLogAccessFile}
else
echo -e "${LRED}[-]\e[0m '${nSiteLogAccessFile}' File already exist!"
fi;
}
function createVHostFiles {
## creating the vhost configration files
## =================================
if [ ! -e ${nSiteVHostConfigFile} ]; then
echo '' > ${nSiteVHostConfigFile}
echo -e "${LGREEN}[+]\e[0m Creating file: " ${nSiteVHostConfigFile}
## write the configration
vhostFileSchame
else
echo -e "${LRED}[-]\e[0m '${nSiteVHostConfigFile}' File already exist!"
fi;
}
function vhostFileSchame {
## creating vhost config file for the web site
## =================================
echo -e "${LGREEN}[+]\e[0m writeing vhost config file schame!"
echo "<VirtualHost *:${port}>" >> ${nSiteVHostConfigFile}
printf "\t # this 1\n" >> ${nSiteVHostConfigFile}
printf "\t ServerName www.%s \n" "${siteName}" >> ${nSiteVHostConfigFile}
printf "\t ServerAlias %s \n" "${siteName}" >> ${nSiteVHostConfigFile}
printf "\t # this 2\n" >> ${nSiteVHostConfigFile}
printf "\t ServerAdmin admin@%s \n" "${siteName}" >> ${nSiteVHostConfigFile}
printf "\t DocumentRoot %s \n" "${nSitePubHTM}" >> ${nSiteVHostConfigFile}
printf "\t # this 3\n" >> ${nSiteVHostConfigFile}
printf "\t ErrorLog %s \n" "${nSiteLogErrorFile}" >> ${nSiteVHostConfigFile}
printf "\t CustomLog %s combined\n" "${nSiteLogAccessFile}" >> ${nSiteVHostConfigFile}
echo "</VirtualHost>" >> ${nSiteVHostConfigFile}
echo -e "${LGREEN}[+]\e[0m writeing vhost config file, done!"
}
function indexPageSchame {
## simplfiy some stuff
## =================================
if [ ! -e ${nSiteIndex} ]; then
echo -e "${LGREEN}[+]\e[0m creating the index page..."
echo "<!DOCTYPE html>" > ${nSiteIndex}
echo "<html>" >> ${nSiteIndex}
echo "<head>" >> ${nSiteIndex}
echo "<title> ${siteName} </title> " >> ${nSiteIndex}
echo "</head>" >> ${nSiteIndex}
echo "<body>" >> ${nSiteIndex};
echo "<h1>This is index page for $siteName virtual host</h1>" >> ${nSiteIndex}
echo "</body>" >> ${nSiteIndex}
echo "</html>" >> ${nSiteIndex}
echo -e "${LGREEN}[+]\e[0m changing index permissions "
sudo chown -R $USER:$USER ${nSiteIndex}
else
echo -e "${LRED}[-]\e[0m '${nSiteIndex}' File already exist!"
fi;
}
function deleteFolders {
## =================================
## delete the Main website dir and its content
## this delete with option -d
if [ -d ${nSiteDIR} ]; then
## back it up first
rm -f -r ${nSitePubHTM}
rm -f -r ${nSiteDIR}
echo -e "${LGREEN}[+]\e[0m deleting web site folder: " ${nSiteDIR}
echo -e "${LGREEN}[+]\e[0m deleting web site public_html folder: " ${nSitePubHTM}
else
echo -e "${LRED}[-]\e[0m '${nSiteDIR}' Folder dos not exist!"
echo -e "${LRED}[-]\e[0m '${nSitePubHTM}' Folder dos not exist!"
fi;
## delete website logs folder
if [ -d ${nSiteLogsDIR} ]; then
rm -r -f ${nSiteLogsDIR}
echo -e "${LGREEN}[+]\e[0m deleting Log Folder" ${nSiteLogsDIR}
else
echo -e "${LRED}[-]\e[0m '${nSiteLogsDIR}' Folder not exist!"
fi;
}
function deleteVHostFile {
## delete website vhost configration file
if [ -e ${nSiteVHostConfigFile} ]; then
## back it up first
rm -f ${nSiteVHostConfigFile}
echo -e "${LGREEN}[+]\e[0m deleting web site config file: " ${nSiteVHostConfigFile}
else
echo -e "${LRED}[-]\e[0m '${nSiteVHostConfigFile}' File dos not exist!"
fi;
}
function deleteLogFiles {
## ==================================
## delete the log files
## this delete with -d option
if [ -e ${nSiteLogErrorFile} ]; then
rm -f ${nSiteLogErrorFile};
echo -e "${LGREEN}[+]\e[0m deleting file: ", ${nSiteLogErrorFile}
else
echo -e "${LRED}[-]\e[0m '${nSiteLogErrorFile}' File dos not exist!"
fi;
if [ -e ${nSiteLogAccessFile} ]; then
rm -f ${nSiteLogAccessFile};
echo -e "${LGREEN}[+]\e[0m deleting file: ", ${nSiteLogAccessFile}
else
echo -e "${LRED}[-]\e[0m '${nSiteLogAccessFile}' File not exist!"
fi;
}
function deleteAllDir {
## =================================
## check and create logs folders
## main Logs Dir
## here the delete for the hole logs folder and files P.S do not implement it
## if you make sure its have a spreated command like -da -l --deleteAll --logs
## backup main configration file
if [ -e ${mainConfigBackupFile} ]; then
echo "Removing configration file."
rm -r -f ${mainConfigFile}
mv ${mainConfigBackupFile} ${mainConfigFile}
if [ ! -e ${mainConfigBackupFile} ]; then
echo -e "${LGREEN}[+]\e[0m file removed successfuly."
else
echo -e "${LRED}[-]\e[0m Error!, configration file not removed!"
fi;
else
echo -e "${LRED}[-]\e[0m Configration file not exist!"
fi;
if [ ! -d ${nSiteLogsDIR} ]; then
rm -f -r ${nSiteLogsDIR};
echo -e "${LGREEN}[+]\e[0m deleting file: ", ${nSiteLogsDIR}
else
echo -e "${LRED}[-]\e[0m '${nSiteLogsDIR}' File dos not exist!"
fi;
## =================================
## delete the Main vhosts Dir
## here the delete for the hole vhost folder and files P.S do not implement it ## if you make sure its have a spreated command like -da --deleteAll
## this dingers do not use it at all
if [ -d ${mainvHostDir} ]; then
## back it up first
rm -f -r ${mainvHostDir};
echo -e "${LGREEN}[+]\e[0m deleting web site folder: ", ${mainvHostDir}
else
echo -e "${LRED}[-]\e[0m '${mainvHostDir}' Folder dos not exist!"
fi;
}
function createSiteDB {
# connect to mysql server and create database under site name
createDB=$(mysql -u ${mysqlUser} -p${mysqlPass} -e "CREATE DATABASE ${mysqlDBName};")
if [[ -z ${createDB} && "${createDB}" == "" ]]; then
echo -e "${LGREEN}[+]\e[0m Database Created!"
else
echo -e "${LRED}[-]\e[0m error!, could not create database"
fi;
## create username for site and get access to database only
createUser=$(mysql -u ${mysqlUser} -p${mysqlPass} -e "CREATE USER '${dbNewUser}' IDENTIFIED BY '${randPassword}'; GRANT ALL PRIVILEGES ON ${mysqlDBName} TO ${dbNewUser}; FLUSH PRIVILEGES;")
if [[ -z ${createUser} && "${createUser}" == "" ]]; then
echo -e "${LGREEN}[+]\e[0m User Created!"
else
echo -e "${LRED}[-]\e[0m error!, could not create user"
fi;
}
function deleteSiteDB {
## connect to mysql server and create database under site name
deleteDB=$(mysql -u ${mysqlUser} -p${mysqlPass} -e "DROP DATABASE ${mysqlDBName};")
if [[ -z ${deleteDB} && "${deleteDB}" == "" ]]; then
echo -e "${LGREEN}[+]\e[0m Database deleted!"
else
echo -e "${LRED}[-]\e[0m error!, could not delete database"
fi;
## delete username for site and its permisstions access to database only
deleteUser=$(mysql -u ${mysqlUser} -p${mysqlPass} -e "DROP USER ${dbNewUser}; FLUSH PRIVILEGES;")
if [[ -z ${deleteUser} && "${deleteUser}" == "" ]]; then
echo -e "${LGREEN}[+]\e[0m User deleted!"
else
echo -e "${LRED}[-]\e[0m error!, could not delete user"
fi;
}
function restartApache2 {
## restart apache server for the new changes
echo "[*] Restarting apache2 server."
if [[ -n $ubuntu && "$ubuntu" == "ubuntu" ]]; then
## restrt apache2 in Ubuntu system
sudo /etc/init.d/apache2 restart
elif [[ -n $centOS && "$centOS" == "centos" ]]; then
## other commands for apache on CentOS
sudo /etc/init.d/httpd stop
sudo /etc/init.d/httpd start
fi;
}
function restartMySQL {
## restart MySQL server for the new changes
echo "[*] Restarting MySQL server."
if [[ -n $ubuntu && "$ubuntu" == "ubuntu" ]]; then
## restrt MySQL in Ubuntu system
sudo /etc/init.d/mysql restart
elif [[ -n $centOS && "$centOS" == "centos" ]]; then
## other commands for apache on CentOS
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start
fi;
}
function reportData {
echo " "
echo "====================================== "
echo " "
echo " Domain name: ${siteName}"
echo " Server: localhost or 127.0.0.1"
echo " Database Name: ${mysqlDBName}"
echo " Database User: ${dbNewUser}"
echo " Database Password: ${randPassword}"
echo " "
echo "====================================== "
echo " "
exit;
}
function createSite {
## all folder created first
## call the functions to do this jobi
## creating new website in virtual host
# Editing the configration file of Apache server
# Tested and working
editApache2Config
# creating all folder for the site
createAllFolders
# creating the logs files error and access
createLogsFiles
# creating the vhost config file
createVHostFiles
# adding the html page
indexPageSchame
# creating database for the site
#createSiteDB
# restarting the MySQL server for the new configration
# restartMySQL # no need for it for now
# restarting the apache server for the new configration
restartApache2
## report all data of website and database connection string
#reportData
}
function deleteSite {
## all files deleted first
## this delete the web site and it logs only
# delete the site logs
deleteLogFiles
## delete vhost configration file
deleteVHostFile
## delete the website folders and all its content
deleteFolders
# delete the site database
#deleteSiteDB
# restarting the MySQL server for the new configration
# restartMySQL # no need for now
# restarting the apache server for the new configration
restartApache2
}
function sysCheck {
## find system type
## CentOS or Ubuntu
ubuntu=$(cat /proc/version | grep -o ubuntu)
centOS=$(cat /proc/version | grep -o centos)
if [[ -n $ubuntu && "$ubuntu" == "ubuntu" ]]; then
echo -e "${LGREEN}[+]\e[0m System detacted: " $ubuntu
elif [[ -n $centOS && "$centOS" == "centos" ]]; then
echo -e "${LGREEN}[+]\e[0m System detacted: " $centOS
else
echo -e "${LRED}[-]\e[0m error!, can't detact system"
fi;
}
function main {
## the execution of the program
if [ "$opt" = "-da" ]; then
read -p "deleting all logs and vhosts, are you sure [Y - N] ? " useropt
if [[ "$useropt" = "y" || "$useropt" = "Y" ]]; then
deleteAllDir
elif [[ "$useropt" = "n" || "$useropt" = "N" ]]; then
exit 1
fi;
fi;
if [ -z $siteName ]; then
echo " "
echo "Usage: $0 [option] [-c, -d] domanin.com"
echo "Example: $0 -c example.com"
echo "-c creating new virtual host website"
echo "-d deleting the site you define"
echo "-da deleting all logs and vhosts, example: $0 -da "
echo " "
exit 1
fi;
## checking the domain name with Regex and adding it to a variable
regex=$(echo $siteName | grep -P '(?=^.{5,254}$)(^(?:(?!\d+\.)[a-zA-Z0-9_\-]{1,63}\.?)+\.(?:[a-z]{2,})$)')
if [ "$regex" = "$siteName" ]; then
if [ "$opt" = "-c" ]; then
echo -e "${LGREEN}[+]\e[0m Createing the new virtual host for '$siteName' ..."
createSite
elif [ "$opt" = "-d" ]; then
echo -e "${LGREEN}[+]\e[0m deleting $siteName !"
deleteSite
else
echo -e "${LRED}[-]\e[0m Error!, unknown option."
fi;
else
echo -e "${LRED}[-]\e[0m error!, please check your domain name."
exit 1
fi;
}
## Execute the Script from the main function
main