Linux 下自动化脚本安装Jdk、Nginx等软件

1、安装JDK脚本

#!/bin/bash

# jdk install
# 请将下载的jdk-xxx-linux-xxx.tar.gz包与此脚本放置到同一目录
# 授予此脚本可执行权限(chmod +x install_jdk.sh)
# 在终端执行此脚本开始安装(./文件名)
# 注意:不可有多个版本的jdk包!
#      为了使配置的环境变量生效,安装完成后你应该重新登陆。

jvmpath=/usr/local/java
# 不存在
if [ ! -d "$jvmpath" ]; then
    echo "正在创建$jvmpath目录"
    sudo mkdir $jvmpath
    echo "目录$jvmpath创建成功"
fi

jdkfile=$(ls | grep jdk-*-linux-*.gz)

#注意这里的文件夹名称,和下面解压的jdk文件夹名称要一致,否则 执行java -version会失败
jdkdirname="jdk1.8.0_162"

os_version=`uname -a`
echo $os_version
architecture="64"
echo "$os_version" | grep -q "$architecture"

if [ $? -eq 0 ]
    then
        # 不存在即去外网下载jdk文件
        if [ ! -f "$jdkfile" ]; then
            echo "您正在使用64位操作系统,为您选择64位JDK"
            wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u162-b12/0da788060d494f5095bf8624735fa2f1/jdk-8u162-linux-x64.tar.gz"
        fi
    else
        # 不存在即去外网下载jdk文件
        if [ ! -f "$jdkfile" ]; then
            echo "您正在使用32位操作系统,为您选择32位JDK"
            wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u162-b12/0da788060d494f5095bf8624735fa2f1/jdk-8u162-linux-i586.tar.gz"
        fi
    fi

jdkfile=$(ls | grep jdk-*-linux-*.gz)

if [ -f "$jdkfile" ]; then

    #注意这里,$jdkfile解压之后会生成一个 解压包里面的文件夹,如这里的:jdk1.8.0_162/
    sudo tar -zxvf $jdkfile -C /usr/local/java/

    echo "安装JDK成功"

    echo "配置环境变量"
    # touch environment
    # echo "PATH=\"$PATH:/usr/lib/jvm/$jdkdirname/bin\"" >> environment
    # echo "JAVA_HOME=/usr/lib/jvm/$jdkdirname" >> environment
    # echo "CLASSPATH=.:%JAVA_HOME%/lib/dt.jar:%JAVA_HOME%/lib/tools.jar" >> environment
    # sudo mv /etc/environment /etc/environment.backup.java
    # sudo mv environment /etc
    # source /etc/environment

    mv ~/.bashrc ~/.bashrc.backup.java
    cat ~/.bashrc.backup.java >> ~/.bashrc
    echo "PATH=\"$PATH:$jvmpath/$jdkdirname/bin\"" >> ~/.bashrc
    echo "JAVA_HOME=$jvmpath/$jdkdirname" >> ~/.bashrc
    echo "CLASSPATH=.:%JAVA_HOME%/lib/dt.jar:%JAVA_HOME%/lib/tools.jar" >> ~/.bashrc
    source ~/.bashrc
    echo "配置环境成功"

    echo "测试是否安装成功"
    java -version
    echo "安装成功"

fi

2、安装nginx脚本:

#!/bin/bash
#判断是否是root用户
# 授予此脚本可执行权限(chmod +x install_nginx.sh)

if [ $(id -u) != "0" ]; then
        echo "Error:You must be root to run this script"
fi

#每次使用只需修改自定义内容即可
#自定义用户名和组
Group_Name="nginx"
User_Name="nginx"

#自定义nginx变量
Install_Path="/usr/local/nginx"
Package_Type=".tar.gz"
Version="nginx-1.13.9"
Package=$Version$Package_Type
Setup_path="/root/"
RPM="nginx"

#自定义/var/tmp/nginx目录
DIR_File="/var/tmp/nginx"

#下载nginx 文件
cd $Setup_path
wget http://nginx.org/download/nginx-1.13.9.tar.gz

#安装依赖关系
yum group install "Development Tools" "Server Platform Deveopment"
yum install -y curl openssl-devel pcre-devel
Group_User(){
egrep "^$Group_Name" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
    echo "nginx 用户组正在添加."
    groupadd $Group_Name
else
    echo " The $Group_Name user group already exists."
    echo "nginx 用户组已经添加."
fi

#判断nginx用户是否存在
egrep "^$User_Name" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
    echo "nginx 用户正在添加."
    useradd -g $Group_Name $User_Name
else
-- VISUAL --
    fi
}

#编译安装nginx
cd $Setup_path

#解压nginx包到/usr/local/nginx
tar -zxvf $Package -C $Install_Path

cd $Version

#自定义配置(不推荐)
configure_opts=(
--prefix=$Install_Path
--sbin-path=$Install_Path/sbin/nginx
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--pid-path=/var/run/nginx/nginx.pid
--lock-path=/var/lock/nginx.lock
--user=nginx
--group=nginx
--with-http_ssl_module
--with-http_flv_module
--with-http_stub_status_module
--with-http_gzip_static_module
--http-client-body-temp-path=/var/tmp/nginx/client
--http-proxy-temp-path=/var/tmp/nginx/proxy
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
--http-scgi-temp-path=/var/tmp/nginx/scgi
--with-pcre
)
./configure ${configure_opts[@]}
if [[ $? -eq 0 ]]
then
    make && make install
else
    echo "编译失败,请重新编译" && exit 1
fi

#添加Nginx命令到环境变量
cat >/etc/profile.d/nginx.sh <<EOF
export PATH=/usr/local/nginx/sbin/:$PATH
EOF
source /etc/profile

#启动服务
/usr/local/nginx/sbin/nginx
ss -tnlp | grep nginx

3、重启nginx 80端口脚本

#!/bin/bash
fuser -k 80/tcp

if [ $? -eq 0 ]
   then
        echo "正在启动nginx..."
        ./nginx
        if [ $? -eq 0 ]
                then
                        echo "启动成功!"
        fi
fi

参考:
1、centos7上JDK安装与一键安装脚本的编写

2、Linux一键安装JDK8、Tomcat8脚本

3、centos上一键安装jdk、tomcat脚本

4、tar 命令

5、Nginx脚本一键安装

6、Nginx脚本一键安装第二版

7、nginx download

8、Vim中如何全选并复制?

阅读 2.1k

推荐阅读
全栈工程师进阶
用户专栏

日常学习总结与分享,包括:前端、后台与运维,讲解的知识点包括:javascript、vuejs、reactjs、springb...

79 人关注
44 篇文章
专栏主页