今天帖上来一个简单的一键式自动虚拟主机搭建shell脚本,脚本实现自动配置nginx虚拟主机,自动创建ftp账户,自动创建数据库,用户,并自动实现mysql自动定时备份,日志切割,程序备份。
这里因为其他原因,就将备份机制去掉了,代码如下。
首先需要一个基础的nginx虚拟主机配置文件,一般情况下,我们配置虚拟主机都是建一个vhost目录,这里我在/etc/nginx下面建了一个default.conf文件
log_format #host# '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
server {
listen 80;
server_name #host#;
root #hosts#/#host#/html;
index index.html index.htm index.php default.html default.htm default.php;
location / {
autoindex on;
}
error_page 404 /404.html;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#注意这一句
location ~ ^(.+.php)(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# redirect server error pages to the static page /50x.html
access_log #hosts#/#host#/log/#host#.log #host#;
}
注意里面一些关键路径,我用特殊字符组合来表示,这样方便我们添加虚拟注意的时候进行匹配替换
host.sh脚本如下
#/bin/sh
hosts="/var/www/html" #主机总目录
default="/etc/nginx/default.conf" #default.conf位置
vhost="/etc/nginx/vhost" #虚拟主机配置文件目录
echo "1.add ftp user"
echo "please input websit:"
read host
if [ -d "$hosts/$host" ]
then
echo $hosts/$host
echo '[warning]dir is already exists!'
exit 0
fi
echo "add user,please input user name:"
read name
del -r $name >/dev/null 2>&1
adduser -d $hosts/$host -g ftp -s /sbin/nologin $name
passwd $name
chmod 755 $hosts/$host
mkdir -p $hosts/$host/html $hosts/$host/bak $hosts/$host/log
mkdir -p $hosts/$host/bak/code $hosts/$host/bak/sql
echo "mkdir:"$hosts/$host/html $hosts/$host/bak $hosts/$host/log
echo "mkdir:"$hosts/$host/bak/code $hosts/$host/bak/sql
chown -R $name:ftp $hosts/$host
echo "ok,add user success!name=$name,password=youwrite"
echo "If you need a database, please enter a database name, if not required, blank can be"
read database
if [ -n "$database" ]
then
echo "please input dbuser"
read dbuser
echo "please input dbpwd"
read dbpwd
HOSTNAME="127.0.0.1"
PORT="3306"
USERNAME="root"
echo "input root pwd"
read PASSWORD
fi
echo "2.To configure nginx"
cat $default | sed -e "s:#hosts#:${hosts}:g"|sed -e "s/#host#/${host}/g" > $vhost/$host.conf
/usr/sbin/nginx -s reload
echo "config nginx success"
if [ -z "$database" ]
then
echo 'ok,finish!'
exit 0
fi
echo "3.add mysql user database"
create_db_sql="insert into mysql.user(Host,User,Password) values('localhost','${dbuser}',password('${dbpwd}'))"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "{create_db_sql}"
if [ $? -ne 0 ]
then
echo 'add db user error'
exit 0
fi
sleep 1
create_db_sql="create database IF NOT EXISTS ${database}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "{create_db_sql}"
if [ $? -ne 0 ]
then
echo 'add db error'
exit 0
fi
sleep 1
create_db_sql="flush privileges"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "{create_db_sql}"
create_db_sql="gran t all on ${database}.* to ${dbuser}@localhost identified by '${dbpwd}'"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "${create_db_sql}"
if [ $? -ne 0 ]
then
echo 'user to db user error'
echo $create_db_sql
exit 0
fi
create_db_sql="flush privileges"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "{create_db_sql}"
echo 'ok,finish!'
看一下运行效果,sh host.sh
[root@localhost nginx]# sh 2.sh
1.add ftp user
please input websit:
hhh.com
add user,please input user name:
hhh
更改用户 hhh 的密码 。
新的 密码:
无效的密码: WAY 过短
无效的密码: 是回文
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
mkdir:/var/www/html/hhh.com/html /var/www/html/hhh.com/bak /var/www/html/hhh.com/log
mkdir:/var/www/html/hhh.com/bak/code /var/www/html/hhh.com/bak/sql
ok,add user success!name=hhh,password=youwrite
If you need a database, please enter a database name, if not required, blank can be
hhh
please input dbuser
hhh
please input dbpwd
hhh
input root pwd
123456
2.To configure nginx
config nginx success
3.add mysql user database
ok,finish!
[root@localhost nginx]#
系统会逐个询问输入参数,并且参数是先统一填完,然后程序进行执行,避免中间不小心输入错误无法修改,然后如果无需创建数据库,那么database参数不输入即可。
感兴趣的可以看一下。 标签: 虚拟主机,request,配置文件,listen,server