使用 shell 脚本实现 LANMP 一键安装

使用 shell 脚本来实现 LANMP 系统的一键安装。使用的操作系统是 CentOS 6 ,不区分 32 位和 64 位,要求机器可以连通互联网。支持 LAMP 和 LNMP ,MySQL 支持 5.1 和 5.6 两个版本, php 支持 5.3 和 5.6 两个版本, apache 2.2 ,nginx 1.8。

代码如下:

#!/bin/bash
echo "It will install lamp or lnmp."
sleep 1

# get the archive of the system: i686 or x86_64.
ar=`arch`

# check last command is OK or Not.
function check_ok()
{
    if [ $? != 0 ];then
        echo "Error, Check the error log."
        exit 1
    fi
}

# close seliux
function close_selinux()
{
    sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/‘ /etc/selinux/config
    selinux_s=`getenforce`
    if [ $selinux_s == "Enforcing"  -o $selinux_s == "enforcing" ];then
        setenforce 0
    fi
}

# close iptables
function clear_iptables()
{
    iptables-save > /etc/sysconfig/iptables_`date +%s`
    iptables -F
    service iptables save
}

# if the packge installed ,then omit.
function myum() {
    if ! rpm -qa|grep -q "^$1" ;then
        yum install -y $1
        check_ok
    else
        echo $1 already installed.
    fi
}

## install some packges.
function install_pre_packages
{
    for p in gcc wget perl perl-devel libaio libaio-devel pcre-devel zlib-devel ;do
        myum $p
    done
}
install_pre_packages

# install epel repo.
function install_epel()
{
    if rpm -qa epel-release >/dev/null ;then
        rpm -e epel-release
    fi
    if ls /etc/yum.repos.d/epel-6.repo* >/dev/null 2>&1 ;then
        /bin/rm -f /etc/yum.repos.d/epel-6.repo*
    fi
    wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo
}
install_epel

# function of installing mysqld.
function install_mysqld() {
    echo "Chose the version of mysql."
    select mysql_v in 5.1 5.6
    do
        case $mysql_v in
            5.1)
                cd /usr/local/src
                [ -f mysql-5.1.72-linux-$ar-glibc23.tar.gz ] || wget                 http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.72-linux-$ar-glibc23.tar.gz
                tar zxf mysql-5.1.72-linux-$ar-glibc23.tar.gz
                check_ok
                [ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_`date +%s`
                /bin/mv mysql-5.1.72-linux-$ar-glibc23 /usr/local/mysql
                check_ok
                if ! grep ‘^mysql:‘ /etc/passwd ;then
                    useradd -M mysql -s /sbin/nologin
                    check_ok
                fi
                myum compat-libstdc++-33
                [ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_`date +%s`
                mkdir -p /data/mysql
                chown -R mysql:mysql /data/mysql
                cd /usr/local/mysql
                ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
                check_ok
                /bin/cp support-files/my-huge.cnf /etc/my.cnf
                check_ok
                sed -i ‘/^\[mysqld\]$/a\datadir = /data/mysql‘ /etc/my.cnf
                /bin/cp support-files/mysql.server /etc/init.d/mysqld
                sed -i ‘s#^datadir=#datadir=/data/mysql#‘ /etc/init.d/mysqld
                chmod 755 /etc/init.d/mysqld
                chkconfig --add mysqld
                chkconfig mysqld on
                service mysqld start
                check_ok
                break
                ;;
            5.6)
                cd /usr/local/src
                [ -f mysql-5.6.35-linux-glibc2.5-$ar.tar.gz ] || wget                 http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-$ar.tar.gz
                tar zxf mysql-5.6.35-linux-glibc2.5-$ar.tar.gz
                check_ok
                [ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_bak
                mv mysql-5.6.35-linux-glibc2.5-$ar /usr/local/mysql
                if ! grep ‘^mysql:‘ /etc/passwd
                then
                    useradd -M mysql -s /sbin/nologin
                fi
                myum compat-libstdc++-33
                [ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_bak
                mkdir -p /data/mysql
                chown -R mysql:mysql /data/mysql
                cd /usr/local/mysql
                ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
                check_ok
                /bin/cp support-files/my-default.cnf /etc/my.cnf
                check_ok
                sed -i ‘/^\[mysqld\]$/a\datadir = /data/mysql‘ /etc/my.cnf
                /bin/cp support-files/mysql.server /etc/init.d/mysqld
                sed -i ‘s#^datadir=#datadir=/data/mysql#‘ /etc/init.d/mysqld
                chmod 755 /etc/init.d/mysqld
                chkconfig --add mysqld
                chkconfig mysqld on
                service mysqld start
                check_ok
                break
                ;;
             *)
                echo "only 1(5.1) or 2(5.6)"
                exit 1
                ;;
        esac
    done
}

# function of install httpd.
function install_httpd() {
    echo "Install apache version 2.2."
    cd /usr/local/src
    [ -f httpd-2.2.31.tar.gz ] || wget  http://mirror.bit.edu.cn/apache/httpd/httpd-2.2.31.tar.gz
    tar zxf  httpd-2.2.31.tar.gz && cd httpd-2.2.31
    check_ok
    ./configure     --prefix=/usr/local/apache2     --with-included-apr     --enable-so     --enable-deflate=shared     --enable-expires=shared     --enable-rewrite=shared     --with-pcre
    check_ok
    make && make install
    check_ok
}
# function of install lamp‘s php.
function install_php() {
    echo -e "Install php.\nPlease chose the version of php."
    select php_v in 5.4 5.6
    do
        case $php_v in
            5.4)
                cd /usr/local/src/
                [ -f php-5.4.45.tar.bz2 ] || wget                 ‘http://cn2.php.net/get/php-5.4.45.tar.bz2/from/this/mirror‘ -O php-5.4.45.tar.bz2
                tar jxf php-5.4.45.tar.bz2 && cd php-5.4.45
                for p in openssl-devel bzip2-devel                 libxml2-devel curl-devel libpng-devel                 libjpeg-devel freetype-devel libmcrypt-devel                libtool-ltdl-devel perl-devel
                do
                    myum $p
                done
                check_ok
                ./configure                 --prefix=/usr/local/php                 --with-apxs2=/usr/local/apache2/bin/apxs                 --with-config-file-path=/usr/local/php/etc                  --with-mysql=/usr/local/mysql                 --with-libxml-dir                 --with-gd                 --with-jpeg-dir                 --with-png-dir                 --with-freetype-dir                 --with-iconv-dir                 --with-zlib-dir                 --with-bz2                 --with-openssl                 --with-mcrypt                 --enable-soap                 --enable-gd-native-ttf                 --enable-mbstring                 --enable-sockets                 --enable-exif                 --disable-ipv6
                check_ok
                make && make install
                check_ok
                [ -f /usr/local/php/etc/php.ini ] || /bin/cp php.ini-production                     /usr/local/php/etc/php.ini
                break
                ;;
            5.6)
                cd /usr/local/src/
                [ -f php-5.6.6.tar.gz ] || wget http://mirrors.sohu.com/php/php-5.6.6.tar.gz
                tar zxf php-5.6.6.tar.gz &&   cd php-5.6.6
                for p in openssl-devel bzip2-devel                 libxml2-devel curl-devel libpng-devel                 libjpeg-devel freetype-devel libmcrypt-devel                libtool-ltdl-devel perl-devel
                do
                    myum $p
                done
                ./configure                 --prefix=/usr/local/php                 --with-apxs2=/usr/local/apache2/bin/apxs                 --with-config-file-path=/usr/local/php/etc                  --with-mysql=/usr/local/mysql                 --with-libxml-dir                 --with-gd                 --with-jpeg-dir                 --with-png-dir                 --with-freetype-dir                 --with-iconv-dir                 --with-zlib-dir                 --with-bz2                 --with-openssl                 --with-mcrypt                 --enable-soap                 --enable-gd-native-ttf                 --enable-mbstring                 --enable-sockets                 --enable-exif                 --disable-ipv6
                check_ok
                make && make install
                check_ok
                [ -f /usr/local/php/etc/php.ini ] || /bin/cp php.ini-production                     /usr/local/php/etc/php.ini
                break
                ;;
            *)
                echo "only 1(5.4) or 2(5.6)"
                ;;
        esac
    done
}
##function of apache and php configue.
function join_apa_php() {
    sed -i ‘/AddType .*.gz .tgz$/a\AddType application\/x-httpd-php .php‘         /usr/local/apache2/conf/httpd.conf
    check_ok
    sed -i ‘s/DirectoryIndex index.html/DirectoryIndex index.php index.html index.htm/‘         /usr/local/apache2/conf/httpd.conf
    check_ok
    echo -e "<?php\n    phpinfo();\n?>" > /usr/local/apache2/htdocs/index.php
    if /usr/local/php/bin/php -i |grep -iq ‘date.timezone => no value‘ ;then
        sed -i ‘/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"‘  /usr/local/php/etc/php.ini
    fi
    /usr/local/apache2/bin/apachectl restart
    check_ok
}

# function of check service is running or not, example nginx, httpd, php-fpm.
function check_service() {
    if [ "$1" == "phpfpm" ];then
        s="php-fpm"
    else
        s=$1
    fi
    n=`ps aux |grep "$s"|wc -l`
    if [ $n -gt 1 ];then
        echo "$1 service is already started."
    else
        if [ -f /etc/init.d/$1 ];then
            /etc/init.d/$1 start
            check_ok
        else
            install_$1
        fi
    fi
}

# function of install lamp
function lamp() {
    check_service mysqld
    check_service httpd
    install_php
    join_apa_php
    echo "LAMP done,Please use ‘http://your ip/index.php‘ to access."
}

# function of install nginx
function install_nginx()
{
    cd /usr/local/src
    [ -f nginx-1.8.0.tar.gz ] || wget http://mirrors.sohu.com/nginx/nginx-1.8.0.tar.gz
    tar zxf nginx-1.8.0.tar.gz
    cd nginx-1.8.0
    myum pcre-devel
    ./configure --prefix=/usr/local/nginx
    check_ok
    make && make install
    check_ok
    if [ -f /etc/init.d/nginx ];then
        /bin/mv /etc/init.d/nginx  /etc/init.d/nginx_`date +%s`
    fi
    curl http://www.apelearn.com/study_v2/.nginx_init  -o /etc/init.d/nginx
    check_ok
    chmod 755 /etc/init.d/nginx
    chkconfig --add nginx
    chkconfig nginx on
    curl http://www.apelearn.com/study_v2/.nginx_conf -o /usr/local/nginx/conf/nginx.conf
    check_ok
    service nginx start
    check_ok
    echo -e "<?php\n    phpinfo();\n?>" > /usr/local/nginx/html/index.php
    check_ok
}

# function of install php-fpm
function install_phpfpm() {
    echo -e "Install php.\nPlease chose the version of php."
    select php_v in 5.4 5.6
    do
        case $php_v in
            5.4)
                cd /usr/local/src/
                [ -f php-5.4.45.tar.bz2 ] || wget                     ‘http://cn2.php.net/get/php-5.4.45.tar.bz2/from/this/mirror‘ -O php-5.4.45.tar.bz2
                tar jxf php-5.4.45.tar.bz2 && cd php-5.4.45
                for p in  openssl-devel bzip2-devel                 libxml2-devel curl-devel libpng-devel                 libjpeg-devel freetype-devel libmcrypt-devel                libtool-ltdl-devel perl-devel
                do
                    myum $p
                done
                if ! grep -q ‘^php-fpm:‘ /etc/passwd ;then
                    useradd -M -s /sbin/nologin php-fpm
                    check_ok
                fi
                ./configure                 --prefix=/usr/local/php-fpm                 --with-config-file-path=/usr/local/php-fpm/etc                 --enable-fpm                 --with-fpm-user=php-fpm                 --with-fpm-group=php-fpm                 --with-mysql=/usr/local/mysql                 --with-mysql-sock=/tmp/mysql.sock                 --with-libxml-dir                 --with-gd                 --with-jpeg-dir                 --with-png-dir                 --with-freetype-dir                 --with-iconv-dir                 --with-zlib-dir                 --with-mcrypt                 --enable-soap                 --enable-gd-native-ttf                 --enable-ftp                 --enable-mbstring                 --enable-exif                 --enable-zend-multibyte                 --disable-ipv6                 --with-pear                 --with-curl                 --with-openssl
                check_ok
                make && make install
                check_ok
                [ -f /usr/local/php-fpm/etc/php.ini ] || /bin/cp php.ini-production                     /usr/local/php-fpm/etc/php.ini
                if /usr/local/php-fpm/bin/php -i |grep -iq ‘date.timezone => no value‘
                then
                    sed -i ‘/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"‘                         /usr/local/php-fpm/etc/php.ini
                    check_ok
                fi
                [ -f /usr/local/php-fpm/etc/php-fpm.conf ] || curl                     http://www.apelearn.com/study_v2/.phpfpm_conf -o /usr/local/php-fpm/etc/php-fpm.conf
                [ -f /etc/init.d/phpfpm ] || /bin/cp sapi/fpm/init.d.php-fpm /etc/init.d/phpfpm
                chmod 755 /etc/init.d/phpfpm
                chkconfig phpfpm on
                service phpfpm start
                check_ok
                break
                ;;
            5.6)
                cd /usr/local/src/
                [ -f php-5.6.6.tar.gz ] || wget http://mirrors.sohu.com/php/php-5.6.6.tar.gz
                tar zxf php-5.6.6.tar.gz &&   cd php-5.6.6
                for p in  openssl-devel bzip2-devel                 libxml2-devel curl-devel libpng-devel                 libjpeg-devel freetype-devel libmcrypt-devel                libtool-ltdl-devel perl-devel
                do
                    myum $p
                done
                if ! grep -q ‘^php-fpm:‘ /etc/passwd
                then
                    useradd -M -s /sbin/nologin php-fpm
                fi
                check_ok
                ./configure                 --prefix=/usr/local/php-fpm                 --with-config-file-path=/usr/local/php-fpm/etc                 --enable-fpm                 --with-fpm-user=php-fpm                 --with-fpm-group=php-fpm                 --with-mysql=/usr/local/mysql                 --with-mysql-sock=/tmp/mysql.sock                 --with-libxml-dir                 --with-gd                 --with-jpeg-dir                 --with-png-dir                 --with-freetype-dir                 --with-iconv-dir                 --with-zlib-dir                 --with-mcrypt                 --enable-soap                 --enable-gd-native-ttf                 --enable-ftp                 --enable-mbstring                 --enable-exif                 --disable-ipv6                 --with-pear                 --with-curl                 --with-openssl
                check_ok
                make && make install
                check_ok
                [ -f /usr/local/php-fpm/etc/php.ini ] || /bin/cp php.ini-production                     /usr/local/php-fpm/etc/php.ini
                if /usr/local/php-fpm/bin/php -i |grep -iq ‘date.timezone => no value‘
                then
                    sed -i ‘/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"‘                         /usr/local/php-fpm/etc/php.ini
                    check_ok
                fi
                [ -f /usr/local/php-fpm/etc/php-fpm.conf ] || curl                 http://www.apelearn.com/study_v2/.phpfpm_conf -o /usr/local/php-fpm/etc/php-fpm.conf
                check_ok
                [ -f /etc/init.d/phpfpm ] || /bin/cp sapi/fpm/init.d.php-fpm /etc/init.d/phpfpm
                chmod 755 /etc/init.d/phpfpm
                chkconfig phpfpm on
                service phpfpm start
                check_ok
                break
                ;;
            *)
                echo ‘only 1(5.4) or 2(5.6)‘
                ;;
        esac
    done
}

##function of install lnmp
function lnmp()
{
    check_service mysqld
    check_service nginx
    check_service phpfpm
    echo "The lnmp done, Please use ‘http://your ip/index.php‘ to access."
}

read -p "Please chose which type env you install, (lamp|lnmp)? " t
case $t in
    lamp)
        lamp
        ;;
    lnmp)
        lnmp
        ;;
    *)
        echo "Only ‘lamp‘ or ‘lnmp‘ your can input."
        ;;
esac
时间: 2024-10-25 17:39:34

使用 shell 脚本实现 LANMP 一键安装的相关文章

shell编程之【一键安装 lanmp】

脚本说明 本脚本可以实现一键安装 lamp 或者 lnmp,具体要求如下: 1.要求是一台可以上外网的新机器 2.支持 centos6 系统,不区分32位,64位 3.支持 lamp 和 lnmp 4.mysql 支持 5.1 和 5.6 两个版本 5.php 支持 5.4 和 5.6 两个版本 6.支持 apache2.2 7.支持 nginx1.8 脚本实现 [[email protected] ~]# cd /usr/local/sbin/ [[email protected] sbin]

Ubuntu系统编写shell脚本程序一次安装多个软件包

编写shell脚本程序一次安装多个软件,主要用于一些软件依赖环境配置. 1.shell脚本程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使用/bin/sh来执行程序. 2.当编辑好脚本后,还必须使其可执行. 使脚本可执行: chmod +x filename 3.然后,您可以通过输入: ./filename 来执行您的脚本. 如下: 下面是我安装NS3之前,安装NS3依赖的多个软件包: 1.在vi中编

配置lanmp一键安装脚本

#!/bin/bash echo "It will install lamp or lnmp." sleep 1 ##check last command is OK or not. check_ok() { if [ $? != 0 ] then echo "Error, Check the error log." exit 1 fi } ##get the archive of the system,i686 or x86_64. ar=`arch` ##clo

wdcp的环境搭建shell脚本分析--lanmp.sh---lib/common.conf

#分析文件lanmp.sh,在其头部引入了一系列的函数文件 . lib/common.conf . lib/common.sh . lib/mysql.sh . lib/apache.sh . lib/nginx.sh . lib/php.sh . lib/na.sh . lib/libiconv.sh . lib/eaccelerator.sh . lib/zend.sh . lib/pureftp.sh . lib/pcre.sh . lib/webconf.sh . lib/service

使用Shell脚本实现自动化静默安装Oracle软件

1.首先需要搭建一个Web站点,用于提供yum服务和oracle软体下载,类似软件资料库一样.(也可使用Ftp服务代替Web服务,看自己的选择) 2.Oracle软件安装时,建议不要安装在根目录下,所以此脚本中/u 目录为一个分区,若无/u分区,则相关目录会在根目录下.此脚本还可以结合PXE+KICKSTART无人值守安装实现批量部署. 3.脚本内容如下: #!/bin/bash ##########################################################

《懒人Shell脚本》之六——一键构造待采集批量sql语句

1.需求 采集任务构造入库.实现200多个网站的标题.时间.内容等信息的采集信息配置,并插入mysql中. 2.实现步骤 步骤1:人工实现Excel表格配置. 定义好唯一索引,如第一列的序号.好处: 1)序号可以成为mysql中的索引. 2)序号规定后,可以实现分布式,1个人4个小时.真正可以实现4个人1小时完成任务.(真正的分布式) 这点,非常重要. 关于索引,实践表明,对于每个网站入口唯一值的定义unique索引,能更进一步防止冲突,确保唯一. 步骤2:将表格存入到txt中. 在linux环

Linux Shell实战之二 一键安装Discuz_v2

#!/bin/bash # #Target: Auto install Discuz! #Date: 2015-05-07 #Author: Jacken #QQ:654001593 #Version: 2.0 #Note  #Define Function ,Add Select View,Auto Install ALL,More Stronger # # ####################################################################

wdcp的环境搭建shell脚本分析--lanmp.sh

#lanmp环境配置的主要文件 . lib/common.conf . lib/common.sh . lib/mysql.sh . lib/apache.sh . lib/nginx.sh . lib/php.sh . lib/na.sh . lib/libiconv.sh . lib/eaccelerator.sh . lib/zend.sh . lib/pureftp.sh . lib/pcre.sh . lib/webconf.sh . lib/service.sh #以上都是引用函数文

wdcp的环境搭建shell脚本分析--lanmp.sh---lib/common.sh

#common.sh,里面主要是一些函数 #执行编译后的清理工作 function make_clean {     #$RE_INS 根据变量的值,来判断是否执行清理工作     if [ $RE_INS == 1 ]; then         make clean >/dev/null 2>&1     fi } #下载函数 function wget_down {     #判断$SOFT_DOWN的值,如果为1,执行下载     if [ $SOFT_DOWN == 1 ];