forked from linuxeye/lnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vhost.sh
executable file
·423 lines (393 loc) · 12.6 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
#!/bin/bash
# Author: yeho <lj2007331 AT gmail.com>
# Blog: http://blog.linuxeye.com
# Check if user is root
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
clear
printf "
#######################################################################
# LNMP/LAMP/LANMP for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
# For more information please visit http://blog.linuxeye.com/31.html #
#######################################################################
"
. ./options.conf
HHVM_YN()
{
if [ -e "/usr/bin/hhvm" ];then
while :
do
echo
echo 'Please choose to use PHP or HHVM:'
echo -e "\t\033[32m1\033[0m. Use php"
echo -e "\t\033[32m2\033[0m. Use hhvm"
read -p "Please input a number:(Default 1 press Enter) " PHP_HHVM
[ -z "$PHP_HHVM" ] && PHP_HHVM=1
if [ $PHP_HHVM != 1 -a $PHP_HHVM != 2 ];then
echo -e "\033[31minput error! Please only input number 1,2\033[0m"
else
break
fi
done
fi
if [ "$PHP_HHVM" == '2' ];then
NGX_CONF="fastcgi_pass unix:/var/run/hhvm/sock;\n\tfastcgi_index index.php;\n\tfastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n\tinclude fastcgi_params;"
else
NGX_CONF="#fastcgi_pass remote_php_ip:9000;\n\tfastcgi_pass unix:/dev/shm/php-cgi.sock;\n\tfastcgi_index index.php;\n\tinclude fastcgi.conf;"
fi
}
Input_domain()
{
while :
do
echo
read -p "Please input domain(example: www.linuxeye.com): " domain
if [ -z "`echo $domain | grep '.*\..*'`" ]; then
echo -e "\033[31minput error! \033[0m"
else
break
fi
done
if [ -e "$web_install_dir/conf/vhost/$domain.conf" -o -e "$apache_install_dir/conf/vhost/$domain.conf" ]; then
[ -e "$web_install_dir/conf/vhost/$domain.conf" ] && echo -e "$domain in the Nginx/Tengine already exist! \nYou can delete \033[32m$web_install_dir/conf/vhost/$domain.conf\033[0m and re-create"
[ -e "$apache_install_dir/conf/vhost/$domain.conf" ] && echo -e "$domain in the Apache already exist! \nYou can delete \033[32m$apache_install_dir/conf/vhost/$domain.conf\033[0m and re-create"
exit 1
else
echo "domain=$domain"
fi
while :
do
echo ''
read -p "Do you want to add more domain name? [y/n]: " moredomainame_yn
if [ "$moredomainame_yn" != 'y' ] && [ "$moredomainame_yn" != 'n' ];then
echo -e "\033[31minput error! Please only input 'y' or 'n'\033[0m"
else
break
fi
done
if [ "$moredomainame_yn" == 'y' ]; then
while :
do
echo
read -p "Type domainname,example(linuxeye.com www.example.com): " moredomain
if [ -z "`echo $moredomain | grep '.*\..*'`" ]; then
echo -e "\033[31minput error\033[0m"
else
[ "$moredomain" == "$domain" ] && echo -e "\033[31mDomain name already exists! \033[0m" && continue
echo domain list="$moredomain"
moredomainame=" $moredomain"
break
fi
done
Domain_alias=ServerAlias$moredomainame
fi
echo
echo "Please input the directory for the domain:$domain :"
read -p "(Default directory: /home/wwwroot/$domain): " vhostdir
if [ -z "$vhostdir" ]; then
vhostdir="/home/wwwroot/$domain"
echo -e "Virtual Host Directory=\033[32m$vhostdir\033[0m"
fi
echo
echo "Create Virtul Host directory......"
mkdir -p $vhostdir
echo "set permissions of Virtual Host directory......"
chown -R www.www $vhostdir
}
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' ] && [ "$anti_hotlinking_yn" != 'n' ];then
echo -e "\033[31minput error! Please only input 'y' or 'n'\033[0m"
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\tvalid_referers none blocked $domain_allow_all;\n\tif (\$invalid_referer) {\n\t\t#rewrite ^/ http://www.linuxeye.com/403.html;\n\t\treturn 403;\n\t\t}\n\t}")
else
anti_hotlinking=
fi
}
Nginx_rewrite()
{
while :
do
echo ''
read -p "Allow Rewrite rule? [y/n]: " rewrite_yn
if [ "$rewrite_yn" != 'y' ] && [ "$rewrite_yn" != 'n' ];then
echo -e "\033[31minput error! Please only input 'y' or 'n'\033[0m"
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 -e "\033[32mwordpress\033[0m,\033[32mdiscuz\033[0m,\033[32mphpwind\033[0m,\033[32mtypecho\033[0m,\033[32mecshop\033[0m,\033[32mdrupal\033[0m,\033[32mjoomla\033[0m rewrite was exist."
read -p "(Default rewrite: other):" rewrite
if [ "$rewrite" == "" ]; then
rewrite="other"
fi
echo -e "You choose rewrite=\033[32m$rewrite\033[0m"
if [ -s "conf/$rewrite.conf" ];then
/bin/cp conf/$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' ] && [ "$access_yn" != 'n' ];then
echo -e "\033[31minput error! Please only input 'y' or 'n'\033[0m"
else
break
fi
done
if [ "$access_yn" == 'n' ]; then
N_log="access_log off;"
else
N_log="access_log /home/wwwlogs/${domain}_nginx.log combined;"
echo -e "You access log file=\033[32m/home/wwwlogs/${domain}_nginx.log\033[0m"
fi
}
Create_nginx_conf()
{
[ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
cat > $web_install_dir/conf/vhost/$domain.conf << EOF
server {
listen 80;
server_name $domain$moredomainame;
$N_log
index index.html index.htm index.jsp index.php;
include $rewrite.conf;
root $vhostdir;
#error_page 404 /404.html;
if ( \$query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
$anti_hotlinking
`echo -e $ngx_pagespeed`
location ~ .*\.(php|php5)?$ {
`echo -e $NGX_CONF`
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 7d;
}
}
EOF
echo
$web_install_dir/sbin/nginx -t
if [ $? == 0 ];then
echo "Restart Nginx......"
$web_install_dir/sbin/nginx -s reload
else
rm -rf $web_install_dir/conf/vhost/$domain.conf
echo -e "Create virtualhost ... \033[31m[FAILED]\033[0m"
exit 1
fi
printf "
#######################################################################
# LNMP for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
# For more information please visit http://blog.linuxeye.com/31.html #
#######################################################################
"
echo -e "`printf "%-32s" "Your domain:"`\033[32m$domain\033[0m"
echo -e "`printf "%-32s" "Virtualhost conf:"`\033[32m$web_install_dir/conf/vhost/$domain.conf\033[0m"
echo -e "`printf "%-32s" "Directory of:"`\033[32m$vhostdir\033[0m"
[ "$rewrite_yn" == 'y' ] && echo -e "`printf "%-32s" "Rewrite rule:"`\033[32m$rewrite\033[0m"
}
Apache_log()
{
while :
do
echo ''
read -p "Allow Apache access_log? [y/n]: " access_yn
if [ "$access_yn" != 'y' ] && [ "$access_yn" != 'n' ];then
echo -e "\033[31minput error! Please only input 'y' or 'n'\033[0m"
else
break
fi
done
if [ "$access_yn" == 'n' ]; then
A_log='CustomLog "/dev/null" common'
else
A_log="CustomLog \"/home/wwwlogs/${domain}_apache.log\" common"
echo "You access log file=/home/wwwlogs/${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
$Domain_alias
ErrorLog "/home/wwwlogs/${domain}_error_apache.log"
$A_log
<Directory "$vhostdir">
SetOutputFilter DEFLATE
Options FollowSymLinks
$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 -e "Create virtualhost ... \033[31m[FAILED]\033[0m"
exit 1
fi
printf "
#######################################################################
# LAMP for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
# For more information please visit http://blog.linuxeye.com/82.html #
#######################################################################
"
echo -e "`printf "%-32s" "Your domain:"`\033[32m$domain\033[0m"
echo -e "`printf "%-32s" "Virtualhost conf:"`\033[32m$apache_install_dir/conf/vhost/$domain.conf\033[0m"
echo -e "`printf "%-32s" "Directory of $domain:"`\033[32m$vhostdir\033[0m"
}
Create_nginx_apache_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 {
listen 80;
server_name $domain$moredomainame;
$N_log
index index.html index.htm index.jsp index.php;
root $vhostdir;
#error_page 404 /404.html;
if ( \$query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
$anti_hotlinking
`echo -e $ngx_pagespeed`
location / {
try_files \$uri @apache;
}
location @apache {
internal;
proxy_pass http://127.0.0.1:8080;
}
location ~ .*\.(php|php5)?$ {
proxy_pass http://127.0.0.1:8080;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 7d;
}
}
EOF
echo
$web_install_dir/sbin/nginx -t
if [ $? == 0 ];then
echo "Restart Nginx......"
$web_install_dir/sbin/nginx -s reload
else
rm -rf $web_install_dir/conf/vhost/$domain.conf
echo -e "Create virtualhost ... \033[31m[FAILED]\033[0m"
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 *:8080>
ServerAdmin [email protected]
DocumentRoot "$vhostdir"
ServerName $domain
$Domain_alias
ErrorLog "/home/wwwlogs/${domain}_error_apache.log"
$A_log
<Directory "$vhostdir">
SetOutputFilter DEFLATE
Options FollowSymLinks
$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 "
#######################################################################
# LANMP for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
# For more information please visit http://blog.linuxeye.com/31.html #
#######################################################################
"
echo -e "`printf "%-32s" "Your domain:"`\033[32m$domain\033[0m"
echo -e "`printf "%-32s" "Nginx Virtualhost conf:"`\033[32m$web_install_dir/conf/vhost/$domain.conf\033[0m"
echo -e "`printf "%-32s" "Apache Virtualhost conf:"`\033[32m$apache_install_dir/conf/vhost/$domain.conf\033[0m"
echo -e "`printf "%-32s" "Directory of:"`\033[32m$vhostdir\033[0m"
[ "$rewrite_yn" == 'y' ] && echo -e "`printf "%-32s" "Rewrite rule:"`\033[32m$rewrite\033[0m"
}
if [ -d "$web_install_dir" -a ! -d "$apache_install_dir" ];then
HHVM_YN
Input_domain
Nginx_anti_hotlinking
Nginx_rewrite
Nginx_log
Create_nginx_conf
elif [ ! -d "$web_install_dir" -a -d "$apache_install_dir" ];then
HHVM_YN
Input_domain
Apache_log
Create_apache_conf
elif [ -d "$web_install_dir" -a -d "$apache_install_dir" ];then
HHVM_YN
Input_domain
Nginx_anti_hotlinking
#Nginx_rewrite
Nginx_log
Apache_log
Create_nginx_apache_conf
fi