Nginx之基础入门

  一、Nginxi简介

  Nginx是什么?它是一个开源、高性能的WEB服务器软件和代理服务器软件,由俄罗斯人Igor Sysoev 开发实现。它的功能主要分三类,第一是它作为一个WEB服务软件使用;第二它可作为一个反向代理服务器使用,邮件代理服务(通常邮件代理用的非常少);第三它可以基于upstream 或stream模块实现负载均衡,这种负载均衡是基于前面的反向代理增加的功能,它可以基于http协议进行应用层代理,也可以基于tcp协议进行伪四层代理(它不是真正意义上的四层代理,因为它工作在应用层);

  二、Nginx的程序架构

  Nginx是master/worker架构,由一个master主控进程和一个或多个worker进程组成;master进程主要功能是负责加载和分析配置文件、管理worker进程、平滑升级;worker进程主要负责处理并响应客户端的请求。每个worker进程依靠各种模块以流水线的形式实现各种功能处理;面向客户端网络IO连接请求,nginx的worker进程是基于事件驱动模型使用epoll机制来实现一个worker进程可以处理响应多路请求,如果是BSD系统,它可以使用kevent事件驱动模型实现一个worker进程处理并响应多路请求,从而实现高并发请求处理;面向本地磁盘IO连接请求,它是基于高级IO的sendfile机制,AIO异步机制和mmap内存映射机制,内核直接构建响应报文来完成磁盘IO的高级特性;面向网络IO它不是真正意义上的实现一个纯异步的功能,对于网络IO的第一阶段(数据从磁盘到内核内存)是非阻塞,异步模式,第二阶段(内核内存到进程内存)依然是阻塞的,所以从某种意义上讲它在网络IO上不是一个真正意义上的异步机制,对于磁盘IO它是一个纯异步机制;当客户端发送一个请求到Nginx服务器时,由一个worker进程接收请求,首先它会基于某种策略(根据配置文件中配置),来确定用户请求是否查询本地缓存,如果配置文件中定义的有某某资源需要查询缓存,那么它就会去本地磁盘去查询缓存,如果有,就直接把缓存内容响应给客户端,如果本地缓存里没有客户端请求的资源,那么它会通过代理向后端发送请求,并将后端服务器返回的结果在本地缓存一份,然后再发送给客户端;这样一来当客户端或者其他客户端再来访问同一资源时,它就可以直接从本地缓存中响应客户端;这就是nginx缓存工作逻辑;除此之外,它还可以基于各种协议去代理后端服务器,前端客户端请求到达nginx服务器,它会根据配置文件中定义的规则,把客户端请求代理到后端服务器,从而实现反向代理服务器响应客户端请求;

  三、Nginx的安装配置

  1、编译安装

   首先准备编译环境

[root@www ~]# yum groupinstall "development tools" "server platform development" 
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.aliyun.com
Warning: group server platform development does not exist.
Resolving Dependencies
--> Running transaction check
---> Package autoconf.noarch 0:2.69-11.el7 will be installed
--> Processing Dependency: m4 >= 1.4.14 for package: autoconf-2.69-11.el7.noarch
---> Package automake.noarch 0:1.13.4-3.el7 will be installed
--> Processing Dependency: perl(Thread::Queue) for package: automake-1.13.4-3.el7.noarch
……省略部分内容
Installed:
  autoconf.noarch 0:2.69-11.el7                        automake.noarch 0:1.13.4-3.el7         bison.x86_64 0:3.0.4-2.el7            byacc.x86_64 0:1.9.20130304-3.el7      
  cscope.x86_64 0:15.8-10.el7                          ctags.x86_64 0:5.8-13.el7              diffstat.x86_64 0:1.57-4.el7          doxygen.x86_64 1:1.8.5-3.el7           
  elfutils.x86_64 0:0.176-2.el7                        flex.x86_64 0:2.5.37-6.el7             gcc.x86_64 0:4.8.5-39.el7             gcc-c++.x86_64 0:4.8.5-39.el7          
  gcc-gfortran.x86_64 0:4.8.5-39.el7                   git.x86_64 0:1.8.3.1-21.el7_7          indent.x86_64 0:2.2.11-13.el7         intltool.noarch 0:0.50.2-7.el7         
  libtool.x86_64 0:2.4.2-22.el7_3                      patch.x86_64 0:2.7.1-12.el7_7          patchutils.x86_64 0:0.3.3-4.el7       rcs.x86_64 0:5.9.0-5.el7               
  redhat-rpm-config.noarch 0:9.1.0-88.el7.centos       rpm-build.x86_64 0:4.11.3-40.el7       rpm-sign.x86_64 0:4.11.3-40.el7       subversion.x86_64 0:1.7.14-14.el7      
  swig.x86_64 0:2.0.10-5.el7                           systemtap.x86_64 0:4.0-10.el7_7       

Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7                            apr-util.x86_64 0:1.5.2-6.el7                                avahi-libs.x86_64 0:0.6.31-19.el7                       
  boost-date-time.x86_64 0:1.53.0-27.el7              boost-system.x86_64 0:1.53.0-27.el7                          boost-thread.x86_64 0:1.53.0-27.el7                     
  bzip2.x86_64 0:1.0.6-13.el7                         cpp.x86_64 0:4.8.5-39.el7                                    dwz.x86_64 0:0.11-3.el7                                 
  dyninst.x86_64 0:9.3.1-3.el7                        efivar-libs.x86_64 0:36-12.el7                               emacs-filesystem.noarch 1:24.3-22.el7                   
  gdb.x86_64 0:7.6.1-115.el7                          gettext-common-devel.noarch 0:0.19.8.1-2.el7                 gettext-devel.x86_64 0:0.19.8.1-2.el7                   
  glibc-devel.x86_64 0:2.17-292.el7                   glibc-headers.x86_64 0:2.17-292.el7                          gnutls.x86_64 0:3.3.29-9.el7_6                          
  json-c.x86_64 0:0.11-4.el7_0                        kernel-debug-devel.x86_64 0:3.10.0-1062.12.1.el7             kernel-headers.x86_64 0:3.10.0-1062.12.1.el7            
  libdwarf.x86_64 0:20130207-4.el7                    libgfortran.x86_64 0:4.8.5-39.el7                            libmodman.x86_64 0:2.0.1-8.el7                          
  libmpc.x86_64 0:1.0.1-3.el7                         libproxy.x86_64 0:0.4.11-11.el7                              libquadmath.x86_64 0:4.8.5-39.el7                       
  libquadmath-devel.x86_64 0:4.8.5-39.el7             libstdc++-devel.x86_64 0:4.8.5-39.el7                        m4.x86_64 0:1.4.16-10.el7                               
  mokutil.x86_64 0:15-2.el7.centos                    mpfr.x86_64 0:3.1.1-4.el7                                    neon.x86_64 0:0.30.0-4.el7                              
  nettle.x86_64 0:2.7.1-8.el7                         pakchois.x86_64 0:0.4-10.el7                                 perl-Error.noarch 1:0.17020-2.el7                       
  perl-Git.noarch 0:1.8.3.1-21.el7_7                  perl-TermReadKey.x86_64 0:2.30-20.el7                        perl-Test-Harness.noarch 0:3.28-3.el7                   
  perl-Thread-Queue.noarch 0:3.02-2.el7               perl-XML-Parser.x86_64 0:2.41-10.el7                         perl-srpm-macros.noarch 0:1-8.el7                       
  python-srpm-macros.noarch 0:3-32.el7                rsync.x86_64 0:3.1.2-6.el7_6.1                               subversion-libs.x86_64 0:1.7.14-14.el7                  
  systemtap-client.x86_64 0:4.0-10.el7_7              systemtap-devel.x86_64 0:4.0-10.el7_7                        systemtap-runtime.x86_64 0:4.0-10.el7_7                 
  trousers.x86_64 0:0.3.14-2.el7                      unzip.x86_64 0:6.0-20.el7                                    zip.x86_64 0:3.0-11.el7                                 

Dependency Updated:
  elfutils-libelf.x86_64 0:0.176-2.el7         elfutils-libs.x86_64 0:0.176-2.el7        glibc.x86_64 0:2.17-292.el7              glibc-common.x86_64 0:2.17-292.el7       
  libgcc.x86_64 0:4.8.5-39.el7                 libgomp.x86_64 0:4.8.5-39.el7             libstdc++.x86_64 0:4.8.5-39.el7          rpm.x86_64 0:4.11.3-40.el7               
  rpm-build-libs.x86_64 0:4.11.3-40.el7        rpm-libs.x86_64 0:4.11.3-40.el7           rpm-python.x86_64 0:4.11.3-40.el7       

Complete!
[root@www ~]# 

  提示:基础的编译环境基本搭建完,接下来再装几个nginx依赖的包

[root@www ~]# yum install pcre-devel openssl-devel zlib-devel -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package openssl-devel.x86_64 1:1.0.2k-19.el7 will be installed
--> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-19.el7 for package: 1:openssl-devel-1.0.2k-19.el7.x86_64
--> Processing Dependency: krb5-devel(x86-64) for package: 1:openssl-devel-1.0.2k-19.el7.x86_64
---> Package pcre-devel.x86_64 0:8.32-17.el7 will be installed
……省略部分内容
Installed:
  openssl-devel.x86_64 1:1.0.2k-19.el7                       pcre-devel.x86_64 0:8.32-17.el7                       zlib-devel.x86_64 0:1.2.7-18.el7                      

Dependency Installed:
  keyutils-libs-devel.x86_64 0:1.5.8-3.el7    krb5-devel.x86_64 0:1.15.1-37.el7_7.2    libcom_err-devel.x86_64 0:1.42.9-16.el7    libkadm5.x86_64 0:1.15.1-37.el7_7.2   
  libselinux-devel.x86_64 0:2.5-14.1.el7      libsepol-devel.x86_64 0:2.5-10.el7       libverto-devel.x86_64 0:0.2.5-4.el7       

Dependency Updated:
  e2fsprogs.x86_64 0:1.42.9-16.el7       e2fsprogs-libs.x86_64 0:1.42.9-16.el7         krb5-libs.x86_64 0:1.15.1-37.el7_7.2         libcom_err.x86_64 0:1.42.9-16.el7      
  libselinux.x86_64 0:2.5-14.1.el7       libselinux-python.x86_64 0:2.5-14.1.el7       libselinux-utils.x86_64 0:2.5-14.1.el7       libsepol.x86_64 0:2.5-10.el7           
  libss.x86_64 0:1.42.9-16.el7           openssl.x86_64 1:1.0.2k-19.el7                openssl-libs.x86_64 1:1.0.2k-19.el7          zlib.x86_64 0:1.2.7-18.el7             

Complete!
[root@www ~]# 

  提示:环境准备好了后我们就开始准备源码包和创建nginx用户

  先创建用户吧

[root@www ~]# useradd -r -s /sbin/nologin -c "web server for nginx" -d /var/lib/nginx nginx      
[root@www ~]# getent passwd nginx
nginx:x:997:995:web server for nginx:/var/lib/nginx:/sbin/nologin
[root@www ~]# 

  提示:一般这个用户用于运行nginx,所以通常都创建成shell类型为/sbin/nologin

  下载源码包,并解压源码包

[root@www ~]# ls
[root@www ~]# wget  http://nginx.org/download/nginx-1.10.3.tar.gz
--2020-02-14 04:45:51--  http://nginx.org/download/nginx-1.10.3.tar.gz
Resolving nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 911509 (890K) [application/octet-stream]
Saving to: ‘nginx-1.10.3.tar.gz’

100%[=================================================================================================================================>] 911,509     4.73KB/s   in 1m 53s 

2020-02-14 04:47:45 (7.87 KB/s) - ‘nginx-1.10.3.tar.gz’ saved [911509/911509]

[root@www ~]# ls
nginx-1.10.3.tar.gz
[root@www ~]# tar xf nginx-1.10.3.tar.gz 
[root@www ~]# cd nginx-1.10.3
[root@www nginx-1.10.3]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@www nginx-1.10.3]#

  提示:接下来我们需要指定编译选项,编译选项说明可通过./configure --help查看

[root@www nginx-1.10.3]# ./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-ipv6                        enable IPv6 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_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_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
  --without-stream_limit_conn_module disable ngx_stream_limit_conn_module
  --without-stream_access_module     disable ngx_stream_access_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-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-md5=DIR                     set path to md5 library sources
  --with-md5-opt=OPTIONS             set additional build options for md5
  --with-md5-asm                     use md5 assembler sources

  --with-sha1=DIR                    set path to sha1 library sources
  --with-sha1-opt=OPTIONS            set additional build options for sha1
  --with-sha1-asm                    use sha1 assembler sources

  --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

[root@www nginx-1.10.3]# 

  提示:nginx的编译选项大致可以划分三类,一般以--with开头表示默认不装载的模块或功能,我们如果需要使用需要手动指定,其次是--without开头的表示默认我们不指定就有点功能或模块,如果我们不需要该功能或模块,可手动指定;最后就是添加第三方模块可以使用 --add-module来加载第三方模块,第三方模块动态加载,在1.9.11版本后才支持,可以使用--add-dynamic-module来加载,其他选项几乎同其他编译软件类似;

  指定编译选项,检查编译环境

[root@www nginx-1.10.3]#  ./configure --prefix=/usr/share/nginx \
> --sbin-path=/usr/sbin/nginx \
> --modules-path=/usr/lib64/nginx/modules \
> --conf-path=/etc/nginx/nginx.conf \
> --error-log-path=/var/log/nginx/error.log \
> --http-log-path=/var/log/nginx/access.log \
> --http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
> --http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
> --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
> --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \
> --http-scgi-temp-path=/var/lib/nginx/tmp/scgi \
> --pid-path=/var/run/nginx.pid \
> --lock-path=/var/lock/subsys/nginx \
> --user=nginx \
> --group=nginx \
> --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=dynamic \
> --with-http_image_filter_module=dynamic \
> --with-http_geoip_module=dynamic \
> --with-http_sub_module \
> --with-http_dav_module \
> --with-http_flv_module \
> --with-http_mp4_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_random_index_module \
> --with-http_secure_link_module \
> --with-http_degradation_module \
> --with-http_slice_module \
> --with-http_stub_status_module \
> --with-http_perl_module=dynamic \
> --with-mail=dynamic \
> --with-mail_ssl_module \
> --with-pcre \
> --with-pcre-jit \
> --with-stream=dynamic \
> --with-stream_ssl_module --with-debug \
> --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' \
> --with-ld-opt=' -Wl,-E'

  提示:以上是yum安装的nginx编译时指定的选项,我们就拿这个用吧,如果有特别的功能添加,可以指定即可

checking for libxslt in /usr/local/ ... not found
checking for libxslt in /usr/pkg/ ... not found
checking for libxslt in /opt/local/ ... not found

./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.

[root@www nginx-1.10.3]# 

  提示:在预编译检查环境时报错,说我们缺少libxm12或者是libxslt这个包,我们可以选择安装这两个包,或者把--with-http_xslt_module=dynamic这个选项给去掉也行;通常情况下我们安装缺少包的devel包即可

[root@www nginx-1.10.3]# yum install libxml2-devel libxslt-devel -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package libxml2-devel.x86_64 0:2.9.1-6.el7_2.3 will be installed
--> Processing Dependency: xz-devel for package: libxml2-devel-2.9.1-6.el7_2.3.x86_64
---> Package libxslt-devel.x86_64 0:1.1.28-5.el7 will be installed
--> Processing Dependency: libxslt = 1.1.28-5.el7 for package: libxslt-devel-1.1.28-5.el7.x86_64
--> Processing Dependency: libgcrypt-devel for package: libxslt-devel-1.1.28-5.el7.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: libxslt-devel-1.1.28-5.el7.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: libxslt-devel-1.1.28-5.el7.x86_64
--> Running transaction check
---> Package libgcrypt-devel.x86_64 0:1.5.3-14.el7 will be installed
--> Processing Dependency: libgpg-error-devel for package: libgcrypt-devel-1.5.3-14.el7.x86_64
---> Package libxslt.x86_64 0:1.1.28-5.el7 will be installed
---> Package xz-devel.x86_64 0:5.2.2-1.el7 will be installed
--> Running transaction check
---> Package libgpg-error-devel.x86_64 0:1.12-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================================================
 Package                                         Arch                                Version                                       Repository                         Size
===========================================================================================================================================================================
Installing:
 libxml2-devel                                   x86_64                              2.9.1-6.el7_2.3                               base                              1.0 M
 libxslt-devel                                   x86_64                              1.1.28-5.el7                                  base                              309 k
Installing for dependencies:
 libgcrypt-devel                                 x86_64                              1.5.3-14.el7                                  base                              129 k
 libgpg-error-devel                              x86_64                              1.12-3.el7                                    base                               16 k
 libxslt                                         x86_64                              1.1.28-5.el7                                  base                              242 k
 xz-devel                                        x86_64                              5.2.2-1.el7                                   base                               46 k

Transaction Summary
===========================================================================================================================================================================
Install  2 Packages (+4 Dependent packages)

Total download size: 1.8 M
Installed size: 12 M
Downloading packages:
(1/6): libgpg-error-devel-1.12-3.el7.x86_64.rpm                                                                                                     |  16 kB  00:00:00     
(2/6): libxslt-1.1.28-5.el7.x86_64.rpm                                                                                                              | 242 kB  00:00:00     
(3/6): xz-devel-5.2.2-1.el7.x86_64.rpm                                                                                                              |  46 kB  00:00:00     
(4/6): libxml2-devel-2.9.1-6.el7_2.3.x86_64.rpm                                                                                                     | 1.0 MB  00:00:02     
(5/6): libxslt-devel-1.1.28-5.el7.x86_64.rpm                                                                                                        | 309 kB  00:00:06     
(6/6): libgcrypt-devel-1.5.3-14.el7.x86_64.rpm                                                                                                      | 129 kB  00:00:10     
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                      163 kB/s | 1.8 MB  00:00:11     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libgpg-error-devel-1.12-3.el7.x86_64                                                                                                                    1/6 
  Installing : libgcrypt-devel-1.5.3-14.el7.x86_64                                                                                                                     2/6 
  Installing : libxslt-1.1.28-5.el7.x86_64                                                                                                                             3/6 
  Installing : xz-devel-5.2.2-1.el7.x86_64                                                                                                                             4/6 
  Installing : libxml2-devel-2.9.1-6.el7_2.3.x86_64                                                                                                                    5/6 
  Installing : libxslt-devel-1.1.28-5.el7.x86_64                                                                                                                       6/6 
  Verifying  : xz-devel-5.2.2-1.el7.x86_64                                                                                                                             1/6 
  Verifying  : libxml2-devel-2.9.1-6.el7_2.3.x86_64                                                                                                                    2/6 
  Verifying  : libxslt-devel-1.1.28-5.el7.x86_64                                                                                                                       3/6 
  Verifying  : libxslt-1.1.28-5.el7.x86_64                                                                                                                             4/6 
  Verifying  : libgpg-error-devel-1.12-3.el7.x86_64                                                                                                                    5/6 
  Verifying  : libgcrypt-devel-1.5.3-14.el7.x86_64                                                                                                                     6/6 

Installed:
  libxml2-devel.x86_64 0:2.9.1-6.el7_2.3                                                libxslt-devel.x86_64 0:1.1.28-5.el7                                               

Dependency Installed:
  libgcrypt-devel.x86_64 0:1.5.3-14.el7         libgpg-error-devel.x86_64 0:1.12-3.el7         libxslt.x86_64 0:1.1.28-5.el7         xz-devel.x86_64 0:5.2.2-1.el7        

Complete!
[root@www nginx-1.10.3]# 

  提示:安装了这两个包后,我们重新运行./configure 加指定的选项,再检查环境

checking for GD library ... not found
checking for GD library in /usr/local/ ... not found
checking for GD library in /usr/pkg/ ... not found
checking for GD library in /opt/local/ ... not found

./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

[root@www nginx-1.10.3]# 

  提示:重新运行,又报缺少GD库,遇到这种缺包的情况,我们就想上面那样,用yum去安装对应的包和devel包

[root@www nginx-1.10.3]# yum install  gd-devel  
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package gd-devel.x86_64 0:2.0.35-26.el7 will be installed
--> Processing Dependency: gd = 2.0.35-26.el7 for package: gd-devel-2.0.35-26.el7.x86_64
--> Processing Dependency: libpng-devel for package: gd-devel-2.0.35-26.el7.x86_64
--> Processing Dependency: libjpeg-devel for package: gd-devel-2.0.35-26.el7.x86_64
--> Processing Dependency: libXpm-devel for package: gd-devel-2.0.35-26.el7.x86_64
……省略部分内容
Installed:
  gd.x86_64 0:2.0.35-26.el7                                                         gd-devel.x86_64 0:2.0.35-26.el7                                                        

Dependency Installed:
  dejavu-fonts-common.noarch 0:2.33-6.el7               dejavu-sans-fonts.noarch 0:2.33-6.el7                   expat-devel.x86_64 0:2.1.0-10.el7_3                      
  fontconfig.x86_64 0:2.13.0-4.3.el7                    fontconfig-devel.x86_64 0:2.13.0-4.3.el7                fontpackages-filesystem.noarch 0:1.44-8.el7              
  freetype-devel.x86_64 0:2.8-14.el7                    libICE.x86_64 0:1.0.9-9.el7                             libSM.x86_64 0:1.2.2-2.el7                               
  libX11.x86_64 0:1.6.7-2.el7                           libX11-common.noarch 0:1.6.7-2.el7                      libX11-devel.x86_64 0:1.6.7-2.el7                        
  libXau.x86_64 0:1.0.8-2.1.el7                         libXau-devel.x86_64 0:1.0.8-2.1.el7                     libXext.x86_64 0:1.3.3-3.el7                             
  libXpm.x86_64 0:3.5.12-1.el7                          libXpm-devel.x86_64 0:3.5.12-1.el7                      libXt.x86_64 0:1.1.5-3.el7                               
  libjpeg-turbo.x86_64 0:1.2.90-8.el7                   libjpeg-turbo-devel.x86_64 0:1.2.90-8.el7               libpng.x86_64 2:1.5.13-7.el7_2                           
  libpng-devel.x86_64 2:1.5.13-7.el7_2                  libsmartcols.x86_64 0:2.23.2-61.el7_7.1                 libuuid-devel.x86_64 0:2.23.2-61.el7_7.1                 
  libxcb.x86_64 0:1.13-1.el7                            libxcb-devel.x86_64 0:1.13-1.el7                        xorg-x11-proto-devel.noarch 0:2018.4-1.el7               

Dependency Updated:
  freetype.x86_64 0:2.8-14.el7                libblkid.x86_64 0:2.23.2-61.el7_7.1       libmount.x86_64 0:2.23.2-61.el7_7.1       libuuid.x86_64 0:2.23.2-61.el7_7.1      
  util-linux.x86_64 0:2.23.2-61.el7_7.1      

Complete!
[root@www nginx-1.10.3]# 

  提示:在搭建编译环境时,有的包可能来源epel源,所以前期准备工作需要准备好epel源

checking for perl
 + perl version: This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
Can't locate ExtUtils/Embed.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).
BEGIN failed--compilation aborted.

./configure: error: perl module ExtUtils::Embed is required

  提示:重新检查环境报perl-ExtUtils-Embed没有,我们perl-devel和perl-Extutils-Embed这个包即可

[root@www nginx-1.10.3]# yum install perl-devel perl-ExtUtils-Embed 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package perl-ExtUtils-Embed.noarch 0:1.30-294.el7_6 will be installed
……省略部分内容
Installed:
  perl-ExtUtils-Embed.noarch 0:1.30-294.el7_6                                             perl-devel.x86_64 4:5.16.3-294.el7_6                                            

Dependency Installed:
  gdbm-devel.x86_64 0:1.10-8.el7                         libdb-devel.x86_64 0:5.3.21-25.el7                      perl-ExtUtils-Install.noarch 0:1.58-294.el7_6           
  perl-ExtUtils-MakeMaker.noarch 0:6.68-3.el7            perl-ExtUtils-Manifest.noarch 0:1.61-244.el7            perl-ExtUtils-ParseXS.noarch 1:3.18-3.el7               
  pyparsing.noarch 0:1.5.6-9.el7                         systemtap-sdt-devel.x86_64 0:4.0-10.el7_7              

Dependency Updated:
  libdb.x86_64 0:5.3.21-25.el7                                                      libdb-utils.x86_64 0:5.3.21-25.el7                                                     

Complete!
[root@www nginx-1.10.3]# 

  提示:安装了包后继续进行预编译检查环境,直到不报错为止,不报错表示编译环境是可用的

checking for GeoIP library ... not found
checking for GeoIP library in /usr/local/ ... not found
checking for GeoIP library in /usr/pkg/ ... not found
checking for GeoIP library in /opt/local/ ... not found

./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.

[root@www nginx-1.10.3]# 

  提示:解决办法yum install -y geoip geoip-devel

checking for GeoIP IPv6 support ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/share/nginx"
  nginx binary file: "/usr/sbin/nginx"
  nginx modules path: "/usr/lib64/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/lib/nginx/tmp/client_body"
  nginx http proxy temporary files: "/var/lib/nginx/tmp/proxy"
  nginx http fastcgi temporary files: "/var/lib/nginx/tmp/fastcgi"
  nginx http uwsgi temporary files: "/var/lib/nginx/tmp/uwsgi"
  nginx http scgi temporary files: "/var/lib/nginx/tmp/scgi"

[root@www nginx-1.10.3]# 

  提示:能看到以上信息表示预编译环境检查没有问题,接下来我们就可以进行编译了

[root@www nginx-1.10.3]# make -j 4 && make install 
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.10.3'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -I src/core -I src/event -I src/event/modules -I src/os/unix -I src/http/modules/perl -I /usr/include/libxml2 -I objs \
        -o objs/src/core/nginx.o \
        src/core/nginx.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -I src/core -I src/event -I src/event/modules -I src/os/unix -I src/http/modules/perl -I /usr/include/libxml2 -I objs \
        -o objs/src/core/ngx_log.o \
        src/core/ngx_log.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -I src/core -I src/event -I src/event/modules -I src/os/unix -I src/http/modules/perl -I /usr/include/libxml2 -I objs \
        -o objs/src/core/ngx_palloc.o \
……省略部分信息
make[1]: Leaving directory `/root/nginx-1.10.3'
make -f objs/Makefile install
make[1]: Entering directory `/root/nginx-1.10.3'
cd objs/src/http/modules/perl && make install
make[2]: Entering directory `/root/nginx-1.10.3/objs/src/http/modules/perl'
Files found in blib/arch: installing files in blib/lib into architecture dependent library tree
Installing /usr/local/lib64/perl5/auto/nginx/nginx.bs
Installing /usr/local/lib64/perl5/auto/nginx/nginx.so
Installing /usr/local/lib64/perl5/nginx.pm
Installing /usr/local/share/man/man3/nginx.3pm
Appending installation info to /usr/lib64/perl5/perllocal.pod
make[2]: Leaving directory `/root/nginx-1.10.3/objs/src/http/modules/perl'
test -d '/usr/share/nginx' || mkdir -p '/usr/share/nginx'
test -d '/usr/sbin' \
        || mkdir -p '/usr/sbin'
test ! -f '/usr/sbin/nginx' \
        || mv '/usr/sbin/nginx' \
                '/usr/sbin/nginx.old'
cp objs/nginx '/usr/sbin/nginx'
test -d '/etc/nginx' \
        || mkdir -p '/etc/nginx'
cp conf/koi-win '/etc/nginx'
cp conf/koi-utf '/etc/nginx'
cp conf/win-utf '/etc/nginx'
test -f '/etc/nginx/mime.types' \
        || cp conf/mime.types '/etc/nginx'
cp conf/mime.types '/etc/nginx/mime.types.default'
test -f '/etc/nginx/fastcgi_params' \
        || cp conf/fastcgi_params '/etc/nginx'
cp conf/fastcgi_params \
        '/etc/nginx/fastcgi_params.default'
test -f '/etc/nginx/fastcgi.conf' \
        || cp conf/fastcgi.conf '/etc/nginx'
cp conf/fastcgi.conf '/etc/nginx/fastcgi.conf.default'
test -f '/etc/nginx/uwsgi_params' \
        || cp conf/uwsgi_params '/etc/nginx'
cp conf/uwsgi_params \
        '/etc/nginx/uwsgi_params.default'
test -f '/etc/nginx/scgi_params' \
        || cp conf/scgi_params '/etc/nginx'
cp conf/scgi_params \
        '/etc/nginx/scgi_params.default'
test -f '/etc/nginx/nginx.conf' \
        || cp conf/nginx.conf '/etc/nginx/nginx.conf'
cp conf/nginx.conf '/etc/nginx/nginx.conf.default'
test -d '/var/run' \
        || mkdir -p '/var/run'
test -d '/var/log/nginx' \
        || mkdir -p '/var/log/nginx'
test -d '/usr/share/nginx/html' \
        || cp -R html '/usr/share/nginx'
test -d '/var/log/nginx' \
        || mkdir -p '/var/log/nginx'
test -d '/usr/lib64/nginx/modules' \
        || mkdir -p '/usr/lib64/nginx/modules'
test ! -f '/usr/lib64/nginx/modules/ngx_http_xslt_filter_module.so' \
        || mv '/usr/lib64/nginx/modules/ngx_http_xslt_filter_module.so' \
                '/usr/lib64/nginx/modules/ngx_http_xslt_filter_module.so.old'
cp objs/ngx_http_xslt_filter_module.so '/usr/lib64/nginx/modules/ngx_http_xslt_filter_module.so'
test ! -f '/usr/lib64/nginx/modules/ngx_http_image_filter_module.so' \
        || mv '/usr/lib64/nginx/modules/ngx_http_image_filter_module.so' \
                '/usr/lib64/nginx/modules/ngx_http_image_filter_module.so.old'
cp objs/ngx_http_image_filter_module.so '/usr/lib64/nginx/modules/ngx_http_image_filter_module.so'
test ! -f '/usr/lib64/nginx/modules/ngx_http_geoip_module.so' \
        || mv '/usr/lib64/nginx/modules/ngx_http_geoip_module.so' \
                '/usr/lib64/nginx/modules/ngx_http_geoip_module.so.old'
cp objs/ngx_http_geoip_module.so '/usr/lib64/nginx/modules/ngx_http_geoip_module.so'
test ! -f '/usr/lib64/nginx/modules/ngx_http_perl_module.so' \
        || mv '/usr/lib64/nginx/modules/ngx_http_perl_module.so' \
                '/usr/lib64/nginx/modules/ngx_http_perl_module.so.old'
cp objs/ngx_http_perl_module.so '/usr/lib64/nginx/modules/ngx_http_perl_module.so'
test ! -f '/usr/lib64/nginx/modules/ngx_mail_module.so' \
        || mv '/usr/lib64/nginx/modules/ngx_mail_module.so' \
                '/usr/lib64/nginx/modules/ngx_mail_module.so.old'
cp objs/ngx_mail_module.so '/usr/lib64/nginx/modules/ngx_mail_module.so'
test ! -f '/usr/lib64/nginx/modules/ngx_stream_module.so' \
        || mv '/usr/lib64/nginx/modules/ngx_stream_module.so' \
                '/usr/lib64/nginx/modules/ngx_stream_module.so.old'
cp objs/ngx_stream_module.so '/usr/lib64/nginx/modules/ngx_stream_module.so'
make[1]: Leaving directory `/root/nginx-1.10.3'
[root@www nginx-1.10.3]# 

  提示:最后编译没有ERROR的字眼表示编译成功了,我们可以去我们指定的目录查看对应的二进制文件是否都已经拷贝进去了

[root@www nginx-1.10.3]# ll /sbin/nginx 
-rwxr-xr-x 1 root root 7410432 Feb 14 05:36 /sbin/nginx
[root@www nginx-1.10.3]# ll /usr/lib64/nginx/modules
total 1960
-rwxr-xr-x 1 root root 138184 Feb 14 05:36 ngx_http_geoip_module.so
-rwxr-xr-x 1 root root 153136 Feb 14 05:36 ngx_http_image_filter_module.so
-rwxr-xr-x 1 root root 184216 Feb 14 05:36 ngx_http_perl_module.so
-rwxr-xr-x 1 root root 167112 Feb 14 05:36 ngx_http_xslt_filter_module.so
-rwxr-xr-x 1 root root 736904 Feb 14 05:36 ngx_mail_module.so
-rwxr-xr-x 1 root root 621952 Feb 14 05:36 ngx_stream_module.so
[root@www nginx-1.10.3]# ll /etc/nginx/
total 60
-rw-r--r-- 1 root root 1077 Feb 14 05:36 fastcgi.conf
-rw-r--r-- 1 root root 1077 Feb 14 05:36 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 Feb 14 05:36 fastcgi_params
-rw-r--r-- 1 root root 1007 Feb 14 05:36 fastcgi_params.default
-rw-r--r-- 1 root root 2837 Feb 14 05:36 koi-utf
-rw-r--r-- 1 root root 2223 Feb 14 05:36 koi-win
-rw-r--r-- 1 root root 3957 Feb 14 05:36 mime.types
-rw-r--r-- 1 root root 3957 Feb 14 05:36 mime.types.default
-rw-r--r-- 1 root root 2656 Feb 14 05:36 nginx.conf
-rw-r--r-- 1 root root 2656 Feb 14 05:36 nginx.conf.default
-rw-r--r-- 1 root root  636 Feb 14 05:36 scgi_params
-rw-r--r-- 1 root root  636 Feb 14 05:36 scgi_params.default
-rw-r--r-- 1 root root  664 Feb 14 05:36 uwsgi_params
-rw-r--r-- 1 root root  664 Feb 14 05:36 uwsgi_params.default
-rw-r--r-- 1 root root 3610 Feb 14 05:36 win-utf
[root@www nginx-1.10.3]# 

  提示:可看到对应的二进制文件,模块,以及配置文件都已经放到我们指定的目录里了。通常情况下我们编译完成后,要加环境变量,我们这里指定安装位置在环境变量里,所以可不再添加环境变量,如果自定义其他目录,需要添加环境变量;检查nginx是否需要添加环境变量,我们可以在命令行敲nginx -t 来测试。nginx -t表示测试配置文件的语法,如果没有报nginx命令未找到,就表示我们的环境变量没有问题

[root@www nginx-1.10.3]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] mkdir() "/var/lib/nginx/tmp/client_body" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

  提示:我们运行nginx -t 它报了一个错,说/var/lib/nginx/tmp/client_body不存在,当然这种错误我们可以创建对应的目录即可

[root@www nginx-1.10.3]# mkdir -p /var/lib/nginx/tmp/client_body
[root@www nginx-1.10.3]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www nginx-1.10.3]#

  提示:到此nginx编译就完了,当然接下来就是启动nginx 看看是否可访问到默认主页

[root@www nginx-1.10.3]# cd 
[root@www ~]# ss -ntl
State       Recv-Q Send-Q                                        Local Address:Port                                                       Peer Address:Port              
LISTEN      0      128                                                       *:22                                                                    *:*                  
LISTEN      0      100                                               127.0.0.1:25                                                                    *:*                  
LISTEN      0      128                                                      :::22                                                                   :::*                  
LISTEN      0      100                                                     ::1:25                                                                   :::*                  
[root@www ~]# nginx
[root@www ~]# ss -ntl
State       Recv-Q Send-Q                                        Local Address:Port                                                       Peer Address:Port              
LISTEN      0      128                                                       *:80                                                                    *:*                  
LISTEN      0      128                                                       *:22                                                                    *:*                  
LISTEN      0      100                                               127.0.0.1:25                                                                    *:*                  
LISTEN      0      128                                                      :::22                                                                   :::*                  
LISTEN      0      100                                                     ::1:25                                                                   :::*                  
[root@www ~]# ps aux |grep nginx
root      32348  0.0  0.0  46172  1148 ?        Ss   05:51   0:00 nginx: master process nginx
nginx     32349  0.0  0.0  46608  1896 ?        S    05:51   0:00 nginx: worker process
root      32352  0.0  0.0 112712   964 pts/0    S+   05:52   0:00 grep --color=auto nginx
[root@www ~]# 

  提示:我们启动nginx 直接在命令行敲nginx即可启动,当然我们也可以选择写一个unit file 让其systed来管理我们nginx

[root@www ~]# cat > /usr/lib/systemd/system/nginx.service 
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
^C
[root@www ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@www ~]# 

  提示:我们提供了unit file 后 运行 systemctl daemon-reload 来加载nginx.service文件,让其systemctl 重新加载配置文件,识别我们之前写的unit file;以上unit file是从参考yum安装的nginx提供的unit file文件

[root@www ~]# nginx -s stop
[root@www ~]# systemctl daemon-reload
[root@www ~]# systemctl start nginx
[root@www ~]# ss -ntl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:80                       *:*                  
LISTEN     0      128          *:22                       *:*                  
LISTEN     0      100    127.0.0.1:25                       *:*                  
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      100        ::1:25                      :::*                  
[root@www ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-02-14 06:12:59 EST; 14s ago
  Process: 32445 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 32441 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 32439 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 32446 (nginx)
   CGroup: /system.slice/nginx.service
           ├─32446 nginx: master process /usr/sbin/nginx
           └─32447 nginx: worker process

Feb 14 06:12:58 www.ilinux.io systemd[1]: Starting The nginx HTTP and revers....
Feb 14 06:12:58 www.ilinux.io nginx[32441]: nginx: the configuration file /e...k
Feb 14 06:12:58 www.ilinux.io nginx[32441]: nginx: configuration file /etc/n...l
Feb 14 06:12:59 www.ilinux.io systemd[1]: Started The nginx HTTP and reverse....
Hint: Some lines were ellipsized, use -l to show in full.
[root@www ~]# 

  提示:到此Nginx就可以基于systemctl来管理了;基本上都没有什么要配置了,我们来访问下默认主页是否能够访问得到

    提示:到此nginx的编译安装整个过程就完了;总结一点编译安装就是太麻烦,太繁琐,生产环境中如果没有特殊的要求或功能要启用,我们都用yum安装吧;yum安装相对编译安装要简单很多,但是需要注意一点的是yum安装nginx需要提前配置好epel源;epel源可参考http://nginx.org/en/linux_packages.html#RHEL-CentOS

  2、nginx命令选项说明

[root@www ~]# nginx -h
nginx version: nginx/1.10.3
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/share/nginx/)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

[root@www ~]# 

  -?或者-h表示查看nginx命令的帮助选项信息

  -v:查看nginx的版本信息

[root@www ~]# nginx -v
nginx version: nginx/1.10.3
[root@www ~]# 

  -V(大写):查看版本信息以及编译选项

[root@www ~]# nginx -V
nginx version: nginx/1.10.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --user=nginx --group=nginx --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=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --with-ld-opt=' -Wl,-E'
[root@www ~]# 

  -t:测试配置文件语法并退出

[root@www ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www ~]# 

  提示:通常我们配置nginx后都要先检查配置文件是否有语法问题,然后再去重载

  -T:测试配置文件,并dump配置文件内容显示到标准输出

[root@www ~]# nginx -T
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

# configuration file /etc/nginx/mime.types:

types {
    text/html                             html htm shtml;
    text/css                              css;
    text/xml                              xml;
    image/gif                             gif;
    image/jpeg                            jpeg jpg;
    application/javascript                js;
    application/atom+xml                  atom;
    application/rss+xml                   rss;

    text/mathml                           mml;
    text/plain                            txt;
    text/vnd.sun.j2me.app-descriptor      jad;
    text/vnd.wap.wml                      wml;
    text/x-component                      htc;

    image/png                             png;
    image/tiff                            tif tiff;
    image/vnd.wap.wbmp                    wbmp;
    image/x-icon                          ico;
    image/x-jng                           jng;
    image/x-ms-bmp                        bmp;
    image/svg+xml                         svg svgz;
    image/webp                            webp;

    application/font-woff                 woff;
    application/java-archive              jar war ear;
    application/json                      json;
    application/mac-binhex40              hqx;
    application/msword                    doc;
    application/pdf                       pdf;
    application/postscript                ps eps ai;
    application/rtf                       rtf;
    application/vnd.apple.mpegurl         m3u8;
    application/vnd.ms-excel              xls;
    application/vnd.ms-fontobject         eot;
    application/vnd.ms-powerpoint         ppt;
    application/vnd.wap.wmlc              wmlc;
    application/vnd.google-earth.kml+xml  kml;
    application/vnd.google-earth.kmz      kmz;
    application/x-7z-compressed           7z;
    application/x-cocoa                   cco;
    application/x-java-archive-diff       jardiff;
    application/x-java-jnlp-file          jnlp;
    application/x-makeself                run;
    application/x-perl                    pl pm;
    application/x-pilot                   prc pdb;
    application/x-rar-compressed          rar;
    application/x-redhat-package-manager  rpm;
    application/x-sea                     sea;
    application/x-shockwave-flash         swf;
    application/x-stuffit                 sit;
    application/x-tcl                     tcl tk;
    application/x-x509-ca-cert            der pem crt;
    application/x-xpinstall               xpi;
    application/xhtml+xml                 xhtml;
    application/xspf+xml                  xspf;
    application/zip                       zip;

    application/octet-stream              bin exe dll;
    application/octet-stream              deb;
    application/octet-stream              dmg;
    application/octet-stream              iso img;
    application/octet-stream              msi msp msm;

    application/vnd.openxmlformats-officedocument.wordprocessingml.document    docx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet          xlsx;
    application/vnd.openxmlformats-officedocument.presentationml.presentation  pptx;

    audio/midi                            mid midi kar;
    audio/mpeg                            mp3;
    audio/ogg                             ogg;
    audio/x-m4a                           m4a;
    audio/x-realaudio                     ra;

    video/3gpp                            3gpp 3gp;
    video/mp2t                            ts;
    video/mp4                             mp4;
    video/mpeg                            mpeg mpg;
    video/quicktime                       mov;
    video/webm                            webm;
    video/x-flv                           flv;
    video/x-m4v                           m4v;
    video/x-mng                           mng;
    video/x-ms-asf                        asx asf;
    video/x-ms-wmv                        wmv;
    video/x-msvideo                       avi;
}

[root@www ~]# 

  提示:这个选项用的很少-t即可,通常不打印配置文件到屏幕

  -q:表示非错误信息不予显示,即静默模式

[root@www ~]# nginx -q
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@www ~]# nginx -qt
[root@www ~]# 

  提示:此选项表示有错误信息就输出到标准输出,没有就什么都不显示

  -s:给nginx主控进程发送信号,能够发送到信号有 stop 、reload、quit、reopen

[root@www ~]# ps aux |grep nginx
root      32473  0.0  0.0  46172  1144 ?        Ss   06:39   0:00 nginx: master process nginx
nginx     32474  0.0  0.0  46608  1892 ?        S    06:39   0:00 nginx: worker process
root      32478  0.0  0.0 112712   960 pts/0    S+   06:40   0:00 grep --color=auto nginx
[root@www ~]# nginx -s quit     
[root@www ~]# ps aux |grep nginx
root      32481  0.0  0.0 112712   960 pts/0    S+   06:40   0:00 grep --color=auto nginx
[root@www ~]# nginx
[root@www ~]# ps aux |grep nginx
root      32484  0.0  0.0  46172  1148 ?        Ss   06:40   0:00 nginx: master process nginx
nginx     32485  0.0  0.0  46608  1896 ?        S    06:40   0:00 nginx: worker process
root      32487  0.0  0.0 112712   964 pts/0    S+   06:40   0:00 grep --color=auto nginx
[root@www ~]# nginx -s stop
[root@www ~]# ps aux |grep nginx
root      32490  0.0  0.0 112712   960 pts/0    S+   06:40   0:00 grep --color=auto nginx
[root@www ~]#

  提示:stop 和quit都可以停止nginx服务

  reload :重载nginx worker进程

[root@www ~]# nginx 
[root@www ~]# ps aux |grep nginx
root      32492  0.0  0.0  46172  1144 ?        Ss   06:42   0:00 nginx: master process nginx
nginx     32493  0.0  0.0  46608  1892 ?        S    06:42   0:00 nginx: worker process
root      32495  0.0  0.0 112712   964 pts/0    S+   06:42   0:00 grep --color=auto nginx
[root@www ~]# nginx -s reload
[root@www ~]# ps aux |grep nginx
root      32492  0.0  0.0  46172  1964 ?        Ss   06:42   0:00 nginx: master process nginx
nginx     32497  0.0  0.0  46628  2004 ?        S    06:42   0:00 nginx: worker process
root      32499  0.0  0.0 112712   960 pts/0    S+   06:42   0:00 grep --color=auto nginx
[root@www ~]# 

  提示:重载后可以看到只有worker进程的pid发生了变化,master进程并未发生变化

  reopen:重新打开的意思,常用于日志切割;比如我们把默认日志重命名后,新建一个access.log 如果给主控进程发送reopen信号,这个时候nginx是不会记录日志的。又或者当我们日志目录下没有access.log文件时(不一定是这个名,要看配置文件中的定义),nginx也不会记录日志,我们可给主控进程发送reopen信号 它就可以自动新建缺少的日志文件,然后记录日志

[root@www ~]# ll /var/log/nginx/
total 8
-rw-r--r-- 1 nginx root  744 Feb 14 06:55 access.log
-rw-r--r-- 1 nginx root 1635 Feb 14 06:55 error.log
[root@www ~]# rm -rf /var/log/nginx/access.log 
[root@www ~]# ll /var/log/nginx/
total 4
-rw-r--r-- 1 nginx root 1635 Feb 14 06:55 error.log
[root@www ~]# curl -I http://192.168.0.30/
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Fri, 14 Feb 2020 12:00:45 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 14 Feb 2020 10:36:27 GMT
Connection: keep-alive
ETag: "5e46782b-264"
Accept-Ranges: bytes

[root@www ~]# ll /var/log/nginx/     
total 4
-rw-r--r-- 1 nginx root 1635 Feb 14 06:55 error.log
[root@www ~]# touch /var/log/nginx/access.log
[root@www ~]# ll /var/log/nginx/
total 4
-rw-r--r-- 1 root  root    0 Feb 14 07:01 access.log
-rw-r--r-- 1 nginx root 1635 Feb 14 06:55 error.log
[root@www ~]# curl -I http://192.168.0.30/   
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Fri, 14 Feb 2020 12:01:25 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 14 Feb 2020 10:36:27 GMT
Connection: keep-alive
ETag: "5e46782b-264"
Accept-Ranges: bytes

[root@www ~]# ll /var/log/nginx/     
total 4
-rw-r--r-- 1 root  root    0 Feb 14 07:01 access.log
-rw-r--r-- 1 nginx root 1635 Feb 14 06:55 error.log
[root@www ~]# nginx -s reopen
[root@www ~]# curl -I http://192.168.0.30/
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Fri, 14 Feb 2020 12:01:56 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 14 Feb 2020 10:36:27 GMT
Connection: keep-alive
ETag: "5e46782b-264"
Accept-Ranges: bytes

[root@www ~]# curl -I http://192.168.0.30/
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Fri, 14 Feb 2020 12:01:58 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 14 Feb 2020 10:36:27 GMT
Connection: keep-alive
ETag: "5e46782b-264"
Accept-Ranges: bytes

[root@www ~]# ll /var/log/nginx/
total 8
-rw-r--r-- 1 nginx root  176 Feb 14 07:01 access.log
-rw-r--r-- 1 nginx root 1696 Feb 14 07:01 error.log
[root@www ~]# 

  提示:当然我们可直接nginx -s reopen ,如果对应的日志文件没有,它就会自动创建,并记录日志

[root@www ~]# rm -rf /var/log/nginx/*
[root@www ~]# ll /var/log/nginx/
total 0
[root@www ~]# nginx -s reopen
[root@www ~]# ll /var/log/nginx/
total 4
-rw-r--r-- 1 nginx root  0 Feb 14 07:08 access.log
-rw-r--r-- 1 nginx root 61 Feb 14 07:08 error.log
[root@www ~]# 

  -p:设置前缀路径,不设置默认%%PREFIX%%所指定路径

  -c:指定启动配置文件路径,如果不指定默认/etc/nginx/nginx.conf

  -g:指定全局指定配置文件

  到此我们对nginx有了一个大概的了解,知道nginx是什么?它有哪些功能?能干嘛,它的架构等等,知道该怎么去编译安装一个nginx(虽然编译安装非常麻烦,但我们需要知道该怎么去编译安装),怎么使用nginx命令,怎么启动,停止,重载等,后续我会持续更新nginx的其他内容,有兴趣的小伙伴可以加加关注,谢谢!!!

posted @ 2020-02-26 22:25  Linux-1874  阅读(4351)  评论(0编辑  收藏  举报