一键搭建LNMP环境

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一键搭建LNMP环境相关的知识,希望对你有一定的参考价值。

    #!/bin/bash

    #This Shell Is To Install LNMP
    language(){   #判断环境语言
    echo $LANG |grep -q zh
        if [ $? -eq 0 ];then
        return 0
        else
        return 1
        fi
    }
    error_yum(){
    language
        if [ $? -eq 0 ];then
            clear
            echo
            echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            echo "错误:本机YUM不可用,请正确配置YUM后重试."
            echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            echo
            exit
        else
            clear
            echo
            echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            echo "ERROR:Yum is disable,please modify yum repo file then try again."
            echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            echo
            exit
        fi
    }
    #判断本机yum,不可用直接退出脚本!
    #运行脚本请先配置好yum!
    test_yum(){
    repolist=yum repolist 2>/dev/null |awk '/repolist:/{print $2}'|sed 's/,//'
        if [ $repolist -le 0 ];then
        error_yum
        fi
    }
    #安装nginx依赖包
    install_depaned(){
    language
        if [ $? -eq 0 ];then
            echo -en "\033[1;34m正在安装依赖包,请稍后...\033[0m"
        else
            echo -e "\033[1;34mInstalling dependent software,please wait a moment...\033[0m"
        fi
    rpmlist="gcc pcre-devel openssl-devel zlib-devel make"
    for i in $rpmlist
    do
        #判断依赖包是否已经安装
        rpm -q $rpmlist &>/dev/null
        if [ $? -ne 0 ];then
            yum install -y $rpmlist
        fi
    done
    }
    error_nofile(){
    language
    if [ $? -eq 0 ];then
        clear
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo -e "\033[1;34m错误:未找到软件包,请下载软件包至当前目录.\033[0m"
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        exit
    else
        clear
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo -e "\033[1;34mERROR:Not found package in current directory, please download it.\033[0m"
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        exit
    fi
    }

    #安装nginx
    install_nginx(){
    test_yum
    install_depaned
    nginx_version=nginx-1.8.0
    format1=tar.gz
    #判断用户是否存在
    grep -q nginx /etc/passwd
    if [ $? -ne 0 ];then
        useradd -s /sbin/nologin nginx
    fi
    #当前目录下是否有nginx-1.8.0.tar.gz,无则报错退出脚本
    if [ -f ${nginx_version}.${format1} ];then
        tar -xf ${nginx_version}.${format1}
        cd $nginx_version
        ./configure --prefix=/usr/local/nginx --with-http_ssl_module
        make
        make install
        #作符号连接,可用ngnix启动服务或用nginx -s stop/reload停止重启
        ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
        cd ..
    else
        error_nofile
    fi
    }
    #安装php
    install_php(){
    yum install -y php-mysql php
    #判断当前目录有无此软件包,无则报错退出脚本
    if [ -f php-fpm-5.4.16-36.el7_1.x86_64.rpm ];then
        rpm -ivh php-fpm-5.4.16-36.el7_1.x86_64.rpm
    else
        error_nofile
    fi
    }
    #安装mariadb

    install_mariadb(){
    yum -y install mariadb-server mariadb mariadb-devel
    }
    #启动服务
    service_start(){
    nginx
    systemctl start mariadb
    if [ -f /usr/lib/systemd/system/php-fpm.service ];then
        systemctl start php-fpm.service
    fi    
    }

install_nginx
install_php
install_mariadb
service_start

            

以上是关于一键搭建LNMP环境的主要内容,如果未能解决你的问题,请参考以下文章

Linux系统下LNMP一键搭建LinuxPHPMySQL环境(适合新手搭建linux下的web生成环境)

一键搭建LNMP环境

lnmp一键安装报搭建laravel环境报500

centos6:lnmp一键搭建脚本

centos怎么搭建lnmp环境

PHP之开发环境搭建