-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvhost.sh
More file actions
222 lines (198 loc) · 6.53 KB
/
vhost.sh
File metadata and controls
222 lines (198 loc) · 6.53 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
#!/bin/bash
# Check if user is root
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script, use sudo sh $0"
exit 1
fi
echo "========================================================================="
echo "Add Virtual Host for LNMP V0.9 , Written by Licess Modified by GF<[email protected]>"
echo "========================================================================="
echo "LNMP is a tool to auto-compile & install Nginx+MySQL+PHP on Linux "
echo "This script is a tool to add virtual host for nginx "
echo "For more information please visit http://www.lnmp.org/"
echo "./vhost.sh rootdir rewritefile "
echo ""
echo "========================================================================="
function addHost() {
if [ "$mode" != "auto" ]; then
domain="www.lnmp.org"
echo "Please input domain:"
read -p "(Default domain: www.lnmp.org):" domain
if [ "$domain" = "" ]; then
domain="www.lnmp.org"
fi
if [ ! -f "/usr/local/nginx/conf/vhost/$domain.conf" ]; then
echo "==========================="
echo "domain=$domain"
echo "==========================="
else
echo "==========================="
echo "$domain is exist!"
echo "==========================="
fi
echo "Do you want to add more domain name? (y/n)"
read add_more_domainame
if [ "$add_more_domainame" == 'y' ]; then
echo "Type domainname,example(bbs.vpser.net forums.vpser.net luntan.vpser.net):"
read moredomain
echo "==========================="
echo domain list="$moredomain"
echo "==========================="
moredomainame=" $moredomain"
fi
vhostdir="/home/wwwroot/$domain"
echo "Please input the directory for the domain:$domain :"
read -p "(Default directory: /home/wwwroot/$domain):" vhostdir
if [ "$vhostdir" = "" ]; then
vhostdir="/home/wwwroot/$domain"
fi
echo "==========================="
echo Virtual Host Directory="$vhostdir"
echo "==========================="
echo "==========================="
echo "Allow Rewrite rule? (y/n)"
echo "==========================="
read allow_rewrite
if [ "$allow_rewrite" == 'n' ]; then
rewrite="none"
else
rewrite="other"
echo "Please input the rewrite of programme :"
echo "wordpress,discuz,typecho,sablog,dabr rewrite was exist."
read -p "(Default rewrite: other):" rewrite
if [ "$rewrite" = "" ]; then
rewrite="other"
fi
fi
echo "==========================="
echo You choose rewrite="$rewrite"
echo "==========================="
echo "==========================="
echo "Allow access_log? (y/n)"
echo "==========================="
read access_log
if [ "$access_log" == 'n' ]; then
al="access_log off;"
else
echo "Type access_log name(Default access log file:$domain.log):"
read al_name
if [ "$al_name" = "" ]; then
al_name="$domain"
fi
alf="log_format $al_name '\$remote_addr - \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" \$http_x_forwarded_for';"
al="access_log /home/wwwlogs/$al_name.log $al_name;"
echo "==========================="
echo You access log file="$al_name.log"
echo "==========================="
fi
get_char()
{
SAVEDSTTY=`stty -g`
stty -echo
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty $SAVEDSTTY
}
echo ""
echo "Press any key to start create virtul host..."
char=`get_char`
if [ ! -f /usr/local/nginx/conf/$rewrite.conf ]; then
echo "Create Virtul Host ReWrite file......"
touch /usr/local/nginx/conf/$rewrite.conf
echo "Create rewirte file successful,now you can add rewrite rule into /usr/local/nginx/conf/$rewrite.conf."
else
echo "You select the exist rewrite rule:/usr/local/nginx/conf/$rewrite.conf"
fi
rewrite="include $rewrite.conf;"
else
al_name="$domain"
alf="log_format $al_name '\$remote_addr - \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" \$http_x_forwarded_for';"
al="access_log /home/wwwlogs/$al_name.log $al_name;"
moredomainame=" www.$domain"
fi
if [ ! -d /usr/local/nginx/conf/vhost ]; then
mkdir /usr/local/nginx/conf/vhost
fi
if [ ! -d "$vhostdir" ];then
echo "Create Virtul Host directory......"
mkdir -p $vhostdir
fi
if [ ! -f /home/wwwlogs/$al_name.log ]; then
touch /home/wwwlogs/$al_name.log
fi
chmod -R 755 $vhostdir
chown -R www:www $vhostdir
# if [ ! -f /usr/local/nginx/conf/vhost/$domain.conf ];then
cat >/usr/local/nginx/conf/vhost/$domain.conf<<eof
$alf
server
{
listen 80;
server_name $domain$moredomainame;
index index.html index.htm index.php default.html default.htm default.php;
root $vhostdir;
$rewrite
location ~ .*\.(php|php5)?$
{
try_files \$uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
$al
}
eof
# fi
if [ "$mode" != "auto" ];then
echo "========================================================================="
echo "Add Virtual Host for LNMP V0.9 , Written by Licess "
echo "========================================================================="
echo "For more information please visit http://www.lnmp.org/"
echo ""
echo "Your domain:$domain"
echo "Directory of $domain:$vhostdir"
echo ""
echo "========================================================================="
else
echo "$domain¡ú$vhostdir"
fi
}
function reloadNginx() {
echo "Test Nginx configure file......"
/usr/local/nginx/sbin/nginx -t
echo ""
echo "Restart Nginx......"
/usr/local/nginx/sbin/nginx -s reload
}
if [ -d "$1" ]; then
mode="auto";
for file in `ls "$1"`
do
if [ -d "$1/$file" ]; then
domain=$file
vhostdir="$1/$file"
if [ -f "$2" ]; then
rewrite="include $2;"
fi
addHost
fi
done
reloadNginx
elif [ "$1" != "--help" ]; then
addHost
reloadNginx
fi