最近经常项目需求开通WEB空间,手动实在太麻烦了,决定写个脚下自动创建MYSQL数据库和用户,NGINX虚似主机,FTP。我平台构架是Nginx+Mysql+Pureftpd+PHP,需要求意是,WEB主程序像nginx或apach
最近经常项目需求开通WEB空间,手动实在太麻烦了,决定写个脚下自动创建MYSQL数据库和用户,NGINX虚似主机,FTP。我平台构架是Nginx+Mysql+Pureftpd+PHP,需要求意是,WEB主程序像nginx或apache它们的域名主机配置文件必须独立分开,FTP支持虚似用户(支持MYSQL)就这两点,以下运行此脚本的效果图,基本输入域名一路回车就完事了,源码中用文指说明的,是修改脚本的重点部分,另新建:QQ群:64280040 如不明白加Q一起讨论下
源脚下载地址:http://down.51cto.com/data/509268
以下是源码代
import os import re import sys import random import string import urllib2 VIRTUAL_HOST='/usr/local/nginx/vhost' //WEB程序虚似主机目录 DIRECTORY='/var/www' //默认的WEB路经VHOST_TEMP='nginx_vhost_conf_template' //创建自己的WEB模板 MYSQL_PATH='/var/lib/mysql' //默义的数据路经路址 LIST=[] MYSQL_USER='root' //MYSQL管理员用户名 MYSQL_PASS='' //MYSQL管理员密码 FTP_PASS = ''.join(random.sample(string.ascii_letters + string.digits , 16)) SQL_PASS = ''.join(random.sample(string.ascii_letters + string.digits , 8)) IP=re.search('\d+\.\d+\.\d+\.\d+',urllib2.urlopen("http://www.whereismyip.com").read()).group(0) NGINX='/shell/nginx.sh restart' //WEB程序重起参数,像APACHE /etc/init.d/httpd restart def create_mysql_exe(EXE): MYSQL_CREA_DATA="CREATE DATABASE %s;"%EXE MYSQL_CREA_USER="grant all on %s.* to %s@localhost identified by '%s';" %(EXE,EXE+'_user',SQL_PASS) MYSQL_DATA_USER= MYSQL_CREA_DATA + MYSQL_CREA_USER MYSQL_USER_PASS='mysql -u%s -p%s -e "%s"'%(MYSQL_USER,MYSQL_PASS,MYSQL_DATA_USER) MYSQL_EXE= MYSQL_USER_PASS MYSQL_PATH_DATA_NAME=MYSQL_PATH+os.sep+EXE if not os.path.isdir(MYSQL_PATH_DATA_NAME): os.system(MYSQL_EXE) else: print 'Error:%s has been exist!' %EXE def permission(file): if os.path.isdir(file): os.chmod(file,256|128|64|32|16|8|4) //设置权限/var/www/xxxx.com 774 os.chown(file,99,2000) //设置/var/www/xxx.com用户和组 else: print "No such file or directory" sys.exit(1) def Change_Name(filter_name): global ADD_NAME do_list=[] for do_name in filter_name.split('.'): if do_name != 'www': if do_name != 'cn': if do_name != 'net': if do_name != 'com': if do_name != 'org': do_list.append(do_name) SIXTY_NAME=''.join(do_list) ADD_NAME=SIXTY_NAME[:9] class vhost: def domain(self): self.DOMAIN_NAME = raw_input('Please input domain:') if len(self.DOMAIN_NAME) < 5: print 'Domain name can\'t be empty or illegal' sys.exit(1) else: for ARG in open(VHOST_TEMP).readlines(): RE_WWW = ARG.replace(DIRECTORY,DIRECTORY+os.sep+self.DOMAIN_NAME) RE_DOMAIN = RE_WWW.replace('domain',self.DOMAIN_NAME) LIST.append(RE_DOMAIN) VHOST_CONF=VIRTUAL_HOST+os.sep+self.DOMAIN_NAME if not os.path.isfile(VHOST_CONF): for info in LIST: open(VHOST_CONF,'a').write(info) else: print 'Error:%s vhost have existed'%self.DOMAIN_NAME sys.exit(1) def directory(self): print "\nPlease input the directory for the domain:"+self.DOMAIN_NAME Default_dir=raw_input('Default directy:%s:'%(DIRECTORY+os.sep+self.DOMAIN_NAME)) if len(Default_dir) == 0: self.WWW_DIR=DIRECTORY+os.sep+self.DOMAIN_NAME os.makedirs(self.WWW_DIR) permission(self.WWW_DIR) else: self.WWW_DIR=Default_dir os.makedirs(Default_dir) permission(Default_dir) def sql(self): Change_Name(self.DOMAIN_NAME) print "\nPlease input the name for the database:" Default_data=raw_input('Default database: %sdb:'%ADD_NAME) if len(Default_data) == 0: self.DATABASE_NAME='%sdb'%ADD_NAME create_mysql_exe(self.DATABASE_NAME) else: self.DATABASE_NAME=Default_data create_mysql_exe(self.DATABASE_NAME) def ftp(self): global FTP_USER FTP_USER='%s_user_ftp'%ADD_NAME print "\nPlease input the name for the ftp:" RE_FTP_USER=raw_input('Default FTP name:%s:'%FTP_USER) if len(RE_FTP_USER) != 0: FTP_USER=RE_FTP_USER FTP_UID='2000' \\FTP用户 FTP_GID='2000' \\FTP组 FTP_DATA_USE='use pureftpd;' FTP_DIR=self.WWW_DIR FTP_USER_ID_DIR="insert into users set User='%s',Password='%s',Uid=%s,Gid=%s,Dir='%s';"%(FTP_USER,FTP_PASS,FTP_UID,FTP_GID,FTP_DIR) MYSQL_USER_PASS='mysql -u%s -p%s -e "%s"'%(MYSQL_USER,MYSQL_PASS,FTP_DATA_USE+FTP_USER_ID_DIR) FTP_EXE=MYSQL_USER_PASS os.system(FTP_EXE) def info(self): if os.system('echo "Mysql_2012"|passwd --stdin mysql') == 0: print "\nDefault Domain: %s"%self.DOMAIN_NAME print "Default IP address: %s"%IP print "Default www: %s"%self.WWW_DIR print "Default database name: %s"%self.DATABASE_NAME print "Default database user: %s passwd: %s" %(ADD_NAME+'_user', SQL_PASS) print "Default ftp user: %s passwd: %s" %(FTP_USER, FTP_PASS) print "Default SSH user: mysql passwd: Mysql_2012\n" os.system(NGINX) V=vhost() V.domain() V.directory() V.sql() V.ftp() V.info()