Nginx_Nginx 安装 与 增加模块

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010003835/article/details/86583614

本文主要讲解 Nginx 安装,Nginx 的版本为   nginx/1.14.2

安装文档主要参考的资料:

1.《深入理解Nginx 模块开发与架构解析》 第2版  1.3节

2.已安装nginx动态添加模块

https://www.cnblogs.com/lixigang/articles/5130052.html

1.安装nginx前的准备

2.编译安装 以及 重要的参数

3.编译好的 nginx 增加模块

大前提:这里,我们通过编译安装。可以通过此种方式,灵活配置Nginx, 如 :增加模块等。 

1.安装nginx前的准备

首先,我们要检查下 Linux 的内核版本。这里我们需要 epoll 模型,来解决时间的多路复用。Linux 上使用select 或 poll 来解决事件的多路复用。

我们需要 nginx的内核版本为 2.6及以上的版本。

[root@nginx1 my_bin]# uname -a
Linux nginx1 3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

可以看到已经是 2.6 以上。

好了,我们开始装Nginx 的基础组件。

1.GCC 编译器

GCC(GNU Compiler Collection) 可用来编译C 语言程序。

如果有开发Nginx模块是必须直接编译源代码进行的。

yum install -y gcc

除了 C编译器,我们还需要安装 C++ 编译器

yum install -y gcc-c++

2.PRCE库

PRCE(Perl Compatible Regular Expressions, Perl 正则表达式) 。该库支持正则表达式,由RegEx 演化而来。Nginx 需要使用正则表达式,那么编译Nginx 安装 PRCE 库。

pcre-devel 是使用 PRCE 做二次开发时 所需要的开发库,包括头文件。这也是编译Nginx 所必需使用的。

yum install -y pcre pcre-devel

3.zlib库

zlib 库用于对 Http 包的内容做 gzip 格式的压缩,如果在 nginx.conf 里配置了 gzip on, 并指定对于某些类型(content-type)的HTTP 响应使用 gzip 来进行压缩以减少网络传输。那么在编译时就必须把 zlib 编译进 Nginx.

zlib 是直接使用的库, zlib-devel 是二次开发所需要的库。

yum install -y zlib zlib-devel

4.OpenSSL 开发库

如果不仅仅需要支持Http.还需要在SSL 协议上传输 HTTP, 那么就需要 OpenSSL.

yum install -y openssl openssl-devel

.

2.编译安装 以及 重要的参数

yum 安装好基础需要的软件后,我们就可以进行编译安装了。

在编译安装的时候,我们需要注意一些参数的配置。

1.首先,我们把 tgz 包解压出来。

2.然后,我们进行预编译,进行一些设置。这个流程 与 一般的 makefile 安装一致。

这里,我们要学会自己看帮助文档:

./configure --help

[root@nginx1 nginx-1.14.2]# ./configure --help

  --help                             print this message

  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes

  --build=NAME                       set build name
  --builddir=DIR                     set build directory

  --with-select_module               enable select module
  --without-select_module            disable select module
  --with-poll_module                 enable poll module
  --without-poll_module              disable poll module

  --with-threads                     enable thread pool support

  --with-file-aio                    enable file AIO support

  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-http_v2_module              enable ngx_http_v2_module
  --with-http_realip_module          enable ngx_http_realip_module
  --with-http_addition_module        enable ngx_http_addition_module
  --with-http_xslt_module            enable ngx_http_xslt_module
  --with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
  --with-http_image_filter_module    enable ngx_http_image_filter_module
  --with-http_image_filter_module=dynamic
                                     enable dynamic ngx_http_image_filter_module
  --with-http_geoip_module           enable ngx_http_geoip_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_sub_module             enable ngx_http_sub_module
  --with-http_dav_module             enable ngx_http_dav_module
  --with-http_flv_module             enable ngx_http_flv_module
  --with-http_mp4_module             enable ngx_http_mp4_module
  --with-http_gunzip_module          enable ngx_http_gunzip_module
  --with-http_gzip_static_module     enable ngx_http_gzip_static_module
  --with-http_auth_request_module    enable ngx_http_auth_request_module
  --with-http_random_index_module    enable ngx_http_random_index_module
  --with-http_secure_link_module     enable ngx_http_secure_link_module
  --with-http_degradation_module     enable ngx_http_degradation_module
  --with-http_slice_module           enable ngx_http_slice_module
  --with-http_stub_status_module     enable ngx_http_stub_status_module

  --without-http_charset_module      disable ngx_http_charset_module
  --without-http_gzip_module         disable ngx_http_gzip_module
  --without-http_ssi_module          disable ngx_http_ssi_module
  --without-http_userid_module       disable ngx_http_userid_module
  --without-http_access_module       disable ngx_http_access_module
  --without-http_auth_basic_module   disable ngx_http_auth_basic_module
  --without-http_mirror_module       disable ngx_http_mirror_module
  --without-http_autoindex_module    disable ngx_http_autoindex_module
  --without-http_geo_module          disable ngx_http_geo_module
  --without-http_map_module          disable ngx_http_map_module
  --without-http_split_clients_module disable ngx_http_split_clients_module
  --without-http_referer_module      disable ngx_http_referer_module
  --without-http_rewrite_module      disable ngx_http_rewrite_module
  --without-http_proxy_module        disable ngx_http_proxy_module
  --without-http_fastcgi_module      disable ngx_http_fastcgi_module
  --without-http_uwsgi_module        disable ngx_http_uwsgi_module
  --without-http_scgi_module         disable ngx_http_scgi_module
  --without-http_grpc_module         disable ngx_http_grpc_module
  --without-http_memcached_module    disable ngx_http_memcached_module
  --without-http_limit_conn_module   disable ngx_http_limit_conn_module
  --without-http_limit_req_module    disable ngx_http_limit_req_module
  --without-http_empty_gif_module    disable ngx_http_empty_gif_module
  --without-http_browser_module      disable ngx_http_browser_module
  --without-http_upstream_hash_module
                                     disable ngx_http_upstream_hash_module
  --without-http_upstream_ip_hash_module
                                     disable ngx_http_upstream_ip_hash_module
  --without-http_upstream_least_conn_module
                                     disable ngx_http_upstream_least_conn_module
  --without-http_upstream_keepalive_module
                                     disable ngx_http_upstream_keepalive_module
  --without-http_upstream_zone_module
                                     disable ngx_http_upstream_zone_module

  --with-http_perl_module            enable ngx_http_perl_module
  --with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
  --with-perl_modules_path=PATH      set Perl modules path
  --with-perl=PATH                   set perl binary pathname

  --http-log-path=PATH               set http access log pathname
  --http-client-body-temp-path=PATH  set path to store
                                     http client request body temporary files
  --http-proxy-temp-path=PATH        set path to store
                                     http proxy temporary files
  --http-fastcgi-temp-path=PATH      set path to store
                                     http fastcgi temporary files
  --http-uwsgi-temp-path=PATH        set path to store
                                     http uwsgi temporary files
  --http-scgi-temp-path=PATH         set path to store
                                     http scgi temporary files

  --without-http                     disable HTTP server
  --without-http-cache               disable HTTP cache

  --with-mail                        enable POP3/IMAP4/SMTP proxy module
  --with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  --without-mail_pop3_module         disable ngx_mail_pop3_module
  --without-mail_imap_module         disable ngx_mail_imap_module
  --without-mail_smtp_module         disable ngx_mail_smtp_module

  --with-stream                      enable TCP/UDP proxy module
  --with-stream=dynamic              enable dynamic TCP/UDP proxy module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_realip_module        enable ngx_stream_realip_module
  --with-stream_geoip_module         enable ngx_stream_geoip_module
  --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --without-stream_limit_conn_module disable ngx_stream_limit_conn_module
  --without-stream_access_module     disable ngx_stream_access_module
  --without-stream_geo_module        disable ngx_stream_geo_module
  --without-stream_map_module        disable ngx_stream_map_module
  --without-stream_split_clients_module
                                     disable ngx_stream_split_clients_module
  --without-stream_return_module     disable ngx_stream_return_module
  --without-stream_upstream_hash_module
                                     disable ngx_stream_upstream_hash_module
  --without-stream_upstream_least_conn_module
                                     disable ngx_stream_upstream_least_conn_module
  --without-stream_upstream_zone_module
                                     disable ngx_stream_upstream_zone_module

  --with-google_perftools_module     enable ngx_google_perftools_module
  --with-cpp_test_module             enable ngx_cpp_test_module

  --add-module=PATH                  enable external module
  --add-dynamic-module=PATH          enable dynamic external module

  --with-compat                      dynamic modules compatibility

  --with-cc=PATH                     set C compiler pathname
  --with-cpp=PATH                    set C preprocessor pathname
  --with-cc-opt=OPTIONS              set additional C compiler options
  --with-ld-opt=OPTIONS              set additional linker options
  --with-cpu-opt=CPU                 build for the specified CPU, valid values:
                                     pentium, pentiumpro, pentium3, pentium4,
                                     athlon, opteron, sparc32, sparc64, ppc64

  --without-pcre                     disable PCRE library usage
  --with-pcre                        force PCRE library usage
  --with-pcre=DIR                    set path to PCRE library sources
  --with-pcre-opt=OPTIONS            set additional build options for PCRE
  --with-pcre-jit                    build PCRE with JIT compilation support

  --with-zlib=DIR                    set path to zlib library sources
  --with-zlib-opt=OPTIONS            set additional build options for zlib
  --with-zlib-asm=CPU                use zlib assembler sources optimized
                                     for the specified CPU, valid values:
                                     pentium, pentiumpro

  --with-libatomic                   force libatomic_ops library usage
  --with-libatomic=DIR               set path to libatomic_ops library sources

  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

  --with-debug                       enable debug logging

--prefix=PATH  

意义:Nginx 安装部署后的根目录。

默认值:默认值为 /usr/local/nginx 目录。注意,这个目标的设置会影响其他参数中的相对目录。

       例如,如果设置了 --sbin-path=sbin/nginx , 那么实际上可执行文件会被放到 /usr/local/nginx/sbin/nginx 中。

--conf-path=PATH

意义:配置文件的存放路径。

默认值:<prefix>/conf/nginx.conf

除此之外:

configure 由shell 脚本编写。中间会调用 <nginx-source>/auto/目录下的脚本

编译安装比较简单,指令如下: 

./configure ...

make

make install 

安装好之后,我们执行 sbin 目录下的nginx。

 ps -ef nginx 看到 nginx 后,证明我们安装没有问题。

[root@nginx1 nginx-1.14.2]# ps -ef | grep nginx
root      53568      1  0 12:22 ?        00:00:00 nginx: master process nginx
nobody    53569  53568  0 12:22 ?        00:00:00 nginx: worker process
root      59045  55771  0 21:11 pts/1    00:00:00 grep --color=auto nginx

 

3.编译好的 nginx 增加模块

基本安装好的nginx  功能比较有限。这个时候就需要我们去安装扩展的第三方组件。

注意:

    源码包不要删除。安装第三方包的时候,需要重新编译。

nginx echo 是一个比较有用的第三方组件。项目在 github 上管理,地址如下:

https://github.com/openresty/echo-nginx-module

我们把代码下载下来,进行重新编译安装。

1.查看已安装的扩展包

我们可以通过以下的指令去查看已经安装了那些扩展包。

nginx  -V   (注意 V 大写)

[root@nginx1 nginx-1.14.2]# nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
configure arguments: --prefix=/opt/nginx --add-module=/opt/nginx_source/nginx-extend/echo-nginx-module-master

2.重新编译

加入需要安装的模块,重新编译,如这里添加–add-module=/data/software/ngx_http_google_filter_module

# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=/data/software/ngx_http_substitutions_filter_module --add-module=/data/software/ngx_http_google_filter_module
# make    //千万不要make install,不然就真的覆盖了

多个参数,用多个 --add-module 指令。

讲解:

   这里,我们增加包,相当于 nginx 在重新编译的时候,会把这些源码打包进去,进行重新编译。所以,之后这些代码是否还在编译时所在的位置并不重要。

 

3.替换编译文件

重新编译后的nginx 可执行文件在 objs 目录下,我们只需要将 nginx 文件替换即可

[root@nginx1 objs]# tree ./
./
├── addon
│   └── src
│       ├── ngx_http_echo_echo.o
│       ├── ngx_http_echo_filter.o
│       ├── ...
│       └── ngx_http_echo_var.o
├── autoconf.err
├── Makefile
├── nginx
├── nginx.8
├── ngx_auto_config.h
├── ngx_auto_headers.h
├── ngx_modules.c
├── ngx_modules.o
└── src
    ├── core
    │   ├── nginx.o
    │   ├── ...
    │   └── ngx_times.o
    ├── event
    │   ├── modules
    │   │   └── ngx_epoll_module.o
    │   ├── ngx_event_accept.o
    │   └── ...
    ├── http
    │   ├── modules
    │   │   ├── ngx_http_access_module.o
    │   │   ├── ...
    │   │   ├── ngx_http_uwsgi_module.o
    │   │   └── perl
    │   ├── ngx_http_copy_filter_module.o
    │   ├── ngx_http_core_module.o
    │   ├── ...
    │   ├── ngx_http_write_filter_module.o
    │   └── v2
    ├── mail
    ├── misc
    ├── os
    │   ├── unix
    │   │   ├── ngx_alloc.o
    │   │   ├── ngx_channel.o
    │   │   ├── ...
    │   │   └── ngx_writev_chain.o
    │   └── win32
    └── stream

16 directories, 137 files

替换nginx 可执行文件前,我们做一下备份:

# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
# cp ./objs/nginx /usr/local/nginx/sbin/

这完成了已经安装的Nginx,增加模块的目的。

猜你喜欢

转载自blog.csdn.net/u010003835/article/details/86583614