1.准备工作,安装nginx需要依赖以下三个包,也就是说得先安装这三个。。
- SSL功能需要openssl库,下载地址:https://www.openssl.org/source/
- gzip模块需要zlib库,下载地址:http://www.zlib.net/
- rewrite模块需要pcre库,下载地址:https://ftp.pcre.org/pub/pcre/
- Nginx的安装包:下载地址为:http://nginx.org/en/download.html
将以上下载的几个包使用 Xftp 工具上传到 Linux 系统中,一般我们是上传到 /usr/local 此路径下。
[root@centos7u6 ~]# cd /usr/local
[root@centos7u6 local]# ll
total 220244
-rw-r--r-- 1 root root 1064925 Jul 14 16:08 nginx-1.21.1.tar.gz
-rw-r--r-- 1 root root 9823400 Jun 23 15:17 openssl-1.1.1k.tar.gz
-rw-r--r-- 1 root root 2053336 Jul 14 16:33 pcre-8.38.tar.gz
-rw-r--r-- 1 root root 607698 Jun 23 15:21 zlib-1.2.11.tar.gz
注:以上几个版本经过本人亲测是没有问题的,nginx-1.20 之前的版本,官方已经宣布是有漏洞的;pcre-8.00 版本有漏包,可能会导致后续 make 操作无法执行。
2.安装三个依赖包
①首先安装 openssl ,先进入到 /usr/local/ 路径下,再依次执行以下命令:
a. tar -zxvf openssl-1.1.1k.tar.gz
b. cd openssl-1.1.1k
c. ./config
d. make
e. make install
②接下来安装 zlib ,方法同上,先进入到 /usr/local/ 路径下,再依次执行以下命令:
a. tar -zxvf zlib-1.2.11.tar.gz
b. cd zlib-1.2.11
c. ./configure
d. make
e. make install
③接下来安装 pcre ,方法同上,先进入到 /usr/local/ 路径下,再依次执行以下命令:
a. tar -zxvf pcre-8.38.tar.gz
b. cd pcre-8.38
c. ./configure
d. make
e. make install
注:在安装 pcre 时,执行 ./configure 可能会出现以下报错:
error: You need a C++ compiler for C++ support
解决方法: 执行 yum install -y gcc gcc-c++ 安装 gcc
3.接下来正式安装NGINX
先进入到 /usr/local/ 路径下,再依次执行以下命令:
a. tar -zxvf nginx-1.21.1.tar.gz
b. cd nginx-1.21.1
c. ./configure --with-pcre=../pcre-8.38/ --with-zlib=../zlib-1.2.11/ --with-openssl=../openssl-1.1.1k/
d. make
e. make install
注:执行 ./configure 可能会出现以下报错:
error: You need a C++ compiler for C++ support
解决方法:同上执行 yum install -y gcc gcc-c++ 安装 gcc
4.检测NGINX是否安装成功
依次执行以下命令:
[root@centos7u6 ~]# cd /usr/local/nginx/sbin
[root@centos7u6 sbin]# ll
total 11620
-rwxr-xr-x 1 root root 7878392 Jul 14 17:25 nginx
-rwxr-xr-x 1 root root 4015408 Jul 14 16:56 nginx.old
[root@centos7u6 sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
看到 ok 和 successful 我们就知道 nginx 已经安装成功了,接下来执行启动命令 ./nginx 就大功告成了。
[root@centos7u6 sbin]# ./nginx