zoukankan      html  css  js  c++  java
  • nginx学习之详细安装篇(二)

    1. 选择稳定版还是主线版

    主线版:包含最新的功能且修复了已知的bug,但该版本可能会含有一些属于实验性的模块,同时可能会引入新的其他bug,所以如果只是做测试使用,可以使用主线版。

    稳定版:不包含最新的功能,但修复了严重的bug,建议用在生产环境。

    2. 使用预编译安装包还是源码安装包

    预编译包:这种是最简单和最快速的用于安装源码nginx的方式。预编译包几乎包含了所有的官方模块,且大部分的操作系统都能安装。

    源码安装:这种方式的特点是灵活,你可以只安装需要的模块,或者第三方模块,或者一些最近发布的安全补丁。

    3. 源码安装nginx

    (1) 安装nginx的依赖

    首先是PCRE库nginx的core模块rewrite模块依赖于这个库,同时这个库还提供了正则表达式的支持。

    $ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
    $ tar -zxf pcre-8.40.tar.gz
    $ cd pcre-8.40
    $ ./configure
    $ make
    $ sudo make install

    然后是zlib库,Gzip模块需要这个库来做headers压缩。

    $ wget http://zlib.net/zlib-1.2.11.tar.gz
    $ tar -zxf zlib-1.2.11.tar.gz
    $ cd zlib-1.2.11
    $ ./configure
    $ make
    $ sudo make install

    最后是openssl库nginx ssl模块需要这个库来支持https协议。

    $ wget http://www.openssl.org/source/openssl-1.0.2f.tar.gz
    $ tar -zxf openssl-1.0.2f.tar.gz
    $ cd openssl-1.0.2f
    $ ./configure darwin64-x86_64-cc --prefix=/usr
    $ make
    $ sudo make install

    (2) 下载nginx源码包

    下载主线版本:

    $ wget http://nginx.org/download/nginx-1.11.13.tar.gz
    $ tar zxf nginx-1.11.13.tar.gz
    $ cd nginx-1.11.13

    下载稳定版本:

    $ wget http://nginx.org/download/nginx-1.12.0.tar.gz
    $ tar zxf nginx-1.12.0.tar.gz
    $ cd nginx-1.12.0

    (3) 配置构建选项

    下面是一个样例,具体的安装路径你可以根据你的情况来

    $ ./configure
    --sbin-path=/usr/local/nginx/nginx
    --conf-path=/usr/local/nginx/nginx.conf
    --pid-path=/usr/local/nginx/nginx.pid
    --with-pcre=../pcre-8.40
    --with-zlib=../zlib-1.2.11
    --with-http_ssl_module
    --with-stream
    --with-mail=dynamic
    --add-module=/usr/build/nginx-rtmp-module
    --add-dynamic-module=/usr/build/3party_module

    上面的配置选项中,我们指定了二进制程序的目录,配置文件的目录,PID文件的目录,以及依赖库的目录(这样指定是为了将pcre库和ssl库静态链接到nginx二进制程

    序中,以免使用时动态寻找pcre库和ssl库),同时带上了http_ssl_module,mail,stream,另外指定了一个静态模块nginx-rtmp-module和一个动态的第三方模块。

    还可以指定的选项有:

    --error-log-path=path  默认是prefix/logs/error.log
    
    --http-log-path=path   默认是prefix/logs/access.log
    
    --user=user            默认是nobody,表示由哪个用户运行nginx worker thread
    
    --group=group          组名经常设置为一个没有权限的用户名
    
    --with-pcre-jit        构建pcre库时,同时带上"just-in-time compilation"的功能,这样就能在配置文件中使用pcre_jit指令了

    除了指定nginx的构建选项,同时还可以指定编译器的选项:

    --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   添加linking时需要的额外参数
    在FreeBSD下,如果使用的是系统自带的pcre库,那么编译时必须添加:
    --with-ld-opt="-L /usr/local/lib";

    指定nginx的连接处理方式:

    --with-select_module
    
    --without-select_module

    select是默认的连接处理方式,如果平台不支持kqueue,epoll,/dev/poll,默认会自动构建这个方法。

    --with-poll_module
    
    --without-poll_module

    如果想使用poll这个方法,就使用上面的方式。这样就能替换掉默认的select。

    nginx的模块选择:

    某些模块默认已经编译了,所以不需要在configure后面加上它们。但是如果你不想要这个模块,可以在configure后面加上--without来去掉它。

    模块可以静态链接到nginx的二进制程序中,这样当nginx启动的时候,就会加载它们;使用--add-module选项来做静态链接。所谓静态链接,就是将模块完全打进nginx的二进制文件中。nginx启动,它就运行。

    模块可以动态链接到nginx的二进制程序中,这样只有在配置文件中指定这个模块时,nginx才会去加载它们,使用--add-dynamic-module选项来做动态链接。所谓动态链接,就是只在需要的时候才去加载那个模块,不需要的时候,就不加载,很好地控制了内存的使用。通过在配置文件中来指定是否使用某模块。

    nginx中默认已编译的模块列表:

    http_charset_module
    http_gzip_module
    http_ssi_module
    http_userid_module
    http_access_module
    http_auth_basic_module
    http_autoindex_module
    http_geo_module
    http_map_module
    http_split_clients_module
    http_referer_module   
    http_rewrite_module
    http_proxy_module    
    http_fastcgi_module    
    http_uwsgi_module    
    http_scgi_module   
    http_memcached_module   
    http_limit_conn_module    
    http_limit_req_module   
    http_empty_gif_module    
    http_browser_module    
    http_upstream_hash_module    
    http_upstream_ip_hash_module    
    http_upstream_least_conn_module   
    http_upstream_keepalive_module   
    http_upstream_zone_module 

    nginx中默认没有编译的模块:

    --with-threads
    --with-file-aio
    --with-ipv6
    --with-http_ssl_module
    --with-http_v2_module
    --with-http_realip_module
    --with-http_addition_module
    --with-http_xslt_module or --with-http_xslt_module=dynamic
    --with-http_image_filter_module or --with-http_image_filter_module=dynamic
    --with-http_geoip_module or --with-http_geoip_module=dynamic
    --with-http_sub_module
    --with-http_dav_module
    --with-http_flv_module
    --with-mp4_module
    --with-http_gunzip_module
    --with-http_gzip_static_module
    --with-http_auth_request_module
    --with-http_random_index_module
    --with-http_secure_link_module
    --with-http_slice_module
    --with-http_degradation_module
    --with-http_stub_status_module
    --with-http_perl_module or --with-http_perl_module=dynamic
    --with-mail or --with-mail=dynamic
    --with-mail_ssl_module
    --with-stream or --with-stream=dynamic
    --with-stream_ssl_module
    --with-google_perftools_module
    --with-cpp_test_module
    --with-debug

    第三方模块的地址:

    https://www.nginx.com/resources/wiki/modules/

    注意:第三方模块的质量不被保证,所以需要你自己承担风险。

    静态链接第三方模块:

    $  ./configure ... --add-module=/usr/build/nginx-rtmp-module

    动态链接第三方模块:

    $  ./configure ... --add-dynamic-module=/path/to/module

    编译出的动态模块会出现在目录/usr/local/nginx/modules/目录下,以*.so的格式存在。

    使用时需要在配置文件中添加load_module指令。

    完成安装:

    $ make
    $ sudo make install

    4. 使用预编译安装包

    最快速的方式是使用Redhat/CentOS仓库的预编译包nginx.***.rpm,但是这些包都是过期的,比如对于CentOS 7.0,它的仓库中,nginx的版本居然还是1.6.5的,现在的

    官方版本都到1.12.0了,所以最好是替换掉CentOS的默认仓库地址,修改为nginx官方的仓库地址(修改repo文件)。

    (1)使用默认的RedHat/CentOS仓库

    安装EPEL仓库(这是红帽维护的一个仓库)

    $ sudo yum install epel-release

    更新仓库,从而安装开源的nginx

    $ sudo yum update

    确认安装

    $ sudo nginx -v
    nginx version: nginx/1.6.3

    (2)使用nginx的官方仓库

    首先编辑仓库文件:

    $ sudo vi /etc/yum.repos.d/nginx.repo

    添加内容:

    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/mainline/OS/OSRELEASE/$basearch/
    gpgcheck=0
    enabled=1

    说明:

    “OS” is either rhel or centos
    “OSRELEASE” is the release number: 6, 6.x, 7, 7.x
    “/mainline” points to the latest mainline verson. Delete to get the latest stable version

    比如:如果你的操作系统是CentOS 7.0,你要安装主线版的nginx,那么文件内容如下:

    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
    gpgcheck=0
    enabled=1

    更新仓库:

    $ sudo yum update

    运行nginx:

    $ sudo nginx

    确认nginx已经起来并且运行正常:

    $ curl -I 127.0.0.1
    HTTP/1.1 200 OK
    Server: nginx/1.11.9
  • 相关阅读:
    linux学习笔记
    HDMI之CEC DDC学习笔记(可能有误)
    MAP按照value排序
    Map遍历四种方法
    Java native方法
    [PAT] 1143 Lowest Common Ancestor (30 分)Java
    [PAT] 1148 Werewolf
    [PAT] 1096 Consecutive Factors (20 分)Java
    [PAT] 1092 To Buy or Not to Buy (20 分)Java
    [PAT] 1088 Rational Arithmetic (20 分)Java
  • 原文地址:https://www.cnblogs.com/t-road/p/6732805.html
Copyright © 2011-2022 走看看