脚本说明:

  1. 实现自动化搭建LAMP服务,或者是LNMP服务,还有Tomcat

  2. 注意安装Tomcat需要下载jdk-8u161-linux-x64.tar.gz到/usr/local/src

  3. 不能同时安装LAMP 和 LNMP

  4. 安装完成后需要执行source /etc/profile

安装菜单:

LAMP、LNMP、Tomcat自动安装脚本_安装

脚本源码:

#!/bin/bash
# create by kindy
# date: 2018.03.26
# centos7 + apache2.4 + mysql5.6 + php5.6 + nginx1.8 + jdk1.8 + tomcat8

dependence(){
    echo -e "\e[1;32m ----- install wget pcre gcc libxml2 expat perl autocon ----- \e[0m"
    yum install -y wget pcre-devel gcc libxml2-devel expat-devel perl-Module-Install autoconf
    echo -e "\e[1;32m ----- install openssl bzip2 libjpeg libpng freetype epel mcrypt curl ----- \e[0m"
    yum install -y openssl-devel bzip2-devel libjpeg-devel libpng-devel freetype-devel epel-release libmcrypt-devel curl-devel
}

download(){
    cd /usr/local/src
    echo -e "\e[1;32m ----- downloading apache-2.4.29 ----- \e[0m"
    wget http://mirrors.sohu.com/apache/httpd-2.4.29.tar.gz
    wget http://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.6.3.tar.gz
    wget http://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
    echo -e "\e[1;32m ----- downloading mysql-5.6.36 ----- \e[0m"
    wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
    echo -e "\e[1;32m ----- downloading php-5.6.9 ----- \e[0m"
    wget http://mirrors.sohu.com/php/php-5.6.9.tar.gz
    echo -e "\e[1;32m ----- downloading nginx-1.8.0 ----- \e[0m"
    wget http://mirrors.sohu.com/nginx/nginx-1.8.0.tar.gz
    echo -e "\e[1;32m ----- download xcache-3.2 ----- \e[0m"
    wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
    echo -e "\e[1;32m ----- download tomcat-8.5 ----- \e[0m"
    wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.29/bin/apache-tomcat-8.5.29.tar.gz
    echo -e "\e[1;32m ********** download finished ********* \e[0m"
    ls /usr/local/src/
}

install_apache(){
    cd /usr/local/src/
    [ -d apr-1.6.3 ] || tar -zxf apr-1.6.3.tar.gz
    [ -d apr-util-1.6.1 ] || tar -zxf apr-util-1.6.1.tar.gz
    [ -d httpd-2.4.29 ] || tar -zxf httpd-2.4.29.tar.gz

    cd /usr/local/src/apr-1.6.3/ 
    ./configure --prefix=/usr/local/apr && make && make install 
    if [ $? -ne 0 ];then
        echo -e "\e[1;31m ----- apr install failed! ----- \e[0m"
    else
        echo -e "\e[1;32m ----- apr is installed. ----- \e[0m"
    fi

    cd /usr/local/src/apr-util-1.6.1/ 
    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install 
    if [ $? -ne 0 ];then
        echo -e "\e[1;31m ----- apr-util install failed! ----- \e[0m"
    else
        echo -e "\e[1;32m ----- apr-util is installed. ----- \e[0m"
    fi

    cd /usr/local/src/httpd-2.4.29/ 
    ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most && make && make install 
    if [ $? -ne 0 ];then
        echo -e "\e[1;31m ----- apache2.4 install failed! ----- \e[0m"
    else
        echo -e "\e[1;32m ----- apache2.4 is installed. ----- \e[0m"
    fi
}
config_apache(){
    echo "PATH=$PATH:/usr/local/apache2.4/bin/" >> /etc/profile
    /usr/local/apache2.4/bin/apachectl start
    port=`netstat -lnp|grep httpd|wc -l`
    if [ $port -gt 0 ];then
        echo -e "\e[1;32m ----- httpd service on ----- \e[0m"
    else
        echo -e "\e[1;31m ----- httpd start failed. ----- \e[0m"
    fi
    echo -e "\e[1;32m ----- httpd config php ----- \e[0m "
    sed -i '203s/denied/granted/' /usr/local/apache2.4/conf/httpd.conf
    sed -i '/AddType application\/x-gzip .gz .tgz/a\    AddType application\/x-httpd-php .php' /usr/local/apache2.4/conf/httpd.conf
    sed -i 's/.*index.html/& index.php/' /usr/local/apache2.4/conf/httpd.conf
    sed -i 's/^#ServerName/ServerName/' /usr/local/apache2.4/conf/httpd.conf
    /usr/local/apache2.4/bin/apachectl restart
    echo -e "\e[1;32m -------- httpd config php finished.  ------ \e[0m"
}

install_nginx(){
    cd /usr/local/src/ 
    [ -d nginx-1.8.0 ] || tar -zxf nginx-1.8.0.tar.gz
    cd /usr/local/src/nginx-1.8.0/
    ./configure --prefix=/usr/local/nginx && make && make install 
    if [ $? -ne 0 ];then
        echo -e "\e[1;31m ----- nginx is installed. ----- \e[0m"
    else
        echo -e "\e[1;32m ----- nginx is installed. ----- \e[0m"
    fi
    echo "PATH=$PATH:/usr/local/nginx/sbin/" >> /etc/profile
}

install_mysql(){
    cd /usr/local/src/ 
    tar -zxf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
    [ -d /usr/local/mysql ] && mv /usr/local/mysql /usr/local/mysql_bak
    mv /usr/local/src/mysql-5.6.36-linux-glibc2.5-x86_64/ /usr/local/mysql/
    useradd -s /sbin/nologin mysql
    mkdir -p /data/mysql
    chown -R mysql:mysql /data/mysql
    /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql  --datadir=/data/mysql
    if [ $? -ne 0 ];then
        echo -e "\e[1;31m ----- mysql install failed ----- \e[0m"
    else
        echo -e "\e[1;32m ----- mysql is installed. ----- \e[0m"
    fi
}
config_mysql(){
    cd /usr/local/mysql/
    mv /etc/my.cnf  /etc/my.cnf_bak
    cp support-files/my-default.cnf /etc/my.cnf 
    sed -ir 's@# basedir = .....@basedir = /usr/local/mysql@' /etc/my.cnf 
    sed -ir 's@# datadir = .....@datadir = /data/mysql@' /etc/my.cnf 
    cp support-files/mysql.server /etc/init.d/mysqld
    chmod 755 /etc/init.d/mysqld 
    sed -i 's@^datadir=@datadir=/data/mysql@' /etc/init.d/mysqld 
    chkconfig --add mysqld && chkconfig mysqld on
    service mysqld start
    port=`netstat -lnp|grep 3306|wc -l`
    if [ $port -eq 1 ];then
        echo -e "\e[1;32m --- mysql service on --- \e[0m"
    fi
}

install_php(){
    cd /usr/local/src/ 
    [ -d php-5.6.9 ] || tar -zxf php-5.6.9.tar.gz
    cd /usr/local/src/php-5.6.9/ 
    ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php/etc  --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --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 && make && make install 
    if [ $? -ne 0 ];then
        echo -e "\e[1;31m ----- php install failed ----- \e[0m"
    else
        echo -e "\e[1;32m ----- php is installed. ----- \e[0m"
    fi
}
config_php(){
    cp /usr/local/src/php-5.6.9/php.ini-production /usr/local/php/etc/php.ini
}

install_php_fpm(){
    cd /usr/local/src/ 
    [ -d php-5.6.9 ] || tar -zxf php-5.6.9.tar.gz
    cd /usr/local/src/php-5.6.9/ 
    ./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-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-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 --with-pear --with-curl  --with-openssl && make && make install 
    if [ $? -ne 0 ];then
        echo -e "\e[1;31m ----- php-fpm install failed ----- \e[0m"
    else
        echo -e "\e[1;32m ----- php-fpm is installed. ----- \e[0m"
    fi
}
config_php_fpm(){
    useradd -s /sbin/nologin php-fpm
    cp /usr/local/src/php-5.6.9/php.ini-production /usr/local/php-fpm/etc/php.ini
    touch /usr/local/php-fpm/etc/php-fpm.conf
    (
    cat >/usr/local/php-fpm/etc/php-fpm.conf << EOF
[global]
pid=/usr/local/php-fpm/var/run/php-fpm.pid
error_log=/usr/local/php-fpm/var/log/pgp-fpm.log
[www]
listen=/tmp/php-fcgi.sock
listen.mode=666
user=php-fpm
group=php-fpm
pm=dynamic
pm.max_children=50
pm.start_servers=20 
pm.min_spare_servers=5
pm.max_spare_servers=35
pm.max_requests=500
rlimit_files=1024
EOF
    )
    cp /usr/local/src/php-5.6.9/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    chmod 755 /etc/init.d/php-fpm
    chkconfig php-fpm on
    service php-fpm start
}

install_xcache_php(){
    cd /usr/local/src/ 
    [ -d xcache-3.2.0 ] || tar -zxf xcache-3.2.0.tar.gz
    cd /usr/local/src/xcache-3.2.0/
    /usr/local/php/bin/phpize
    ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config && make && make install 
    if [ $? -ne 0 ];then
        echo -e "\e[1;31m ----- php xcache install failed ----- \e[0m"
    else
        echo -e "\e[1;32m ----- php xcache is installed. ----- \e[0m"
    fi
}

install_xcache_fpm(){
    cd /usr/local/src/ 
    [ -d xcache-3.2.0 ] || tar -zxf xcache-3.2.0.tar.gz
    cd /usr/local/src/xcache-3.2.0/
    /usr/local/php-fpm/bin/phpize
    ./configure --enable-xcache --with-php-config=/usr/local/php-fpm/bin/php-config && make && make install 
    if [ $? -ne 0 ];then
        echo -e "\e[1;31m ----- php-fpm xcache install failed ----- \e[0m"
    else
        echo -e "\e[1;32m ----- php-fpm xcache is installed. ----- \e[0m"
    fi
}

install_jdk(){
    cd /usr/local/src
    [ -f jdk-8u161-linux-x64.tar.gz ] && echo -e "\e[1;31m --- please download package first. --- \e[0m"
    [ -d jdk1.8.0_161 ] || tar -zxf jdk-8u161-linux-x64.tar.gz
    [ -d /usr/local/jdk1.8 ] && mv /usr/local/jdk1.8 /usr/local/jdk1.8_bak
    mv jdk1.8.0_161/ /usr/local/jdk1.8/
    echo "JAVA_HOME=/usr/local/jdk1.8" >> /etc/profile
    echo "JAVA_BIN=/usr/local/jdk1.8/bin" >> /etc/profile
    echo "JRE_HOME=/usr/local/jdk1.8/jre" >> /etc/profile
    echo "CLASSPATH=/usr/local/jdk1.8/jre/lib:/usr/local/jdk1.8/lib:/usr/local/jdk1.8/jre/lib/charsets.jar" >> /etc/profile
    echo "PATH=$PATH:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin" >> /etc/profile
    echo -e "\e[1;32m ----- jdk is installed. ----- \e[0m"
}
install_tomcat(){
    cd /usr/local/src
    [ -d apache-tomcat-8.5.29 ] || tar -zxf apache-tomcat-8.5.29.tar.gz
    [ -d /usr/local/tomcat ] && mv /usr/local/tomcat /usr/local/tomcat_bak
    mv apache-tomcat-8.5.29/ /usr/local/tomcat/
    /usr/local/tomcat/bin/startup.sh
    port=`netstat -lnp|grep java|wc -l`
    if [ $port -gt 0 ];then
        echo -e "\e[1;32m ----- tomcat is installed. ----- \e[0m"
    else
        echo -e "\e[1;31m ----- tomcat install failed. ----- \e[0m"
    fi
}

install_lamp(){
    install_apache
    install_mysql
    install_php
    install_xcache_php
}
config_lamp(){
    config_apache
    config_mysql
    config_php
}

install_lnmp(){
    install_nginx
    install_mysql
    install_php_fpm
    install_xcache_fpm
}
config_lnmp(){
    config_mysql
    config_php_fpm
}

menu(){
    echo -e "\e[1;32m ----- please input your choice: ----- \e[0m"
    cat << EOF
    dependece|DP) install necessary dependence package
    download|D)   download all packages to /usr/local/src/;If had,ignore this option.
    apache|A)     install apache source package
    nginx|N)      install nginx source package
    mysql|M)      install mysql source package
    php|P)        install php source package
    php_fpm|PF)   install php_fpm source package
    lamp)         install lamp source package
    lnmp)         install lnmp source package
    tomcat|T)     install tomcat source package
    help|H)       print menu list !!!
    q|Q)          quit
EOF
}

menu
read -p "Please choose to install: " choice
echo -e "\e[1;32m ----- your choice is $choice, processing... ----- \e[0m"
until [ $choice == q -o $choice == Q ];do
    case $choice in 
        dependence|DP)
            dependence
            ;;
        download|D)
            echo -e "[1;31m ----- Notice, download without jdk1.8 package --- \e[0m"
            echo -e "[1;31m ----- Please download in official website --- \e[0m"
            download
            ;;
        apache|A)
            install_apache
            config_apache
            ;;
        nginx|N)
            install_nginx
            ;;
        mysql|M)
            install_mysql
            config_mysql
            ;;
        php|P)
            install_php
            config_php
            ;;
        php_fpm|PF)
            install_php_fpm
            config_php_fpm
            ;;
        xcache|X)
            install_xcache
            ;;
        lamp)
            install_lamp
            config_lamp
            ;;
        lnmp)
            install_lnmp
            config_lnmp
            ;;
        tomcat|T)
            install_jdk
            install_tomcat
            ;;
        help|H)
            menu
            ;;
        q|Q)
            break
    esac
    read -p "Have any choice again? If no,please enter q|Q to quit: " choice
done


初学shell,难免有不足的地方!