1. 程式人生 > >Linux中Nginx安裝與配置詳解(CentOS-6.5:nginx-1.5.0)

Linux中Nginx安裝與配置詳解(CentOS-6.5:nginx-1.5.0)

1 Nginx簡介
Nginx ("engine x") 是一個高效能的 HTTP 和 反向代理 伺服器,也是一個 IMAP/POP3/SMTP 代理伺服器。 Nginx 是由 Igor Sysoev 為俄羅斯訪問量第二的 Rambler.ru 站點開發的,第一個公開版本0.1.0釋出於2004年10月4日。其將原始碼以類BSD許可證的形式釋出,因它的穩定性、豐富的功能集、示例配置檔案和低系統資源的消耗而聞名。2011年6月1日,nginx 1.0.4釋出。

3 Nginx安裝
3.1 安裝前的準備
1)準備 pcre-8.12.tar.gz。該檔案為正則表示式庫。讓nginx支援rewrite需要安裝這個庫。


2) 準備 nginx-1.5.0.tar.gz。該檔案為nginx的linux版本安裝檔案。
3)確保進行了安裝了linux常用必備支援庫。

Linux中必備常用支援庫的安裝(CentOS-6.5)

在CentOS安裝軟體的時候,可能缺少一部分支援庫,而報錯。這裡首先安裝系統常用的支援庫。那麼在安裝的時候就會減少很多的錯誤的出現。

# yum install -y gcc gdb strace gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs patch e2fsprogs-devel krb5-devel libidn libidn-devel openldap-devel nss_ldap openldap-clients openldap-servers libevent-devel libevent uuid-devel uuid mysql-devel

3.2 正則表示式庫安裝
1)確保進行了安裝了linux常用必備支援庫。檢查是否安裝了g++、gcc。rpm -qa | grep gcc 之後需要出現3個包如下圖所示。如果沒有出現。需要安裝g++、gcc。

# yum install gcc-c++


2) 上傳pcre-8.12.tar.gz, nginx-1.5.0.tar.gz 到 /usr/local/src/nginx目錄下。

3)解壓pcre-8.12.tar.gz
# cd /usr/local/src/nginx
# tar zxvf pcre-8.12.tar.gz

4)進入解壓後的目錄
# cd pcre-8.12

5)配置
# ./configure


6) 編譯
# make
7) 安裝
# make install

3.3 Nginx安裝
0) 建立使用者nginx使用的www使用者。
# groupadd www #新增www組 
# useradd -g www www -s /bin/false #建立nginx執行賬戶www並加入到www組,不允許www使用者直接登入系統

建立安裝目錄與日誌目錄
a) 安裝目錄
# mkdir /usr/local/nginx
b) 日誌目錄
# mkdir /data0/logs/nginx
# chown www:www /data0/logs/nginx -R

1) 判斷系統是否安裝了zlib-devel。如果沒有安裝。使用
# yum install -y zlib-devel



2) 解壓
# cd /usr/local/src/nginx
# tar zxvf nginx-1.5.0.tar.gz

3) 進入目錄
# cd nginx-1.5.0

4) 配置。通常將軟體安裝在/usr/local/目錄下。
# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
5)編譯
# make

6) 安裝
# make install

7) 檢查是否安裝成功
# cd /usr/local/nginx/sbin
# ./nginx -t
結果顯示:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

3.4 配置防火牆80埠
#修改防火牆配置:
# vi + /etc/sysconfig/iptables
#新增配置項
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
#重啟防火牆
# service iptables restart

3.5 上傳配置檔案
1)上傳nginx.conf (檔案下載地址見上面的Linux公社資源站)
# cd /usr/local/nginx/conf
# rz nginx.conf
2) 上傳fastcgi_params.phis
# rz fastcgi_params.phis

3.6 啟動停止重啟與測試
1)啟動
#方法1
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#方法2
# cd /usr/local/nginx/sbin
# ./nginx


2) 停止
#查詢nginx主程序號
ps -ef | grep nginx
#停止程序
kill -QUIT 主程序號
#快速停止
kill -TERM 主程序號
#強制停止
pkill -9 nginx

3) 重啟(首次啟動需:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf)
/usr/local/nginx/sbin/nginx -s reload

4)測試
#測試埠
netstat -na | grep 80
#瀏覽器中測試

#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;  
  
    #kafka監控
    server {  
        listen      9921;    #外網埠
        server_name  kafkaOffsetServer;  #服務名
  
        #charset koi8-r;  
  
        #access_log  logs/host.access.log  main;  
  
        #location / {  
          #root  html;  
            #index  index.html index.htm;  
        #}  
        location / {  
            proxy_pass http://10.2.20.49:8089;  #埠對應叢集服務頁面地址
        }  
  
        #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;  
        }  
    }  
  
    #hdfs服務  
    server {  
        listen      9922;  
        server_name  hadoopHdfsServer;  
  
        #charset koi8-r;  
  
        #access_log  logs/host.access.log  main;  
  
        #location / {  
          #root  html;  
            #index  index.html index.htm;  
        #}  
        location / {  
            proxy_pass http://10.2.20.40:50070;  
        }  
  
        #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;  
        }  
    }  
    
    #yarn服務
    server {  
        listen      9923;    #外網埠
        server_name  yarnServer;  #服務名
  
        #charset koi8-r;  
  
        #access_log  logs/host.access.log  main;  
  
        #location / {  
          #root  html;  
            #index  index.html index.htm;  
        #}  
        location / {  
            proxy_pass http://10.2.20.39:8088;  #埠對應叢集服務頁面地址
        }  
  
        #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;  
        }  
    }
    
    #jobHistory服務
    server {  
        listen      9924;    #外網埠
        server_name  jobHistoryServer;  #服務名
  
        #charset koi8-r;  
  
        #access_log  logs/host.access.log  main;  
  
        #location / {  
          #root  html;  
            #index  index.html index.htm;  
        #}  
        location / {  
            proxy_pass http://10.2.20.41:19888;  #埠對應叢集服務頁面地址
        }  
  
        #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;  
        }  
    }
    
    #hue服務
    server {  
        listen      9925;    #外網埠
        server_name  hueServer;  #服務名
  
        #charset koi8-r;  
  
        #access_log  logs/host.access.log  main;  
  
        #location / {  
          #root  html;  
            #index  index.html index.htm;  
        #}  
        location / {  
            proxy_pass http://10.2.20.44:8888;  #埠對應叢集服務頁面地址
        }  
  
        #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;  
        }  
    }
    
    #oozie服務
    server {  
        listen      9926;    #外網埠
        server_name  oozieServer;  #服務名
  
        #charset koi8-r;  
  
        #access_log  logs/host.access.log  main;  
  
        #location / {  
          #root  html;  
            #index  index.html index.htm;  
        #}  
        location / {  
            proxy_pass http://10.2.20.69:11000;  #埠對應叢集服務頁面地址
        }  
  
        #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;  
        }  
    }
}