该篇文章是在 CentOS 7 上安装 Nginx 的教程。

查看安装nginx所需要的环境

1
2
3
4
5
6
#查看 C++ 环境是否安装(查看版本号)
gcc -v
#查看 zlib 是否安装
cat /usr/lib64/pkgconfig/zlib.pc
#查看 pcre 是否安装(查版本号)
pcre-config --version

配置 nginx 安装所需的环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#一次安装4个插件
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

#一次安装如果有问题,可以试一下分开安装(上面命令执行成功了就无需执行以下命令了)
#安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境
yum install gcc-c++

#pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库
yum install -y pcre pcre-devel

#zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip,所以需要安装
yum install -y zlib zlib-devel

#nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos安装 OpenSSL 库
yum install -y openssl openssl-devel

使用wget命令下载

确保系统已经安装了wget,如果没有安装,执行 yum install wget 安装。

1.进入 usr/local 里面创建 nginx 文件夹,方便后期删除干净

1
2
3
4
5
6
#进入usr下的local目录
cd usr/local
#在local目录下创建 mysql 文件夹
mkdir nginx
#进入nginx目录
cd nginx

2.通过 wget 下载 nginx 安装包

1
2
# Mainline version
wget https://nginx.org/download/nginx-1.23.4.tar.gz

3.解压,并进入解压后的目录

1
2
3
4
#解压
tar xvf nginx-1.23.4.tar.gz
#进入解压后的目录
cd nginx-1.23.4

4.编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
./configure --prefix=/usr/local/nginx \
--with-http_stub_status_module \
--without-http_fastcgi_module \
--without-http_upstream_ip_hash_module \
--without-http_autoindex_module \
--without-http_ssi_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--without-http_uwsgi_module \
--without-http_scgi_module \
--without-http_memcached_module \
--with-http_ssl_module

#编译
make
#安装
make install

5.启动和关闭nginx服务

1
2
3
4
5
6
7
8
9
10
11
#启动
/usr/local/nginx/sbin/nginx

#校验配置文件
/usr/local/nginx/sbin/nginx -t

#重载配置
/user/local/nginx/sbin/nginx -s reload

#关闭
/user/local/nginx/sbin/nginx -s stop