Centos7下一键安装LNMP环境脚本

#!/bin/bash
read -p 'The installation process takes about 5 to 10 minutes, depending on the performance of your server. To confirm the installation of the lnmp environment, press the Enter key.'
function red(){
    echo -e "\t\033[31m$1\033[0m"
}
function green(){
    echo -e "\t\033[32m$1\033[0m"
}
yum clean all &> /dev/null
yum -y install epel-release  &> /dev/null
while :
do
    cat /etc/ld.so.conf | grep lib64 &> /dev/null 
    if [ $? -ne 0 ];then
        echo  '''/usr/local/lib
/usr/local/lib64
/usr/lib
/usr/lib64''' >> /etc/ld.so.conf
        ldconfig -v &> /dev/null
    fi
        libzip_stat=`rpm -qa |grep libzip &> /dev/null && echo 'yes' || echo 'no'`
        mariadb_stat=`rpm -qa |grep mariadb &> /dev/null && echo 'yes' || echo 'no'`
    yum_stat=`yum repolist |tail -1 |awk '{print $2}'`
        if [ $libzip_stat = yes ];then
                rpm -e --nodeps `rpm -qa | grep libzip`
        continue
        elif [ $mariadb_stat = yes ];then
                rpm -e --nodeps `rpm -qa | grep mariadb`
        continue
    elif [ $yum_stat = 0 ];then
        red 'Yum error,Check your yum source!!!(maybe network,yum configure...)'
        exit
    else
            echo -e "\033[05m\033[35mStart testing the environment...\033[0m"
        echo -e "\t\033[32mStart testing the environment...   [ OK ]\033[0m"
        break
    fi        
done
function need_rpm()
{
    yum -y install make
    yum -y install gmake 
    yum -y install gcc
    yum -y install gcc-c++
    yum -y install wget
    yum -y install openssl
    yum -y install openssl-devel
    yum -y install curl
    yum -y install curl-devel
    yum -y install libjpeg
    yum -y install libjpeg-devel
    yum -y install libpng
    yum -y install libpng-devel
    yum -y install freetype
    yum -y install freetype-devel
    yum -y install pcre
    yum -y install pcre-devel
    yum -y install libxslt
    yum -y install libxslt-devel
    yum -y install bzip2
    yum -y install bzip2-devel
    yum -y install libxml2
    yum -y install libxml2-devel
}
function down_app()
{
        wget -P /lnmp/ https://nginx.org/download/nginx-1.14.2.tar.gz   &> /dev/null && echo -e "\t\033[32mNginx Download completed!\033[0m" || echo -e "\t\033[31mNginx Download Faild!\033[0m"
        wget -P /lnmp/ http://cn2.php.net/get/php-7.3.0.tar.gz/from/this/mirror   &> /dev/null && echo -e "\t\033[32mPHP Download completed!\033[0m" || echo -e "\t\033[31mPHP Download Faild!\033[0m"  
        wget -P /lnmp/ https://nih.at/libzip/libzip-1.2.0.tar.gz   &> /dev/null && echo -e "\t\033[32mLibzip Download completed!\033[0m" ||  echo -e "\t\033[31mLibzip Download Faild!\033[0m"
    wget -P /lnmp/ https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz    &> /dev/null && green 'Mysql Download completed!' || red 'Mysql Download Faild!'
}

function nginx_install_configure()
{
    id nginx &> /dev/null && userdel nginx
    useradd -M -s /sbin/nologin nginx &>/dev/null
    cd /lnmp / && tar -xf nginx-1.14.2.tar.gz && cd nginx-1.14.2/    
    ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-stream &>> /tmp/.nginx.configure.log
    if [ $? -ne 0 ];then
        red 'Nginx configure error!!! See /tmp/.nginx.configure.log' 
        exit
    else
        echo ''' Configuration summary:
              nginx path prefix: "/usr/local/nginx"
               nginx binary file: "/usr/local/nginx/sbin/nginx"
               nginx modules path: "/usr/local/nginx/modules"
               nginx configuration prefix: "/usr/local/nginx/conf"
               nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
               nginx pid file: "/usr/local/nginx/logs/nginx.pid"
               nginx error log file: "/usr/local/nginx/logs/error.log"
               nginx http access log file: "/usr/local/nginx/logs/access.log"
               nginx http client request body temporary files: "client_body_temp"
               nginx http proxy temporary files: "proxy_temp"
               nginx http fastcgi temporary files: "fastcgi_temp"
               nginx http uwsgi temporary files: "uwsgi_temp"
               nginx http scgi temporary files: "scgi_temp"
            '''
    fi
    make &>> /tmp/.nginx.make.log
    if [ $? -ne 0 ];then
        red 'Nginx make error!!! See /tmp/.nginx.make.log'
    fi
    make install &>> /tmp/.nginx.makeinstall.log
    if [ $? -ne 0 ];then
        red 'Nginx make install error!!! See /tmp/.nginx.makeinstall.log'
    fi    

    cd /usr/local/nginx/
    cp -p conf/nginx.conf conf/nginx.conf.bak
    sed -i '65,68s/#//g' conf/nginx.conf
    sed -i '70,71s/#//g' conf/nginx.conf
    sed -i 's/fastcgi_params/fastcgi.conf/g' conf/nginx.conf
    
    green 'Nginx installed and configure successful.'
    /usr/local/nginx/sbin/nginx -t &> /dev/null && /usr/local/nginx/sbin/nginx
    netstat -nutlp |grep 80 &> /dev/null
    if [ $? -ne 0 ];then
        red 'Nginx start error!!!'
    else
        green 'Nginx started.'
    fi
}


function mysql_install_configure()
{
        id mysql &> /dev/null && userdel mysql
        useradd -M -s /sbin/nologin mysql &> /dev/null
        cd /lnmp/
        tar -xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
        mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql
        cd /usr/local/mysql/
        mkdir data && chown -R mysql:mysql /usr/local/mysql/
        ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize &>> /tmp/.mysql.initialize.log
        if [ $? -ne 0 ];then
                red 'Mysql initialize error!!! See /tmp/.mysql.initialize.log'
    else
        pass=`tail -1 /tmp/.mysql.initialize.log | awk '{print $NF}'`
        green 'Mysql initialize successful.)'
        green  "Initial password: $pass"
        echo ''' Configuration summary:
              Mysql path prefix: "/usr/local/mysql"
              Mysql data path: "/usr/local/mysql/data"
            '''
        fi
        ./support-files/mysql.server start &>> /tmp/.mysql.start.log
        netstat -nutlp | grep mysql &> /dev/null
        if [ $? -ne 0 ];then
                red  'Mysql start error!!! See /tmp/.mysql.start.log'
        else
                green  'Mysql started.'
        fi
}


function libzip_install_configure()
{
    cd /lnmp/
    tar -xf libzip-1.2.0.tar.gz
    cd libzip-1.2.0
    ./configure &> /dev/null
    make    &> /dev/null
    make install    &> /dev/null
    cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
    green 'Libzip installed.'
}


function php_install_configure()
{
        cd /lnmp/
        tar -xf mirror
        cd  php-7.3.0/
        while :
        do
                ./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip  &>> /tmp/.php.configure.log
                if [ $? -ne 0 ];then
                        red 'PHP configure error!!! See /tmp/.php.configure.log'
                           exit 
                else
                        green 'PHP configure is ok'
                   while :
                   do
                           make  &>> /tmp/.php.make.log
                           if [ $? -ne 0 ];then
                                   red 'PHP make error!!! See /tmp/.php.make.log'
                                      exit 
                           else
                                   green 'PHP make is ok'
                        while :
                        do
                        cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
                                make install &>> /tmp/.php.makeinstall.log
                                if [ $? -ne 0 ];then
                                        red 'PHP makeinstall error!!! See /tmp/.php.makeinstall.log'
                            exit
                                else
                                        green 'PHP Installed.'
                            break
                                fi
                        done
                    break
                           fi
                   done
            break
                fi
        done

        cp php.ini-development /usr/local/php/lib/php.ini
        cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
        cp sapi/fpm/php-fpm /usr/local/bin
        sed -i 's/; cgi.fix_pathinfo=1/ cgi.fix_pathinfo=0/g' /usr/local/php/lib/php.ini
        groupadd www-data &> /dev/null
        useradd -g www-data www-data &> /dev/null
        cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
        sed -i 's/user = nobody/user = www-data/g' /usr/local/php/etc/php-fpm.d/www.conf
        sed -i 's/group = nobody/group = www-data/g' /usr/local/php/etc/php-fpm.d/www.conf
        /usr/local/bin/php-fpm
        netstat -nutlp |grep 9000
        if [ $? -ne 0 ];then
                red 'PHP-FPM start error!!!'
        else
                green 'PHP-FPM started'
        fi
}




#start execute
echo -e "\033[05m\033[35mInstall dependent software...\033[0m"
need_rpm &> /dev/null
green "Install dependent software...\t\t[ OK ]" 
echo -e "\033[05m\033[35mStart download apps...\033[0m"
down_app
echo -e "\033[05m\033[35mStart install nginx and configure it...\033[0m"
nginx_install_configure
echo -e "\033[05m\033[35mStart install mysql and configure it...\033[0m"
mysql_install_configure
echo -e "\033[05m\033[35mStart install Libzip and configure it...\033[0m"
libzip_install_configure
echo -e "\033[05m\033[35mStart install php and configure it...\033[0m"
php_install_configure

/usr/local/nginx/sbin/nignx -s stop &> /dev/null && /usr/local/nginx/sbin/nginx 
echo '''
<?php
    phpinfo();
?>''' >> /usr/local/nginx/html/test.php
curl http://127.0.0.1/test.php
if [ $? -eq '0'];then
    green 'LNMP is ok !!!'
else
    red 'Access PHP  Faild!!!'
fi
    原文作者:CastlePeak
    原文地址: https://segmentfault.com/a/1190000018141298
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞