【shell脚本】自动编译安装LAMP脚本auto_lamp.sh

准备:

  从官网下载好httpd、mysql、PHP软件源码包(也可以直接使用wget命令下载。前提是需要配置好网络和yum源)。这里可以参考我另外几篇博客,有记载:https://www.cnblogs.com/HeiDi-BoKe/p/11788558.html

  源码编译安装需要配置好环境,比如安装pcre,gc等环境

步骤:

  • 安装apache web服务
    • 通过wget下载httpd软件,解压,进入安装目录,配置configure,编译make,编译安装make install
  • 安装MySQL DB服务
    • 同上
  • 安装PHP服务
    • 同上
  • 整合LAMP架构并启动服务

脚本内容:

[root@rhel8 shell]# cat auto_lamp.sh 
#!/bin/bash
# auto make instal LAMP
# by autors tanbaobao 2020/06/08

# Httpd define path variable
H_FILES=httpd-2.4.43.tar.bz2
H_FILES_DIR=httpd-2.4.43
H_URL=https://mirror.bit.edu.cn/apache//httpd/
H_PREFIX=/usr/local/apache2/
# apr
APR_URL=https://mirror.bit.edu.cn/apache//apr/
APR_FILES=apr-1.7.0.tar.gz
APR_FILES_DIR=apr-1.7.0
APR_PREFIX=/usr/local/apr
# apr-util
APRU_URL=https://mirror.bit.edu.cn/apache//apr/
APRU_FILES=apr-util-1.6.1.tar.gz
APRU_FILES_DIR=apr-util-1.6.1
APRU_PREFIX=/usr/local/apr-util
# pcre
PCRE_URL=https://sourceforge.net/projects/pcre/files/pcre/8.44/
PCRE_FILES=pcre-8.44.tar.gz
PCRE_FILES_DIR=pcre-8.44
PCRE_PREFIX=/usr/local/pcre
# https://sourceforge.net/projects/pcre/files/pcre/8.44/pcre-8.44.tar.gz/download

# MySQL define path variable 
M_URL=http://mirrors.sohu.com/mysql/MySQL-8.0/
M_FILES=mysql-8.0.11.tar.gz
M_FILES_DIR=mysql-8.0.11
M_PREFIX=/usr/local/mysql

# PHP define path variable 
P_URL=http://mirrors.sohu.com/php/
P_FILES=php-7.4.6.tar.gz
P_FILES_DIR=php-7.4.6
P_PREFIX=/usr/local/php
# LICONV
LICONV_URL=http://ftp.gnu.org/gnu/libiconv/
LICONV_FILES=libiconv-1.16.tar.gz
LICONV_FILES_DIR=libiconv-1.16
LICONV_PREFIX=/usr/local/libiconv1.16

# define LAMP Select Install Menu
if [ -z "$1" ];then
    echo -e "33[36mPlease Select Install Menu Follow:33[0m"
    echo -e "33[32m1)编译安装Apache服务器33[1m"
    echo "2)编译安装MySQL服务器"
    echo "3)编译安装PHP服务器"
    echo "4)配置index.php并启动LAMP服务"
    echo -e "33[36mUsage:{/bin/sh $0 1|2|3|4|help}33[0m"
    exit
fi

# auto install httpd
if [[ "$1" -eq "1" ]];then
    wget -c $APR_URL/$APR_FILES && tar -zxvf $APR_FILES && cd $APR_FILES_DIR ;./configure --prefix=$APR_PREFIX
    if [ $? -eq 0 ];then
        make && make install
        echo -e "33[32mThe $APR_FILES_DIR Server Install Successfully!33[0m"
    else
        echo -e "33[32mThe $APR_FILES_DIR Server Install Failed,Please check...!33[0m"
        exit
    fi

    wget -c $APRU_URL/$APRU_FILES && yum -y install expat-devel libtool libtool-ltdl-devel && tar -zxvf $APRU_FILES && cd $APRU_FILES_DIR ;./configure --prefix=$APRU_PREFIX    --with-apr=$APR_PREFIX/bin/apr-1-config

    if [ $? -eq 0 ];then
        make && make install
        echo -e "33[32mThe $APRU_FILES_DIR Server Install Successfully!33[0m"
    else
        echo -e "33[32mThe $APRU_FILES_DIR Server Install Failed,Please check...!33[0m"
        exit
    fi

    wget -c $PCRE_URL/$PCRE_FILES/download -O $PCRE_FILES && tar -zxvf $PCRE_FILES && cd $PCRE_FILES_DIR ;./configure --prefix=$PCRE_PREFIX 
    if [ $? -eq 0 ];then
        make && make install
        echo -e "33[32mThe $PCRE_FILES_DIR Server Install Successfully!33[0m"
    else
        echo -e "33[32mThe $PCRE_FILES_DIR Server Install Failed,Please check...33[0m"
        exit
    fi

    if [ -d $APR_PREFIX ] &&  [ -d $APRU_PREFIX ];then

        wget -c $H_URL/$H_FILES && tar -jxvf $H_FILES && cd $H_FILES_DIR ;./configure --prefix=$H_PREFIX --with-apr=$APR_PREFIX --with-apr-util=$APRU_PREFIX --with-pcre=$PCRE_PREFIX

        if [ $? -eq 0 ];then
            make && make install
            echo -e "33[32mThe $H_FILES_DIR Server Install Successfully!33[0m"
        else
            echo -e "33[32mThe $H_FILES_DIR Server Install Failed,Please check...!33[0m"
            exit
        fi
    fi
fi

# auto install MySQL
if [[ "$1" -eq "2" ]];then
    wget -c $M_URL/$M_FILES && tar -zxvf $M_FILES && cd $M_FILES_DIR && yum install cmake -y 
    cmake . -DCMAKE_INSTALL_PREFIX=$M_PREFIX 
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock 
-DMYSQL_DATADIR=/data/mysql 
-DSYSCONFDIR=/etc 
-DMYSQL_USER=mysql 
-DMYSQL_TCP_PORT=3306 
-DWITH_XTRADB_STORAFE_ENGINE=1 
-DWITH_INNOBASE_STORAGE_ENGINE=1 
-DWITH_PARTITION_STORAGE_ENGINE=1 
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 
-DWITH_MYISAM_STORAGE_ENGINE=1 
-DWITH_READLINE=1 
-DENABLED_LOCAL_INFILE=1 
-DWITH_EXTRA_CHARSETS=1 
-DDEFAULT_CHARSET=utf8 
-DDEFAULT_COLLATION=utf8_general_ci 
-DEXTRA_CHARSETS=ALL 
-DWITH_VIG_TABLES=1 
-DWITH_DEBUG=0
make && make install

# /usr/bin/cp support-files/my-small.cnf /etc/my.conf
# /usr/bin/cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on

    if [ $? -eq 0 ];then
        make && make install
        echo -e "33[32mThe $M_FILES_DIR Server Install Successfully!33[0m"
    else
        echo -e "33[32mThe $M_FILES_DIR Server Install Failed,Please check...!33[0m"
        exit 0
    fi
fi

# auto install php
if [[ "$1" -eq "3" ]];then
    wget -c $LICONV_URL/$LICONV_FILES && tar -zxvf $LICONV_FILES && cd $LICONV_FILES_DIR ;./configure --prefix=$LICONV_PREFIX 
    if [ $? -eq 0 ];then
        make && make install 
        cp -f $LICONV_PREFIX/lib/libcharset.so.1 /usr/lib
        cp -f $LICONV_PREFIX/lib/libiconv.so.2 /usr/lib
        ln -s /usr/lib/libiconv.so.2 /usr/lib/libiconv.so
        echo -e "33[32mThe $LICONV_FILES_DIR Server Install Successfully!33[0m"
    else
        echo -e "33[32mThe $LICONV_FILES_DIR Server Install Failed,Please check...!33[0m"
                exit
    fi

    wget -c $P_URL/$P_FILES && yum -y install libxml2-devel oniguruma sqlite-devel gcc glibc && tar -zxvf $P_FILES && cd $P_FILES_DIR ;./configure --prefix=$P_PREFIX --with-config-file-path=$P_PREFIX/etc --with-mysql=$M_PREFIX --with-apxs2=$H_PREFIX/bin/ap
xs
    if [ $? -eq 0 ];then
        echo "/usr/local/lib" >> /etc/ldso.conf && ldconfig
        make clean && make ZEND_EXTRA_LIBS='-liconv' && ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/ && make install
        echo -e "33[32mThe $P_FILES_DIR Server Install Successfully!33[0m"

    else
        echo -e "33[32mThe $P_FILES_DIR Server Install Failed,Please check...!33[0m"
        exit
    fi
fi

# LAMP 服务配置
if [[ "$1" -eq "4" ]];then
    sed -i '/DirectoryIndex/s/index.html/index.php index.html/g' $H_PREFIX/conf/httpd.conf
    $H_PREFIX/bin/apachectl restart
    echo "AddType application/x-httpd-php .php" >> $H_PREFIX/conf/httpd.conf
    IP=`ifconfig ens160 | grep -w 'inet' | awk '{print $2}' | cut -d: -f2`
    echo "You can access http://$IP/"
cat >$H_PREFIX/htdocs/index.php <<EOF
<?php
phpinfo();
?>
EOF
fi

最后效果:

使用函数版本:

[root@rhel8 shell]# cat auto_lamp.sh 
#!/bin/bash
# auto make instal LAMP
# by autors tanbaobao 2020/06/08

# Httpd define path variable
H_FILES=httpd-2.4.43.tar.bz2
H_FILES_DIR=httpd-2.4.43
H_URL=https://mirror.bit.edu.cn/apache//httpd/
H_PREFIX=/usr/local/apache2/
# apr
APR_URL=https://mirror.bit.edu.cn/apache//apr/
APR_FILES=apr-1.7.0.tar.gz
APR_FILES_DIR=apr-1.7.0
APR_PREFIX=/usr/local/apr
# apr-util
APRU_URL=https://mirror.bit.edu.cn/apache//apr/
APRU_FILES=apr-util-1.6.1.tar.gz
APRU_FILES_DIR=apr-util-1.6.1
APRU_PREFIX=/usr/local/apr-util
# pcre
PCRE_URL=https://sourceforge.net/projects/pcre/files/pcre/8.44/
PCRE_FILES=pcre-8.44.tar.gz
PCRE_FILES_DIR=pcre-8.44
PCRE_PREFIX=/usr/local/pcre
# https://sourceforge.net/projects/pcre/files/pcre/8.44/pcre-8.44.tar.gz/download

# MySQL define path variable 
M_URL=http://mirrors.sohu.com/mysql/MySQL-8.0/
M_FILES=mysql-8.0.11.tar.gz
M_FILES_DIR=mysql-8.0.11
M_PREFIX=/usr/local/mysql
# rpcgen
R_URL=https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4.1/
R_FILES=rpcsvc-proto-1.4.1.tar.xz
R_FILES_DIR=rpcsvc-proto-1.4.1
R_PREFIX=/usr/local/rpcsvc-1.4.1

# PHP define path variable 
P_URL=http://mirrors.sohu.com/php/
P_FILES=php-7.4.6.tar.gz
P_FILES_DIR=php-7.4.6
P_PREFIX=/usr/local/php
# LICONV
LICONV_URL=http://ftp.gnu.org/gnu/libiconv/
LICONV_FILES=libiconv-1.16.tar.gz
LICONV_FILES_DIR=libiconv-1.16
LICONV_PREFIX=/usr/local/libiconv1.16

# define LAMP Select Install Menu
#if [ -z "$1" ];then
#    echo -e "33[36mPlease Select Install Menu Follow:33[0m"
#    echo -e "33[32m1)编译安装Apache服务器33[1m"
#    echo "2)编译安装MySQL服务器"
#    echo "3)编译安装PHP服务器"
#    echo "4)配置index.php并启动LAMP服务"
#    echo -e "33[36mUsage:{/bin/sh $0 1|2|3|4|help}33[0m"
#    exit
#fi

# auto install httpd
function apache_install(){
# if [[ "$1" -eq "1" ]];then
    wget -c $APR_URL/$APR_FILES && tar -zxvf $APR_FILES && cd $APR_FILES_DIR ;./configure --prefix=$APR_PREFIX
    if [ $? -eq 0 ];then
        make && make install
        echo -e "33[32mThe $APR_FILES_DIR Server Install Successfully!33[0m"
    else
        echo -e "33[32mThe $APR_FILES_DIR Server Install Failed,Please check...!33[0m"
        exit
    fi

    wget -c $APRU_URL/$APRU_FILES && yum -y install expat-devel libtool libtool-ltdl-devel && tar -zxvf $APRU_FILES && cd $APRU_FILES_DIR ;./configure --prefix=$APRU_PREFIX    --with-apr=$APR_PREFIX/bin/apr-1-config

    if [ $? -eq 0 ];then
        make && make install
        echo -e "33[32mThe $APRU_FILES_DIR Server Install Successfully!33[0m"
    else
        echo -e "33[32mThe $APRU_FILES_DIR Server Install Failed,Please check...!33[0m"
        exit
    fi

    wget -c $PCRE_URL/$PCRE_FILES/download -O $PCRE_FILES && tar -zxvf $PCRE_FILES && cd $PCRE_FILES_DIR ;./configure --prefix=$PCRE_PREFIX 
    if [ $? -eq 0 ];then
        make && make install
        echo -e "33[32mThe $PCRE_FILES_DIR Server Install Successfully!33[0m"
    else
        echo -e "33[32mThe $PCRE_FILES_DIR Server Install Failed,Please check...33[0m"
        exit
    fi

    if [ -d $APR_PREFIX ] &&  [ -d $APRU_PREFIX ];then

        wget -c $H_URL/$H_FILES && tar -jxvf $H_FILES && cd $H_FILES_DIR ;./configure --prefix=$H_PREFIX --with-apr=$APR_PREFIX --with-apr-util=$APRU_PREFIX --with-pcre=$PCRE_PREFIX

        if [ $? -eq 0 ];then
            make && make install
            echo -e "33[32mThe $H_FILES_DIR Server Install Successfully!33[0m"
        else
            echo -e "33[32mThe $H_FILES_DIR Server Install Failed,Please check...!33[0m"
            exit
        fi
    fi
# fi
}

# auto install MySQL
function mysql_install(){
# if [[ "$1" -eq "2" ]];then
    wget -c $R_URL/$R_FILES && tar -Jxvf $R_FILES && cd $R_FILES_DIR ;./configure
    if [ $? -eq 0 ];then
        make && make install
        echo -e "33[32mThe $R_FILES_DIR Server Install Successfully!33[0m"
    else
        echo -e "33[32mThe $R_FILES_DIR Server Install Failed,Please check...!33[0m"
        exit
    fi

    wget -c $M_URL/$M_FILES && yum -y install openssl openssl-devel libtirpc-devel && tar -zxvf $M_FILES && cd $M_FILES_DIR && yum install cmake -y && pwd && rm -rf /tanbaobao/shell/mysql-8.0.11/CMakeCache.txt  && yum -y install ncurses-devel
    make clean
    cmake . -DCMAKE_INSTALL_PREFIX=$M_PREFIX 
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock 
-DMYSQL_DATADIR=/data/mysql 
-DSYSCONFDIR=/etc 
-DMYSQL_USER=mysql 
-DMYSQL_TCP_PORT=3306 
-DWITH_XTRADB_STORAFE_ENGINE=1 
-DWITH_INNOBASE_STORAGE_ENGINE=1 
-DWITH_PARTITION_STORAGE_ENGINE=1 
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 
-DWITH_MYISAM_STORAGE_ENGINE=1 
-DWITH_READLINE=1 
-DENABLED_LOCAL_INFILE=1 
-DWITH_EXTRA_CHARSETS=1 
-DDEFAULT_CHARSET=utf8 
-DDEFAULT_COLLATION=utf8_general_ci 
-DEXTRA_CHARSETS=ALL 
-DWITH_VIG_TABLES=1 
-DWITH_DEBUG=0 
-DDOWNLOAD_BOOST=1 
-DWITH_BOOST=/usr/local/boost
make && make install

# /usr/bin/cp support-files/my-small.cnf /etc/my.conf
# /usr/bin/cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on

    if [ $? -eq 0 ];then
        make && make install
        echo -e "33[32mThe $M_FILES_DIR Server Install Successfully!33[0m"
    else
        echo -e "33[32mThe $M_FILES_DIR Server Install Failed,Please check...!33[0m"
        exit 0
    fi
# fi
}

# auto install php
function php_install(){
# if [[ "$1" -eq "3" ]];then
    wget -c $LICONV_URL/$LICONV_FILES && tar -zxvf $LICONV_FILES && cd $LICONV_FILES_DIR ;./configure --prefix=$LICONV_PREFIX 
    if [ $? -eq 0 ];then
        make && make install 
        cp -f $LICONV_PREFIX/lib/libcharset.so.1 /usr/lib
        cp -f $LICONV_PREFIX/lib/libiconv.so.2 /usr/lib
        ln -s /usr/lib/libiconv.so.2 /usr/lib/libiconv.so
        echo -e "33[32mThe $LICONV_FILES_DIR Server Install Successfully!33[0m"
    else
        echo -e "33[32mThe $LICONV_FILES_DIR Server Install Failed,Please check...!33[0m"
                exit
    fi

    wget -c $P_URL/$P_FILES && yum -y install libxml2-devel oniguruma sqlite-devel gcc glibc && tar -zxvf $P_FILES && cd $P_FILES_DIR ;./configure --prefix=$P_PREFIX --with-config-file-path=$P_PREFIX/etc --with-mysql=$M_PREFIX --with-apxs2=$H_PREFIX/bin/ap
xs
    if [ $? -eq 0 ];then
        echo "/usr/local/lib" >> /etc/ldso.conf && ldconfig
        make clean && make ZEND_EXTRA_LIBS='-liconv' && ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/ && make install
        echo -e "33[32mThe $P_FILES_DIR Server Install Successfully!33[0m"

    else
        echo -e "33[32mThe $P_FILES_DIR Server Install Failed,Please check...!33[0m"
        exit
    fi
# fi
}
# LAMP 服务配置
function server_conf(){
# if [[ "$1" -eq "4" ]];then
    sed -i '/DirectoryIndex/s/index.html/index.php index.html/g' $H_PREFIX/conf/httpd.conf
    $H_PREFIX/bin/apachectl restart
    echo "AddType application/x-httpd-php .php" >> $H_PREFIX/conf/httpd.conf
    IP=`ifconfig ens160 | grep -w 'inet' | awk '{print $2}' | cut -d: -f2`
    echo "You can access http://$IP/"
cat >$H_PREFIX/htdocs/index.php <<EOF
<?php
phpinfo();
?>
EOF
#fi
}

# 选择
echo -e "33[32m====================================================33[1m"
PS3="Please select Menu follow:"

select i in "apache_install" "mysql_install" "php_install" "server_conf"
do
    case $i in
        apache_install)
            apache_install
            ;;
        mysql_install)
            mysql_install 
            ;;
        php_install)
            php_install
            ;;
        server_conf)
            server_conf
            ;;
        *)
                echo -e "33[36mUsage:{/bin/sh $0 1|2|3|433[0m"
            exit
            ;;
    esac

done
原文地址:https://www.cnblogs.com/HeiDi-BoKe/p/13068966.html