最近一直在学Python,东西比较多,时间持续的也比较长,为了能够学以致用,想到了原来写过的shell一键安装部署LNMP脚本,既然shell能写,Python也一定能写,就用学到的知识写了下面这个版本,这可能并不是最优版本,等学到更多东西的时候再进行优化升级!
环境介绍:
Python 2.6.6
Centos 6.5
nginx 1.10.1
mysql 5.6.34
php 5.3.3
代码如下:
#!/bin/env python
import os
import sys
def enter(i): #定义一个函数,传递一个参数,用作计数器
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) #如果是就转换成整型,raw_input接收类型默认是字符串型
if n<=5 and="" n="">=1: #数字必须在可选范围之内
if not os.path.isdir('/data'): #判断是否存在/data目录
os.mkdir('/data') #不存在就创建
else:
if n==1: #如果选的是1
os.system('yum -y install nginx') #运行shell命令安装nginx
enter(i) #装完返回继续选择
if n==2: #编译安装mysql,每个命令都在屏幕上显示
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,要先选4进行必要组件安装
print "please install Required Components 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 php 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 openssl 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 will be quite!"
sys.exit()
else: #对应输入错数字,如果输入错误次数超过3次就退出程序
i-=1
if i==0:
print "Input error times too many! Program will 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 will be quite!"
sys.exit()
print "input error! please input again!"
enter(i)
i=3 #定义计数器
enter(i) #函数调用,传入参数