[Linux 常用技巧] 以前一个虚拟主机用户添加的的shell

Nov 21st, 2008

转载本站文章请注明,转载自:扶凯[http://www.php-oa.com]

本文链接: http://www.php-oa.com/2008/11/21/useradd_http_mysql_ftp_quota.html

#! /bin/sh
ROOT_UID=0
E_NOTROOT=67

#判断是否root用户
if [ "$UID" -ne "$ROOT_UID" ]
then
    echo "Must be root to run this script."
    exit $E_NOTROOT
fi

#输入用户名
echo -n "Enter User Name:"
    read UserName
if [ ! -n "$UserName" ]; then
    echo "User Name is empty"
    exit 0
fi

#判断用户是否存在.存在就退出
for UN in `cat /etc/passwd |grep -i $UserName |awk -F ":" ‘{print $1}’`
do
if [[ $UserName == $UN ]]; then
                echo "Sorry,User $UserName is exists"
                exit 0
        else
                echo aaaaaa
        fi
done

#用户空间密码
stty -echo    # 关闭屏幕的echo.
echo -n "Enter password:"
    read pw1
echo
echo -n "Retype Enter password:"
    read pw2
if [ "$pw1" == "$pw2" ] ; then
    Pawd=$pw1
else
    echo "Sorry, passwords do not match."
    exit 0
fi
stty echo     # 恢复屏幕的echo.
echo
echo -n "Enter Domain Name:"
    read DomainName
if [ ! -n "$DomainName" ]; then
    echo "User DomainName is empty"
    exit 0
fi

#MySql管理员密码
stty -echo    # 关闭屏幕的echo.
echo -n "Enter LocalHost Mysql Root Password:"
    read Mysqlpwd
if [ ! -n "$Mysqlpwd" ]; then
    echo " Mysql Password is empty"
    exit 0
fi
stty echo     # 恢复屏幕的echo.

#检查MySql连接是否正常,shell中的if语法,-z参数判断条件是否为0
/usr/bin/mysqladmin -uroot -p$Mysqlpwd ping &>/dev/null
if [ -z $? ]; then
                echo "MySql connection is fail"
        else
                #mysql用户加入
                mysql -uroot -p"$Mysqlpwd" -e "create database $UserName;"
                mysql -uroot -p"$Mysqlpwd" -e "use $UserName;GRANT SELECT, INSERT, UPDATE, DELETE, INDEX, ALTER, CREATE, LOCK TABLES, CREATE TEMPORARY TABLES, DROP, REFERENCES ON ${UserName}.* TO ${UserName}@localhost IDENTIFIED BY ‘$Pawd’;"
                mysql -uroot -p"$Mysqlpwd" -e "flush privileges;"
                echo "Create  databases $UserName finish."
fi

DomainNamePrefix=$UserName

#useradd加入
useradd $UserName -s /sbin/nologin -d /home/$UserName
mkdir /home/$UserName/public_html
chmod 755 /home/$UserName
chown ${UserName}:${UserName}  /home/$UserName/public_html
usermod  -d /home/$UserName/public_html/ $UserName
echo ${UserName}:$Pawd | chpasswd
gpasswd -a apache $UserName
echo "Create User $UserName finish."

Userid=$(id -u $UserName)
Groupsid=$(id -g $UserName)

echo "Get uid and gid id finish"

Date=$(date -d "today" +"%y%m%d%H%M")
cp  /etc/httpd/conf/httpd.conf  /etc/httpd/conf/httpd.conf.${Date}.bak
echo -ne "
# This Info add time is `date`.
<VirtualHost *:80>
    SuexecUserGroup \"#${Userid}\" \"#${Groupsid}\"
    ServerName $DomainName
    DocumentRoot /home/$UserName/public_html
    ErrorLog logs/${UserName}_error.log
    CustomLog \"|/usr/sbin/cronolog /var/log/httpd/${UserName}_access_%Y%m%d.log\"   combined
    DirectoryIndex index.html index.htm index.php

    <Directory /home/$UserName/public_html>
        Options Indexes IncludesNOEXEC
        allow from all
        AllowOverride None
    </Directory>
</VirtualHost>

" >  /etc/httpd/conf.d/$UserName.conf

service httpd reload
echo "Create Apache user is  finish"
#quota的加入
#edquota -p user $UserName
sed -e "s/model/$UserName/g" /etc/awstats/awstats.model.conf >/etc/awstats/awstats.$UserName.conf
echo "$UserName" >> /etc/vsftpd/chroot_list
echo "Create Quota and awstats is finish"

Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪 ViVi 365Key 网摘 天极网摘 和讯网摘 博拉网 POCO 网摘 饭否 QQ 书签 Digbuzz 我挖网 Mister Wong
Tags:
No comments yet.