forked from centos-bz/ezhttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathez
More file actions
420 lines (378 loc) · 11.9 KB
/
ez
File metadata and controls
420 lines (378 loc) · 11.9 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
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
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#===============================================================================
# SYSTEM REQUIRED: Linux
# DESCRIPTION: automatic deploy your linux
# AUTHOR: Zhu Maohai.
# website: http://www.centos.bz/ezhttp/
#===============================================================================
yes_or_no(){
local prompt=$1
local yaction=$2
local naction=$3
while true; do
read -p "${prompt}?[Y/n]: " yn
yn=`upcase_to_lowcase $yn`
case $yn in
y ) eval "$yaction";break;;
n ) eval "$naction";break;;
* ) echo "input error,please only input y or n."
esac
done
}
nginx_vhost(){
local action=$1
case $action in
add ) nginx_vhost_add;;
list ) nginx_vhost_list;;
del ) nginx_vhost_del;;
*) echo "action $action not found";exit 1;;
esac
}
php_support(){
php_support_code="location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}"
}
nginx_vhost_add(){
#输入域名
while true; do
read -p "input server names(ie.www.centos.bz centos.bz): " server_names
for i in $server_names;do
if vhost_is_exist $i;then
echo "vhost $i is exist,please reinput server_names."
break
fi
break 2
done
done
#设置默认网站目录
default_root="/home/${server_names%% *}"
#输入网站目录
read -p "input website root(default:$default_root/web): " website_root
website_root=${website_root:=$default_root}
#是否添加重写规则
yes_or_no "add nginx rewrite rule" "select_rewrite_rule" "rewrite_rule="""
#是否支持php
yes_or_no "enable php support" "php_support" "php_support_code=''"
cat > ${nginx_location}/conf/vhost/${server_names%% *}.conf << EOF
server {
server_name ${server_names};
listen 80;
index index.php index.html index.htm;
root ${website_root}/web;
${rewrite_rule}
${php_support_code}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 12h;
}
access_log ${website_root}/logs/access.log access;
error_log ${website_root}/logs/error.log error;
}
EOF
echo "congratulations.vhost ${server_names%% *} had created."
#创建目录
mkdir -p ${website_root}/logs ${website_root}/web
chown -R www ${website_root}/web
#重载配置
echo "reloading the nginx config file..."
if ${nginx_location}/sbin/nginx -t;then
${nginx_location}/sbin/nginx -s reload
echo "reload success."
exit 0
else
echo "reload failed.config file had an error,please fix it."
exit 1
fi
}
nginx_vhost_list(){
sed -n -r -e 's/server_name\s+(.*);/\1/p' -e 's/root\s+(.*);/\1/p' ${nginx_location}/conf/vhost/*.conf | awk 'BEGIN{printf ("%-50s %-50s\n%-50s %-50s\n","server_name","root","-----------","----")}{printf("%-50s",$0);getline;printf $0"\n" }'
}
nginx_vhost_del(){
read -p "input server_name of vhost you'll delete(ie.www.centos.bz): " domain
if vhost_is_exist "$domain";then
rm -f ${nginx_location}/conf/vhost/$domain.conf
echo "complete deleted the server_name ${domain} of vhost."
echo "reloading the nginx config file..."
if ${nginx_location}/sbin/nginx -t;then
${nginx_location}/sbin/nginx -s reload
echo "reload success."
exit 0
else
echo "reload failed.config file had an error,please fix it."
exit 1
fi
else
echo "vhost $domain not found,failed to delete."
exit 1
fi
}
apache_vhost(){
local action=$1
case $action in
add ) apache_vhost_add;;
list ) apache_vhost_list;;
del ) apache_vhost_del;;
*) echo "action $action not found";exit 1;;
esac
}
apache_vhost_add(){
#输入域名
while true; do
read -p "input server names(ie.www.centos.bz centos.bz): " server_names
for i in $server_names;do
if apache_vhost_is_exist $i;then
echo "vhost $i is exist,please reinput server_names."
break
fi
break 2
done
done
#设置默认网站目录
default_root="/home/${server_names%% *}"
#输入网站目录
read -p "input website root(default:$default_root/web): " website_root
website_root=${website_root:=$default_root}
#开始写入配置文件
cat > ${apache_location}/conf/vhost/${server_names%% *}.conf << EOF
<VirtualHost *:80>
ServerName ${server_names%% *}
ServerAlias ${server_names}
DocumentRoot ${website_root}/web
DirectoryIndex index.php index.html index.htm
<Directory ${website_root}/web>
Options +Includes -Indexes
AllowOverride All
Order Deny,Allow
Allow from All
php_admin_value open_basedir ${website_root}/web:/tmp:/proc
</Directory>
ErrorLog ${website_root}/logs/error.log
TransferLog ${website_root}/logs/access.log
</VirtualHost>
EOF
echo "congratulations.vhost ${server_names%% *} had created."
#创建目录
mkdir -p ${website_root}/logs ${website_root}/web
chown -R www ${website_root}/web
#重载配置
echo "reloading the apache config file..."
if ${apache_location}/bin/apachectl -t;then
/etc/init.d/httpd restart
echo "reload success."
exit 0
else
echo "reload failed.config file had an error,please fix it."
exit 1
fi
}
apache_vhost_list(){
sed -n -r -e 's/ServerAlias\s+(.*)/\1/p' -e 's/DocumentRoot\s+(.*)/\1/p' ${apache_location}/conf/vhost/*.conf | awk 'BEGIN{printf ("%-50s %-50s\n%-50s %-50s\n","server_name","root","-----------","----")}{printf("%-50s",$0);getline;printf $0"\n" }'
}
apache_vhost_del(){
read -p "input server_name of vhost you'll delete(ie.www.centos.bz): " domain
if ! apache_vhost_is_exist "$domain";then
echo "vhost $domain not found"
exit 1
else
rm -f ${apache_location}/conf/vhost/$domain.conf
echo "complete deleted the server_name ${domain} of vhost."
#重载配置
echo "reloading the apache config file..."
if ${apache_location}/bin/apachectl -t;then
/etc/init.d/httpd restart
echo "reload success."
exit 0
else
echo "reload failed.config file had an error,please fix it."
exit 1
fi
fi
}
#判断root密码是否正确
check_root_passwd(){
while true
do
read -p "please input your mysql root password: " root_password
echo "show databases" | mysql -uroot -p${root_password} > /tmp/mysql_err 2>&1
if [ $? != 0 ];then
grep -q "1045" /tmp/mysql_err && echo "your root password is not correct"
else
break
fi
done
}
mysql_manger(){
local action=$1
case $action in
add ) mysql_add;;
mod ) mysql_user_mod;;
del ) mysql_del;;
reset ) mysql_reset;;
*) echo "action $action not found";exit 1
esac
}
mysql_add_db(){
read -p "please input the database name: " dbname
echo "create database ${dbname};" | mysql -uroot -p${root_password} > /dev/null 2>&1 && echo "successfully create database ${dbname}" || echo "create database ${dbname} failed."
}
mysql_add_user(){
read -p "please input the user name: " user
read -p "please input the user password: " passwd
read -p "input the database you'll grant privileges on to this user,input \"*\" means grant all databases.(default:all databases.): " grantdb
grantdb=${grantdb:=*}
read -p "input the client you'll allow access to this user,input \"%\" means allow all client.(default:localhost 127.0.0.1): " grantclient
grantclient=${grantclient:="localhost 127.0.0.1"}
for client in $grantclient; do
echo "GRANT ALL PRIVILEGES ON ${grantdb}.* TO '${user}'@'$client' IDENTIFIED BY '$passwd'" | mysql -uroot -p${root_password} > /dev/null 2>&1
done
echo "flush privileges;" | mysql -uroot -p${root_password} > /dev/null 2>&1
echo "successfully add user ${user}"
}
mysql_add(){
check_root_passwd
#是否添加数据库
yes_or_no "add a database" "mysql_add_db"
#是否添加用户
yes_or_no "add a user" "mysql_add_user"
}
mysql_del_db(){
read -p "please input the database name: " $db
echo "drop database $db;" | mysql -uroot -p${root_password} > /dev/null 2>&1
echo "successfully delete database $db."
}
mysql_del_user(){
read -p "please input the user name: " user
echo "use mysql;delete from user where User='$user' " | mysql -uroot -p${root_password} > /dev/null 2>&1
echo "successfully delete user $user."
}
mysql_user_mod(){
check_root_passwd
read -p "please input the user: " user
read -p "please input a new password: " passwd
echo "use mysql;update user set password=PASSWORD("$passwd") where User='$user';flush privileges;" | mysql -uroot -p${root_password} > /dev/null 2>&1
echo "successfully modify user $user password."
}
mysql_del(){
check_root_passwd
yes_or_no "delete a database" "mysql_del_db"
yes_or_no "delete a user" "mysql_del_user"
}
mysql_reset(){
read -p "please input a new password for root user: " password
/etc/init.d/mysqld stop 2>&1 >/dev/null
if [ "$mysql_location" == "" ];then
read -p "mysql location not found,input mysql location(ie./usr/local/mysql): " mysql_location
fi
${mysql_location}/bin/mysqld_safe --defaults-file=${mysql_location}/etc/my.cnf --skip-grant-tables 2>&1 >/dev/null &
echo "wait for 8 seconds..."
sleep 8 && mysql -u root -e "use mysql;update user set password=PASSWORD(\"${password}\") where User='root';flush privileges;"
/etc/init.d/mysqld restart 2>&1 >/dev/null
echo "show databases;" | mysql -uroot -p${password} > /dev/null 2>&1 && echo "successfully reset your mysql root password" || echo "Sorry,failed to reset your root password."
}
ftp_user(){
local action=$1
case $action in
add ) ftp_user_add;;
list ) ftp_user_list;;
del ) ftp_user_del;;
mod ) ftp_user_mod;;
*) echo "action $action not found";exit 1;;
esac
}
ftp_user_add(){
read -p "input ftp user name: " ftp_username
read -p "input ftp password: " ftp_pass
read -p "input ftp user $ftp_username Home Directory: " ftp_home
useradd -d $ftp_home -c pure-ftpd -s /bin/sh $ftp_username
echo $ftp_username:$ftp_pass | chpasswd
chown -R "$ftp_username" "$ftp_home"
echo "successfully add ftp user $ftp_username."
}
ftp_user_list(){
printf "FTPUser\t\tRoot Directory\n"
cat /etc/passwd | grep pure-ftpd | awk 'BEGIN {FS=":"} {print $1"\t\t"$6}'
}
ftp_user_del(){
read -p "(Please input the ftpuser you'll delete):" ftpuser
userdel $ftpuser
echo "successfully delete ftpuser $ftpuser"
}
ftp_user_mod(){
read -p "(Please input the ftpuser you'll modify): " ftpuser
echo "1、modify ftpuser $ftpuser password"
echo "2、modify ftpuser $ftpuser home directory"
while true; do
read -p "please input a number: " ftp_mod
case $ftp_mod in
1 ) read -s -p "please enter a new password: " ftp_new_pass;echo $ftpuser:$ftp_new_pass | chpasswd;echo "change ftp user $ftpuser password successfully.";break;;
2 ) read -p "please enter ftpuser $ftpuser new home directory: " ftp_home;usermod -d $ftp_home $ftpuser;break;;
* ) echo "input error.";;
esac
done
}
#检查vhost是否存在
vhost_is_exist(){
local domain=$1
local conf_file="${nginx_location}/conf/vhost/$1.conf"
if [ -f "$conf_file" ];then
return 0
else
return 1
fi
}
#检查apahcevhost是否存在
apache_vhost_is_exist(){
local domain=$1
local conf_file="${apache_location}/conf/vhost/$1.conf"
if [ -f "$conf_file" ];then
return 0
else
return 1
fi
}
#大写转换成小写
upcase_to_lowcase(){
words=$1
echo $words | tr '[A-Z]' '[a-z]'
}
#nginx重写规则选择
select_rewrite_rule(){
local rewrite_rules=("include rewrite/DEDECMS.conf;" "include rewrite/Discuz_7.conf;" "include rewrite/Discuz_X.conf;" "include rewrite/ecshop.conf;" "include rewrite/phpcms.conf;" "include rewrite/PHPWind.conf;" "include rewrite/shopex.conf;" "include rewrite/Typecho.conf;" "include rewrite/wordpress.conf;" )
echo "available nginx rewrite rules:"
echo -e "1、DEDECMS\n2、Discuz_7\n3、Discuz_X\n4、ECshop\n5、PHPCMS\n6、PHPWind\n7、Shopex\n8、Typecho\n9、WordPress"
while true; do
read -p "please input a number: " rule
if ! echo $rule | sed -n "/^[1-9]$/q0;q1" -;then
echo "input error."
else
break
fi
done
rewrite_rule=${rewrite_rules[$rule-1]}
}
if [ $# != 2 ];then
echo "Usage: lx program_name action"
exit 1
fi
#使read能支持回格删除
stty erase "^H"
#引入各软件安装路径
. /tmp/ezhttp_info_do_not_del
#接收脚本传参
proname=$1
action=$2
case $proname in
nginx ) nginx_vhost $action;;
apache ) apache_vhost $action;;
ftp ) ftp_user $action;;
mysql ) mysql_manger $action;;
*) echo "program_name $proname not found.";exit 1;;
esac