1. 程式人生 > >linux之 Nginx的安裝配置

linux之 Nginx的安裝配置

Nginx安裝手冊 (http://nginx.org/download/)
1 nginx安裝環境
nginx是C語言開發,建議在linux上執行,本教程使用Centos6.5作為安裝環境。
n gcc
安裝nginx需要先將官網下載的原始碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc:yum install gcc-c++
n PCRE
PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 相容的正則表示式庫。nginx的http模組使用pcre來解析正則表示式,所以需要在linux上安裝pcre庫。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。
n zlib
zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。
yum install -y zlib zlib-devel

n openssl
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼演算法、常用的金鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程式供測試或其它目的使用。
nginx不僅支援http協議,還支援https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。
yum install -y openssl openssl-devel

2 編譯安裝
將nginx-1.8.0.tar.gz拷貝至linux伺服器。

解壓:
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0

./configure —prefix=/usr/local/nginx —with-http_stub_status_module —with-http_ssl_module
1、 configure
./configure —help查詢詳細引數(參考本教程附錄部分:nginx編譯引數)

引數設定如下:
./configure \
—prefix=/usr/local/nginx \
—pid-path=/var/run/nginx/nginx.pid \
—lock-path=/var/lock/nginx.lock \
—error-log-path=/var/log/nginx/error.log \
—http-log-path=/var/log/nginx/access.log \
—with-http_gzip_static_module \
—http-client-body-temp-path=/var/temp/nginx/client \
—http-proxy-temp-path=/var/temp/nginx/proxy \
—http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
—http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
—http-scgi-temp-path=/var/temp/nginx/scgi

注意:上邊將臨時檔案目錄指定為/var/temp/nginx,需要在/var下建立temp及nginx目錄

2、 編譯安裝
make
make install
3 啟動nginx
cd /usr/local/nginx/sbin/
./nginx
15098是nginx主程序的程序id,15099是nginx工作程序的程序id

注意:執行./nginx啟動nginx,這裡可以-c指定載入的nginx配置檔案,如下:
./nginx -c /usr/local/nginx/conf/nginx.conf
如果不指定-c,nginx在啟動時預設載入conf/nginx.conf檔案,此檔案的地址也可以在編譯安裝nginx時指定./configure的引數(—conf-path= 指向配置檔案(nginx.conf))

4 停止nginx
方式1,快速停止:
cd /usr/local/nginx/sbin
./nginx -s stop
此方式相當於先查出nginx程序id再使用kill命令強制殺掉程序。

方式2,完整停止(建議使用):
cd /usr/local/nginx/sbin
./nginx -s quit
此方式停止步驟是待nginx程序處理任務完畢進行停止。

5 重啟nginx
方式1,先停止再啟動(建議使用):
對nginx進行重啟相當於先停止nginx再啟動nginx,即先執行停止命令再執行啟動命令。
如下:
./nginx -s quit
./nginx

方式2,重新載入配置檔案:
當nginx的配置檔案nginx.conf修改後,要想讓配置生效需要重啟nginx,使用-s reload不用先停止nginx再啟動nginx即可將配置資訊在nginx中生效,如下:
./nginx -s reload

6 測試
nginx安裝成功,啟動nginx,即可訪問虛擬機器上的nginx: