LNMP環境一鍵部署

最愛喝酸奶發表於2019-01-24

LNMP環境一鍵部署指令碼:

#!/bin/bash
#用於安裝LNMP環境

[ ! -d /software/ ] && mkdir /software

#功能選擇選單
menu(){
        echo
        echo -e "\033[31;32m                              LNMP編譯安裝指令碼                                 \033[0m"
        echo -e "\033[31;32m================================================================================\033[0m"
        echo -e "\033[34m此指令碼能直接在rhel7.5、Centos 7.5上成功執行\033[0m"
        echo "  安裝包版本:  nginx:1.14.0   mysql5.7.23   php7.2.6"
        echo "  相關依賴包:pcre-devel、gd-devel、openssl、openssl-devel、php-mysql、wget  "
        echo "              perl-devel、libxml2-devel bzip2-devel libcurl-devel   "
        echo -e "\033[47;34m------------安裝需聯網下載軟體包,若下載地址失效,需自行更新下載地址------------\033[0m"
        echo -e "\033[31;32m================================================================================\033[0m"
        echo "   nginx安裝目錄:/usr/local/nginx,日誌目錄:/data/logs/nginx/   "
        echo "   mysql安裝目錄:/usr/local/mysql,資料存放目錄:/data/mysql/  "
        echo "   php安裝目錄:/usr/local/php7   "
        echo -e "\033[31;32m================================================================================\033[0m"
        echo -e "\033[34m請選擇:\033[0m"
        echo -e "\033[36m0、安裝依賴包   1、安裝nginx   2、原始碼編譯安裝mysql   3、安裝php  \033[0m"
        echo -e "\033[36m4、整合nginx和php   5、啟動nginx、mysql、php-fpm服務  \033[0m"
        echo -e "\033[36m6、一鍵安裝並部署LNMP   7、退出指令碼\033[0m"
        echo -e "\033[31;32m================================================================================\033[0m"
        echo
        read -p "請輸入數字:0-5[單獨安裝](單獨安裝需要先安裝依賴包),6[一鍵安裝],7[退出指令碼]:"   num
}

#(0)安裝依賴包
install_package(){
        cd /software;echo
        #echo -e "\033[34m==========yum安裝依賴包==========\033[0m"

        #yum groupinstall "Development Tools" -y
	    yum install -y vim-enhanced lrzsz net-tools gcc rsync epel-release wget unzip curl
        yum install -y bzip2-devel openssl-devel gnutls-devel gcc gcc-c++ cmake ncurses-devel bison-devel libaio-devel openldap  openldap-devel
        yum install -y autoconf bison libxml2-devel libcurl-devel libevent libevent-devel gd-devel expat-devel numactl

        #curl -O https://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
        if [ ! -f libmcrypt-2.5.8.tar.gz ]
        then
            wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
        fi

        [ ! -d libmcrypt-2.5.8 ] && tar -zxf libmcrypt-2.5.8.tar.gz
        cd libmcrypt-2.5.8
        ./configure && make && make install

        if [ $? -eq 0 ]
        then
               sleep 3
               echo -e "\033[36m==========依賴包安裝成功==========\033[0m"
          else
               echo -e "\033[31m**********安裝依賴包失敗,請檢查**********\033[0m"
        fi
}

#(1)編譯安裝nginx
install_nginx(){
        cd /software;echo
        #echo -e "\033[34m==========編譯安裝nginx==========\033[0m"

        #1、新建nginx系統使用者
        id nginx &> /dev/null
        [ $? -ne 0 ] && useradd -r -s /sbin/nologin nginx

        #2、新建nginx日誌存放目錄
        [ ! -d /data/logs/nginx/ ] &&  mkdir /data/logs/nginx/ -pv && chown -R  nginx:nginx /data/logs/nginx/

        #3、定義nginx安裝的版本
        NGINX_VERS=nginx-1.14.0

        #4、下載nginx
        if [ ! -f $NGINX_VERS.tar.gz ]
        then
           echo -e "\033[34m==========正在下載nginx原始碼包==========\033[0m"
           curl -O  http://nginx.org/download/$NGINX_VERS.tar.gz
        fi

        #5、解壓下載包
        [ ! -d $NGINX_VERS ] && tar xf $NGINX_VERS.tar.gz

        #6、進入nginx解壓後的目錄
        cd $NGINX_VERS

        #7、編譯安裝
        ./configure --prefix=/usr/local/nginx \
        --user=nginx \
        --group=nginx \
        --http-log-path=/data/logs/nginx/access.log \
        --error-log-path=/data/logs/nginx/error.log \
        --with-http_ssl_module \
        --with-http_realip_module \
        --with-http_flv_module \
        --with-http_mp4_module \
        --with-http_gunzip_module \
        --with-http_gzip_static_module \
        --with-http_image_filter_module \
        --with-http_stub_status_module &&  make && make install

        if [ $? -eq 0 ]
        then
               sleep 3
               echo -e "\033[36m==========nginx編譯安裝成功==========\033[0m"
        else
               echo -e "\033[31m**********nginx編譯安裝失敗,請檢查**********\033[0m"
               exit 1
        fi
	
	    cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running \`nginx -t\` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
	    systemctl enable nginx && systemctl daemon-reload
}

#(2)原始碼編譯安裝mysql
install_mysql(){
        cd /software;echo
        #echo -e "\033[34m==========編譯安裝mysql==========\033[0m"
        VERSION=5.7.23      #mysql版本

        if [ ! -f mysql-$VERSION.tar.gz ]
        then
            #下載mysql
            echo -e "\033[34m==========下載mysql原始碼包==========\033[0m"
            curl -o mysql-$VERSION.tar.gz https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-$VERSION-linux-glibc2.12-x86_64.tar.gz
        fi

        # 1、建立mysql使用者
        id mysql &> /dev/null
        [ $? -ne 0 ] && useradd -r -s /sbin/nologin mysql

        #2、解壓mysql
        echo -e "\033[34m==========解壓mysql...==========\033[0m"

        if [ -f mysql-$VERSION.tar.gz ]
        then
           tar xvf mysql-$VERSION.tar.gz
           mv mysql-$VERSION-* /usr/local/mysql
        fi
	
	    #3、建立資料庫資料存放目錄、安裝目錄
        [ ! -d  /data/mysql/ ] && mkdir -pv /data/mysql/
	    [ ! -f /usr/local/mysql/log/error.log ] && mkdir -pv /usr/local/mysql/log/ && touch /usr/local/mysql/log/error.log
        chown -R mysql:mysql  /data/mysql/
        chown -R mysql:mysql  /usr/local/mysql/

        #4、初始化
	
	    cd /usr/local/mysql
	
        if [ $? -eq 0 ]
        then
            echo -e "\033[34m==========資料庫開始初始化==========\033[0m"
            ./bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/  --datadir=/data/mysql/
	        if [ $? -eq 0 ]
	        then
		        echo -e "\033[36m==========資料庫初始化成功==========\033[0m"
	        else
		        echo -e "\033[31m**********編譯安裝錯誤!初始化失敗**********\033[0m"
            	exit 1
	        fi
        fi

        #5、配置資料庫配置檔案
        cat > /etc/my.cnf << EOF
[client]
port    = 3306
socket = /usr/local/mysql/tmp/mysql.sock

[mysql]
default-character-set=utf8

[mysqld]
default-storage-engine=INNODB
character_set_server=utf8
explicit_defaults_for_timestamp
basedir=/usr/local/mysql/
datadir=/data/mysql/
socket=/usr/local/mysql/tmp/mysql.sock
log_error = /usr/local/mysql/log/error.log

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF

        [ ! -f /etc/init.d/mysql ] && \cp  /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
	    sed -i '46s/basedir=/basedir=\/usr\/local\/mysql/' /etc/init.d/mysql
	    sed -i '47s/datadir=/datadir=\/data\/mysql/' /etc/init.d/mysql
	
        chmod +x /etc/init.d/mysql
	
        #6、配置環境變數
        n=`grep "/usr/local/mysql/bin" /etc/profile |wc -l`
	
	    if [ $n -eq 0 ]
	    then
            echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
            source /etc/profile
        else
        	source /etc/profile
	    fi

        #7、建立日誌目錄、/var/run/mysqld/
        [ ! -d /var/log/mysql/ ] && mkdir /var/log/mysql/ && chown -R mysql:mysql /var/log/mysql/
        [ ! -d /var/run/mysqld/ ] && mkdir /var/run/mysqld/ && chown -R mysql:mysql /var/run/mysqld/
	    [ ! -d /usr/local/mysql/tmp/ ] && mkdir /usr/local/mysql/tmp/ && chown -R mysql:mysql /usr/local/mysql/tmp/
	
	    #8、配置成mysqld服務並啟動
	    cat > /usr/lib/systemd/system/mysqld.service << EOF
[Unit]
Description=MySQL Server
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf

#連線數限制
LimitNOFILE=65535
LimitNPROC=65535

#Restart配置可以在程式被kill掉之後,讓systemctl產生新的程式,避免服務掛掉
#Restart=always
PrivateTmp=false
EOF

	    systemctl daemon-reload && systemctl enable mysqld && systemctl start mysqld && systemctl daemon-reload
	
	    if [ $? -eq 0 ]
	    then
	        echo -e "\033[36m==========mysql安裝成功==========\033[0m"
	    fi	
		
		n=`grep "/usr/local/mysql/bin" /etc/profile |wc -l`
	
	    if [ $n -eq 0 ]
	    then
            echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
            source /etc/profile
        else
            source /etc/profile
	    fi
		
	    mysql -uroot -D mysql -e "UPDATE user SET authentication_string=PASSWORD("123456789") WHERE user='root';"
	    mysql -uroot -e "FLUSH PRIVILEGES;"
	    mysql -uroot -p123456789 -e "grant all privileges on *.* to root@'%'  identified by '123456789';"
	
	    if [ $? -eq 0 ]
        then
           echo -e "\033[36m==========資料庫root密碼修改為123456789成功==========\033[0m"
        else
           echo -e "\033[31m**********資料庫root密碼修改為123456789失敗,為空**********\033[0m"
        fi	
}

#(3)編譯安裝php
install_php(){
        cd /software;echo
        PHPVERS=7.2.6       #PHP版本
        DLOAD_PHP=http://mirrors.sohu.com/php/php-$PHPVERS.tar.gz  #PHP下載地址

        #0 、建立php-fpm使用者,使用者名稱:php-fpm
        id php-fpm &> /dev/null
        [ $? -ne 0 ] && useradd -r -s /sbin/nologin php-fpm

        #1、下載php
        [ ! -f  php-$PHPVERS.tar.gz ] && echo -e "\033[36m正在下載php原始碼包...\033[0m"  && curl -O $DLOAD_PHP

        #2、解壓
        [ ! -d php-$PHPVERS/ ] && tar xf php-$PHPVERS.tar.gz
        echo -e "\033[36m編譯安裝php需要很長時間,請慢慢等待...\033[0m"
        sleep 3
        cd php-$PHPVERS/

        #3、編譯安裝
        ./configure  --prefix=/usr/local/php7 \
        --with-config-file-path=/usr/local/php7/etc \
        --with-mysqli=mysqlnd  \
        --with-pdo-mysql=mysqlnd \
	    --with-mysql=/usr/local/mysql \
        --with-mysql-sock=/usr/local/mysql/tmp/mysql.sock \
        --with-iconv-dir \
        --with-freetype-dir \
        --with-jpeg-dir \
        --with-png-dir \
        --with-zlib \
        --with-bz2 \
        --with-libxml-dir \
        --with-curl \
        --with-gd \
        --with-openssl \
        --with-mhash  \
        --with-xmlrpc \
        --with-pdo-mysql \
        --with-libmbfl \
        --with-onig \
        --with-pear \
        --enable-xml \
        --enable-bcmath \
        --enable-shmop \
        --enable-sysvsem \
        --enable-inline-optimization \
        --enable-mbregex \
        --enable-fpm \
        --enable-mbstring \
        --enable-pcntl \
        --enable-sockets \
        --enable-zip \
        --enable-soap \
        --enable-opcache \
        --enable-pdo \
        --enable-mysqlnd-compression-support \
        --enable-maintainer-zts  \
        --enable-session \
        --with-fpm-user=php-fpm \
        --with-fpm-group=php-fpm  && make -j 2 && make -j 2 install

        if [ $? -eq 0 ]
        then
            echo -e "\033[36m==========php編譯安裝成功==========\033[0m"
        else
            echo -e "\033[31m**********php編譯安裝失敗,請檢查**********\033[0m"
            exit 1
        fi

        #4、配置php-fpm服務檔案
        [ ! -d /usr/local/php7/etc/ ] && mkdir /usr/local/php7/etc/
        \cp php.ini-production  /usr/local/php7/etc/php.ini
        sed -i '/post_max_size/s/8/16/g;/max_execution_time/s/30/300/g;/max_input_time/s/60/300/g;s#\;date.timezone.*#date.timezone \= Asia/Shanghai#g' /usr/local/php7/etc/php.ini
        \cp sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm
        chmod +x /etc/init.d/php-fpm

        #5、將php-fpm新增到系統服務
        chkconfig --add php-fpm
        chkconfig php-fpm on

        #6、php-fpm配置檔案
        cd /usr/local/php7/
        cat > etc/php-fpm.conf << EOF
[global]
pid = /usr/local/php7/var/run/php-fpm.pid
error_log = /usr/local/php7/var/log/php-fpm.log
[www]
listen = 127.0.0.1:9000
listen.mode = 666
listen.owner = nobody
listen.group = nobody
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
EOF

        #7、編譯ldap模組
        echo -e "\033[34m==========安裝ldap模組==========\033[0m"
        cd /software/php-$PHPVERS/ext/ldap
        \cp -af /usr/lib64/libldap* /usr/lib/
        /usr/local/php7/bin/phpize
        [ $? -eq 0 ] && ./configure --with-php-config=/usr/local/php7/bin/php-config && make && make install
        sed -i '/\;extension=bz2/aextension=ldap.so' /usr/local/php7/etc/php.ini

        #8、編譯gettext模組
        echo -e "\033[34m==========安裝gettext模組==========\033[0m"
        cd /software/php-$PHPVERS/ext/gettext
        \cp -af /usr/lib64/libldap* /usr/lib/
        /usr/local/php7/bin/phpize
        [ $? -eq 0 ] && ./configure --with-php-config=/usr/local/php7/bin/php-config && make && make install
        sed -i '/\;extension=bz2/aextension=gettext.so' /usr/local/php7/etc/php.ini
	
		echo -e "\033[36m==========PHP安裝步驟完成==========\033[0m"
}

#(4)整合nginx和php
config_lnmp(){
        #echo -e "\033[34m==========整合nginx與php==========\033[0m"
        cd /usr/local/nginx

        #1、編輯nginx配置檔案
	    cat > conf/nginx.conf << EOF
user nobody nobody;	
worker_processes  1;
error_log /data/logs/nginx/error.log crit;
pid /run/nginx.pid;
worker_rlimit_nofile 51200;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
                      '\$status \$body_bytes_sent "\$http_referer" '
                      '"\$http_user_agent" "\$http_x_forwarded_for"';

    access_log  /data/logs/nginx/access.log  main;

    server_tokens       off;
    sendfile        	on;
    send_timeout        3m;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    client_header_timeout 3m;
    client_body_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;    

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;

    default_type  application/octet-stream;
    include  /usr/local/nginx/conf.d/*.conf;
}
EOF
        #2、將server配置段從nginx.conf分離出來
#        sed -i '/server/,$d' conf/nginx.conf
        [ ! -d conf.d ] && mkdir conf.d	
	
	
        #3、建立conf.d/server.conf檔案整合nginx與php
        cat > conf.d/server.conf << EOF
server {
	listen       80;
	server_name  localhost;
	location / {
		root   /usr/local/nginx/html;
		index  index.php index.html index.htm;
	}
	
	error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        	root   html;
        }	

	location ~ \.php$ {
	root   /usr/local/nginx/html;
	fastcgi_pass   127.0.0.1:9000;
	fastcgi_index  index.php;
	fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
	include        fastcgi_params;
	}
}
EOF

        #4、修改conf/fastcgi_params
        > conf/fastcgi_params
        cat > conf/fastcgi_params << EOF
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       \$query_string;
fastcgi_param  REQUEST_METHOD     \$request_method;
fastcgi_param  CONTENT_TYPE       \$content_type;
fastcgi_param  CONTENT_LENGTH     \$content_length;
fastcgi_param  SCRIPT_FILENAME    \$document_root\$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        \$fastcgi_script_name;
fastcgi_param  REQUEST_URI        \$request_uri;
fastcgi_param  DOCUMENT_URI       \$document_uri;
fastcgi_param  DOCUMENT_ROOT      \$document_root;
fastcgi_param  SERVER_PROTOCOL    \$server_protocol;
fastcgi_param  REMOTE_ADDR        \$remote_addr;
fastcgi_param  REMOTE_PORT        \$remote_port;
fastcgi_param  SERVER_ADDR        \$server_addr;
fastcgi_param  SERVER_PORT        \$server_port;
fastcgi_param  SERVER_NAME        \$server_name;
EOF

        #5、關閉SELinux、防火牆
        setenforce 0 && sed -i 's/enforcing/disabled/g' /etc/selinux/config
        systemctl stop firewalld && systemctl disable firewalld

        #6、建立測試頁,備份nginx預設的頁面

        cat > html/1.php << EOF
<?php
echo "php解析正常";
?>
EOF

        if [ $? -eq 0 ]
        then
            sleep 3
            echo -e "\033[36m==========整合nginx和php成功==========\033[0m"
        else
            echo -e "\033[31m**********整合nginx和php失敗,請檢查**********\033[0m"
            # exit 1
        fi
}

#(5)啟動nginx、mysql、php-fpm服務
start_service(){
	    systemctl daemon-reload && systemctl start nginx 
       
	    if [ $? -eq 0 ]
            then
                echo -e "\033[36m==========nginx服務啟動成功==========\033[0m"
        else
            echo -e "\033[31m**********nginx服務啟動失敗**********\033[0m"
        fi

        systemctl start mysqld  &&  /etc/init.d/php-fpm start

        if [ $? -eq 0 ]
        then
            sleep 3
            echo -e "\033[36m==========mysql、php-fpm服務啟動成功==========\033[0m"
            echo -e "\033[33m檢視埠啟用情況:\033[0m"
            ss -tnl
            echo -e "\033[33m埠:80、3306、9000已啟動!\033[0m"
            echo -e "\033[36m==========編譯安裝lnmp已完成==========\033[0m"
            echo -e "\033[36m==========資料庫root密碼為123456789==========\033[0m"
            echo -e "\033[36m--------開啟瀏覽器輸入你的ip/1.php,看看測試頁--------\033[0m"
        else
             echo -e "\033[31m**********mysql、php-fpm服務啟動失敗**********\033[0m"
        fi
        
        source /etc/profile
}

#指令碼執行入口
run_install(){
        while true;do
        menu

        case $num in
        "0")
                #0、安裝依賴包
                    echo -e "\033[34m==========安裝依賴包==========\033[0m"
                    install_package
                    ;;
        "1")
                #1、編譯安裝nginx
                   echo -e "\033[34m==========編譯安裝nginx==========\033[0m"
                   install_nginx
                   ;;
        "2")
                #2、編譯安裝mysql
                   echo -e "\033[34m==========編譯安裝mysql==========\033[0m"
               	   install_mysql
                   ;;
        "3")    #3、編譯安裝php
                   echo -e "\033[34m==========編譯安裝php==========\033[0m"
                   install_php
                   ;;
        "4")    #4、整合nginx和php
                   echo -e "\033[34m==========整合nginx和php==========\033[0m"
               	   config_lnmp
                   ;;
        "5")    #5、啟動nginx、mysql、php-fpm服務
                   echo -e "\033[34m==========啟動nginx、mysql、php-fpm服務==========\033[0m"
                   start_service
                   ;;
        "6")    #6、一鍵編譯安裝lnmp
                   echo -e "\033[34m==========一鍵編譯安裝並配置lnmp==========\033[0m"
                   install_package
                   install_nginx
                   install_mysql
                   install_php
                   config_lnmp
                   start_service
                   exit 0
                   ;;
        "7")    #7、退出指令碼
                   exit 0
                   ;;
          *)
                   ;;
        esac
        done
}

#呼叫指令碼執行入口
run_install

手動單獨安裝可以參考這裡:https://blog.csdn.net/miss1181248983/article/details/80890649


相關文章