Nginx安裝教學 - 吉姆的電腦閣誌

吉姆的電腦閣誌

HTML5,Jquery,PHP,FreeBSD分享教學。電腦是用來節省時間及提升樂趣,不是用來把人綁住的。隨心而行,順著生命的脈動重新創造屬於自己的生活。

Breaking

Home Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

2015-04-09

Nginx安裝教學

Nginx(讀成engine x)是近年來快速竄紅的一套結合了網頁伺服器以及郵件伺服的軟體,由俄國程式設計師Igor Sysoev所開發。它不僅具備輕量級的優點,在與Wordpress的完美搭配更讓許多站長稱讚不已。
Nginx更新速度也很快,安裝時建議查看官網是否有釋出新的版本,依目前看來幾乎1~2個月就會釋出新版。Nginx跨平台的支援同時能在Unix、BSD、OS X、Solaries及Windows下執行網頁伺服器的工作,不過在官網上也有明示,因為使用的是傳統的Win32 API,所以在Windows上跑這怪物時,別想期待它能給你帶來多大的效率。
  1. 系統環境

    FreeBSD 8.4-RELEASE

  2. 準備工作

    1. 下戴 nginx-1.7.10並解壓縮
      [root@dmz /data/work]# wget http://nginx.org/download/nginx-1.7.12.tar.gz 
  3. 安裝 nginx

      編譯參數

      [root@dmz /data/work/nginx-1.7.10]#./configure --sbin-path=/usr/local/nginx/nginx \
      --conf-path=/usr/local/etc/nginx/nginx.conf \
      --pid-path=/var/run/nginx.pid \
      --with-http_ssl_module \
      --with-pcre=../pcre-8.35 --with-zlib=../zlib-1.2.8
  4. 編譯

    [root@dmz /data/work/nginx-1.7.10]# make
  5. 安裝

    [root@dmz /data/work/nginx-1.7.10]# make install
  6. rc.conf設定

    加入這行使nginx在開機時能自動執行:
    # nginx
    nginx_enable="YES"
    下戴nginx啟動檔,並將它放在/usr/local/etc/rc.d/目錄下即可,記得改變屬性為755。
    wget https://googledrive.com/host/0B7mMj8dra51DaW9OdFJGdGh4UHM -O /usr/local/etc/rc.d/nginx
    chmod +x /usr/local/etc/rc.d/nginx
  7. nginx.conf調整設定

    編輯nginx設定檔,複制預設的檔案來修改即可。
    cp /usr/local/etc/nginx/nginx.conf-dist /usr/local/etc/nginx/nginx.conf
    vi /usr/local/etc/nginx/nginx.conf
    小編吉姆的設定:
    worker_processes(L5)是CPU處理器的數量,請看下文解釋。server_name(L37)則是網站網址(FQDN)。
    #網站路徑#請改為自己的www路徑即可(L39,L50)。如果您還未完成php-fpm的安裝與設定,可先前往php-fpm教學參考。
    #----------------------------------------------------------
    #--- /usr/local/etc/nginx/nginx.conf
    #----------------------------------------------------------
    user  nobody;
    worker_processes  1;
    error_log  /data/weblogs/nginx-error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    pid        /var/run/nginx.pid;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        #access_log  logs/access.log  main;
        sendfile        on;
        #tcp_nopush     on;
        keepalive_timeout  1;
       #charset koi8-r;
        port_in_redirect off;
        gzip on;
        gzip_http_version 1.1;
        gzip_vary on;
        gzip_comp_level 6;
        gzip_proxied any;
        gzip_types text/plain text/css application/json application/x-javascript application/xml application/xml+rss text/javascript;
        gzip_buffers 16 8k;
        gzip_disable "MSIE [1-6].(?!.*SV1)";
       #access_log  logs/host.access.log  main;
        error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
            server {
                    listen 80;
                    server_name w1.localhost;
                    server_name_in_redirect off;
                    root #網站路徑#;
    
                    location ~* ^.+\.(ico|js|gif|jpg|jpeg|png|bmp)$ {
                      expires 30d;
                    }
                    location / {
                        index index.php;
                    }
                    location ~ \.php$ {
                        fastcgi_pass 127.0.0.1:9000;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME #網站路徑#/$fastcgi_script_name;
                        include fastcgi_params;
                    }
                    location ~ /\.ht {
                        deny all;
                    }
            }
        }
    
  8. 如何找出cpu處理器數量

    #sysctl -a | egrep -i 'hw.machine|hw.model|hw.ncpu'

    # sysctl hw.model hw.machine hw.ncpu
    hw.machine: i386
    hw.model: Intel(R) Pentium(R) 4 CPU 1.80GHz
    hw.ncpu: 1
    hw.machine_arch: i386
  9. 測試設定檔

    [root@dmz /usr/local/etc/nginx]# nginx -t
    nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

    沒問題就可以啟動了。
    [root@dmz /usr/local/etc/nginx]# /usr/local/etc/rc.d/nginx start
    Performing sanity check on nginx configuration:
    nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
    Starting nginx.


  • nginx指令略解

    用法:nginx -s 參數

        nginx -s stop — 立即強迫停止nginx
        nginx -s quit —  正常程序停止nginx
        nginx -s reload — 重新讀取設定檔
        nginx -s reopen — 重新開啟記錄檔(log)
    
  • 參考來源

    沒有留言:

    Post Bottom Ad

    Responsive Ads Here

    Pages