---
title: 通过源码编译安装
description: "下文以Nginx 1.14.2版本为例描述通过源码编译安装Nginx的操作步骤，其他版本的Nginx也可参考本章节。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengwebs/ecosystemEnable/Nginx/kunpengnginx_02_0008.html
sourcePath: /source/zh/kunpengwebs/ecosystemEnable/Nginx/kunpengnginx_02_0008.html
indexId: a1674d4124055993d0f0a28e01884b47953f716b10f48d71282d9f1c3b8aa09070
---
# 通过源码编译安装

下文以Nginx 1.14.2版本为例描述通过源码编译安装Nginx的操作步骤，其他版本的Nginx也可参考本章节。

适合通过源码编译安装的Nginx版本与操作系统版本如表1所示。


**表1 适配的Nginx版本与操作系统版本**

| 适用的软件版本 | 是否适配 CentOS 7.6 | 是否适配 CentOS 8.1 | 是否适配 openEuler 20.03 LTS SP1 | 是否适配 openEuler 22.03 LTS SP1 |
| --- | --- | --- | --- | --- |
| Nginx 1.14.2 | 是 | 是 | 是 | 是 |
| Nginx 1.19.3 | 是 | 否 | 否 | 是 |
| Nginx 1.21.5 | 否 | 否 | 否 | 是 |


以下命令中，“1.14.2”表示Nginx的版本号。安装其他版本的Nginx时，请将“1.14.2”替换为对应的Nginx版本号。

1. 获取Nginx源码。

  a. 在本地浏览器下载源码。

    -       Nginx 1.14.2(https://nginx.org/download/nginx-1.14.2.tar.gz)

    -       Nginx 1.19.3(https://nginx.org/download/nginx-1.19.3.tar.gz)

    -       Nginx 1.21.5(https://nginx.org/download/nginx-1.21.5.tar.gz)

  b. 将源码包上传到服务器“/home”目录下。
    若服务器可以访问网络，则通过wget命令直接下载源码。此处以使用wget命令下载Nginx 1.14.2的源码为例进行说明。下载其他Nginx版本的源码时，替换掉Nginx源码的下载链接即可。 1 2 cd /home wget https://nginx.org/download/nginx-1.14.2.tar.gz --no-check-certificate

2. 解压Nginx安装包。
  1 tar -xvf nginx-1.14.2.tar.gz

3. 进入Nginx源码目录。
  1 cd nginx-1.14.2/

4. 配置Nginx。

  - CentOS 7.6下安装Nginx 1.19.3版本，或CentOS 8.1下安装Nginx 1.14.2版本，使用如下命令配置Nginx。
    1 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_perl_module --with-pcre --with-cc-opt="-fPIC"

  - CentOS 7.6下安装Nginx 1.14.2版本，使用如下命令配置Nginx。
    1 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_perl_module --with-pcre --with-openssl=/home/openssl-OpenSSL_1_1_1a

  - openEuler 20.03或openEuler 22.03下安装Nginx 1.14.2版本，使用如下命令配置Nginx。
    1 ./configure --prefix=/usr/local/nginx --with-http_ssl_module

Nginx的配置参数选项说明如表2所示。

**表2 配置参数选项说明**

| 参数选项 | 说明 |
| --- | --- |
| \-\-prefix=PATH | 指定Nginx的安装目录。默认安装目录为“/usr/local/nginx”。 |
| \-\-with\-http\_ssl\_module | 使用HTTPS协议模块。默认情况下，该模块没有被构建。前提是OpenSSL与openssl\-devel已安装。 |
| \-\-with\-http\_realip\_module | 通过这个模块允许我们改变客户端请求头中客户端IP地址值（例如X\-Real\-IP或X\-Forwarded\-For），意义在于能够使得后台服务器记录原始客户端的IP地址。 |
| \-\-with\-http\_addition\_module | 可以实现在响应之前或之后追加文本内容。 |
| \-\-with\-http\_flv\_module | 加载流媒体（flash）。 |
| \-\-with\-http\_mp4\_module | 加载流媒体（mp4）。 |
| \-\-with\-pcre | 设置PCRE库的源码路径，如果已通过yum方式安装，使用\-\-with\-pcre自动找到库文件。使用\-\-with\-pcre=PATH时，需要从PCRE网站下载pcre库的源码（版本4.4\-8.30）并解压。perl正则表达式使用在location指令和ngx\_http\_rewrite\_module模块中。 |
| \-\-with\-openssl=PATH | 指定OpenSSL（版本1.1.1a）的源码解压目录。 |
| \-\-with\-zlib=PATH | 指定zlib（版本1.1.3\-1.2.5）的源码解压目录。在默认就启用的网络传输压缩模块ngx\_http\_gzip\_module时需要使用zlib。 |
| \-\-with\-http\_stub\_status\_module | 用来看管Nginx的当前状态。 不需要配置with\-http\_stub\_status\_module模块，该统计模块会影响Nginx性能。 |
| \-\-add\-module=PATH | 添加第三方外部模块，如nginx\-sticky\-module\-ng或缓存模块。每次添加新的模块都要重新编译。（Tengine可以在新加入module时无需重新编译。） |


5. 编译并安装Nginx。
  1 make -j60 && make install

  -j60参数表示充分利用CPU多核优势，加快编译速度。参数-j后数字为CPU核数，可通过lscpu或cat /proc/cpuinfo | grep processor | wc -l查看，此数值应小于等于CPU核数。

6. 查看Nginx安装目录。
  1 ls /usr/local/nginx

7. 查看Nginx版本。
  1 /usr/local/nginx/sbin/nginx -v
