nginx部署

内容详细

nginx源码包部署

源码包部署是考虑到生产环境中需要将nginx安装到指定的目录,而yum安装无法做到这一点,所以我们必须掌握源码包安装nginx.

#!/bin/bash
# 安装所需要的依赖包
yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree >/dev/null 2>&1
echo "已安装相关依赖"
# 官网下载源码包(稳定版本)
wget https://nginx.org/download/nginx-1.18.0.tar.gz >/dev/null 2>&1
echo "源码包下载完毕!"
# 解压压缩包
tar -xf nginx-1.18.0.tar.gz
echo "源码包解压完毕"
# 创建用户
groupadd www -g 666 >/dev/null 2>&1
useradd www -u 666 -g 666 -s /sbin/nologin -M >/dev/null 2>&1
echo "用户创建完毕"
# 编译安装
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx-1.18.0 --user=www --group=www --without-http_gzip_module 
echo "编译完毕"
make >/dev/null 2>&1 && make install >/dev/null 2>&1
echo "安装完毕!"
# 创建软链接,方便后续升级
ln -s /usr/local/nginx-1.18.0 /usr/local/nginx
# 配置环境变量
cat >> /etc/profile.d/nginx.sh <<EOF
export PATH=$PATH:/usr/local/nginx/sbin
EOF
source /etc/profile
# 验证配置是否成功
nginx -V
echo "nginx环境变量配置完成!"
# 配置systemctl管理
# 一定要使用绝对路径
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid  
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true 

[Install]
WantedBy=multi-user.target
EOF
# 加载新的unit(*.service)配置文件
systemctl daemon-reload
# 启动nginx,并加入开机自启
systemctl enable --now nginx
echo "nginx已启动..."

官方源安装(如果不指定安装目录的话)

#!/bin/bash
# 配置官方源,nginx官网: https://nginx.org/
cd /etc/yum.repos.d && touch nginx.repo
cat > /etc/yum.repos.d/nginx.repo << EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
echo "官方源配置完毕"
# 清除,生成缓存
yum clean all
yum makecache
# 查看有哪些nginx版本可以安装
yum list nginx --show-duplicates
安装指定nginx的指定版本
yum install -y nginx-1.18.0 >/dev/null 2>&1
# 查看安装的nginx版本
nginx -v
# 启动nginx并加入开机自启
systemctl enable --now nginx
echo "nginx已启动."
posted @ 2021-08-10 22:17  堇雪月寒风  阅读(48)  评论(0编辑  收藏  举报
Live2D