首页 » 工作 » 正文

CentOS脚本:自动添加站点、FTP、MySQL

发布者:站点默认
2012/07/11 浏览数(1,546) 分类:工作 CentOS脚本:自动添加站点、FTP、MySQL已关闭评论
#!/bin/bash
# 说明:站点添加工具[独立代码、数据库]
# 日期:2012-03-31 22:31:21 [6]
# 用法: x dn.ext
# 补充:mpm-itk、vsftpd、mysql
#    需要手动删除mysql数据库、vsftpd用户
#      buildhttpdpwd buildmysqlpwd 用于根据域名计算密码
# 作者:/

# 显示帮助
if [ -z "$1" ]; then
   echo " Usage: `basename $0` domainName comment"
   exit
fi

cd /root/bin/
# 变量配置
DATE=`date +%Y%m%d%H%M%S`
USER_PWD=`buildhttpdpwd $1`
USER_QUOTA=204800
FOLDER="/var/www/WebSite/$1"

HTTPD_CONF="/etc/httpd/webSite/"$1".conf"
HTTPD_USER=${1//./_}
HTTPD_GROUP="ftp"

## 如果名称超过16个字符时自动截断(MYSQL用户名最长16字符)
USER_LENGTH=`expr length "$1"`
if [ 16 -lt $USER_LENGTH ]; then
   HTTPD_USER=${HTTPD_USER:0:16}
   echo "Tip: Domain over 16 characters in length, has been truncated"
fi

VSFTPD_USER=$HTTPD_USER
VSFTPD_USER_CONF="/etc/vsftpd/vusers/"$VSFTPD_USER
VSFTPD_USER_DB_FILE="/etc/vsftpd/vusers.db.info"

MYSQL_DB_USER="creater"
MYSQL_DB_PWD="Qa5yUTjtb6AswA4y"
DB_NAME=$HTTPD_USER
DB_USER=$HTTPD_USER
DB_PWD=`buildmysqlpwd $1`

# 家目录层数多时创建外层目录
PARENT_FOLDER=`dirname $FOLDER`
if [ ! -d $PARENT_FOLDER ]; then
   mkdir -p $PARENT_FOLDER
   if [ $? -ne 0 ]; then
      echo $FOLDER"'s parent folder created failed."
      exit
   fi
fi
echo "webRoot://:@$FOLDER/"

# 添加用户
if [ -z "$2" ]; then
   COMMENT=""
else
   COMMENT="$2"
fi
useradd -s /bin/nologin -m -d $FOLDER -g $HTTPD_GROUP $HTTPD_USER -c "$COMMENT"
if [ $? -ne 0 ]; then
   echo "user "$HTTPD_USER" added failed."
   exit
fi
echo $USER_PWD | passwd $HTTPD_USER --stdin > /dev/null 2>&1
echo "osuser://$HTTPD_USER:$USER_PWD/"

# 设置磁盘配额
setquota -u $HTTPD_USER 0 $USER_QUOTA 0 0 /
echo "quota://"$USER_QUOTA"bytes"

# 生成默认提示页面
echo "$1, put file here first~">> $FOLDER/index.php

# 更改目录权限
chown -R $HTTPD_USER.$HTTPD_GROUP $FOLDER
chmod og-rwx -R $FOLDER
# 生成apache虚拟主机配置文件
echo "#By AutoCreater At "`date`> $HTTPD_CONF
echo "<VirtualHost *:80>">> $HTTPD_CONF
echo "   ServerName www.$1">> $HTTPD_CONF
echo "   ServerAlias $1 $1.demo.upall.cn">> $HTTPD_CONF
echo "   DocumentRoot \"$FOLDER\"">> $HTTPD_CONF
echo "   <Directory \"$FOLDER\">">> $HTTPD_CONF
echo "      Options FollowSymLinks">> $HTTPD_CONF
echo "      AllowOverride All">> $HTTPD_CONF
echo "      Order Allow,Deny">> $HTTPD_CONF
echo "      Allow from all">> $HTTPD_CONF
echo "   </Directory>">> $HTTPD_CONF
echo "   <IfModule mpm_itk_module>">> $HTTPD_CONF
echo "      AssignUserId $HTTPD_USER $HTTPD_GROUP">> $HTTPD_CONF
echo "   </IfModule>">> $HTTPD_CONF
echo "</VirtualHost>">> $HTTPD_CONF
echo "vhost://:@$HTTPD_CONF"

# 添加FTP用户
echo $HTTPD_USER>>/etc/vsftpd/chroot_list
# 以下四行添加虚拟用户时使用,现改为系统用户所以注释掉
#cp $VSFTPD_USER_DB_FILE $VSFTPD_USER_DB_FILE"."$DATE
#echo $VSFTPD_USER>> $VSFTPD_USER_DB_FILE
#echo $USER_PWD>> $VSFTPD_USER_DB_FILE
#/etc/vsftpd/makedb.sh
#echo "local_root=$FOLDER"> $VSFTPD_USER_CONF
#echo "anon_umask=007">> $VSFTPD_USER_CONF
#echo "write_enable=YES">> $VSFTPD_USER_CONF
#echo "anon_world_readable_only=NO">> $VSFTPD_USER_CONF
#echo "anon_upload_enable=YES">> $VSFTPD_USER_CONF
#echo "anon_mkdir_write_enable=YES">> $VSFTPD_USER_CONF
#echo "anon_other_write_enable=YES">> $VSFTPD_USER_CONF
echo "ftp://$VSFTPD_USER:$USER_PWD@116.255.205.145:55555/"

# 添加 mysql 数据库及用户
SQL="CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PWD'; "
echo $SQL | mysql -u$MYSQL_DB_USER -p$MYSQL_DB_PWD -hlocalhost
SQL="GRANT USAGE ON *.* "
SQL="${SQL}TO '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PWD' "
SQL="${SQL}WITH MAX_QUERIES_PER_HOUR 0 "
SQL="${SQL}MAX_CONNECTIONS_PER_HOUR 0 "
SQL="${SQL}MAX_UPDATES_PER_HOUR 0 "
SQL="${SQL}MAX_USER_CONNECTIONS 0 ; "
SQL="${SQL}CREATE DATABASE IF NOT EXISTS $DB_USER; "
SQL="${SQL}GRANT ALL PRIVILEGES ON $DB_USER.* TO '$DB_USER'@'localhost'; ";
echo "$SQL" | mysql -u$MYSQL_DB_USER -p$MYSQL_DB_PWD -hlocalhost
echo "mysql://$DB_USER:$DB_PWD@localhost:3306/$DB_USER/"

# 测试网址
echo "demoUrl://$1.demo.upall.cn/"

# 重启httpd、vsftpd
service httpd graceful
#service vsftpd restart

# 日志
echo "[`date +%Y\-%m\-%d\ %H:%M:%S`] dn:$1 usr:$HTTPD_USER pwd:$USER_PWD dir:$FOLDER" >> /root/bin/x_log

#--end--#
echo 补充客户信息:
chfn $HTTPD_USER

echo 请手动设置用户过期时间点,方法:chage -E 2013-6-15 userName
点击返回顶部
  1. 留言
  2. 联系方式