翻譯至https://www.nginx.com官網
原文連結https://www.nginx.com/resources/admin-guide/installing-nginx-open-source
下列包含在括弧中的內容為筆者所添加。
1.選擇欲編譯的版本.
開源Nginx有兩個版本,所以要先選擇想要建置的版本進行原碼下載。
可以事先調查版本特性以及確定功能需求在進行決定,對之後選擇模塊
及檔案最小化有有關鍵性影響。
版本大略分別為:
- 維護中的最新版本(不穩定):擁有最新特性,及最新更新的版本。
- 穩定版,已經被多次測試且修補所有可見BUG,推薦給商品發行版本使用。
2.編譯相依賴庫
Nginx標準編譯依賴下列三種庫,所以在編譯原碼前必須先建好相關類庫。
(當然,如果之後為了嵌入式系統建置,有用不到的類庫,可以跳過此處編譯
並在之後configure時
進行設定。)
分別是:
- pcre 被NGINX Core 和 Rewrite modules所需要的正規表達式庫
- zlib library –被NGINX Gzip模塊要求 ,http2 當中封包包頭header壓縮。
- the OpenSSL library 被 NGINX SSL 模塊要求, 這是為了支持 HTTPS 協議。
建置方法:
- (如果在Windows上進行編譯,建議先下載msys模擬linux底下的auto conf操作)。
- LINUX及MSYS環境可以遵循以下操作
PCRE
$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
$ tar -zxf pcre-8.39.tar.gz
$ cd pcre-8.39
$ ./configure
$ make
$ sudo make install
ZLIB
$ wget http://zlib.net/zlib-1.2.8.tar.gz
$ tar -zxf zlib-1.2.8.tar.gz
$ cd zlib-1.2.8
$ ./configure
$ make
$ sudo make install
OPENSSL
$ wget http://www.openssl.org/source/openssl-1.0.2f.tar.gz
$ tar -zxf openssl-1.0.2f.tar.gz
$ cd openssl-1.0.2f
$ ./configure darwin64-x86_64-cc --prefix=/usr
$ make
$ sudo make install
3.選擇原碼
官網查詢連結:https://www.nginx.com/.
如步驟一所述,選擇想要編譯的原碼進行下載
- 日期:2016/11/27 日期相差過遠請至官網查詢正確版本,現在的Matian(維護版)下載
$ wget http://nginx.org/download/nginx-1.11.6.tar.gz
$ tar zxf nginx-1.11.6.tar.gz
$ cd nginx-1.11.6
- 日期:2016/11/27日期相差過遠請至官網查詢正確版本,現在的Stable(穩定版本)下載
$ wget http://nginx.org/download/nginx-1.10.2.tar.gz
$ tar zxf nginx-1.10.2.tar.gz
$ cd nginx-1.10.2
4.建置選項設定
設定編譯選項來決定編譯的方法以及決定那些模塊會編入主程式中。
- 範例:
$ ./configure
--sbin-path=/usr/local/nginx/nginx
--conf-path=/usr/local/nginx/nginx.conf
--pid-path=/usr/local/nginx/nginx.pid
--with-pcre=../pcre-8.39
--with-zlib=../zlib-1.2.8
--with-http_ssl_module
--with-stream
--with-mail=dynamic
--add-module=/usr/build/nginx-rtmp-module
--add-dynamic-module=/usr/build/3party_module
步驟一 模塊及路徑配置選項設置
配置NGINX路徑configure腳本能設置NIGNX二進製文件和配置文件以及依賴庫(如PCRE或SSL)的路徑,以便將它們靜態鏈接到NGINX執行檔。- --prefix = path定義將保留NGINX文件的目錄。該目錄還將用於由./configure腳本(不包括庫路徑)設置的所有相對路徑以及nginx.conf配置文件的路徑。默認情況下,路徑設置為/ usr / local / nginx。
- --sbin-path = path設置NGINX可執行文件的名稱。此名稱僅在安裝期間使用。默認情況下,文件命名為/ sbin / nginx前綴。
- --conf-path = path設置NGINX配置文件的名稱。默認情況下,文件命名為prefix / conf / nginx.conf。請注意,無論此選項如何,都可以通過在命令行中使用-c選項指定不同的配置文件,始終啟動NGINX。
- --pid-path = path設置存儲主進程進程ID的nginx.pid文件的名稱。安裝後,可以使用pid指令在nginx.conf配置文件中始終更改文件名的路徑。默認情況下,文件命名為prefix / logs / nginx.pid。
- --error-log-path = path設置主錯誤,警告和診斷文件的名稱。在成功安裝nginx後,可以使用error_log命令在nginx.conf配置文件中始終更改文件名。默認情況下,文件命名為prefix / logs / error.log。
- --http-log-path = path設置HTTP服務器的主要請求日誌文件的名稱。在成功安裝nginx後,可以使用access_log命令在nginx.conf配置文件中始終更改文件名。默認情況下,文件命名為prefix / logs / access.log。
- --user = name設置非特權用戶的名稱,其憑據將由NGINX工作進程使用。在成功安裝nginx後,可以使用user指令在nginx.conf配置文件中更改名稱。默認用戶名為nobody。
- --group = name設置其憑據將由NGINX工作進程使用的組的名稱。在成功安裝nginx後,可以使用user指令在nginx.conf配置文件中始終更改名稱。默認情況下,組名稱設置為非特權用戶的名稱。
- --with-pcre = path設置PCRE庫源的路徑。在location指令和ngx_http_rewrite_module模塊中,正則表達式支持需要庫。
- --with-pcre-jit使用“即時編譯”支持(pcre_jit指令)構建PCRE庫。
- --with-zlib = path設置zlib庫的源的路徑。庫是ngx_http_gzip_module模塊所必需的。
步驟二 gcc選項設置
在配置腳本中,還可以指定編譯器相關的選項。(如果想額外添加功能上去可以在這做設置)--with-cc-opt = parameters
設置將添加到CFLAGS變量的其他參數。
當在FreeBSD下使用系統PCRE庫時,必須指定--with-cc-opt =“ - I / usr / local / include”。
如果需要增加select()支持的文件數量,也可以在這裡指定,例如:--with-cc-opt =“ - D FD_SETSIZE = 2048”。
--with-ld-opt = parameters
設置將在鏈接期間使用的其他參數。 當在FreeBSD下使用系統PCRE庫時,必須指定--with-ld-opt =“ - L / usr / local / lib”。
步驟三 指定NGINX連接處理方法
在配置腳本中,您可以重新定義基於事件的輪詢方法。 有關詳細信息,請參見連接處理方法。文章連結
(可以把nginx異步調用的方式改成serial方式實現,嵌入式設備須注意這裡細節)
--with- select_module,--without-select_module
啟用或禁用構建允許NGINX使用select()方法的模塊。 如果平台似乎不支持更合適的方法,如kqueue,epoll或/ dev / poll,則此模塊將自動構建。
--with-poll_module,--without-poll_module
啟用或禁用構建允許NGINX使用poll()方法的模塊。 如果平台似乎不支持更合適的方法,如kqueue,epoll或/ dev / poll,則此模塊將自動構建。
--with- select_module,--without-select_module
啟用或禁用構建允許NGINX使用select()方法的模塊。 如果平台似乎不支持更合適的方法,如kqueue,epoll或/ dev / poll,則此模塊將自動構建。
--with-poll_module,--without-poll_module
啟用或禁用構建允許NGINX使用poll()方法的模塊。 如果平台似乎不支持更合適的方法,如kqueue,epoll或/ dev / poll,則此模塊將自動構建。
步驟四 指定模塊
NGINX由模塊組成。 模塊集以及其他構建選項都使用./configure腳本進行配置。
某些模塊是默認構建的 - 它們不需要在configure腳本中指定。 但是可以使用configure腳本中的--without-configure選項從NGINX執行檔文件中排除任何默認模塊。
默認情況下不包括的模塊以及第三方模塊必須在configure腳本中與其他構建選項一起顯式指定。 這樣的模塊可以靜態鏈接到NGINX執行檔,並且每次NGINX啟動時動態加載,或僅當它們在NGINX配置文件中指定時才會加載。
某些模塊是默認構建的 - 它們不需要在configure腳本中指定。 但是可以使用configure腳本中的--without-configure選項從NGINX執行檔文件中排除任何默認模塊。
默認情況下不包括的模塊以及第三方模塊必須在configure腳本中與其他構建選項一起顯式指定。 這樣的模塊可以靜態鏈接到NGINX執行檔,並且每次NGINX啟動時動態加載,或僅當它們在NGINX配置文件中指定時才會加載。
以下為可選用的模塊名稱,太多就不逐條翻譯了。
Module Name Description http_charset_module Adds the specified charset to the “Content-Type” response header field, can convert data from one charset to another. http_gzip_module Compresses responses using the gzip method, helping to reduce the size of transmitted data by half or more. http_ssi_module Processes SSI (Server Side Includes) commands in responses passing through it. http_userid_module Sets cookies suitable for client identification. http_access_module Limits access to certain client addresses. http_auth_basic_module Limits access to resources by validating the user name and password using the HTTP Basic Authenticationprotocol. http_autoindex_module Processes requests ending with the slash character (‘/’) and produces a directory listing. http_geo_module Creates variables with values depending on the client IP address. http_map_module Creates variables whose values depend on values of other variables. http_split_clients_module Creates variables suitable for A/B testing, also known as split testing. http_referer_module Blocks access to a site for requests with invalid values in the Referer header field. http_rewrite_module Changes the request URI using regular expressions and return redirects; conditionally selects configurations. Requires the PCRE library. http_proxy_module Passes requests to another server. http_fastcgi_module Passes requests to a FastCGI server http_uwsgi_module Passes requests to a uwsgi server. http_scgi_module Passes requests to an SCGI server. http_memcached_module Obtains responses from a memcached server. http_limit_conn_module Limits the number of connections per the defined key, in particular, the number of connections from a single IP address. http_limit_req_module Limits the request processing rate per a defined key, in particular, the processing rate of requests coming from a single IP address. http_empty_gif_module Emits single-pixel transparent GIF. http_browser_module Creates variables whose values depend on the value of the “User-Agent” request header field. http_upstream_hash_module Enables the hash load balancing method. http_upstream_ip_hash_module Enables the IP hash load balancing method. http_upstream_least_conn_module Enables the least_conn load balancing method. http_upstream_keepalive_module Enables keepalive connections. http_upstream_zone_module Enables the shared memory zone
本文介绍如何从源码安装Nginx,包括选择版本、编译依赖库、配置选项等步骤,并提供了详细的模块说明。

2767

被折叠的 条评论
为什么被折叠?



