Linux下安裝nginx

  1. 編譯安裝nginx
    • 下載php

      wget http://nginx.org/download/nginx-1.14.0.tar.gzhtml

    • 解壓node

      tar -xzvf nginx-1.14.0.tar.gzlinux

    • 配置安裝目錄nginx

      ./configure --prefix=/usr/local/nginxweb

    • 編譯安裝算法

      make && make insatll瀏覽器

    注:在 configure 過程當中可能遇到的問題
checking for PCRE library ... not found
    checking for PCRE library in /usr/local/ ... not found
    checking for PCRE library in /usr/include/pcre/ ... not found
    checking for PCRE library in /usr/pkg/ ... not found
    checking for PCRE library in /opt/local/ ... not found

    ./configure: error: the HTTP rewrite module requires the PCRE library.
    You can either disable the module by using --without-http_rewrite_module
    option, or install the PCRE library into the system, or build the PCRE library
    statically from the source with nginx by using --with-pcre=<path> option.
出錯的緣由是 Nginx 模塊須要依賴一些 lib 庫,解決辦法以下

yum -y install pcre-devel zlib-devel緩存

  1. 配置nginx環境變量
    • 添加環境變量安全

      vi /etc/profile

    在末尾添加

    export PATH=$PATH:/usr/local/nginx/sbin

    最後,執行命令 source /etc/profile 使其修改生效,執行完可經過echo $PATH命令查看是否添加成功。

    • 訪問80端口,查看是否安裝成功

    • Linux經常使用命令
    啓動服務:nginx
        退出服務:nginx -s quit
        強制關閉服務:nginx -s stop
        重載服務:nginx -s reload  (重載服務配置文件,相似於重啓,但服務不會停止)
        驗證配置文件:nginx -t
        使用配置文件:nginx -c "配置文件路徑"
        使用幫助:nginx -h
  2. nginx 配置文件說明
    在項目使用中,使用最多的三個核心功能是靜態服務器、反向代理和負載均衡。
    這三個不一樣的功能的使用,都跟Nginx的配置密切相關,Nginx服務器的配置信息主要集中在"nginx.conf"這個配置文件中,而且全部的可配置選項大體分爲如下幾個部分.

    ```xml {.line-numbers}
    main
    # 全局配置
    events { # 工做模式配置
    }

    http { # http設置
    ....
    server { # 服務器主機配置(虛擬主機、反向代理 等)
    ....
    location { # 路由配置(虛擬目錄等)
    ....
    }
    location path {
    ....
    }
    location otherpath {
    ....
    }
    }

    server {
         ....
    
         location {
             ....
         }
     }

    upstream name { # 負載均衡配置
    ....
    }
    }
    ```

    • main模塊
      • user 用來指定nginx worker進程運行用戶以及用戶組,默認nobody帳號運行
      • worker_processes 指定nginx要開啓的子進程數量,運行過程當中監控每一個進程消耗內存(通常幾M~幾十M不等)根據實際狀況進行調整,一般數量是CPU內核數量的整數倍
      • error_log 定義錯誤日誌文件的位置及輸出級別【debug / info / notice / warn / error / crit】
      • pid 用來指定進程id的存儲文件的位置
      • worker_rlimit_nofile 用於指定一個進程能夠打開最多文件數量的描述
        ...
    • event模塊
      • worker_connections 指定最大能夠同時接收的鏈接數量,這裏必定要注意,最大鏈接數量是和worker processes共同決定的。
      • multi_accept 配置指定nginx在收到一個新鏈接通知後儘量多的接受更多的鏈接
      • use epoll 配置指定了線程輪詢的方法,若是是linux2.6+,使用epoll,若是是BSD如Mac請使用Kqueue
        ...
    • http模塊
      • 做爲web服務器,http模塊是nginx最核心的一個模塊,配置項也是比較多的,項目中會設置到不少的實際業務場景,須要根據硬件信息進行適當的配置。
#main 模塊

#user  nobody;      用來指定nginx worker進程運行用戶以及用戶組,默認nobody帳號運行

worker_processes  1;     指定nginx要開啓的子進程數量,一般數量是CPU內核數量的整數倍

#error_log  logs/error.log;               定義錯誤日誌文件的位置及輸出級別 【debug / info / notice / warn / error / crit】
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;             用來指定進程id的存儲文件的位置


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;                配置on讓sendfile發揮做用,將文件的回寫過程交給數據緩衝去去完成,而不是放在應用中完成,這樣的話在性能提高有有好處
    #tcp_nopush     on;                讓nginx在一個數據包中發送全部的頭文件,而不是一個一個單獨發
    #tcp_nodelay on                    讓nginx不要緩存數據,而是一段一段發送,若是數據的傳輸有實時性的要求的話能夠配置它,發送完一小段數據就馬上能獲得返回值,可是不要濫用哦

    #keepalive_timeout  0;             給客戶端分配鏈接超時時間,服務器會在這個時間事後關閉鏈接。通常設置時間較短,可讓nginx工做持續性更好
    keepalive_timeout  65;

    # client_header_timeout            設置請求頭的超時時間
    # client_body_timeout              設置請求體的超時時間

    # send_timeout                     指定客戶端響應超時時間,若是客戶端兩次操做間隔超過這個時間,服務器就會關閉這個連接


    # limit_conn_zone $binary_remote_addr zone=addr:5m           設置用於保存各類key的共享內存的參數,
    # limit_conn addr 100                                        給定的key設置最大鏈接數


    # server_tokens                    雖然不會讓nginx執行速度更快,可是能夠在錯誤頁面關閉nginx版本提示,對於網站安全性的提高有好處哦


    # default_type application/octet-stream                      指定默認處理的文件類型能夠是二進制

    # type_hash_max_size 2048          混淆數據,影響三列衝突率,值越大消耗內存越多,散列key衝突率會下降,檢索速度更快;值越小key,佔用內存較少,衝突率越高,檢索速度變慢


    
    # ssl_protocols                    指令用於啓動特定的加密協議,nginx在1.1.13和1.0.12版本後默認是ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2,TLSv1.1與TLSv1.2要確保OpenSSL >= 1.0.1 ,SSLv3                                                      如今還有不少地方在用但有很多被攻擊的漏洞。

    # ssl prefer server ciphers        設置協商加密算法時,優先使用咱們服務端的加密套件,而不是客戶端瀏覽器的加密套件

    #gzip  on;                         是告訴nginx採用gzip壓縮的形式發送數據。這將會減小咱們發送的數據量

    server {                           一個虛擬主機的配置,一個http中能夠配置多個server
        listen       80;
        server_name  localhost;        用來指定ip地址或者域名,多個配置之間用空格分隔

        #charset koi8-r;               用於設置www/路徑中配置的網頁的默認編碼格式

        #access_log  logs/host.access.log  main;                 用於指定該虛擬主機服務器中的訪問記錄日誌存放路徑

        location / {                   location模塊是Nginx配置中出現最多的一個配置,主要用於配置路由訪問信息    / 表示匹配訪問根目錄
            root   html;               用於指定訪問根目錄時,訪問虛擬主機的web目錄
            index  index.html index.htm;                         在不指定訪問具體資源時,默認展現的資源文件列表
        }



        location / {                   經過反向代理代理服務器訪問模式,經過proxy_set配置讓客戶端訪問透明化
            proxy_pass http://localhost:8888;
            proxy_set_header X-real-ip $remote_addr;
            proxy_set_header Host $http_host;
        }



        location / {                   uwsgi配置
            include uwsgi_params;
            uwsgi_pass localhost: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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
相關文章
相關標籤/搜索