linux上安裝nginx

  1. 將安裝包放在指定目錄下.
    這裏寫圖片描述
  2. 解壓到指定目錄 .
    tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/application
    這裏寫圖片描述
  3. 安裝之前的準備
    下載所需要的依賴庫文件:
    yum install pcre , yum install pcre-devel
    yum install zlib , yum install zlib-devel

  4. 進行configure配置 .
    cd nginx-1.6.2 && ./configure –prefix=/usr/local/application/nginx
    查看是否報錯

  5. 編譯安裝
    make && make install

  6. 啓動
    cd /usr/local/application/nginx/sbin
    執行./nginx 關閉(-s stop) 重啓 (-s reload)
    然後查看80端口看是否啓動.


補充:

記錄一個常見配置文件模板, 方便學習配置:


#user  nobody;

#開啓進程數 <=CPU數 
worker_processes  1;

#錯誤日誌保存位置
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#進程號保存文件
#pid        logs/nginx.pid;

#每個進程最大連接數(最大連接=連接數x進程數)每個worker允許同時產生多少個鏈接,默認1024
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壓縮
    #gzip  on;

    #設定請求緩衝
    #client_header_buffer_size 1k;
    #large_client_header_buffers 4 4k;

    #設定負載均衡的服務器列表
    #upstream myproject {
        #weigth參數表示權值,權值越高被分配到的機率越大
        #max_fails 當有#max_fails個請求失敗,就表示後端的服務器不可用,默認爲1,將其設置爲0可以關閉檢查
        #fail_timeout 在以後的#fail_timeout時間內nginx不會再把請求發往已檢查出標記爲不可用的服務器
    #}

    #webapp
    #upstream myapp {   
    # server 192.168.1.171:8080 weight=1 max_fails=2 fail_timeout=30s;   
    # server 192.168.1.172:8080 weight=1 max_fails=2 fail_timeout=30s;   
    #} 

    #配置虛擬主機,基於域名、ip和端口
    server {
        #監聽端口
        listen       80;
        #監聽域名
        server_name  localhost;

        #charset koi8-r;

        #nginx訪問日誌放在logs/host.access.log下,並且使用main格式(還可以自定義格式)
        #access_log  logs/host.access.log  main;

        #返回的相應文件地址
        location / {
            #設置客戶端真實ip地址
            #proxy_set_header X-real-ip $remote_addr;      
            #負載均衡反向代理
            #proxy_pass http://myapp;

            #返回根路徑地址(相對路徑:相對於/usr/local/nginx/)
            root   html;
            #默認訪問文件
            index  index.html index.htm;
        }

        #配置反向代理tomcat服務器:攔截.jsp結尾的請求轉向到tomcat
        #location ~ \.jsp$ {
        #    proxy_pass http://192.168.1.171:8080;
        #}      

        #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;
        #}
    }

    #虛擬主機配置:
    server {
        listen 1234;
        server_name bhz.com;
        location / {
        #正則表達式匹配uri方式:在/usr/local/nginx/bhz.com下 建立一個test123.html 然後使用正則匹配
        #location ~ test {
            ## 重寫語法:if return (條件 = ~ ~*)
            #if ($remote_addr = 192.168.1.200) {
            #       return 401;
            #}      

            #if ($http_user_agent ~* firefox) {
            #      rewrite ^.*$ /firefox.html;
            #      break;
            #}          

            root bhz.com;
            index index.html;
        }

        #location /goods {
        #       rewrite "goods-(\d{1,5})\.html" /goods-ctrl.html;
        #       root bhz.com;
        #       index index.html;
        #}

        #配置訪問日誌
        access_log logs/bhz.com.access.log main;
    }



    # 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;
    #    }
    #}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章