Nginx安裝記錄小結

------

##1. Nginx安裝

首先需要預先安裝gcc-g++、PCRE, zlib。PCRE用於重寫rewrite,zlib用於gzip壓縮

到安裝目錄下,
下載nginx

    wget http://nginx.org/download/nginx-1.5.9.tar.gz

解壓

    tar -zxvf nginx-1.5.9.tar.gz

進入到新的目錄下

    cd nginx-1.5.9

###**Nginx的安裝配置**
進行安裝配置configure
在configure命令後面加上相應配置項:

 - --prefix=path    定義一個目錄,存放服務器上的文件 ,也就是nginx的安裝目錄。默認使用    /usr/local/nginx。

 - --prefix=path    定義一個目錄,存放服務器上的文件 ,也就是nginx的安裝目錄。默認使用    /usr/local/nginx。 
 - --sbin-path=path 設置nginx的可執行文件的路徑,默認爲     prefix/sbin/nginx. 
 - --conf-path=path     設置在nginx.conf配置文件的路徑。nginx允許使用不同的配置文件啓動,通過命令行中的-c選項。默認爲prefix/conf/nginx.conf.    
 - --pid-path=path  設置nginx.pid文件,將存儲的主進程的進程號。安裝完成後,可以隨時改變的文件名 ,    在nginx.conf配置文件中使用 PID指令。默認情況下,文件名 爲prefix/logs/nginx.pid. 
 - --error-log-path=path 設置主錯誤,警告,和診斷文件的名稱。安裝完成後,可以隨時改變的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默認情況下,文件名 爲prefix/logs/error.log. 
 - --http-log-path=path 設置主請求的HTTP服務器的日誌文件的名稱。安裝完成後,可以隨時改變的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默認情況下,文件名 爲prefix/logs/access.log.    
 - --user=name  設置nginx工作進程的用戶。安裝完成後,可以隨時更改的名稱在nginx.conf配置文件中 使用的user指令。默認的用戶名是nobody。 
 - --group=name  設置nginx工作進程的用戶組。安裝完成後,可以隨時更改的名稱在nginx.conf配置文件中 使用的 user指令。默認的爲非特權用戶。    
 - --with-select_module --without-select_module    啓用或禁用構建一個模塊來允許服務器使用select()方法。該模塊將自動建立,如果平臺不支持的kqueue,epoll,rtsig或/dev/poll。   
 - --with-poll_module --without-poll_module啓用或禁用構建一個模塊來允許服務器使用poll()方法。該模塊將自動建立,如果平臺不支持的kqueue,epoll,rtsig或/dev/poll。   
 - --without-http_gzip_module — 不編譯壓縮的HTTP服務器的響應模塊。編譯並運行此模塊需要zlib庫。 
 - --without-http_rewrite_module  不編譯重寫模塊。編譯並運行此模塊需要PCRE庫支持。
 - --without-http_proxy_module — 不編譯http_proxy模塊。 
 - --with-http_ssl_module — 使用https協議模塊。默認情況下,該模塊沒有被構建。建立並運行此模塊的OpenSSL庫是必需的。
 - --with-pcre=path    — 設置PCRE庫的源碼路徑。PCRE庫的源碼(版本4.4 - 8.30)需要從PCRE網站下載並解壓。其餘的工作是Nginx的./    configure和make來完成。正則表達式使用在location指令和 ngx_http_rewrite_module 模塊中。
 - --with-pcre-jit —編譯PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)。
 - --with-zlib=path —設置的zlib庫的源碼路徑。要下載從 zlib(版本1.1.3 -
   1.2.5)的並解壓。其餘的工作是Nginx的./ configure和make完成。ngx_http_gzip_module模塊需要使用zlib 。

 - --with-cc-opt=parameters — 設置額外的參數將被添加到CFLAGS變量。例如,當你在FreeBSD上使用PCRE庫時需要使用:--with-cc-opt="-I    /usr/local/include。.如需要需要增加 select()支持的文件數量:--with-cc-opt="-D FD_SETSIZE=2048".

 - --with-ld-opt=parameters   —設置附加的參數,將用於在鏈接期間。例如,當在FreeBSD下使用該系統的PCRE庫,應指定:--with-ld-opt="-L /usr/local/lib".

例如

    ./configure --sbin-path=/usr/local/nginx/nginx \
    --conf-path=/usr/local/nginx/nginx.conf \
    --pid-path=/usr/local/nginx/nginx.pid \
    --without-http_rewrite_module \
    --without-http_gzip_module \
    --user=nginx \
    --group=nginx  

*(這裏由於我沒有裝pcre, zlib,所以選的是without)*

成功後,可以看到目錄中出現了Makefile文件。
先後使用make和make install,安裝完成。

可以用 `nginx –V`來查看版本號。

##2. Nginx的啓動、停止

啓動代碼:
格式:`nginx安裝目錄地址 -c nginx配置文件地址`
如:`/usr/sbin/nginx -c /root/zhangxiao/nginx-1.5.9/conf/nginx.conf`

查看進程:

    ps -ef|grep nginx

停止進程:

    kill -QUIT 進程號
    
##Nginx的nginx.conf配置文件
默認生成的文件爲:

    #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;
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            #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;
        #    }
        #}
    
    }

文件結構:

    ...              #全局塊
    
    events {         #events塊
       ...
    }
    
    http      #http塊
    {
        ...   #http全局塊
        server        #server塊
        { 
            ...       #server全局塊
            location [PATTERN]   #location塊
            {
                ...
            }
            location [PATTERN] 
            {
                ...
            }
        }
        server
        {
          ...
        }
        ...     #http全局塊
    }
其中:

> 1、全局塊:配置影響nginx全局的指令。一般有運行nginx服務器的用戶組,nginx進程pid存放路徑,日誌存放路徑,配置文件引入,允許生成worker
> process數等。
> 2、events塊:配置影響nginx服務器或與用戶的網絡連接。有每個進程的最大連接數,選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網路連接,開啓多個網絡連接序列化等。
> 3、http塊:可以嵌套多個server,配置代理,緩存,日誌定義等絕大多數功能和第三方模塊的配置。如文件引入,mime-type定義,日誌自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數等。
> 4、server塊:配置虛擬主機的相關參數,一個http中可以有多個server。
> 5、location塊:配置請求的路由,以及各種頁面的處理情況。

##Nginx異常解決

###啓動nginx報錯: 

> nginx: [emerg] getpwnam("nginx") failed in /usr/local/nginx/conf/nginx.conf:2

是因爲配製文件中使用的啓動賬戶在系統中並沒有找到
解決辦法:

    useradd -G groupname username

###啓動nginx報錯: 
> Starting nginx: nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed
> (2: No such file or directory)

權限不足,解決辦法:

    cd /var/tmp/
    mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}

##References:

 - http://www.cnblogs.com/knowledgesea/p/5175711.html
 - http://blog.csdn.net/a2796749/article/details/50492664
 - http://blog.csdn.net/tjcyjd/article/details/50695922

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