python安装lnmp

#!/usr/bin/python
# -*- coding:utf-8 -*-
# 注意:本实验用root用户。已经安装python3.6.5  用pycharm运行,首先把nginx安装包放在 /usr/local 下面;mysql安装包放在/root/soft/下面;php7.11安装包不用提前准备,网络下载就行
import subprocess
import sys
import os

info = '''    #定义开头显示的提示选择信息
    ----- Select Install option -----
    1.Install Nginx-1.12.2
    2.Install mysql-5.7.22
    3.Install PHP-7.1.1

    5.Exit Program
    ---------------------------------
    '''
nginx_service = '''
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
'''
my_cnf = '''
[mysqld]
basedir=/usr/local/mysql                           #介质目录
datadir=/usr/local/mysql/data                      #数据目录
port=3306                                          #端口
pid-file = /usr/local/mysql/data/mysqld.pid        #进程id
user = mysql                                       #启动用户
socket=/tmp/mysql.sock                             #sock文件地址
bind-address = 0.0.0.0                             #绑定ip 这里表示绑定所有ip
server-id = 1                                      #用于复制环境钟标识实例,这个在复制环境里唯一
character-set-server = utf8                        #服务端默认字符集,很重要,错误设置会出现乱码
max_connections = 1000                             #允许客户端并发连接的最大数量
max_connect_errors = 6000                          #如果客户端尝试连接的错误数量超过这个参数设置的值,则服务器不再接受新的客户端连接
open_files_limit = 65535                           #操作系统允许MySQL服务打开的文件数量。
table_open_cache = 128                             #所有线程能打开的表的数量
max_allowed_packet = 32M                            #网络传输时单个数据包的大小。
binlog_cache_size = 1M
max_heap_table_size = 1288M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 16M
join_buffer_size = 16M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 4096M
query_cache_limit = 4M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30
log_error = /usr/local/mysql/data/mysql-error.log               #错误日志地址
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /usr/local/mysql/data/mysql-slow.log      #慢查询日志
performance_schema = 0
explicit_defaults_for_timestamp
lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 16350
innodb_buffer_pool_size = 10G
innodb_write_io_threads = 16
innodb_read_io_threads = 16
innodb_thread_concurrency = 32
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 128M
innodb_log_file_size = 1G
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 512
wait_timeout = 256
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
sql_mode='NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,PIPES_AS_CONCAT,ANSI_QUOTES'

[client]
port=3306
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock

#开启快速度导出
[mysqldump]
quick
default-character-set = utf8mb4
max_allowed_packet = 256M

[myisamchk]

key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M


'''

while True:
    print(info)
    n = 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,运行shell命令安装nginx
                    print("创建nginx用户")
                    subprocess.call(["useradd nginx"], shell=True)
                    print("安装依赖环境")
                    subprocess.call([
                        "yum install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel autoconf automake libtool make -y"],
                            shell=True)
                    print("编译安装")
                    subprocess.call([
                        "cd /usr/local && tar -xf nginx-1.12.2.tar.gz && cd nginx-1.12.2 && ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module --with-stream && make && make install"],
                            shell=True)
                    print("设置环境变量")
                    subprocess.call(["echo 'export PATH=$PATH:/usr/local/nginx/sbin' >> /etc/profile"], shell=True)
                    subprocess.call(["source /etc/profile"], shell=True)
                    print("设置配置文件")
                    subprocess.call(["/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf"], shell=True)

                    print("创建开机自启文件")
                    with open("/lib/systemd/system/nginx.service", "a") as f:
                        f.write(my_cnf)
                    f.close()
                    print("设置开机启动")
                    subprocess.call(["systemctl enable nginx.service"], shell=True)
                    print("启动nginx服务")
                    subprocess.call(["systemctl start nginx.service"], shell=True)
                    print("重新加载nginx")
                    subprocess.call(["nginx -s reload"], shell=True)

                if n == 2:  # 编译安装mysql,每个命令都在屏幕上显示;安装包提前放在/root/soft目录下
                    print("remove mariadb")
                    subprocess.call(["yum remove maria* -y"], shell=True)
                    print("清空my.cnf")
                    subprocess.call(["echo > /etc/my.cnf"], shell=True)
                    print("useradd mysql")
                    subprocess.call(["useradd mysql && groupadd mysql && useradd -r -g mysql mysql"], shell=True)
                    print("tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz")
                    subprocess.call(["cd /root/soft/ && tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz"],
                                    shell=True)
                    print("mv /root/soft/mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql")
                    subprocess.call(["cd /root/soft/ && mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql"],
                                    shell=True)
                    print("chown -R mysql:mysql /usr/local/mysql")
                    subprocess.call(["chown -R mysql:mysql /usr/local/mysql"], shell=True)
                    print("初始化mysql")
                    subprocess.call([
                        "cd /usr/local/mysql/ && bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data"],
                            shell=True)
                    print("创建RSA private key")
                    subprocess.call(["cd /usr/local/mysql && bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data"],
                                    shell=True)
                    subprocess.call(["chown -R mysql:mysql /usr/local/mysql/data"], shell=True)

                    print("创建mysql配置文件")
                    with open("/etc/my.cnf", "a") as f:
                        f.write(my_cnf)
                    f.close()
                    print("添加开机启动")
                    subprocess.call(["cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld"], shell=True)
                    print("修改配置文件")
                    subprocess.call(["sed -i '2a basedir=/usr/local/mysql' /etc/init.d/mysqld "], shell=True)
                    subprocess.call(["sed -i '3a datadir=/usr/local/mysql/data' /etc/init.d/mysqld "], shell=True)
                    print("添加环境变量")
                    subprocess.call([
                        "sed -i '$aPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/local/mysql/bin' ~/.bash_profile && source ~/.bash_profile"],
                            shell=True)
                    print("启动mysq")
                    subprocess.call(["systemctl daemon-reload && systemctl start mysqld"], shell=True)
                    print("设置mysql的bin目录连接")
                    subprocess.call(["ln -s /usr/local/mysql/bin/mysql /usr/bin"], shell=True)
                    print("设置开机自启")
                    subprocess.call(["chmod +x /etc/init.d/mysqld && chkconfig --add mysqld"], shell=True)

                if n == 3:
                    print("安装依赖包")
                    subprocess.call(
                            [
                                "yum install openldap openldap-devel epel-release gcc gcc-c++ libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel -y"],
                            shell=True)
                    print("下载安装包、编译安装php")
                    subprocess.call(["cd /usr/local && wget -O php7.tar.gz http://cn2.php.net/get/php-7.1.1.tar.gz/from/this/mirror && tar -xvf php7.tar.gz && cd php-7.1.1/ && ./configure 
--prefix=/usr/local/php 
--with-config-file-path=/etc 
--enable-fpm 
--with-fpm-user=nginx 
--with-fpm-group=nginx 
--enable-inline-optimization 
--disable-debug 
--disable-rpath 
--enable-shared 
--enable-soap 
--with-libxml-dir 
--with-xmlrpc 
--with-openssl 
--with-mcrypt 
--with-mhash 
--with-pcre-regex 
--with-sqlite3 
--with-zlib 
--enable-bcmath 
--with-iconv 
--with-bz2 
--enable-calendar 
--with-curl 
--with-cdb 
--enable-dom 
--enable-exif 
--enable-fileinfo 
--enable-filter 
--with-pcre-dir 
--enable-ftp 
--with-gd 
--with-openssl-dir 
--with-jpeg-dir 
--with-png-dir 
--with-zlib-dir 
--with-freetype-dir 
--enable-gd-native-ttf 
--enable-gd-jis-conv 
--with-gettext 
--with-gmp 
--with-mhash 
--enable-json 
--enable-mbstring 
--enable-mbregex 
--enable-mbregex-backtrack 
--with-libmbfl 
--with-onig 
--enable-pdo 
--with-mysqli=mysqlnd 
--with-pdo-mysql=mysqlnd 
--with-zlib-dir 
--with-pdo-sqlite 
--with-readline 
--enable-session 
--enable-shmop 
--enable-simplexml 
--enable-sockets 
--enable-sysvmsg 
--enable-sysvsem 
--enable-sysvshm 
--enable-wddx 
--with-libxml-dir 
--with-xsl 
--enable-zip 
--with-ldap 
--enable-mysqlnd-compression-support 
--with-pear 
--enable-opcache && make && make install"], shell=True)
                    print("配置环境变量")
                    subprocess.call(["sed -i '$aexport PATH=$PATH:/usr/local/php/bin' /etc/profile && source /etc/profile"], shell=True)
                    print("配置php-fpm")
                    subprocess.call([
                        "cp /usr/local/php-7.1.1/php.ini-production /etc/php.ini && cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf"],
                            shell=True)
                    subprocess.call([
                        "cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf"],
                            shell=True)
                    subprocess.call([
                        "cp /usr/local/php-7.1.1/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm"],
                            shell=True)
                    print("给php启动脚本授权")
                    subprocess.call(["chmod +x /etc/init.d/php-fpm"], shell=True)
                    print("启动php")
                    subprocess.call(["/etc/init.d/php-fpm start"], shell=True)
                    print("设置开机自启")
                    subprocess.call(["chkconfig --add php-fpm"], shell=True)

                if n == 5:  # 退出程序
                    print("Program will be quite!")
                    sys.exit()
                else:
                    print('you select is not correct')
        else:
            print('you should input number')
 
原文地址:https://www.cnblogs.com/effortsing/p/9982028.html