Nginx Linux安裝與部署

 

Nginx (engine x) 是一個高性能的HTTP和反向代理服務,也是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。其特色是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好,在高鏈接併發的狀況下,Nginx是Apache服務器不錯的替代品,同時佔用的資源不多,併兼容unix,linux,windows平臺。中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等。php

 

系統環境:cat /etc/redhat-releasehtml

1.安裝編譯工具及庫文件linux

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-develnginx

2.首先要安裝 PCRE(PCRE 做用是讓 Nginx 支持 Rewrite 功能)c++

yum install pcre pcre-devel -yweb

rpm -qa pcre pcre-develvim

3.創建nginx管理用戶:windows

groupadd -r nginx瀏覽器

useradd  –s /sbin/nologin -g nginx –M nginx性能優化

id nginx

4.下載nginx軟件並解壓源碼安裝

mkdir –p /home/bqh/tools

cd /home/bqh/tools

wget –q http://nginx.org/download/nginx-1.6.3.tar.gz

tar zxf nginx-1.6.3.tar.gz

./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module

若執行上面命令提示:

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

解決方法:

yum -y install openssl openssl-devel

再執行./configure命令....


Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using system zlib library

nginx path prefix: "/application/nginx-1.6.3"
nginx binary file: "/application/nginx-1.6.3/sbin/nginx"
nginx configuration prefix: "/application/nginx-1.6.3/conf"
nginx configuration file: "/application/nginx-1.6.3/conf/nginx.conf"
nginx pid file: "/application/nginx-1.6.3/logs/nginx.pid"
nginx error log file: "/application/nginx-1.6.3/logs/error.log"
nginx http access log file: "/application/nginx-1.6.3/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

 出現以上結果表明ok的,接着咱們執行:

make && make install

若是執行make這一步的時候只提示:make[1]: Leaving directory `/home/bqh/tools/nginx-1.6.3'不用管它,繼續執行make install 便可。

注意:此方法只針對報錯一行,若是你安裝nginx走到make這一步的時候,報錯了不少行的make解決辦法就不同了,好比說:有時候安裝nginx須要指定 pcre 的源碼目錄等。

介紹:

--prefix=軟件安裝到哪裏

--user=用戶 (默認nobody)

--group=用戶組 (默認nobody)

--with-http_ssl_module  加密模塊

--with-http_stub_status_module  狀態模塊

cd ..

ln -s /application/nginx-1.6.3/ /application/nginx   作個軟鏈接(快捷方式)隱藏版本

安裝完nginx後,並不能直接對外提供服務,須要先啓動nginx才行。

若是Ngnix軟件不是本人裝的,那麼咱們能夠用如下命令查看安裝路徑及參數信息:

/application/nginx/sbin/nginx –V

啓動nginx :

啓動前檢查配置文件語法:/application/nginx/sbin/nginx -t

/application/nginx/sbin/nginx    啓動nginx

ps -ef|grep nginx|grep -v grep   檢查是否啓動了

ss -lntup|grep nginx      檢查是否啓動了

咱們在本地用curl 127.0.0.1 檢查nginx啓動的實際效果以下:

在windows下經過瀏覽器檢測的方式以下:

到這裏,nginx服務已經完成搭建了。若搭建靜態網站,咱們能夠把靜態網頁放到/application/nginx/html/下便可。

例如:

下面咱們進入安裝nginxd的目錄下:/application/nginx-1.6.3/conf

咱們去除掉默認配置文件裏的註釋和空行並重定向到nginx.conf文件裏,同時咱們須要配置以下:

egrep -v "#|^$" nginx.conf.default >nginx.conf   //去掉包含#號和空行的內容

nginx.conf具體配置以下:

[root@lamp01 conf]# vim nginx.conf
[root@lamp01 conf]# cat nginx.conf
worker_processes  1;   #worker進程的數量
events {               #事件區塊開始
    worker_connections  1024; #每一個worker進程支持的最大鏈接數
}          #事件區塊結束
http {     #http區塊開始
    include       mime.types;  #nginx支持的媒體類型庫文件包含
    default_type  application/octet-stream; #默認的媒體類型
    sendfile        on; #開啓高效傳輸模式
    keepalive_timeout  65; #鏈接超時
    server { #第一個server區塊開始,表示一個獨立的虛擬主機站點
        listen       80; #提供的服務的端口,默認80
        server_name  www.test.com; #提供服務的域名主機名
        location / { #第一個location區塊開始
            root   html; #站點的根目錄,相對於nginx安裝目錄
            index  index.html index.htm; #默認的首頁文件,多個用空格分開
           }
        error_page   500 502 503 504  /50x.html; #出現對應的http狀態碼使用50x.html迴應
     }
}

在/application/nginx/html目錄下一個靜態文件 index.html

[root@lamp01 html]# pwd
/application/nginx/html
[root@lamp01 html]# vim index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>第一個靜態文件</title>
</head>
<body>
Hello world!歡迎來到錦衣衛博客!
</body>
</html>

 咱們經過hosts來解析

 咱們首先刷新配置,從本地訪問www.test.com看是否成功。

/application/nginx/sbin/nginx -t

/application/nginx/sbin/nginx -s reload

一樣在windows系統,配置一下host在「C:\Windows\System32\drivers\etc」下的hosts中配置一下域名解析

 而後cmd再ping一下這個域名是否正確指向了這個IP上

正確指向後在telnet一下80端口看一下是否能夠與端口通訊(若是telnet提示沒有此命令是沒有安裝客戶端,在啓用或禁用windows功能處安裝後再操做便可)

 出現下界面及表示通訊成功。

打開瀏覽器,輸入www.test.com會獲得如下結果,就說明外網訪問成功

測試都成功,ok!

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

nginx經常使用命令:

/application/nginx/sbin/nginx -t                       啓動前檢查配置文件語法

/application/nginx/sbin/nginx                          啓動nginx

/application/nginx/sbin/nginx –V                     查看安裝路徑及參數信息

/application/nginx/sbin/nginx -s reload           從新載入配置文件

/application/nginx/sbin/nginx -s reopen          重啓 Nginx

/application/nginx/sbin/nginx -s stop              中止 Nginx

nginx軟件功能模塊說明:

  nginx http功能模塊:

ngx_http_core_module   //包括一些核心的http參數配置,對應nginx的配置爲http區塊部分

ngx_http_access_module   //訪問控制模塊,用來控制網站用戶對nginx的訪問

ngx_http_gzip_module   //壓縮模塊,對nginx返回的數據壓縮,屬於性能優化模塊

ngx_http_fastcgi_module   //fastcgi模塊,和動態應用相關的模塊,例如php

ngx_http_proxy_module   //代理模塊

ngx_http_upstream_module  //負載均衡模塊,實現網站的負載均衡功能及節點的健康檢查

ngx_http_rewrite_module  //url地址重寫模塊

ngx_http_limit_conn_module  //限制用戶併發鏈接數及請求數模塊

ngx_http_limit_req_module  //限制nginx request processing rate 根據定義的key

ngx_http_log_module  //訪問日誌模塊,以指定的格式記錄nginx客戶訪問日誌等信息

ngx_http_auth_basic_module  //web認證模塊,設置web用戶經過帳號密碼訪問nginx

ngx_http_ssl_module  //ssl模塊,用於加密的http鏈接,如https

ngx_http_stub_status_module  //記錄nginx基本訪問狀態信息等的模塊

相關文章
相關標籤/搜索