Nginx 基于Ubuntu的源碼安裝詳解
Nginx介紹
? ? ?Nginx?是一個高性能的HTTP和反向代理服務(wù)器,也是一個IMAP/POP3/SMTP服務(wù)器。Nginx是由伊戈爾·賽索耶夫為俄羅斯訪問量第二的Rambler.ru站點(俄文:Рамблер)開發(fā)的,第一個公開版本0.1.0發(fā)布于2004年10月4日。Nginx是一款輕量級的Web?服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器,并在一個BSD-like 協(xié)議下發(fā)行。其特點是占有內(nèi)存少,并發(fā)能力強,事實上nginx的并發(fā)能力確實在同類型的網(wǎng)頁服務(wù)器中表現(xiàn)較好,中國大陸使用nginx網(wǎng)站用戶有:百度、京東、新浪、網(wǎng)易、騰訊、淘寶等。
Nginx 的安裝
Nginx 安裝之前需要三個依賴的支持
? ? Nginx模塊依賴性:
? ? ? ? ① gzip 模塊需要 zlib 庫?
? ? ? ? ② rewrite 模塊需要 pcre 庫?
? ? ? ? ③ ssl 功能需要 openssl 庫?
1. pcre庫安裝 ,該庫提供了正則表達式的支持。
$ wge ?http://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
$ tar ?-zxf ? pcre-8.42.tar.gz
$ cd pcre-8.42
$ ./configure
$ make
$ sudo ?make ?install
2.?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
3.?openssl庫,nginx ssl模塊需要這個庫來支持https協(xié)議。
$ ?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/local/openssl
$ make
$ sudo make install
4.下載Nginx源碼包,并進行解壓、安裝。
?$ ?wget ? http://nginx.org/download/nginx-1.12.0.tar.gz ? ?
$ ?tar ? ? zxf ? ?nginx-1.12.0.tar.gz ??
$ ? cd ? nginx-1.12.0 ? ?
$ ?./configure ? ?--prefix=/usr/local/nginx2 ? ?--with-pcre ? --with-http_stub_status_module ? ?--with-http_ssl_module ? ? --with-http_gzip_static_module ? ?--with-http_realip_module ? --add-module=/usr/local/nginx2/nginx_upstream_check_module-master ? ?--with-pcre=/usr/local/pcre/pcre-8.42 --with-zlib=/usr/local/zlib/zlib-1.2.11 --with-openssl=/usr/local/openssl/openssl-1.0.2f ??
$ ?make ??
$ ?make ? install
5. 其他選項指定方式
--error-log-path=path 默認是prefix/logs/error.log
--http-log-path=path 默認是prefix/logs/access.log
--user=user ? ? ? ? ? 默認是nobody,表示由哪個用戶運行nginx worker thread
--group=group ? ? ? ? 組名經(jīng)常設(shè)置為一個沒有權(quán)限的用戶名
--with-pcre-jit ? ? ? 構(gòu)建pcre庫時,同時帶上"just-in-time compilation"的功能,這樣就能在配置文件中使用pcre_jit指令了
指定nginx的構(gòu)建選項,同時還可以指定編譯器的選項:
--with-cc-opt=parameters 添加額外的參數(shù)到CFLAGS 變量中
在FreeBSD下,如果使用的是系統(tǒng)自帶的pcre庫,那么編譯時必須添加:--with-cc-opt="-I /usr/local/include";
如果需要增加select()方法支持的文件數(shù)量,可以添加:--with-cc-opt="-D FD_SETSIZE=2048";
--with-ld-opt=parameters ? 添加linking時需要的額外參數(shù)
在FreeBSD下,如果使用的是系統(tǒng)自帶的pcre庫,那么編譯時必須添加:--with-ld-opt="-L /usr/local/lib";
6.?nginx的模塊選擇
? ? ?模塊可以靜態(tài)鏈接到nginx的二進制程序中,這樣當(dāng)nginx啟動的時候,就會加載它們;使用--add-module選項;
? ? ?模塊可以動態(tài)鏈接到nginx的二進制程序中,這樣只有在配置文件中指定這個模塊時,nginx才會去加載它們,使用--add-dynamic-module選項;
Nginx 的基本操作
?1. Nginx版本以及配置查看
root@ubuntu:/usr/local/nginx2/sbin# ./nginx -V
nginx version: nginx/1.12.0
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)
built with OpenSSL 1.0.1c 10 May 2012
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx2 --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --add-module=/usr/local/nginx2/nginx_upstream_check_module-master --with-pcre=/usr/local/pcre/pcre-8.42 --with-zlib=/usr/local/zlib/zlib-1.2.11 --with-openssl=/usr/local/openssl/openssl-1.0.1c-arm
root@ubuntu:/usr/local/nginx2/sbin# ./configure --prefix=/usr/local/nginx2 --with-pcre --with-http_stub_status_module --with-http_ssl_module? --with-http_gzip_static_module --with-http_realip_module --add-module=/usr/local/nginx2/nginx_upstream_check_module-master --with-pcre=/usr/local/pcre/pcre-8.42? --with-zlib=/usr/local/zlib/zlib-1.2.11? --with-openssl=/usr/local/openssl/openssl-1.0.1c-arm
2.?啟動關(guān)閉nginx
*檢查配置文件是否正確
/usr/local/nginx-1.6/sbin/nginx -t ??conf/nginx.conf
*啟動、關(guān)閉
./sbin/nginx # 默認配置文件 conf/nginx.conf,-c 指定
?./sbin/nginx -s stop 或 pkill nginx
*重啟,不會改變啟動時指定的配置文件
?./sbin/nginx -s reload 或 kill -HUP `cat /usr/local/nginx-1.6/logs/nginx.pid`
3.?確認nginx已經(jīng)起來并且運行正常
$ curl -I127.0.0.1
HTTP/1.1200 OK
Server: nginx/1.11.9
