1. 程式人生 > 其它 >shell一鍵安裝nginx

shell一鍵安裝nginx

#!/bin/bash
#描述:Nginx安裝指令碼
echo    -e "\033[34m 安裝Nginx \033[0m"
sleep 3
echo    "安裝Nginx依賴包"
nginx_gz=nginx-1.20.1.tar.gz
nginx=nginx-1.20.1
nginx_url=/usr/local/nginx
RTDIR=$(pwd)

cd $RTDIR
yum install -y net-tools gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel  pcre pcre-devel make automake    &>/dev/null
echo    "建立Nginx執行使用者"
cat /etc/group    | grep www
if [ $? -eq 0 ];then
    echo    "www已存在"
else
    groupadd www
    echo    "www建立成功"
fi
useradd -g www www -s /sbin/nologin
tar xf $nginx_gz
cd $nginx
./configure --prefix=$nginx_url --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --with-http_v2_module  --user=www --group=www
if [ $? -eq 0 ];then
    echo    "Nginx預編譯完成,開始安裝"
else
    echo    "Nginx預編譯失敗,請檢查相關依賴包是否安裝"
    exit 4
fi
make &>/dev/null
make install &>/dev/null
if [ $? -eq 0 ];then
    echo    "Nginx安裝成功"
else
    echo    "Nginx安裝失敗"
    exit 5
fi
rm -rf  /usr/local/nginx/conf/nginx.conf
cd $RTDIR
cp nginx.conf /usr/local/nginx/conf/
cp proxy.conf /usr/local/nginx/conf/
mkdir /usr/local/nginx/vhosts
mkdir -p /opt/nginx/logs/
cp demo.conf /usr/local/nginx/vhosts/
ln -s $nginx_url/sbin/nginx /usr/local/bin
$nginx_url/sbin/nginx
netstat -anput | grep nginx &>/dev/null
if [ $? -eq 0 ];then
    echo    "Nginx啟動成功"
else
    echo    "Nginx啟動失敗"
    exit 6
fi

#建立快捷軟連結
cd /usr/bin
ln -s $nginx_url/sbin/nginx nginx

#

touch /usr/lib/systemd/system/nginx.service
cat >> /usr/lib/systemd/system/nginx.service << eof
[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=//usr/local/nginx/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target
eof


systemctl enable nginx
systemctl list-unit-files | grep nginx.service  | grep enabled
if [ $? -eq 0 ];then
    echo    "nginx.service開啟自啟設定成功"
else
    echo    "nginx.service開啟自啟設定失敗"
    exit 6
fi