-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvhost.sh
More file actions
executable file
·276 lines (253 loc) · 9.01 KB
/
vhost.sh
File metadata and controls
executable file
·276 lines (253 loc) · 9.01 KB
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
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# Check if user is root
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script, please use root to install lnmp"
exit 1
fi
hr="========================================================================="
clear
echo $hr
web_install_dir=/usr/local/nginx
Input_domain()
{
while :
do
echo
read -p "Please input domain(example: www.boxcore.org boxcore.org: " 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" ]; 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"
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(www.boxcore.org boxcore.org): " 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/www/$domain): " vhostdir
if [ -z "$vhostdir" ]; then
vhostdir="/home/www/$domain"
echo -e "Virtual Host Directory=\033[32m$vhostdir\033[0m"
fi
echo
echo "Create Virtul Host directory......"
mkdir -pv $vhostdir
echo "set permissions of Virtual Host directory......"
chown -R www.www $vhostdir
}
Ngx_pagespeed()
{
# check ngx_pagespeed and add ngx_pagespeed
$web_install_dir/sbin/nginx -V &> ngx_tmp
if [ ! -z "`cat ngx_tmp | grep ngx_pagespeed`" ];then
rm -rf ngx_tmp
while :
do
echo ''
read -p "Do you want to use ngx_pagespeed module? [y/n]: " ngx_pagespeed_yn
if [ "$ngx_pagespeed_yn" != 'y' ] && [ "$ngx_pagespeed_yn" != 'n' ];then
echo -e "\033[31minput error! Please only input 'y' or 'n'\033[0m"
else
if [ "$ngx_pagespeed_yn" == 'y' ];then
ngx_pagespeed='pagespeed on;\npagespeed FileCachePath /var/ngx_pagespeed_cache;\npagespeed RewriteLevel CoreFilters;\npagespeed EnableFilters local_storage_cache;\npagespeed EnableFilters collapse_whitespace,remove_comments;\npagespeed EnableFilters outline_css;\npagespeed EnableFilters flatten_css_imports;\npagespeed EnableFilters move_css_above_scripts;\npagespeed EnableFilters move_css_to_head;\npagespeed EnableFilters outline_javascript;\npagespeed EnableFilters combine_javascript;\npagespeed EnableFilters combine_css;\npagespeed EnableFilters rewrite_javascript;\npagespeed EnableFilters rewrite_css,sprite_images;\npagespeed EnableFilters rewrite_style_attributes;\npagespeed EnableFilters recompress_images;\npagespeed EnableFilters resize_images;\npagespeed EnableFilters convert_meta_tags;\nlocation ~ "\\.pagespeed\\.([a-z]\\.)?[a-z]{2}\\.[^.]{10}\\.[^.]+" { add_header "" ""; }\nlocation ~ "^/ngx_pagespeed_static/" { }\nlocation ~ "^/ngx_pagespeed_beacon$" { }\nlocation /ngx_pagespeed_statistics { allow 127.0.0.1; deny all; }\nlocation /ngx_pagespeed_message { allow 127.0.0.1; deny all; }'
else
ngx_pagespeed=
fi
break
fi
done
fi
}
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.boxcore.org/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 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/www/logs/${domain}_nginx.log combined;"
echo -e "You access log file=\033[32m/home/www/logs/${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)?\$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_param PATH_INFO \$fastcgi_path_info;
fastcgi_param PATH_TRANSLATED \$document_root\$fastcgi_path_info;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
include fastcgi_params;
}
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 $vhostdir
echo -e "Create virtualhost ... \033[31m[FAILED]\033[0m"
exit 1
fi
cat > $vhostdir/index.html << EOF
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>您的站点$domain已经建好 || power by lnmp.boxcore.org</title>
</head>
<body>
<h1>您的站点$domain已经建好!</h1>
<p>欢迎使用<strong>LNMP.BOXCORE</strong>构建站点,如您在使用过程中遇到任何问题请访问<a target="_blank" href="http://lnmp.boxcore.org/feedback.php">lnmp.boxcore.org/feedback.php</a>反馈</p>
</body>
</html>
EOF
printf "
#######################################################################
# LNMP for CentOS #
# For more information please visit http://lnmp.boxcore.org #
#######################################################################
"
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"
}
if [ -d "$web_install_dir" ];then
Input_domain
Ngx_pagespeed
Nginx_anti_hotlinking
Nginx_rewrite
Nginx_log
Create_nginx_conf
fi