LNMP自动添加vhost脚本(功能蛮强大的)

2018-05-31 11:41:09 浏览数 (1)

需求背景:

服务器上,跑的lamp环境,上面有很多客户的项目,每个项目就是一个网站。 由于客户在不断增加,每次增加一个客户(自动创建密码),就需要配置相应的mysql、ftp以及httpd. 这种工作是重复性非常强的,所以用脚本实现非常合适。

mysql增加的是对应客户项目的数据库、用户、密码,ftp增加的是对应项目的用户、密码(使用vsftpd,虚拟用户模式),httpd就是要增加虚拟主机配置段。


首先需要一个基础的nginx虚拟主机配置文件, 一般情况下,我们配置虚拟主机都是建一个vhost目录, 这里我在 /usr/local/nginx/conf/vhost 下面建了一个dd.conf文件

代码语言:javascript复制
server
{
    listen 80;  
    server_name #host#;
    index index.html index.htm index.php;
    root /data/wwwroot/#host#;

##add php jiexi
location ~ .php$
      {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/#host#.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/#host#$fastcgi_script_name;
      }
}

绞尽脑汁也没有找出来如何能够添加一个新的虚拟机配置文件,在网上找到了灵感!(感谢度娘 1)

在 /usr/local/php-fpm/etc 下面建了一个pp.conf文件

代码语言:javascript复制
[[email protected] etc]# cat pp.conf 
[#php#]
listen = /tmp/#php#.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 50
rlimit_files = 1024

在 /etc/vsftpd/vsftpd_user_conf/ 下面建了一个ftpuser文件

代码语言:javascript复制
[[email protected] vsftpd_user_conf]# cat ftpuser 
local_root=/home/ftpuser/#ftpuser#
anonymous_enable=NO    
write_enable=YES      
local_umask=022     
anon_upload_enable=NO      
anon_mkdir_write_enable=NO    
idle_session_timeout=600   
data_connection_timeout=120    
max_clients=10

注意里面一些关键路径,我用特殊字符组合来表示,这样方便我们添加虚拟注意的时候进行匹配替换。


lnmpvhost.sh 脚本如下:

一定要先满足如下条件:

代码语言:javascript复制
关闭iptables 以及selinux

yum install -y expect
代码语言:javascript复制
#! /bin/bash

##add a new vhost and mysql and ftp(before use this shell script, pls it must stop firewall and selinux services!!)
##written by zhdya_20171016

date=`date  %F_%T`
dd="/usr/local/nginx/conf/vhost/dd.conf"
vhost="/usr/local/nginx/conf/vhost"
host="/data/wwwroot"

##create web's dir and configure nginx and php.
read -p "pls input website like "www.baidu.com": " web

if [ -d $host/$web ]
then
	echo "[warning] The $host/$web already exist, pls check it now!"
	exit
else
	mkdir -p $host/$web
	chmod 755 $host/$web
	cat $dd | sed -e "s:#hosts#:${web}:g"|sed -e "s/#host#/${web}/g" > $vhost/$web.conf
	/usr/sbin/nginx -s reload
	cat /usr/local/php-fpm/etc/pp.conf | sed -e "s:#php#:${web}:g"|sed -e "s/#php#/${web}/g" >> /usr/local/php-fpm/etc/php-fpm.conf
	/etc/init.d/php-fpm reload
	echo "already create the $host/$web, and configure php-fpm, nginx success, pls check it!"
fi

##add a user and check the user already exist or not, and add a new ftp user with password!!
read -p "pls input a user: " u
if cat /etc/passwd | awk -F ':' '{print $1}' |grep "$u"
then
	echo "the user already exist."
else
	m=`mkpasswd -l 10 -c 2 -C 2 -d 2`
	useradd $u
	echo "$m" | passwd --stdin $u >/dev/null 2>&1
	echo "$date  username: $u password: $m" >> /tmp/users.txt
	echo "pls check the user's list file."
	read -p "do you need create ftp also? [y/n] " f
		case $f in
		y|yes)
			echo -e "$un$m" >> /etc/vsftpd/vsftpd_login
			cat /etc/vsftpd/vsftpd_user_conf/ftpuser | sed -e "s:#ftpuser#:${u}:g"|sed -e "s/#ftpuser#/${u}/g" > /etc/vsftpd/vsftpd_user_conf/$u
			mkdir -p /home/ftpuser/$u
			touch /home/ftpuser/$u/$u.txt
			chown -R $u.$u /home/ftpuser/$u
		;;
		n|no)
		echo "you choice no need to create, exit..."
		break
		;;
		*)
		echo "you input was wrong, pls check it!"
		break
		;;
		esac 
fi 

##create a new database
read -p "do you need create a new database for this new vhost? [y/n] " d
	case $d in
	y|yes)
		read -p "please input database:" database
		read -p "please input dbuser:" dbuser
		read -p "please input dbpwd:" dbpwd
		HOSTNAME="127.0.0.1"
		PORT="3306"
		USERNAME="root"
		read -p "input root pwd:" PASSWORD
		
		create_db_sql="create database ${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="create user $dbuser"
		/usr/local/mysql/bin/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="grant 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 'all of things now already done, pls check it!!'
	;;
	n|no)
		echo "ok, finished!! pls check!!"
		exit
	;;
	*)
		echo "you input was wrong, pls check it!"
		break
	;;
	esac

0 人点赞