一、准备工作

为了编译 Nginx 源代码,需要标准的 GCC 编译器。GCC 的全称为 GNU Compiler Collection,其由 GNU 开发,并以 GPLLGPL 许可证发行,是自由的类 UNIX 即苹果电脑 Mac OSX 操作系统的标准编译器。因为 GCC 原本只能处理 C 语言,所以原名为 GNU C 语言编译器,后来得到快速扩展,可处理 C++FortranPascalObjective-CJava 以及 Ada 等其他语言。

除此之外,还需要 Automake 工具,以完成自动创建 Makefile 文件的工作。由于 Nginx 的一些模块需要依赖其他第三方库,通常有 pcre 库(支持 rewrite 模块)、zlib 库(支持 gzip 模块)和 openssl 库(支持 ssl 模块)等。

1.1 安装第三方库

如果已经安装过以上软件,则可以略过;如果没有,可以使用一下命令进行在线安装:

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

二、执行安装

2.1 创建 Nginx 用户

  1. groupadd nginx
  2. useradd nginx -g nginx -s /sbin/nologin -M

2.2 下载解压

Nginx 的下载地址在:http://nginx.org/en/download.html,我们推荐下载 Stable 的稳定版本。
nginx-download.png

  1. $ wget http://nginx.org/download/nginx-1.18.0.tar.gz
  2. $ tar zvxf nginx-1.18.0.tar.gz
  3. $ cd nginx-1.18.0
  4. $ ll
  5. total 752
  6. drwxr-xr-x. 6 root root 4096 Nov 18 10:54 auto
  7. -rw-r--r--. 1 root root 296463 Aug 13 20:51 CHANGES
  8. -rw-r--r--. 1 root root 452171 Aug 13 20:51 CHANGES.ru
  9. drwxr-xr-x. 2 root root 168 Nov 18 10:54 conf
  10. -rwxr-xr-x. 1 root root 2502 Aug 13 20:51 configure
  11. drwxr-xr-x. 4 root root 72 Nov 18 10:54 contrib
  12. drwxr-xr-x. 2 root root 40 Nov 18 10:54 html
  13. -rw-r--r--. 1 root root 1397 Aug 13 20:51 LICENSE
  14. drwxr-xr-x. 2 root root 21 Nov 18 10:54 man
  15. -rw-r--r--. 1 root root 49 Aug 13 20:51 README
  16. drwxr-xr-x. 9 root root 91 Nov 18 10:54 src

这里对解压完成后的部分目录和文件做个简单的介绍:

  • src 该目录存放了Nginx的所有源码;
  • man 该目录存放了Nginx的帮助文档;
  • html 该目录存放了两个html文件。这两个文件与Nginx服务器的运行相关,这两个文件的作用会在下文给出,这里不做赘述;
  • conf 该目录存放的是Nginx服务器的配置文件,包含Nginx服务器的基本配置文件;
  • auto 该目录存放了大量脚本文件,和configure脚本程序有关;
  • configure 该文件是Nginx软件的自动脚本程序。运行configure脚本一般会完成两项工作:一是检查环境,根据环境检查结果生成C代码;二是生成编译代码需要的Makefile文件。

2.3 编译安装

在介绍生成 Makefile 文件操作之前,先介绍一下 configure 脚本支持的常用选项:

选项 说明
—prefix=path 定义一个目录,存放服务器上的文件 ,也就是nginx的安装目录。默认使用 /usr/local/nginx。
—sbin-path=path 设置nginx的可执行文件的路径,默认为 prefix/sbin/nginx.
—conf-path=path 设置在nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf.
—pid-path=path 设置nginx.pid文件,将存储的主进程的进程号。安装完成后,可以随时改变的文件名 , 在nginx.conf配置文件中使用 PID指令。默认情况下,文件名 为prefix/logs/nginx.pid.
—error-log-path=path 设置主错误,警告,和诊断文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默认情况下,文件名 为prefix/logs/error.log.
—http-log-path=path 设置主请求的HTTP服务器的日志文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默认情况下,文件名 为prefix/logs/access.log.
—user=name 设置nginx工作进程的用户。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的用户名是nobody。
—group=name 设置nginx工作进程的用户组。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的为非特权用户。
—with-select_module 启用或禁用构建一个模块来允许服务器使用select()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll。
—without-select_module
—with-poll_module 启用或禁用构建一个模块来允许服务器使用poll()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll。
—without-poll_module
—without-http_gzip_module 不编译压缩的HTTP服务器的响应模块。编译并运行此模块需要zlib库。
—without-http_rewrite_module 不编译重写模块。编译并运行此模块需要PCRE库支持。
—without-http_proxy_module 不编译http_proxy模块。
—with-http_ssl_module 使用https协议模块。默认情况下,该模块没有被构建。建立并运行此模块的OpenSSL库是必需的。
—with-pcre=path 设置PCRE库的源码路径。PCRE库的源码(版本4.4 - 8.30)需要从PCRE网站下载并解压。其余的工作是Nginx的./ configure和make来完成。正则表达式使用在location指令和 ngx_http_rewrite_module 模块中。
—with-pcre-jit 编译PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)。
—with-zlib=path 设置的zlib库的源码路径。要下载从 zlib(版本1.1.3 - 1.2.5)的并解压。其余的工作是Nginx的./ configure和make完成。ngx_http_gzip_module模块需要使用zlib 。
—with-cc-opt=parameters 置额外的参数将被添加到CFLAGS变量。例如,当你在FreeBSD上使用PCRE库时需要使用:—with-cc-opt=”-I /usr/local/include。.如需要需要增加 select()支持的文件数量:—with-cc-opt=”-D FD_SETSIZE=2048”.
—with-ld-opt=parameters 设置附加的参数,将用于在链接期间。例如,当在FreeBSD下使用该系统的PCRE库,应指定:—with-ld-opt=”-L /usr/local/lib”.

了解了如上选项后,就可以根据实际情况使用 configure 脚本生成 Makefile 文件了。若上面的选项无法满足需求,可自行 Google 其他选项,上面介绍的只是其中的一小部分。

2.4 生成Makefile文件

使用下面的命令配置并生成Makefile文件:

  1. $ ./configure --prefix=/data/software/nginx-1.18.0 --with-http_ssl_module
  2. checking for OS
  3. + Linux 3.10.0-957.el7.x86_64 x86_64
  4. checking for C compiler ... found
  5. + using GNU C compiler
  6. + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
  7. checking for gcc -pipe switch ... found
  8. checking for -Wl,-E switch ... found
  9. checking for gcc builtin atomic operations ... found
  10. checking for C99 variadic macros ... found
  11. checking for gcc variadic macros ... found
  12. checking for gcc builtin 64 bit byteswap ... found
  13. ...
  14. ... 这里省略n
  15. ...
  16. checking for zlib library ... found
  17. creating objs/Makefile
  18. Configuration summary
  19. + using system PCRE library
  20. + using system OpenSSL library
  21. + using system zlib library
  22. nginx path prefix: "/data/software/nginx-1.18.0"
  23. nginx binary file: "/data/software/nginx-1.18.0/sbin/nginx"
  24. nginx modules path: "/data/software/nginx-1.18.0/modules"
  25. nginx configuration prefix: "/data/software/nginx-1.18.0/conf"
  26. nginx configuration file: "/data/software/nginx-1.18.0/conf/nginx.conf"
  27. nginx pid file: "/data/software/nginx-1.18.0/logs/nginx.pid"
  28. nginx error log file: "/data/software/nginx-1.18.0/logs/error.log"
  29. nginx http access log file: "/data/software/nginx-1.18.0/logs/access.log"
  30. nginx http client request body temporary files: "client_body_temp"
  31. nginx http proxy temporary files: "proxy_temp"
  32. nginx http fastcgi temporary files: "fastcgi_temp"
  33. nginx http uwsgi temporary files: "uwsgi_temp"
  34. nginx http scgi temporary files: "scgi_temp"
  • --prefix 配置置指定了Nginx的安装路径,其他的配置使用默认的配置。

  • 默认情况下,Nginx 的 https/ssl 协议模块是没有被构建的。需要增加 —with-http_ssl_module 建立并运行该模块的 OpenSSL 库。否则在配置 SSL 时可能会导致 nginx: [emerg] unknown directive “ssl” 报错。

  • 在运行过程中,configure 脚本调用 auto 目录中的各种脚本对系统环境及相关配置和设置进行了检查。

2.4 编译安装

得到了 Makefile 文件后,就可以编译源码了。

  1. $ make && make install

到此,我们已经安装后了一个最基本的 Nginx 服务器,其安装路径为 /data/software/nginx-1.18.0。

三、启动与测试

3.1 启动

使用下面的命令启动 nginx:

  1. $ ./sbin/nginx

3.2 测试

使用下面的命令执行测试:

  1. $ curl localhost

nginx-curl-localhost.png

若返回类似如上的结果,证明已成功启动。其实,上面返回的是目录/Nginx/html/下的index.html文件,可以使用cat /Nginx/html/index.html命令进行验证。

若是有图形界面,在浏览器访问 localhost 或者你本机的公网 ip,可以看到类似下图的页面:
welcome-to-nginx.png

四、常用命令

附上一些 nginx 的常用命令。

  1. nginx # 启动nginx
  2. nginx -s quit # 快速停止nginx
  3. nginx -V # 查看版本,以及配置文件地址
  4. nginx -v # 查看版本
  5. nginx -s reload|reopen|stop|quit # 重新加载配置|重启|快速停止|安全关闭nginx
  6. nginx -h # 帮助

五、参考资料

  1. sprinkle_liz,《Centos 7 下编译安装 Nginx》,简书