#!/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 '$a\PATH=/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 '$a\export 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')