nginx學習之詳細安裝篇(二)


1. 選擇穩定版還是主線版

主線版:包含最新的功能且修復了已知的bug,但該版本可能會含有一些屬於實驗性的模塊,同時可能會引入新的其他bug,所以如果只是做測試使用,可以使用主線版。

穩定版:不包含最新的功能,但修復了嚴重的bug,建議用在生產環境。

 

2. 使用預編譯安裝包還是源碼安裝包

預編譯包:這種是最簡單和最快速的用於安裝源碼nginx的方式。預編譯包幾乎包含了所有的官方模塊,且大部分的操作系統都能安裝。

源碼安裝:這種方式的特點是靈活,你可以只安裝需要的模塊,或者第三方模塊,或者一些最近發布的安全補丁。

 

3. 源碼安裝nginx

(1) 安裝nginx的依賴

首先是PCRE庫nginx的core模塊rewrite模塊依賴於這個庫,同時這個庫還提供了正則表達式的支持。

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

 

然后是zlib庫,Gzip模塊需要這個庫來做headers壓縮。

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

 

最后是openssl庫nginx ssl模塊需要這個庫來支持https協議。

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

 

(2) 下載nginx源碼包

下載主線版本:

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

 

下載穩定版本:

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

 

(3) 配置構建選項

下面是一個樣例,具體的安裝路徑你可以根據你的情況來

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

上面的配置選項中,我們指定了二進制程序的目錄,配置文件的目錄,PID文件的目錄,以及依賴庫的目錄(這樣指定是為了將pcre庫和ssl庫靜態鏈接到nginx二進制程

序中,以免使用時動態尋找pcre庫和ssl庫),同時帶上了http_ssl_module,mail,stream,另外指定了一個靜態模塊nginx-rtmp-module和一個動態的第三方模塊。

 

還可以指定的選項有:

--error-log-path=path  默認是prefix/logs/error.log

--http-log-path=path   默認是prefix/logs/access.log

--user=user            默認是nobody,表示由哪個用戶運行nginx worker thread

--group=group          組名經常設置為一個沒有權限的用戶名

--with-pcre-jit        構建pcre庫時,同時帶上"just-in-time compilation"的功能,這樣就能在配置文件中使用pcre_jit指令了

 

除了指定nginx的構建選項,同時還可以指定編譯器的選項:

--with-cc-opt=parameters   添加額外的參數到CFLAGS 變量中

在FreeBSD下,如果使用的是系統自帶的pcre庫,那么編譯時必須添加:--with-cc-opt="-I /usr/local/include";
如果需要增加select()方法支持的文件數量,可以添加:--with-cc-opt="-D FD_SETSIZE=2048"; --with-ld-opt=parameters   添加linking時需要的額外參數
在FreeBSD下,如果使用的是系統自帶的pcre庫,那么編譯時必須添加:
--with-ld-opt="-L /usr/local/lib";

 

指定nginx的連接處理方式:

 

--with-select_module

--without-select_module

select是默認的連接處理方式,如果平台不支持kqueue,epoll,/dev/poll,默認會自動構建這個方法。

 

--with-poll_module

--without-poll_module

如果想使用poll這個方法,就使用上面的方式。這樣就能替換掉默認的select。

 

nginx的模塊選擇:

某些模塊默認已經編譯了,所以不需要在configure后面加上它們。但是如果你不想要這個模塊,可以在configure后面加上--without來去掉它。

模塊可以靜態鏈接到nginx的二進制程序中,這樣當nginx啟動的時候,就會加載它們;使用--add-module選項來做靜態鏈接。所謂靜態鏈接,就是將模塊完全打進nginx的二進制文件中。nginx啟動,它就運行。

模塊可以動態鏈接到nginx的二進制程序中,這樣只有在配置文件中指定這個模塊時,nginx才會去加載它們,使用--add-dynamic-module選項來做動態鏈接。所謂動態鏈接,就是只在需要的時候才去加載那個模塊,不需要的時候,就不加載,很好地控制了內存的使用。通過在配置文件中來指定是否使用某模塊。

 

nginx中默認已編譯的模塊列表:

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

 

nginx中默認沒有編譯的模塊:

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

 

第三方模塊的地址:

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

注意:第三方模塊的質量不被保證,所以需要你自己承擔風險。

 

靜態鏈接第三方模塊:

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

 

動態鏈接第三方模塊:

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

編譯出的動態模塊會出現在目錄/usr/local/nginx/modules/目錄下,以*.so的格式存在。

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

 

完成安裝:

$ make
$ sudo make install

 

4. 使用預編譯安裝包

最快速的方式是使用Redhat/CentOS倉庫的預編譯包nginx.***.rpm,但是這些包都是過期的,比如對於CentOS 7.0,它的倉庫中,nginx的版本居然還是1.6.5的,現在的

官方版本都到1.12.0了,所以最好是替換掉CentOS的默認倉庫地址,修改為nginx官方的倉庫地址(修改repo文件)。

 

(1)使用默認的RedHat/CentOS倉庫

安裝EPEL倉庫(這是紅帽維護的一個倉庫)

$ sudo yum install epel-release

 

更新倉庫,從而安裝開源的nginx

$ sudo yum update

 

確認安裝

$ sudo nginx -v
nginx version: nginx/1.6.3

 

(2)使用nginx的官方倉庫

首先編輯倉庫文件:

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

 

添加內容:

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

說明:

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

 

比如:如果你的操作系統是CentOS 7.0,你要安裝主線版的nginx,那么文件內容如下:

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

 

更新倉庫:

$ sudo yum update

 

運行nginx:

$ sudo nginx

 

確認nginx已經起來並且運行正常:

$ curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.11.9


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM