shell 自动化安装nginx

#!/bin/bash
#判断是否是roo用户
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.15.8"
Package=$Version$Package_Type
Setup_path="/root/"
RPM="nginx"

#创建/usr/local/nginx目录
#mkdir /usr/local/nginx
if [ -e $Install_Path ]
then
    echo " $Install_Path 目录已经存在."
    echo " $Install_Path Directory Already Exists."
else 
    echo " $Install_Path 目录正在创建."
    mkdir $Install_Path
fi 
 
#下载nginx 文件
cd $Setup_path
wget http://nginx.org/download/nginx-1.15.8.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
    echo "nginx 用户已经添加."
    echo " The $User_Name user already exists."
fi
}

Group_User

#判断文件是否存在
if [ -e $Setup_path$Version$Package_Type ]
then
        echo "$Package The Package exists."
else
        echo "$Package The package does not exist."
fi

#编译安装nginx
cd $Setup_path

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

cd $Install_Path

cd $Version

configure_opts=(
    --prefix=$Install_Path 
    --user=nginx 
    --group=nginx 
    --with-http_ssl_module 
    --with-http_flv_module
    --with-http_stub_status_module 
    --with-http_gzip_static_module 
)

./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

 

posted @ 2020-06-08 15:35  sunmmi  阅读(353)  评论(0)    收藏  举报