Linux系統WEB服務之Nginx基礎入門

  1、Nginxi簡介javascript

  Nginx是什麼?它是一個開源、高性能的WEB服務器軟件和代理服務器軟件,由俄羅斯人Igor Sysoev 開發實現。它的功能主要分三類,第一是它做爲一個WEB服務軟件使用;第二它可做爲一個反向代理服務器使用,郵件代理服務(一般郵件代理用的很是少);第三它能夠基於upstream 或stream模塊實現負載均衡,這種負載均衡是基於前面的反向代理增長的功能,它能夠基於http協議進行應用層代理,也能夠基於tcp協議進行僞四層代理(它不是真正意義上的四層代理,由於它工做在應用層);php

  2、Nginx的程序架構css

  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服務器,它會根據配置文件中定義的規則,把客戶端請求代理到後端服務器,從而實現反向代理服務器響應客戶端請求;html

  3、Nginx的安裝配置前端

  一、編譯安裝java

   首先準備編譯環境python

[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依賴的包linux

[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用戶nginx

  先建立用戶吧c++

[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

  二、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的其餘內容,有興趣的小夥伴能夠加加關注,謝謝!!!

相關文章
相關標籤/搜索