天天看點

在Centos上編譯安裝nginx

實驗環境:

    OS: CentOS 6.6 

   nginx:nginx-1.6.2.tar.gz

前期準備:

安裝開發包元件

[root@1inux tmp]# yum -y groupinstall "Development tools" "Server Platform Development"

[root@1inux tmp]# yum -y install pcre-devel

一、 編譯安裝: 

[root@1inux tmp]# useradd -r nginx    //添加nginx系統使用者

[root@1inux tmp]# tar xf nginx-1.6.2.tar.gz 

[root@1inux tmp]# cd nginx-1.6.2

[root@1inux nginx-1.6.2]# ./configure --help        //擷取幫助

[root@1inux nginx-1.6.2]#  mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi,uwsgi}    //建立編譯安裝需要的目錄

<code>[root@1inux nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/</code><code>var</code><code>/log/nginx/error.log --http-log-path=/</code><code>var</code><code>/log/nginx/access.log --pid-path=/</code><code>var</code><code>/run/nginx/nginx.pid --lock-path=/</code><code>var</code><code>/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/</code><code>var</code><code>/tmp/nginx/client --http-proxy-temp-path=/</code><code>var</code><code>/tmp/nginx/proxy --http-fastcgi-temp-path=/</code><code>var</code><code>/tmp/nginx/fastcgi --http-uwsgi-temp-path=/</code><code>var</code><code>/tmp/nginx/uwsgi</code>

添加path路徑

<code>[root@1inux nginx]# </code><code>echo</code>  <code>"export PATH=/usr/local/nginx/sbin/nginx:$PATH"</code> <code>&gt; /etc/profile.d/nginx.sh</code>

加載:

<code>[root@1inux nginx]# . /etc/profile.d/nginx.sh</code>

啟動nginx

<code>[root@1inux nginx]# /usr/local/nginx/sbin/nginx</code>

<code>[root@1inux nginx]# ss -tunlp | grep :80</code>

<code>tcp    LISTEN     0      128                    *:80                    *:*      users:((</code><code>"nginx"</code><code>,52985,6),(</code><code>"nginx"</code><code>,52986,6))</code>

<code>[root@1inux nginx]#</code>

檢視nginx啟動程序情況

<code>[root@1inux nginx]# ps aux | grep nginx</code>

<code>root      52985  0.0  0.1  45044  1064 ?        Ss   03:54   0:00 nginx: master process /usr/local/nginx/sbin/nginx</code>

<code>nginx     52986  0.0  0.1  45472  1636 ?        S    03:54   0:00 nginx: worker process      </code>

<code>root      52991  0.0  0.0 103252   836 pts/8    S+   03:55   0:00 grep nginx</code>

<code>[root@1inux nginx]# /usr/local/nginx/sbin/nginx -h   </code><code>//檢視nginx 選項</code>

<code>nginx version: nginx/1.6.2</code>

<code>Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]</code>

<code>Options:</code>

<code>  </code><code>-?,-h         : this help</code>

<code>  </code><code>-v            : show version </code><code>and</code> <code>exit</code>

<code>  </code><code>-V            : show version </code><code>and</code> <code>configure options then </code><code>exit</code>

<code>  </code><code>-t            : test configuration </code><code>and</code> <code>exit</code>

<code>  </code><code>-q            : suppress non-error messages during configuration testing</code>

<code>  </code><code>-s signal     : send signal to a master process: stop, quit, reopen, reload</code>

<code>  </code><code>-p prefix     : set prefix path (</code><code>default</code><code>: /usr/local/nginx/)</code>

<code>  </code><code>-c filename   : set configuration file (</code><code>default</code><code>: /etc/nginx/nginx.conf)</code>

<code>  </code><code>-g directives : set </code><code>global</code> <code>directives out of configuration file</code>

常用配置指令:

    server {

                }  //定義一個虛拟主機

    2、listen   //定義監聽端口

                listen address[:port];

                listen port;

    3、server_name  NAME [...];         【定義伺服器主機名----隻能用在server中】        

            後可跟多個主機名:名稱還可以使用正規表達式(~)或通配符,檢查标準如下

                    (1)先做精确比對檢查;

                    (2)左側通配符比對檢查;*.1inux.com 

                    (3) 右側通配符比對檢查;如 mail.*  

                    (4) 正規表達式比對檢查:如 ~^.*\.1inux.com\.com$

                    (5) default_server;

--------------------------------------------------------

編輯配置檔案  在http {} 中添加如下:

        server {

                listen 888;

                server_name 888.1inux.com;

                root "/vhost/888/html/";

        }

重新加載

[root@1inux nginx]# /usr/local/nginx/sbin/nginx -s reload

[root@1inux nginx]# ss -tnlp | grep nginx

LISTEN     0      128                       *:888                      *:*      users:(("nginx",6568,11),("nginx",7659,11))

LISTEN     0      128                       *:80                       *:*      users:(("nginx",6568,6),("nginx",7659,6))

[root@1inux nginx]# 

添加首頁面 

 # echo "&lt;h1&gt; This is 888.1inux.com &lt;/h1&gt;" &gt; /vhost/888/html/index.html

然後通路如圖

<a href="http://s3.51cto.com/wyfs02/M01/6E/08/wKioL1VyUtiABYvYAADIcc1KRag782.jpg" target="_blank"></a>

    4、root path;

            設定資源路徑映射;用于指明請求的URL所對應的資源所在的檔案系統上的起始路徑;

                【其使用範圍:http, server, location,   if in location                 location 内的 優先級高于server】

    5、location [  = | ~ | ~* | ^~ ] uri { ... } 

                location @name { ... } 

            使用範圍: server, location 

            功能:允許根據使用者請求的URL來比對定義的各location; 比對到時,此請求将被響應的location配置塊中的配置所處理,例如做通路控制等功能

                = : 精确比對檢查;

                ^~: RUI的前半部分比對,不支援正規表達式;

                ~ : 正規表達式模式比對檢查,區分字元大小寫;

                ~*: 正規表達式子產品比對檢查,不區分字元大小寫;

    比對的優先級:   精确比對(=)   ^~     ~    ~*   不帶任何符号的location;

-------------------------

eg:

建立目錄及檔案 

[root@1inux /]# mkdir /vhost/{www,images/img} -pv

mkdir: created directory `/vhost'

mkdir: created directory `/vhost/www'

mkdir: created directory `/vhost/images'

mkdir: created directory `/vhost/images/img'

[root@1inux /]# echo "&lt;h1&gt; This is www.1inux.com &lt;/h1&gt;" &gt;/vhost/www/index.html

[root@1inux vhost]# tree /vhost

/vhost

|-- images

|   `-- img

|       |-- 1.jpg

|       |-- 2.jpg

|       `-- mylinux2.jpg

`-- www

    `-- index.html

在配置檔案中添加如下:

<code>        </code><code>server {</code>

<code>                </code><code>listen 888;</code>

<code>                </code><code>server_name www.1inux.com;</code>

<code>                </code><code>location / {</code>

<code>                        </code><code>root </code><code>"/vhost/www/"</code><code>;</code>

<code>                        </code><code>}</code>

<code>                </code><code>location /img/ {</code>

<code>                        </code><code>root </code><code>"/vhost/images/"</code><code>;</code>

<code>        </code><code>}</code>

<code>[root@1inux nginx]# /usr/local/nginx/sbin/nginx -s reload</code>

<code>[root@1inux nginx]# ss -tnlp | grep </code><code>"nginx"</code>

<code>LISTEN     0      128                       *:888                      *:*      users:((</code><code>"nginx"</code><code>,18499,11),(</code><code>"nginx"</code><code>,18803,11))</code>

<code>LISTEN     0      128                       *:80                       *:*      users:((</code><code>"nginx"</code><code>,18499,6),(</code><code>"nginx"</code><code>,18803,6))</code>

注意 此時 使用root  定義  通路 http://www.1inux.com:888/img/1.jpg  實際通路的Web伺服器路徑是:/vhost/images/目錄下的/img/1.jpg

--------------------------------

    6、alias path;

        用于location配置段,定義路徑别名

                location /img/ {

                        root "/vhost/images/";

                        }

        //http://www.1inux.com:888/img/2.jpg        ====》  /vhost/images/img/2.jpg 

        //即  通路路徑中的/img/對應的是   Web本地/vhost/images/目錄下的目錄

                location /pic/ {

                        alias "/vhost/picture/";

                //http://www.1inux.com:888/pic/2.jpg        ====》  /vhost/picture/2.jpg

                //即  通路路徑中的/pic/目錄對應的是   Web本地/vhost/picture/目錄

    7、index file;

                預設首頁面;

                    index index.php index.html;

本文轉自 1inux 51CTO部落格,原文連結:http://blog.51cto.com/1inux/1659050

繼續閱讀