# NGINX 安装

# 1、安装

# 安装依赖库

# 安装gcc环境
yum install -y gcc-c++
# 安装PCRE库,用于解析正则表达式
yum install -y pcre pcre-devel
# zlib压缩和解压缩依赖
yum install -y zlib zlib-devel
# SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https
yum install -y openssl openssl-devel
1
2
3
4
5
6
7
8

或者可以放在一起执行

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
1

# 下载

wget http://nginx.org/download/nginx-1.20.2.tar.gz
#国内镜像站,会快一点
wget https://mirrors.huaweicloud.com/nginx/nginx-1.20.2.tar.gz
1
2
3

# 移动

mv nginx-1.20.2.tar.gz /usr/local
cd /usr/local
1
2

# 解压

tar -xvf nginx-1.20.2.tar.gz
1

# 进行 configure 配置

#新建临时文件夹
mkdir /var/temp/nginx -p
#进入源码目录
cd /usr/local/nginx-1.20.2
# 开启监控模块、开启https和http 2.0
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx/nginx.lock \
--error-log-path=/usr/local/nginx/log/error.log \
--http-log-path=/usr/local/nginx/log/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_v2_module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

说明

执行 configure 时,可使用的命令

命令 解释
–prefix 指定 nginx 安装目录
–pid-path 指向 nginx 的 pid
–lock-path 锁定安装文件,防止被恶意篡改或误操作
–error-log 错误日志
–http-log-path http 日志
–with-http_gzip_static_module 启用 gzip 模块,在线实时压缩输出数据流
–http-client-body-temp-path 设定客户端请求的临时目录
–http-proxy-temp-path 设定 http 代理临时目录
–http-fastcgi-temp-path 设定 fastcgi 临时目录
–http-uwsgi-temp-path 设定 uwsgi 临时目录
–http-scgi-temp-path 设定 scgi 临时目录

# 编译

# 编译及安装
make && make install
1
2

# 启动命令

/usr/local/nginx/sbin/nginx //启动

/usr/local/nginx/sbin/nginx -s stop //关闭

/usr/local/nginx/sbin/nginx -s reload //重启

1
2
3
4
5
6

# 2、设置开机自启

编辑启动文件 nginx.service

cat >> /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/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

说明

Description:描述服务 After:描述服务类别 [Service]服务运行参数的设置 Type=forking 是后台运行的形式 ExecStart 为服务的具体运行命令 ExecReload 为重启命令 ExecStop 为停止命令 PrivateTmp=True 表示给服务分配独立的临时空间 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径 [Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为 3

开机自启

systemctl enable nginx.service
1

# 3、设置环境变量(全局可以使用 nginx 命令)

在/etc/profile.d 文件夹下面创建 nginx.sh 文件

cat >> /etc/profile.d/nginx.sh <<EOF
NGINX_HOME=/usr/local/nginx
PATH=\$PATH:\$NGINX_HOME/sbin
export PATH NGINX_HOME
EOF
1
2
3
4
5

然后执行如下命令让环境变量生效

source /etc/profile
1