nginx系列2----從源碼安裝nginx和echo-nginx-module模塊

資源1: 官網: http://nginx.org
資源2: 官方學習資源,     wiki,     nginx安裝之wiki介紹
資源3: 編譯選項列表
資源4: nginx源碼下載列表,當前Stable版本是nginx-1.14.0,
資源5: 官方新手入門
資源6: 內置變量大全(重點掌握),    內置指令大全(重點掌握),    重定向(重點掌握)    核心功能(重點掌握)php

安裝資源: nginx源碼地址(版本1.11.2):http://nginx.org/download/ngi...
安裝資源: echo模塊安裝地址(版本):https://github.com/openresty/...
安裝參考: echo模塊安裝方法html

安裝時間:2018-09-12nginx

一:從源碼安裝nginx和echo-nginx-module模塊(推薦)

01: 準備nginx和echo-nginx-module模塊源碼

nginx版本爲1.11.2,echo-nginx-module版本爲0.61
vagrant@qianjin:~$ wget http://nginx.org/download/nginx-1.11.2.tar.gz
vagrant@qianjin:~$ wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
vagrant@qianjin:~$ ls //查看一下,壓縮包在當前目錄下
code  Dockerfile  nginx-1.11.2.tar.gz  v0.61.tar.gz
vagrant@qianjin:~$ tar -zxvf nginx-1.11.2.tar.gz
vagrant@qianjin:~$ tar -zxvf v0.61.tar.gz

02: 安裝

vagrant@qianjin:~$ cd  nginx-1.11.2 //到解壓後的nginx目錄中
vagrant@qianjin:~/nginx-1.11.2$ ./configure --prefix=/opt/nginx \
      --add-module=/home/vagrant/echo-nginx-module-0.61
// –prefix爲nginx安裝位置,–add-module爲須要添加的模塊路徑,這裏添加解壓後echo-nginx-module路徑
// 執行配置後,目錄下多了 Makefile文件和 objs目錄
vagrant@qianjin:~/nginx-1.11.2$ make -j2

//報錯:
src/core/ngx_murmurhash.c: In function ‘ngx_murmur_hash2’:
src/core/ngx_murmurhash.c:37:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
         h ^= data[2] << 16;
         ~~^~~~~~~~~~~~~~~~
src/core/ngx_murmurhash.c:38:5: note: here
     case 2:
     ^~~~
src/core/ngx_murmurhash.c:39:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
         h ^= data[1] << 8;
         ~~^~~~~~~~~~~~~~~
src/core/ngx_murmurhash.c:40:5: note: here
     case 1:
     ^~~~
緣由,是將警告當成了錯誤處理,打開/home/vagrant/nginx-1.11.2/objs/Makefile,
CFLAGS =  -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g (去年-Werror)
再從新make -j2
-Wall 表示打開gcc的全部警告
-Werror,它要求gcc將全部的警告當成錯誤進行處理

vagrant@qianjin:~/nginx-1.11.2$ make -j2
vagrant@qianjin:~/nginx-1.11.2$ make install

03: 測試

vagrant@qianjin:~$ sudo /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
// -c 指定配置文件的路徑sudo /opt/nginx/sbin/nginx -h瞭解更多用法
在瀏覽器中輸入 http://192.168.10.10/,若是出現Welcome to nginx!頁面表示安裝成功

04: 配置

創建連接

sudo ln -s /opt/nginx/sbin/nginx /usr/bin/nginx 
// 創建軟連接,/usr/local/bin包含於$PATH當中,不須要額外的設置環境變量了,能夠在任何目錄運行nginx命令
// 如今能夠sudo nginx啓動,用sudo nginx -s stop等

開機啓動

編譯安裝須要本身進行設置方可自動啓動git

# 設置nginx自啓動,在/lib/systemd/system/ 目錄下建立一個服務文件
vim /lib/systemd/system/nginx.service
內容以下:

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target
文件說明
[Unit]部分
Description:描述服務
After:依賴,當依賴的服務啓動以後再啓動自定義的服務

[Service]部分
Type=forking是後臺運行的形式
ExecStart爲服務的具體運行命令(須要根據路徑適配)
ExecReload爲重啓命令(須要根據路徑適配)
ExecStop爲中止命令(須要根據路徑適配)
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:啓動、重啓、中止命令所有要求使用絕對路徑

[Install]部分
服務安裝的相關設置,可設置爲多用戶

# 設置了自啓動後,任意目錄下執行
systemctl enable nginx.service
# 啓動nginx服務
systemctl start nginx.service
# 設置開機自動啓動
systemctl enable nginx.service
# 中止開機自動啓動
systemctl disable nginx.service
# 查看狀態
systemctl status nginx.service
# 重啓服務
systemctl restart nginx.service
# 查看全部服務
systemctl list-units --type=service
// 我仍是比較喜歡用下面的方式
vagrant@qianjin:~$ sudo service nginx stop
vagrant@qianjin:~$ sudo service nginx star
// 實際下面敲的代碼少
vagrant@qianjin:~$ sudo nginx #啓動
vagrant@qianjin:~$ sudo nginx -s stop 中止

最簡單的nginx配置文件(只支持靜態html)

events{
}
http {
    server {
        server_name  symfony.cc;
        location / {
            root   /var/www/study/symfony;
            index  index.html index.htm;
       }
    }
}

最簡單的nginx配置文件(支持靜態html和php)

前提:在hosts中定義了 168.192.10.10 symfony.cc
events{
}
http {
    server {
        server_name  symfony.cc;
        root /var/www/study/symfony;
        location / {
            index index.php index.html index.htm;
       }
       location ~ \.php$ {
             # 在/etc/php/7.0/fpm/pool.d/www.conf的listen值與fastcgi_pass的值要相對應
             # ;listen = /run/php/php7.0-fpm.sock
             # listen = 127.0.0.1:9000       
            fastcgi_pass 127.0.0.1:9000;     
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
       }
     }
}
//適合用來檢查nginx是否正常,當配置出問題是,就用此配置,再在這個基礎上修改,必定要明白的是,nginx只是個web服務器,碰到php文件,它也不解析,它交給php-fpm來處理,php-fpm處理完的數據再交給nginx

瞭解工做進程worker_process

配置位置在:在最外面
語法: worker_processes 4;
值:默認爲1,通常設爲cpu數量x2
說明:當設爲4時,1個主進程master process,4個工做進程worker process,master process的pid會動態記錄在/opt/nginx/logs/nginx.pid文件中,其他4個工做進程的pid值是遞加1,如主進程的pid爲3082,那麼別外幾個工做進程的pid分別爲3083,3084,3085,3086,主進程負責監控端口,協調工做進程的工做狀態,分配工做任務;工做進程負責進行任務處理github

vagrant@qianjin:~$ ps -ef|grep nginx
root      3082     1  0 03:56 ?        00:00:00 nginx: master process nginx -c /opt/nginx/conf/2018091201.conf
nobody    3083  3082  0 03:56 ?        00:00:00 nginx: worker process
nobody    3084  3082  0 03:56 ?        00:00:00 nginx: worker process
nobody    3085  3082  0 03:56 ?        00:00:00 nginx: worker process
nobody    3086  3082  0 03:56 ?        00:00:00 nginx: worker process
vagrant   3089  2311  0 03:57 pts/0    00:00:00 grep --color=auto nginx

二:測試echo模塊(重點)

做用範圍:location塊和location if說句中
例子web

events{
} 
http {
    server {
        server_name  localhost;
        root /var/www/study/symfony;
        location / {
            index index.php index.html index.htm;
       }
       location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            echo "hello,word";
            echo Changsha; 
            echo $document_root;
            echo_flush;
       }
     }
}

瀏覽器中輸入http://192.168.10.10/a.php, 網頁中原來的內容不顯示了,只ubuntu

hello,word
Changsha
/var/www/study/symfony

使用單獨的location來放echo語句,不影響網頁vim

events{
}
http {
    server {
        server_name  localhost;
        root /var/www/study/symfony;
        location / {
            index index.php index.html index.htm;
        }
        location /wang {
            echo "hello,word";
            echo -n Changsha; # -n表示這個不換行,它緊接在echo後面,其它地方忽略
            echo $document_root;
            echo_flush;
        }    
        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;      
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

輸入 http://192.168.10.10/wang就會...瀏覽器

hello,word
Changsha/var/www/study/symfony

例3:服務器

location /chen {
            echo_duplicate 1 $echo_client_request_headers;
            echo "\r";
            echo_read_request_body;
            echo $request_body;
        }

輸出

GET /chen HTTP/1.1
Host: 192.168.10.10
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9

備註1: tar參數說明(熟悉的略過)

-c: 創建壓縮檔案
-x:解壓(解壓時要用到這個)
-t:查看內容
-r:向壓縮歸檔文件末尾追加文件
-u:更新原壓縮包中的文件
這5個是獨立的命令,壓縮解壓都要用到其中一個,能夠和別的命令連用但只能用其中一個。
下面的參數是根據須要在壓縮或解壓檔案時可選的。
-z:有gzip屬性的
-j:有bz2屬性的
-Z:有compress屬性的
-v:顯示全部過程
-O:將文件解開到標準輸出
下面的參數-f是必須的
-f: 使用檔案名字,切記,這個參數是最後一個參數,後面只能接檔案名。

備註2:瞭解configure過程

checking for OS
 + Linux 4.15.0-32-generic x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 7.3.0 (Ubuntu 7.3.0-16ubuntu3) 
checking for gcc -pipe switch ... found
省略
checking for getaddrinfo() ... found
configuring additional modules
adding module in /home/vagrant/echo-nginx-module-0.61 //這裏添加了 echo模塊
 + ngx_http_echo_module was configured
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile //生成文件Makefile

Configuration summary
  + using system PCRE library 使用系統PCRE庫
  + OpenSSL library is not used 沒有用到OpenSSL庫
  + using system zlib library 使用系統zlib庫
  // 這些路徑是要了解的
  nginx path prefix: "/opt/nginx"
  nginx binary file: "/opt/nginx/sbin/nginx"
  nginx modules path: "/opt/nginx/modules"
  nginx configuration prefix: "/opt/nginx/conf"
  nginx configuration file: "/opt/nginx/conf/nginx.conf"  這是配置文件
  nginx pid file: "/opt/nginx/logs/nginx.pid"
  nginx error log file: "/opt/nginx/logs/error.log"
  nginx http access log file: "/opt/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
相關文章
相關標籤/搜索