forked from linuxeye/lnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vhost.sh
executable file
·882 lines (820 loc) · 32.7 KB
/
vhost.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
#!/bin/bash
# Author: yeho <lj2007331 AT gmail.com>
# BLOG: https://blog.linuxeye.com
#
# Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
#
# Project home page:
# http://oneinstack.com
# https://github.com/lj2007331/oneinstack
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
clear
printf "
#######################################################################
# OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
# For more information please visit http://oneinstack.com #
#######################################################################
"
. ./options.conf
. ./include/color.sh
. ./include/check_web.sh
. ./include/get_char.sh
# Check if user is root
[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
Usage() {
printf "
Usage: $0 [ ${CMSG}add${CEND} | ${CMSG}del${CEND} ]
${CMSG}add${CEND} --->Add Virtualhost
${CMSG}del${CEND} --->Delete Virtualhost
"
}
Choose_env() {
if [ -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
Number=111
while :
do
echo
echo 'Please choose to use environment:'
echo -e "\t${CMSG}1${CEND}. Use php"
echo -e "\t${CMSG}2${CEND}. Use java"
echo -e "\t${CMSG}3${CEND}. Use hhvm"
read -p "Please input a number:(Default 1 press Enter) " Choose_number
[ -z "$Choose_number" ] && Choose_number=1
if [[ ! $Choose_number =~ ^[1-3]$ ]];then
echo "${CWARNING}input error! Please only input number 1,2,3${CEND}"
else
break
fi
done
[ "$Choose_number" == '1' ] && NGX_FLAG=php
[ "$Choose_number" == '2' ] && NGX_FLAG=java
[ "$Choose_number" == '3' ] && NGX_FLAG=hhvm
elif [ -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
Number=110
while :
do
echo
echo 'Please choose to use environment:'
echo -e "\t${CMSG}1${CEND}. Use php"
echo -e "\t${CMSG}2${CEND}. Use java"
read -p "Please input a number:(Default 1 press Enter) " Choose_number
[ -z "$Choose_number" ] && Choose_number=1
if [[ ! $Choose_number =~ ^[1-2]$ ]];then
echo "${CWARNING}input error! Please only input number 1,2${CEND}"
else
break
fi
done
[ "$Choose_number" == '1' ] && NGX_FLAG=php
[ "$Choose_number" == '2' ] && NGX_FLAG=java
elif [ -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
Number=100
NGX_FLAG=php
elif [ -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
Number=101
while :
do
echo
echo 'Please choose to use environment:'
echo -e "\t${CMSG}1${CEND}. Use php"
echo -e "\t${CMSG}2${CEND}. Use hhvm"
read -p "Please input a number:(Default 1 press Enter) " Choose_number
[ -z "$Choose_number" ] && Choose_number=1
if [[ ! $Choose_number =~ ^[1-2]$ ]];then
echo "${CWARNING}input error! Please only input number 1,2${CEND}"
else
break
fi
done
[ "$Choose_number" == '1' ] && NGX_FLAG=php
[ "$Choose_number" == '2' ] && NGX_FLAG=hhvm
elif [ ! -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
Number=011
while :
do
echo
echo 'Please choose to use environment:'
echo -e "\t${CMSG}1${CEND}. Use java"
echo -e "\t${CMSG}2${CEND}. Use hhvm"
read -p "Please input a number:(Default 1 press Enter) " Choose_number
[ -z "$Choose_number" ] && Choose_number=1
if [[ ! $Choose_number =~ ^[1-2]$ ]];then
echo "${CWARNING}input error! Please only input number 1,2${CEND}"
else
break
fi
done
[ "$Choose_number" == '1' ] && NGX_FLAG=java
[ "$Choose_number" == '2' ] && NGX_FLAG=hhvm
elif [ ! -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
Number=010
NGX_FLAG=java
elif [ ! -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
Number=001
NGX_FLAG=hhvm
else
Number=000
NGX_FLAG=php
fi
if [ "$NGX_FLAG" == 'php' ];then
NGX_CONF=$(echo -e "location ~ [^/]\.php(/|$) {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\n }")
elif [ "$NGX_FLAG" == 'java' ];then
NGX_CONF=$(echo -e "location ~ {\n proxy_pass http://127.0.0.1:8080;\n include proxy.conf;\n }")
elif [ "$NGX_FLAG" == 'hhvm' ];then
NGX_CONF=$(echo -e "location ~ .*\.(php|php5)?$ {\n fastcgi_pass unix:/var/log/hhvm/sock;\n fastcgi_index index.php;\n fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n include fastcgi_params;\n }")
fi
}
Nginx_ssl() {
printf "
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
"
echo
read -p "Country Name (2 letter code) [CN]: " SELFSIGNEDSSL_C
[ -z "$SELFSIGNEDSSL_C" ] && SELFSIGNEDSSL_C=CN
echo
read -p "State or Province Name (full name) [Shanghai]: " SELFSIGNEDSSL_ST
[ -z "$SELFSIGNEDSSL_ST" ] && SELFSIGNEDSSL_ST=Shanghai
echo
read -p "Locality Name (eg, city) [Shanghai]: " SELFSIGNEDSSL_L
[ -z "$SELFSIGNEDSSL_L" ] && SELFSIGNEDSSL_L=Shanghai
echo
read -p "Organization Name (eg, company) [LinuxEye Inc.]: " SELFSIGNEDSSL_O
[ -z "$SELFSIGNEDSSL_O" ] && SELFSIGNEDSSL_O='LinuxEye Inc.'
echo
read -p "Organizational Unit Name (eg, section) [IT Dept.]: " SELFSIGNEDSSL_OU
[ -z "$SELFSIGNEDSSL_OU" ] && SELFSIGNEDSSL_OU='IT Dept.'
if [[ "$($web_install_dir/sbin/nginx -V 2>&1 | grep -Eo 'with-http_v2_module')" = 'with-http_v2_module' ]]; then
LISTENOPT='443 ssl http2'
else
LISTENOPT='443 ssl spdy'
fi
openssl req -new -newkey rsa:2048 -sha256 -nodes -out $web_install_dir/conf/${domain}.csr -keyout $web_install_dir/conf/${domain}.key -subj "/C=${SELFSIGNEDSSL_C}/ST=${SELFSIGNEDSSL_ST}/L=${SELFSIGNEDSSL_L}/O=${SELFSIGNEDSSL_O}/OU=${SELFSIGNEDSSL_OU}/CN=${domain}" > /dev/null 2>&1
/bin/cp $web_install_dir/conf/${domain}.csr{,_bk.`date +%Y-%m-%d_%H%M`}
/bin/cp $web_install_dir/conf/${domain}.key{,_bk.`date +%Y-%m-%d_%H%M`}
openssl x509 -req -days 36500 -sha256 -in $web_install_dir/conf/${domain}.csr -signkey $web_install_dir/conf/${domain}.key -out $web_install_dir/conf/${domain}.crt > /dev/null 2>&1
}
Print_ssl() {
echo "`printf "%-30s" "Self-signed SSL Certificate:"`${CMSG}$web_install_dir/conf/${domain}.crt${CEND}"
echo "`printf "%-30s" "SSL Private Key:"`${CMSG}$web_install_dir/conf/${domain}.key${CEND}"
echo "`printf "%-30s" "SSL CSR File:"`${CMSG}$web_install_dir/conf/${domain}.csr${CEND}"
}
Input_Add_domain() {
if [ -e "$web_install_dir/sbin/nginx" ];then
while :
do
echo
read -p "Do you want to setup SSL under Nginx? [y/n]: " nginx_ssl_yn
if [[ ! $nginx_ssl_yn =~ ^[y,n]$ ]];then
echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
else
break
fi
done
fi
while :
do
echo
read -p "Please input domain(example: www.linuxeye.com): " domain
if [ -z "`echo $domain | grep '.*\..*'`" ]; then
echo "${CWARNING}input error! ${CEND}"
else
break
fi
done
if [ -e "$web_install_dir/conf/vhost/$domain.conf" -o -e "$apache_install_dir/conf/vhost/$domain.conf" -o -e "$tomcat_install_dir/conf/vhost/$domain.xml" ]; then
[ -e "$web_install_dir/conf/vhost/$domain.conf" ] && echo -e "$domain in the Nginx/Tengine already exist! \nYou can delete ${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND} and re-create"
[ -e "$apache_install_dir/conf/vhost/$domain.conf" ] && echo -e "$domain in the Apache already exist! \nYou can delete ${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND} and re-create"
[ -e "$tomcat_install_dir/conf/vhost/$domain.xml" ] && echo -e "$domain in the Tomcat already exist! \nYou can delete ${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND} and re-create"
exit
else
echo "domain=$domain"
fi
if [ "$nginx_ssl_yn" == 'y' ]; then
Nginx_ssl
Nginx_conf=$(echo -e "listen $LISTENOPT;\nssl_certificate $web_install_dir/conf/$domain.crt;\nssl_certificate_key $web_install_dir/conf/$domain.key;\nssl_session_timeout 10m;\nssl_protocols TLSv1 TLSv1.1 TLSv1.2;\nssl_prefer_server_ciphers on;\nssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-RC4-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:RC4-SHA:\!aNULL:\!eNULL:\!EXPORT:\!DES:\!3DES:\!MD5:\!DSS:\!PKS";\nssl_session_cache builtin:1000 shared:SSL:10m;\nresolver 8.8.8.8 8.8.4.4 valid=300s;\nresolver_timeout 5s;")
Nginx_http_to_https=$(echo -e "server {\nlisten 80;\nserver_name $domain;\nrewrite ^/(.*) https://\$server_name/\$1 permanent;\n}")
else
Nginx_conf='listen 80;'
fi
while :
do
echo
read -p "Do you want to add more domain name? [y/n]: " moredomainame_yn
if [[ ! $moredomainame_yn =~ ^[y,n]$ ]];then
echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
else
break
fi
done
if [ "$moredomainame_yn" == 'y' ]; then
while :
do
echo
read -p "Type domainname or IP(example: linuxeye.com 121.43.8.8): " moredomain
if [ -z "`echo $moredomain | grep '.*\..*'`" ]; then
echo "${CWARNING}input error! ${CEND}"
else
[ "$moredomain" == "$domain" ] && echo "${CWARNING}Domain name already exists! ${CND}" && continue
echo domain list="$moredomain"
moredomainame=" $moredomain"
break
fi
done
Apache_Domain_alias=ServerAlias$moredomainame
Tomcat_Domain_alias=$(for D in `echo $moredomainame`; do echo "<Alias>$D</Alias>"; done)
if [ -e "$web_install_dir/sbin/nginx" ];then
while :
do
echo
read -p "Do you want to redirect from $moredomain to $domain? [y/n]: " redirect_yn
if [[ ! $redirect_yn =~ ^[y,n]$ ]];then
echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
else
break
fi
done
[ "$nginx_ssl_yn" == 'y' ] && HTTP_flag=https || HTTP_flag=http
[ "$redirect_yn" == 'y' ] && Nginx_redirect=$(echo -e "if (\$host != $domain) {\n\trewrite ^/(.*)\$ \$scheme://$domain/\$1 permanent;\n\t}")
fi
fi
while :
do
echo
echo "Please input the directory for the domain:$domain :"
read -p "(Default directory: $wwwroot_dir/$domain): " vhostdir
if [ -n "$vhostdir" -a -z "`echo $vhostdir | grep '^/'`" ];then
echo "${CWARNING}input error! Press Enter to continue...${CEND}"
else
if [ -z "$vhostdir" ]; then
vhostdir="$wwwroot_dir/$domain"
echo "Virtual Host Directory=${CMSG}$vhostdir${CEND}"
fi
echo
echo "Create Virtul Host directory......"
mkdir -p $vhostdir
echo "set permissions of Virtual Host directory......"
chown -R ${run_user}.$run_user $vhostdir
break
fi
done
}
Nginx_anti_hotlinking() {
while :
do
echo
read -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_yn
if [[ ! $anti_hotlinking_yn =~ ^[y,n]$ ]];then
echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
else
break
fi
done
if [ -n "`echo $domain | grep '.*\..*\..*'`" ];then
domain_allow="*.${domain#*.} $domain"
else
domain_allow="*.$domain $domain"
fi
if [ "$anti_hotlinking_yn" == 'y' ];then
if [ "$moredomainame_yn" == 'y' ]; then
domain_allow_all=$domain_allow$moredomainame
else
domain_allow_all=$domain_allow
fi
anti_hotlinking=$(echo -e "location ~ .*\.(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv)$ {\n valid_referers none blocked $domain_allow_all;\n if (\$invalid_referer) {\n #rewrite ^/ http://www.linuxeye.com/403.html;\n return 403;\n }\n }")
else
anti_hotlinking=
fi
}
Nginx_rewrite() {
while :
do
echo
read -p "Allow Rewrite rule? [y/n]: " rewrite_yn
if [[ ! $rewrite_yn =~ ^[y,n]$ ]];then
echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
else
break
fi
done
if [ "$rewrite_yn" == 'n' ];then
rewrite="none"
touch "$web_install_dir/conf/$rewrite.conf"
else
echo
echo "Please input the rewrite of programme :"
echo "${CMSG}wordpress${CEND},${CMSG}discuz${CEND},${CMSG}opencart${CEND},${CMSG}thinkphp${CEND},${CMSG}laravel${CEND},${CMSG}typecho${CEND},${CMSG}ecshop${CEND},${CMSG}drupal${CEND},${CMSG}joomla${CEND} rewrite was exist."
read -p "(Default rewrite: other):" rewrite
if [ "$rewrite" == "" ]; then
rewrite="other"
fi
echo "You choose rewrite=${CMSG}$rewrite${CEND}"
[ "$NGX_FLAG" == 'php' -a "$rewrite" == "thinkphp" ] && NGX_CONF=$(echo -e "location ~ \.php {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php-cgi.sock;\n fastcgi_index index.php;\n include fastcgi_params;\n set \$real_script_name \$fastcgi_script_name;\n if (\$fastcgi_script_name ~ \"^(.+?\.php)(/.+)\$\") {\n set \$real_script_name \$1;\n set \$path_info \$2;\n }\n fastcgi_param SCRIPT_FILENAME \$document_root\$real_script_name;\n fastcgi_param SCRIPT_NAME \$real_script_name;\n fastcgi_param PATH_INFO \$path_info;\n }")
if [ -e "config/$rewrite.conf" ];then
/bin/cp config/$rewrite.conf $web_install_dir/conf/$rewrite.conf
else
touch "$web_install_dir/conf/$rewrite.conf"
fi
fi
}
Nginx_log() {
while :
do
echo
read -p "Allow Nginx/Tengine access_log? [y/n]: " access_yn
if [[ ! $access_yn =~ ^[y,n]$ ]];then
echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
else
break
fi
done
if [ "$access_yn" == 'n' ]; then
N_log="access_log off;"
else
N_log="access_log $wwwlogs_dir/${domain}_nginx.log combined;"
echo "You access log file=${CMSG}$wwwlogs_dir/${domain}_nginx.log${CEND}"
fi
}
Create_nginx_tomcat_conf() {
[ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
cat > $web_install_dir/conf/vhost/$domain.conf << EOF
server {
$Nginx_conf
server_name $domain$moredomainame;
$N_log
index index.html index.htm index.jsp;
root $vhostdir;
$Nginx_redirect
$anti_hotlinking
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
$NGX_CONF
}
$Nginx_http_to_https
EOF
cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
<Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
<Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
</Host>
EOF
[ -z "`grep -o "${domain}-vhost SYSTEM" $tomcat_install_dir/conf/server.xml`" ] && sed -i "/localhost-vhost SYSTEM/a<\!ENTITY ${domain}-vhost SYSTEM \"file://$tomcat_install_dir/conf/vhost/$domain.xml\">" $tomcat_install_dir/conf/server.xml
[ -z "`grep -o "${domain}-vhost;" $tomcat_install_dir/conf/server.xml`" ] && sed -i "s@localhost-vhost;@&\n \&${domain}-vhost;@" $tomcat_install_dir/conf/server.xml
echo
$web_install_dir/sbin/nginx -t
if [ $? == 0 ];then
echo "Reload Nginx......"
$web_install_dir/sbin/nginx -s reload
/etc/init.d/tomcat restart
else
rm -rf $web_install_dir/conf/vhost/$domain.conf
echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
exit 1
fi
printf "
#######################################################################
# OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
# For more information please visit http://oneinstack.com #
#######################################################################
"
echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
echo "`printf "%-30s" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
[ "$nginx_ssl_yn" == 'y' ] && Print_ssl
}
Create_tomcat_conf() {
cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
<Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
<Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
</Host>
EOF
[ -z "`grep -o "${domain}-vhost SYSTEM" $tomcat_install_dir/conf/server.xml`" ] && sed -i "/localhost-vhost SYSTEM/a<\!ENTITY ${domain}-vhost SYSTEM \"file://$tomcat_install_dir/conf/vhost/$domain.xml\">" $tomcat_install_dir/conf/server.xml
[ -z "`grep -o "${domain}-vhost;" $tomcat_install_dir/conf/server.xml`" ] && sed -i "s@localhost-vhost;@&\n \&${domain}-vhost;@" $tomcat_install_dir/conf/server.xml
echo
/etc/init.d/tomcat restart
printf "
#######################################################################
# OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
# For more information please visit http://oneinstack.com #
#######################################################################
"
echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
echo "`printf "%-30s" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
echo "`printf "%-30s" "index url:"`${CMSG}http://${domain}:8080/${CEND}"
}
Create_nginx_php-fpm_hhvm_conf() {
[ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
cat > $web_install_dir/conf/vhost/$domain.conf << EOF
server {
$Nginx_conf
server_name $domain$moredomainame;
$N_log
index index.html index.htm index.php;
include $web_install_dir/conf/$rewrite.conf;
root $vhostdir;
$Nginx_redirect
$anti_hotlinking
$NGX_CONF
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
}
$Nginx_http_to_https
EOF
echo
$web_install_dir/sbin/nginx -t
if [ $? == 0 ];then
echo "Reload Nginx......"
$web_install_dir/sbin/nginx -s reload
else
rm -rf $web_install_dir/conf/vhost/$domain.conf
echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
exit 1
fi
printf "
#######################################################################
# OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
# For more information please visit http://oneinstack.com #
#######################################################################
"
echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
echo "`printf "%-30s" "Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
[ "$rewrite_yn" == 'y' ] && echo "`printf "%-30s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/$rewrite.conf${CEND}"
[ "$nginx_ssl_yn" == 'y' ] && Print_ssl
}
Apache_log() {
while :
do
echo
read -p "Allow Apache access_log? [y/n]: " access_yn
if [[ ! $access_yn =~ ^[y,n]$ ]];then
echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
else
break
fi
done
if [ "$access_yn" == 'n' ]; then
A_log='CustomLog "/dev/null" common'
else
A_log="CustomLog \"$wwwlogs_dir/${domain}_apache.log\" common"
echo "You access log file=$wwwlogs_dir/${domain}_apache.log"
fi
}
Create_apache_conf() {
[ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
[ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "$vhostdir"
ServerName $domain
$Apache_Domain_alias
ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
$A_log
<Directory "$vhostdir">
SetOutputFilter DEFLATE
Options FollowSymLinks ExecCGI
$R_TMP
AllowOverride All
Order allow,deny
Allow from all
DirectoryIndex index.html index.php
</Directory>
</VirtualHost>
EOF
echo
$apache_install_dir/bin/apachectl -t
if [ $? == 0 ];then
echo "Restart Apache......"
/etc/init.d/httpd restart
else
rm -rf $apache_install_dir/conf/vhost/$domain.conf
echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
exit 1
fi
printf "
#######################################################################
# OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
# For more information please visit http://oneinstack.com #
#######################################################################
"
echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
echo "`printf "%-30s" "Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
}
Create_nginx_apache_mod-php_conf() {
# Nginx/Tengine
[ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
cat > $web_install_dir/conf/vhost/$domain.conf << EOF
server {
$Nginx_conf
server_name $domain$moredomainame;
$N_log
index index.html index.htm index.php;
root $vhostdir;
$Nginx_redirect
$anti_hotlinking
location / {
try_files \$uri @apache;
}
location @apache {
proxy_pass http://127.0.0.1:88;
include proxy.conf;
}
location ~ .*\.(php|php5|cgi|pl)?$ {
proxy_pass http://127.0.0.1:88;
include proxy.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
}
$Nginx_http_to_https
EOF
echo
$web_install_dir/sbin/nginx -t
if [ $? == 0 ];then
echo "Reload Nginx......"
$web_install_dir/sbin/nginx -s reload
else
rm -rf $web_install_dir/conf/vhost/$domain.conf
echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
fi
# Apache
[ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
[ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
<VirtualHost *:88>
ServerAdmin [email protected]
DocumentRoot "$vhostdir"
ServerName $domain
$Apache_Domain_alias
ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
$A_log
<Directory "$vhostdir">
SetOutputFilter DEFLATE
Options FollowSymLinks ExecCGI
$R_TMP
AllowOverride All
Order allow,deny
Allow from all
DirectoryIndex index.html index.php
</Directory>
</VirtualHost>
EOF
echo
$apache_install_dir/bin/apachectl -t
if [ $? == 0 ];then
echo "Restart Apache......"
/etc/init.d/httpd restart
else
rm -rf $apache_install_dir/conf/vhost/$domain.conf
exit 1
fi
printf "
#######################################################################
# OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
# For more information please visit http://oneinstack.com #
#######################################################################
"
echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
echo "`printf "%-30s" "Apache Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
[ "$rewrite_yn" == 'y' ] && echo "`printf "%-28s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/$rewrite.conf${CEND}"
[ "$nginx_ssl_yn" == 'y' ] && Print_ssl
}
Add_Vhost() {
if [ -e "$web_install_dir/sbin/nginx" -a ! -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
Choose_env
Input_Add_domain
Nginx_anti_hotlinking
if [ "$NGX_FLAG" == 'java' ];then
Nginx_log
Create_nginx_tomcat_conf
else
Nginx_rewrite
Nginx_log
Create_nginx_php-fpm_hhvm_conf
fi
elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
Choose_env
Input_Add_domain
Apache_log
Create_apache_conf
elif [ ! -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/conf/httpd.conf" -a -e "$tomcat_install_dir/conf/server.xml" ];then
Choose_env
Input_Add_domain
Create_tomcat_conf
elif [ -e "$web_install_dir/sbin/nginx" -a -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
Choose_env
Input_Add_domain
Nginx_anti_hotlinking
if [ "$NGX_FLAG" == 'java' ];then
Nginx_log
Create_nginx_tomcat_conf
elif [ "$NGX_FLAG" == 'hhvm' ];then
Nginx_rewrite
Nginx_log
Create_nginx_php-fpm_hhvm_conf
elif [ "$NGX_FLAG" == 'php' ];then
#Nginx_rewrite
Nginx_log
Apache_log
Create_nginx_apache_mod-php_conf
fi
fi
}
Del_NGX_Vhost() {
if [ -e "$web_install_dir/sbin/nginx" ];then
[ -d "$web_install_dir/conf/vhost" ] && Domain_List=`ls $web_install_dir/conf/vhost | sed "[email protected]@@g"`
if [ -n "$Domain_List" ];then
echo
echo "Virtualhost list:"
echo ${CMSG}$Domain_List${CEND}
while :
do
echo
read -p "Please input a domain you want to delete: " domain
if [ -z "`echo $domain | grep '.*\..*'`" ]; then
echo "${CWARNING}input error! ${CEND}"
else
if [ -e "$web_install_dir/conf/vhost/${domain}.conf" ];then
Directory=`grep ^root $web_install_dir/conf/vhost/${domain}.conf | awk -F'[ ;]' '{print $2}'`
rm -rf $web_install_dir/conf/vhost/${domain}.conf
$web_install_dir/sbin/nginx -s reload
while :
do
echo
read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
else
break
fi
done
if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
echo "Press Ctrl+c to cancel or Press any key to continue..."
char=`get_char`
rm -rf $Directory
fi
echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
else
echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
fi
break
fi
done
else
echo "${CWARNING}Virtualhost was not exist! ${CEND}"
fi
fi
}
Del_Apache_Vhost() {
if [ -e "$apache_install_dir/conf/httpd.conf" ];then
if [ -e "$web_install_dir/sbin/nginx" ];then
rm -rf $apache_install_dir/conf/vhost/${domain}.conf
/etc/init.d/httpd restart
else
Domain_List=`ls $apache_install_dir/conf/vhost | grep -v '0.conf' | sed "[email protected]@@g"`
if [ -n "$Domain_List" ];then
echo
echo "Virtualhost list:"
echo ${CMSG}$Domain_List${CEND}
while :
do
echo
read -p "Please input a domain you want to delete: " domain
if [ -z "`echo $domain | grep '.*\..*'`" ]; then
echo "${CWARNING}input error! ${CEND}"
else
if [ -e "$apache_install_dir/conf/vhost/${domain}.conf" ];then
Directory=`grep '^<Directory' $apache_install_dir/conf/vhost/${domain}.conf | awk -F'"' '{print $2}'`
rm -rf $apache_install_dir/conf/vhost/${domain}.conf
/etc/init.d/httpd restart
while :
do
echo
read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
else
break
fi
done
if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
echo "Press Ctrl+c to cancel or Press any key to continue..."
char=`get_char`
rm -rf $Directory
fi
echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
else
echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
fi
break
fi
done
else
echo "${CWARNING}Virtualhost was not exist! ${CEND}"
fi
fi
fi
}
Del_Tomcat_Vhost() {
if [ -e "$tomcat_install_dir/conf/server.xml" ];then
if [ -e "$web_install_dir/sbin/nginx" ];then
if [ -n "`grep ${domain}-vhost $tomcat_install_dir/conf/server.xml`" ];then
sed -i /${domain}-vhost/d $tomcat_install_dir/conf/server.xml
rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
/etc/init.d/tomcat restart
fi
else
Domain_List=`ls $tomcat_install_dir/conf/vhost | grep -v 'localhost.xml' | sed "[email protected]@@g"`
if [ -n "$Domain_List" ];then
echo
echo "Virtualhost list:"
echo ${CMSG}$Domain_List${CEND}
while :
do
echo
read -p "Please input a domain you want to delete: " domain
if [ -z "`echo $domain | grep '.*\..*'`" ]; then
echo "${CWARNING}input error! ${CEND}"
else
if [ -n "`grep ${domain}-vhost $tomcat_install_dir/conf/server.xml`" ];then
sed -i /${domain}-vhost/d $tomcat_install_dir/conf/server.xml
rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
/etc/init.d/tomcat restart
while :
do
echo
read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
else
break
fi
done
if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
echo "Press Ctrl+c to cancel or Press any key to continue..."
char=`get_char`
rm -rf $Directory
fi
echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
else
echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
fi
break
fi
done
else
echo "${CWARNING}Virtualhost was not exist! ${CEND}"
fi
fi
fi
}
if [ $# == 0 ];then
Add_Vhost
elif [ $# == 1 ];then
case $1 in
add)
Add_Vhost
;;
del)
Del_NGX_Vhost
Del_Apache_Vhost
Del_Tomcat_Vhost
;;
*)
Usage
;;
esac
else
Usage
fi