从源码编译和安装 nginx

通过源码的方式安装 nginx 比 RPM 包安装的优势:

① 源码安装比RPM安装更加灵活,安装的时候可以添加特定的模块(来自 nginx 或 第三方)

② 可以应用到最新的安全补丁


一、安装 nginx 依赖项

1)PCRE - 支持正则表达式。NGINX CoreRewrite模块需要

[root@svr5 ~]# cd /data
[root@svr5 data]# ls
[root@svr5 data]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz  
#1.下载 pcre
[root@svr5 data]# tar -zxf pcre-8.42.tar.gz        # 2. 解压 pcre 源码包
[root@svr5 data]# 
[root@svr5 data]# cd pcre-8.42                     # 3. 进入 pcre 解压目录
[root@svr5 pcre-8.42]# ./configure                 # 4. 配置 pcre
[root@svr5 pcre-8.42]# make                        # 5. 编译 pcre
[root@svr5 pcre-8.42]# make install                # 6. 安装 pcre

2)zlib - 支持标头压缩。NGINX Gzip模块需要

[root@svr5 ~]# cd /data
[root@svr5 data]# ls
pcre-8.42  pcre-8.42.tar.gz
[root@svr5 data]# wget http://zlib.net/zlib-1.2.11.tar.gz       # 1. 下载 zlib 源码包
[root@svr5 data]# tar -zxf zlib-1.2.11.tar.gz                   # 2. 解压 zlib 源码包
[root@svr5 data]# cd zlib-1.2.11                                # 3. 进入 zlib 解压目录
[root@svr5 zlib-1.2.11]# ./configure                            # 4. 配置 zlib
[root@svr5 zlib-1.2.11]# make                                   # 5. 编译 zlib
[root@svr5 zlib-1.2.11]# make install                           # 6. 安装 zlib

3)OpenSSL - 支持HTTPS协议。NGINX SSL模块和其他人员要求

[root@svr5 ~]# cd /data
[root@svr5 data]# ls
[root@svr5 data]# openssl version                       # 0. 查看当前 openssl 版本
OpenSSL 1.0.1e-fips 11 Feb 2013
[root@svr5 data]# wget http://www.openssl.org/source/openssl-1.1.1b.tar.gz  # 1.下载openssl源码
[root@svr5 data]# tar -zxf openssl-1.1.1b.tar.gz        # 2. 解压 openssl 源码包
[root@svr5 data]# cd openssl-1.1.1b                     # 3. 进入 解压目录
[root@svr5 openssl-1.1.1b]# ./config --prefix=/usr      # 4. 配置 openssl 
[root@svr5 openssl-1.1.1b]# make                        # 5. 编译 openssl
[root@svr5 openssl-1.1.1b]# make install                # 6. 安装 openssl

[root@svr5 data]# openssl version                       # 7. 查看 安装后 openssl 版本
OpenSSL 1.1.1b  26 Feb 2019
[root@svr5 data]# 



二、源码安装 nginx

0)创建 nginx 用户

[root@svr5 data]# useradd -s /sbin/nologin nginx

1)下载和解压缩最新稳定版本的源文件

[root@svr5 data]# wget https://nginx.org/download/nginx-1.14.2.tar.gz --no-check-certificate
# 1. 下载 nginx 源码包
[root@svr5 data]# tar zxf nginx-1.14.2.tar.gz        # 2. 解压 nginx 源码包
[root@svr5 data]# cd nginx-1.14.2                    # 3. 进入解压目录

2)配置、编译以及安装 nginx

[root@svr5 nginx-1.14.2]# ./configure  \             # 1. 配置
> --sbin-path=/usr/local/nginx/nginx  \
> --conf-path=/usr/local/nginx/nginx.conf  \
> --pid-path=/usr/local/nginx/nginx.pid  \
> --user=nginx  \
> --group=nginx  \
> --with-http_ssl_module  \
> --with-stream  \
> --with-pcre=../pcre-8.42  \
> --with-zlib=../zlib-1.2.11  \
> --without-http_empty_gif_module

[root@svr5 nginx-1.14.2]# make                       # 2. 编译 nginx

[root@svr5 nginx-1.14.2]# make install               # 3. 安装 nginx

3)nginx 服务启动

[root@svr5 nginx]# /usr/local/nginx/nginx
[root@svr5 nginx]# netstat -naptu | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      26303/nginx         
[root@svr5 nginx]# 

4)nginx 服务停止

[root@svr5 nginx]# /usr/local/nginx/nginx -s stop
[root@svr5 nginx]# netstat -naptu | grep nginx
[root@svr5 nginx]# 

5)nginx 重新加载配置文件

[root@svr5 nginx]# /usr/local/nginx/nginx -s reload

猜你喜欢

转载自blog.csdn.net/u010559460/article/details/88822225