Recently has been learning python, things more, time continues to be longer, in order to be able to learn, think of the original shell one-click installation Deployment Lnmp script, since the shell can write, Python will also be able to write, with the knowledge to write the following version, This may not be the optimal version, so when you learn more things, you can optimize and upgrade!
Environment Introduction:
Python 2.6.6
Centos 6.5
Nginx 1.10.1
MySQL 5.6.34
PHP 5.3.3
The code is as follows:
#!/bin/env python
Import OS
Import Sys
def enter (i): #定义一个函数, pass a parameter as the counter
Info= ' #定义开头显示的提示选择信息
-----Select Install Option-----
1.Install Nginx-1.10.1
2.Install Mysql-5.5.6.34
3.Install PHP-5.3.3
4.Install Required Components #安装必备组件
5.Exit Program
---------------------------------
‘‘‘
Print Info
N=raw_input (' Input your Select: ') #接收一个输入数值
If N.isdigit (): #判断是否是数字
N=int (n) #如果是就转换成整型, the Raw_input receive type is string-by-default
If N<=5 and n>=1: #数字必须在可选范围之内
If not Os.path.isdir ('/data '): #判断是否存在/data Directory
Os.mkdir ('/data ') #不存在就创建
Else
If n==1: #如果选的是1
Os.system (' yum-y install Nginx ') #运行shell命令安装nginx
Enter (i) #装完返回继续选择
If n==2: #编译安装mysql, each command is displayed on the screen
print "Useradd MySQL"
Os.system (' Useradd MySQL ')
Print "Tar zxf/root/soft/mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz"
Os.system (' Tar zxf/root/soft/mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz ')
Print "Mv/root/soft/mysql-5.6.34-linux-glibc2.5-x86_64/data/mysql"
Os.system (' Mv/root/soft/mysql-5.6.34-linux-glibc2.5-x86_64/data/mysql ')
Print "Chown-r mysql.mysql/data/mysql/"
Os.system (' Chown-r mysql.mysql/data/mysql/')
Print "/bin/cp/root/soft/my.cnf/etc"
Os.system (' mv/root/soft/my.cnf/etc ')
Print "cd/data/mysql/&&scripts/mysql_install_db--user=mysql--basedir=/data/mysql--datadir=/data/mysql/ data/"
Os.system (' cd/data/mysql/&&scripts/mysql_install_db--user=mysql--basedir=/data/mysql--datadir=/data/ mysql/data/')
Print "Sed-i ' s/path= $PATH: $HOME \/bin/path= $PATH: $HOME \/bin:\/data\/mysql\/bin:\/data\/mysql\/lib/'/root/.bash_ Profile
Os.system (' sed-i ' s/path=\ $PATH: \ $HOME \/bin/path=\ $PATH: \ $HOME \/bin:\/data\/mysql\/bin:\/data\/mysql\/lib/"/ Root/.bash_profile ')
Print "Source/root/.bash_profile"
Os.system (' Source/root/.bash_profile ')
Print "CD/DATA/MYSQL/&&CP support-files/mysql.server/etc/init.d/mysql"
Os.system (' CD/DATA/MYSQL/&&CP support-files/mysql.server/etc/init.d/mysql ')
Print "Service MySQL start"
Os.system (' service MySQL start ')
print "Chkconfig--add MySQL"
Os.system (' chkconfig--add MySQL ')
Enter (i)
If n==3: #如果是编译安装PHP, select 4 to install the necessary components first
Print "Please install Required befor install php!"
Os.system (' yum-y install php-tidy php-common php-devel php-fpm php-mysql php-mysql php-odbc php-pear php-xml php-xmlrpc p HP lighttpd-fastcgi php-cli php-gd php-imap php-ldap ')
Enter (i)
If n==4:
Os.system (' yum-y install epel-release gcc gcc-c++ libxml2 libxml2-devel bzip2 bzip2-devel libmcrypt Libmcrypt-devel opens SL openssl-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel readline readline-devel libxslt-devel perl Perl-devel psmisc.x86_64 recode recode-devel libtidy libtidy-devel ')
Enter (i)
If n==5: #退出程序
Print "program would be quite!"
Sys.exit ()
else: #对应输入错数字, exit the program if the number of input errors exceeds 3 times
I-=1
If i==0:
Print "Input error times too many! Program would be quite! "
Sys.exit ()
print "Input error! Please input again! "
Enter (i)
else: #对应输入如果不是数字
I-=1
If i==0:
Print "Input error times too many! Program would be quite! "
Sys.exit ()
print "Input error! Please input again! "
Enter (i)
I=3 #定义计数器
Enter (i) #函数调用, passed in parameter
This article is from "Linux Operations Technology" blog, please be sure to keep this source http://forall.blog.51cto.com/12356505/1953669
Python implements a one-click installation Deployment LNMP Environment