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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: 使用 shell 脚本来实现 LANMP 系统的一键安装。使用的操作系统是 CentOS 6 ,不区分 32 位和 64 位,要求机器可以连通互联网。支持 LAMP 和 LNMP ,MySQL 支持 5.1 和 5.6 两个版本, php 支持 5.3 和 5.6 两个版本, apache 2.2 ,nginx 1.8。

使用 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

 

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
16天前
|
Shell
一个用于添加/删除定时任务的shell脚本
一个用于添加/删除定时任务的shell脚本
53 1
|
2天前
|
Shell Linux 测试技术
6种方法打造出色的Shell脚本
6种方法打造出色的Shell脚本
15 2
6种方法打造出色的Shell脚本
|
7天前
|
监控 网络协议 Shell
ip和ip网段攻击拦截系统-绿叶结界防火墙系统shell脚本
这是一个名为“小绿叶技术博客扫段攻击拦截系统”的Bash脚本,用于监控和拦截TCP攻击。通过抓取网络数据包监控可疑IP,并利用iptables和firewalld防火墙规则对这些IP进行拦截。同时,该系统能够查询数据库中的白名单,确保合法IP不受影响。此外,它还具备日志记录功能,以便于后续分析和审计。
31 6
|
4天前
|
运维 监控 Shell
深入理解Linux系统下的Shell脚本编程
【10月更文挑战第24天】本文将深入浅出地介绍Linux系统中Shell脚本的基础知识和实用技巧,帮助读者从零开始学习编写Shell脚本。通过本文的学习,你将能够掌握Shell脚本的基本语法、变量使用、流程控制以及函数定义等核心概念,并学会如何将这些知识应用于实际问题解决中。文章还将展示几个实用的Shell脚本例子,以加深对知识点的理解和应用。无论你是运维人员还是软件开发者,这篇文章都将为你提供强大的Linux自动化工具。
|
1月前
|
监控 Unix Shell
shell脚本编程学习
【10月更文挑战第1天】shell脚本编程
58 12
|
1月前
|
存储 运维 监控
自动化运维:使用Shell脚本简化日常任务
【9月更文挑战第35天】在IT运维的日常工作中,重复性的任务往往消耗大量的时间。本文将介绍如何通过编写简单的Shell脚本来自动化这些日常任务,从而提升效率。我们将一起探索Shell脚本的基础语法,并通过实际案例展示如何应用这些知识来创建有用的自动化工具。无论你是新手还是有一定经验的运维人员,这篇文章都会为你提供新的视角和技巧,让你的工作更加轻松。
33 2
|
2月前
|
Shell
shell脚本变量 $name ${name}啥区别
shell脚本变量 $name ${name}啥区别
|
2月前
|
人工智能 监控 Shell
常用的 55 个 Linux Shell 脚本(包括基础案例、文件操作、实用工具、图形化、sed、gawk)
这篇文章提供了55个常用的Linux Shell脚本实例,涵盖基础案例、文件操作、实用工具、图形化界面及sed、gawk的使用。
187 2
|
26天前
|
存储 Shell Linux
【Linux】shell基础,shell脚本
Shell脚本是Linux系统管理和自动化任务的重要工具,掌握其基础及进阶用法能显著提升工作效率。从简单的命令序列到复杂的逻辑控制和功能封装,Shell脚本展现了强大的灵活性和实用性。不断实践和探索,将使您更加熟练地运用Shell脚本解决各种实际问题
18 0
|
2月前
|
网络协议 关系型数据库 MySQL
Shell 脚本案例
Shell 脚本案例
43 8