JavaWeb服务器环境部署

通过shell脚本安装Java1.8、Tomcat8、MySQL5.6、Nginx1.15

定义的安装变量

#! /bin/bash
# 主安装页面


# tomcat 安装变量
TOMCAT_URL="https://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.tar.gz"
TOMCAT_NAME="apache-tomcat-8.0.53"
TOMCAT_NAME_TAR="apache-tomcat-8.0.53.tar.gz"
TOMCAT_INSTALL_DIR="/usr/local/tomcat8"
TOMCAT_SERVICE="/usr/lib/systemd/system/tomcat8.service"
TOMCAT_SERVCIE_FILE="tomcat8.service"
# nginx 安装变量
NGINX_URL="http://nginx.org/download/nginx-1.15.5.tar.gz"
NGINX_NAME_TAR="nginx-1.15.5.tar.gz"
NGINX_NAME="nginx-1.15.5"
NGINX_INSTALL_DIR="/usr/local/nginx"
NGINX_SERVICE="/usr/lib/systemd/system/nginx.service"
NGINX_SERVICE_FILE="nginx.service"

# mysql 安装变量
MYSQL_NAME="mysql-5.6.29"
MYSQL_NAME_TAR="mysql-5.6.29.tar.gz"
MYSQL_INSTALL_DIR="/usr/local/mysql"
MYSQL_INSTALL_DATE="/usr/local/mysql/data";

Java安装函数

# java安装
function java_install() {
    yum install java-1.8.0-openjdk java-1.8.0-openjdk-devel -y
    if [ $? == 0 ]; then
        echo -e "\033[44;37m >>> 设置环境变量... \033[0m";
        echo "JAVA_HOME=/usr/lib/jvm/java" >> /etc/profile
        echo "PATH=$PATH:$JAVA_HOME/bin" >> /etc/profile
        echo "CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar" >> /etc/profile
        echo "export JAVA_HOME CLASSPATH PATH" >> /etc/profile
        source /etc/profile
        read -p ">>> 是否执行验证? y/n" SELECT;
        if [ $SELECT -eq 'y' ]; then
            java -version
            javac -version
            return 0;
        else
            echo -e "\033[44;37m 安装结束... \033[0m";
            return 0;
        fi
    else
        echo -e "\033[41;37m >>> 执行失败... \033[0m";
        exit;
    fi
}

Tomcat安装函数

# tomcat安装
function tomcat_install() {
    echo -e "\033[44;37m >>> 开始下载Tomcat8 \033[0m";
    wget $TOMCAT_URL
    if [ $? -eq 0 ]; then
        echo -e "\033[44;37m >>>下载成功! \033[0m";
        clear && tar -zxvf $TOMCAT_NAME_TAR && mv $TOMCAT_NAME $TOMCAT_INSTALL_DIR && cd $TOMCAT_INSTALL_DIR
        if [ $? -eq 0 ]; then
            clear
            echo -e "\033[44;37m >>> 开始配置服务... \033[0m";
            touch $TOMCAT_SERVICE
            echo "[Unit]" >> $TOMCAT_SERVICE
            echo "Description=Tomcat" >> $TOMCAT_SERVICE
            echo "After=syslog.target network.target remote-fs.target nss-lookup.target" >> $TOMCAT_SERVICE
            echo "[Service]" >> $TOMCAT_SERVICE
            echo "Type=oneshot" >> $TOMCAT_SERVICE
            echo "ExecStart=/usr/local/tomcat8/bin/startup.sh" >> $TOMCAT_SERVICE
            echo "ExecStop=/usr/local/tomcat8/bin/shutdown.sh" >> $TOMCAT_SERVICE
            echo "ExecReload=/bin/kill -s HUP $MAINPID" >> $TOMCAT_SERVICE
            echo "RemainAfterExit=yes" >> $TOMCAT_SERVICE
            echo "[Install]" >> $TOMCAT_SERVICE
            echo "WantedBy=multi-user.target" >> $TOMCAT_SERVICE
            echo "\033[44;37m >>> 设置开机自启... \033[0m";
            systemctl enable $TOMCAT_SERVCIE_FILE
            if [ $? -eq 0 ]; then
                clear && systemctl daemon-reload
                echo -e "\033[44;37m >>> 安装Tomcat8成功 \033[0m";
                systemctl status firewalld | grep "dead"
                if [ $? -eq 0 ]; then
                    systemctl start firewalld
                fi
                firewall-cmd --list-ports | grep "8080"
                if [ $? -eq 1 ]; then
                    echo -e "\033[43;37m >>> 检测到8080未打开,将开启... \033[0m"
                    firewall-cmd --zone=public --add-port=8080/tcp --permanent && firewall-cmd --reload
                    if [ $? -eq 0 ]; then 
                        echo -e "\033[46;37m >>> 启动Tomcat... \033[0m"
                        systemctl start tomcat8
                        return 0;
                    fi
                fi
            else
                echo -e "\033[41;37m >>> 设置开机自启失败!\033[0m";
                exit;
            fi
        else
            echo -e "\033[41;37m >>> 解压失败!\033[0m";
            exit;
        fi
    else
        echo -e "\033[41;37m >>> 下载失败,请确认是否联网!\033[0m";
        exit;
    fi
}

Nginx安装函数

# nginx 安装
function nginx_install() {

    echo -e "\033[42;37m >>> 安装Nginx的相关依赖... \033[0m";
    yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel gcc-c++ && clear
    if [ $? -eq 0 ]; then
        echo -e "\033[42;37m >>> 开始下载Nginx... \033[0m"
        wget $NGINX_URL
        if [ $? -eq 0 ]; then
            echo -e "\033[42;37m >>> 开始解压安装... \033[0m"
            tar -zxvf $NGINX_NAME_TAR && mkdir $NGINX_INSTALL_DIR && cd $NGINX_NAME && ./configure --prefix="$NGINX_INSTALL_DIR" && make && make install
            if [ $? -eq 0 ]; then
                echo -e "\033[42;37m >>> 安装成功!\033[0m"
                sleep 2;
                echo -e "\033[42;37m >>> 开始设置开机重启... \033[0m";
                touch $NGINX_SERVICE
                echo "[Unit]" >> $NGINX_SERVICE
                echo "Description=nginx - high performance web server" >> $NGINX_SERVICE
                echo "After=network.target remote-fs.target nss-lookup.target" >> $NGINX_SERVICE
                echo "[Service]" >> $NGINX_SERVICE
                echo "Type=forking" >> $NGINX_SERVICE
                echo "ExecStart=/usr/local/nginx/sbin/nginx" >> $NGINX_SERVICE
                echo "ExecReload=/usr/local/nginx/sbin/nginx -s reload" >> $NGINX_SERVICE
                echo "ExecStop=/usr/local/nginx/sbin/nginx -s stop" >> $NGINX_SERVICE
                echo "PrivateTmp=true" >> $NGINX_SERVICE
                echo "[Install]" >> $NGINX_SERVICE
                echo "WantedBy=multi-user.target" >> $NGINX_SERVICE
                systemctl daemon-reload && systemctl enable $NGINX_SERVICE_FILE
                clear
                echo -e "\033[42;37m >>> 配置成功,启动Nginx... \033[0m";
                sleep 2;
                systemctl start $NGINX_SERVICE_FILE
                return 0;
            else
                echo -e "\033[41;37m >>> 安装失败!\033[0m";
                exit;
            fi
        else
            echo -e "\033[41;37m >>> 下载失败,请确认是否联网!\033[0m";
            exit;
        fi
    else
        echo -e "\033[41;37m >>> 依赖安装失败!\033[0m";
        exit;
    fi

}

Mysql 安装函数

mysql_install() {

    echo -e "\033[42;37m >>> 正在下载mysql...\033[0m";
    yum install -y wget && wget -c http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.29.tar.gz;
    if [ $? -eq 0 ]; then
        clear
        echo -e "\033[42;37m >>> 依赖安装! \033[0m";
        yum -y install gcc make cmake ncurses-devel libxml2-devel libtool-ltdl-devel gcc-c++ autoconf automake bison zlib-devel
        echo -e "\033[42;37m >>> 解压文件 \033[0m";
        tar -zxvf $MYSQL_NAME_TAR && cd $MYSQL_NAME
        if [ $? -eq 0 ];then
            echo -e "\033[42;37m >>> 安装前的设置 \033[0m";
            cmake -DCMAKE_INSTALL_PREFIX=$MYSQL_INSTALL_DIR \
            -DMYSQL_DATADIR=$MYSQL_INSTALL_DATE \
            -DSYSCONFDIR=/etc \
            -DWITH_MYISAM_STORAGE_ENGINE=1 \
            -DWITH_INNOBASE_STORAGE_ENGINE=1 \
            -DWITH_MEMORY_STORAGE_ENGINE=1 \
            -DWITH_READLINE=1 \
            -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \
            -DMYSQL_TCP_PORT=3306 \
            -DENABLED_LOCAL_INFILE=1 \
            -DWITH_PARTITION_STORAGE_ENGINE=1 \
            -DEXTRA_CHARSETS=all \
            -DDEFAULT_CHARSET=utf8 \
            -DDEFAULT_COLLATION=utf8_general_ci 
            if [ $? -eq 0 ];then
                echo -e "\033[42;37m >>> 设置成功!开始编译安装,大约需要20分钟,耐心等待...\033[0m"; 
                sleep 2s
                make && make install 
                if [ $? -eq 0 ]; then
                    cd ~
                    echo -e "\033[42;37m >>> 编译成功!开始设置组和用户! \033[0m";
                    sleep 2s
                    groupadd mysql && 
                    useradd mysql -g mysql -M -s /sbin/nologin &&
                    chown mysq:mysql $MYSQL_INSTALL_DIR
                else
                    echo -e "\033[41;37m >>>  编译失败! \033[0m";
                    exit;
                fi
            else
                echo -e "\033[41;37m >>>  设置失败! \033[0m";
                exit;
            fi
        else
            echo -e "\033[41;37m >>>  解压失败!\033[0m";
            exit;
        fi
    else
        echo -e "\033[41;37m >>>  请先确认联网! \033[0m";
        exit;
    fi



    echo -e "\033[42;37m >>> 开始配置mysql \033[0m";
    cd $MYSQL_INSTALL_DIR
    echo -e "\033[42;37m >>> 初始化mysql \033[0m";
    ./scripts/mysql_install_db --user=mysql --datadir=$MYSQL_INSTALL_DATE
    cp support-files/my-default.cnf  /etc/my.cnf
    sleep 2s
    echo -e "\033[42;37m >>> 开始修改mysql的配置文件 <======";
    sed -i 's/\# basedir \= ...../basedir \= \/usr\/local\/mysql/g' /etc/my.cnf
    sed -i 's/\[mysqld\]/&\ndefault-storage-engine=InnoDB/' /etc/my.cnf
    sed -i 's/\[mysqld\]/&\nlower_case_table_names=1/' /etc/my.cnf


    echo -e "\033[42;37m >>> 制作服务启动 \033[0m";
    /bin/cp -rf support-files/mysql.server /etc/init.d/mysqld
    if [ $? -eq 0 ]; then
        echo -e "\033[42;37m >>> 服务设置成功! \033[0m";
        cd ~
        service mysqld start
        sleep 2s
        service mysqld stop
        sleep 2s
    else
        echo -e "\033[42;37m >>> 服务设置失败! \033[0m";
    fi

    echo -e "\033[42;37m >>> 添加到开机启动项! \033[0m";
    chkconfig --add mysqld
    if [ $? -eq 0 ]; then
        echo -e "\033[42;37m >>> 设置开机启动项成功! \033[0m";
    else
        echo -e "\033[41;37m >>> 设置开机启动项失败! \033[0m";
    fi


    service mysqld stop
    echo -e "\033[42;37m >>> 配置环境变量 \033[0m";
    echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile
    source /etc/profile

    echo -e "\033[42;37m >>> 启动mysql服务 \033[0m";
    service mysqld start


    cd $MYSQL_INSTALL_DIR
    echo -e "\033[42;37m >>> 设置root密码为: “123456” \033[0m";
    ./bin/mysqladmin -u root password "123456"
    if [ $? -eq 0 ];then
        echo -e "\033[42;37m >>> 设置root密码成功!\033[0m";
        source /etc/profile
    else
        echo -e "\033[41;37m >>> 设置root密码失败!\033[0m";
    fi

}

程序的主体

function main() {
    echo -e "\033[43;37m==================================================\033[0m";
    echo -e "\033[43;37m================JavaWeb环境安装部署===============\033[0m";
    echo -e "\033[43;37m===================1-Mysql5.6=====================\033[0m";
    echo -e "\033[43;37m===================2-Java1.8======================\033[0m";
    echo -e "\033[43;37m===================3-Tomcat8======================\033[0m";
    echo -e "\033[43;37m===================4-Nginx1.15====================\033[0m";
    echo -e "\033[43;37m===================5-全部安装=====================\033[0m";
    echo -e "\033[43;37m===================6-退出=========================\033[0m";
    echo -e "\033[43;37m==================================================\033[0m";
    read -p "输入选择 >>> " SELECT;
    case $SELECT in 
        1)
            echo -e "\033[43;37m选择安装Mysql5.6\033[0m";
            mysql_install
            echo -e "\033[46;37m在控制台中执行 “source /etc/profile” 命令!\033[0m";
        ;;
        2)
            echo -e "\033[43;37m选择安装Java1.8\033[0m";
            java_install
        ;;
        3)
            echo -e "\033[43;37m选择安装Tomcat8\033[0m";
            tomcat_install
        ;;
        4)
            echo -e "\033[43;37m选择安装Nginx1.15\033[0m";
            nginx_install
        ;;
        5)
            echo -e "\033[43;37m将安装全部的JavaWeb环境\033[0m";
            java_install
            tomcat_install
            nginx_install
            mysql_install
            echo -e "\033[46;37m在控制台中执行 “source /etc/profile” 命令!\033[0m";
        ;;
        6)
            echo -e "\033[43;37程序退出...\033[0m";
            exit;
        ;;
        *)
            echo -e "\033[41;37m错误!请选择{mysql|java|tomcat|nginx|all}\033[0m";
            echo "——————————————————————————————";
        ;;
    esac
}
# 主函数的调用
main

Mysql编译安装比较慢,mysql设置了默认密码为 “123456”,如需要可以自己修改mysql密码

Tomcat8:

# tomcat安装目录
/usr/local/tomcat
# 启动Tomcat
systemctl start tomcat8.service
# 关闭Tomcat
systemctl stop tomcat8.service

Nginx1.15

# Nginx 安装目录
/usr/local/nginx
# 启动Nginx
systemctl start nginx.service
# 关闭Nginx
systemctl stop nginx.service

Mysql5.6

需要在安装之后按照提示执行

source /erc/profile

Java8

java -version
javac

Tomcat和Nginx已经添加开机自启,Mysql编译需要10-20分钟,虚拟机>20分钟,主要看配置了。

你可能感兴趣的:(JavaWeb服务器环境部署)