单机部署NGINX脚本

#!/bin/bash
read -p "你想要安装哪个版本:"  get_nginx_version      ##通过变量的方式决定你要安装哪个版本。

if  [ ! -d  "/usr/local/nginx/" -a  ! -e "/tmp/nginx-$get_nginx_version.tar.gz" ]    ##判断本机是否存在NGINX
then
    wget http://nginx.org/download/nginx-$get_nginx_version.tar.gz -P /tmp
else
    echo nginx exist
fi

if [ -e /tmp/nginx-$get_nginx_version.tar.gz ]
then
    tar -zxvf /tmp/nginx-$get_nginx_version.tar.gz --directory /tmp/
else
    echo nginx not exist
fi
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
cd /tmp/nginx-$get_nginx_version
#考虑到后面需要配置TLS证书,加了--with-http_stub_status_module --with-http_ssl_module模块。
./configure --with-http_stub_status_module --with-http_ssl_module && make && make install
ln -s /usr/local/nginx/sbin/nginx /sbin/nginx

cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
# 服务描述
Description=The nginx service that provide http and reverse proxy service
# 配置 Nginx 服务在 network.target remote-fs.target 及 nss-lookup.target 都启动过后才会启动
# network.target remote-fs.target 及 nss-lookup.target 即使启动失败,也不影响 Nginx 服务启动
After=network.target remote-fs.target nss-lookup.target

[Service]
# Fork 模式启动进程
Type=forking
# 提供一个进程ID文件供 systemd 确定要监管的 Nginx 服务进程的 ID
# 根据规范,pid文件应该保存在 /run 文件夹下
PIDFile=/run/nginx.pid
# 启动前必须清理之前生成的 PID 文件
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
# 启动前先检查 nginx 的 conf 文件语法是否正确
ExecStartPre=/usr/local/nginx/sbin/nginx -t
# 执行启动 nginx 的脚本
ExecStart=/usr/local/nginx/sbin/nginx
# 执行启动脚本后,要留一定的时间让 nginx 进程生成 PID 文件,因此挂起 0.5 秒
ExecStartPost=/usr/bin/sleep 0.5
# restart 服务的命令
ExecReload=/bin/kill -s HUP $MAINPID
# stop 服务时候,向进程发送的信号
KillSignal=SIGQUIT
KillMode=process
PrivateTmp=true

[Install]
# 被 multi-user.target 依赖
WantedBy=multi-user.target
EOF
sed  -i.bak '9 c\pid     /run/nginx.pid;' /usr/local/nginx/conf/nginx.conf
systemctl daemon-reload
systemctl start nginx.service
echo nginx service is ok。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值