linux高階-Nginx服務(二)-安裝與配置

Nginx安裝

centos安裝nginx

//配置epel源
yum -y install epel-release

//下載安裝nginx
yum -y install nginx

//查看版本
nginx -v

//查看版本以及配置選項
nginx -V

ubuntu安裝nginx

//配置apt源
vim /etc/apt/sources.list

//源文件內容爲
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

//更新apt源
apt updata

//查看nginx版本列表
apt-cache madison nginx

//下載安裝nginx
apt -y install nginx

//查看版本
nginx -v

//查看版本以及配置選項
nginx -V

查看幫助

//查看幫助
nginx -h

nginx version: nginx/1.16.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit #顯⽰版本和編譯參數
-t : test configuration and exit #測試配置⽂件是否異常
-T : test configuration, dump it and exit #測試並打印
-q : suppress non-error messages during configuration testing #靜默模式
-s signal : send signal to a master process: stop, quit, reopen, reload #發送信號
-p prefix : set prefix path (default: /usr/share/nginx/) #指定Nginx ⽬錄
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #配置⽂件路徑
-g directives : set global directives out of configuration file #設置全局指令

目錄結構

//查看配置文件
rpm -ql nginx

//主要配置文件
--------------------------------------------
/etc/nginx/fastcgi.conf   # fastcgi配置文件
/etc/nginx/nginx.conf   # nginx主配置文件
/usr/sbin/nginx      # nginx主程序
/usr/share/nginx/html/    # 網頁主目錄
/var/log/nginx  #nginx日誌目錄
/etc/nginx/conf.d/*.conf #子配置文件
/etc/nginx/uwsgi_params #uwsgi協議配置文件
/etc/nginx/scgi_params #scgi協議配置文件
/etc/nginx/mime.types #支持的mime類型
------------------------------

編譯安裝

配置基礎編譯環境

yum -y install vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate \
gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel \
systemd-devel nettools iotop bc zip unzip zlib-devel \
bash-completion nfs-utils automake libxml2 libxml2-devel \
libxslt libxslt-devel perl perl-ExtUtils-Embed
  • gcc爲GNU Compiler Collection的縮寫,可以編譯C和C++源代碼等,它是GNU開發的C和C++以及其他很多種語⾔的編譯器(最早的時候只能編譯C,後來很快進化成⼀個編譯多種語⾔的集合,如Fortran、Pascal、Objective-C、Java、Ada、 Go等。)
  • gcc 在編譯C++源代碼的階段,只能編譯 C++ 源⽂件,⽽不能⾃動和 C++ 程序使⽤的庫鏈接(編譯過程分爲編譯、鏈接兩個階段,注意不要和可執⾏⽂件這個概念搞混,相對可執⾏⽂件來說有三個重要的概念:編譯 (compile)、鏈接(link)、加載(load)。源程序⽂件被編譯成⽬標⽂件,多個⽬標⽂件連同庫被鏈接成⼀個最終的可執⾏⽂件,可執⾏⽂件被加載到內存中運⾏)。因此,通常使⽤ g++ 命令來完成 C++ 程序的編譯和連接,該程序會⾃動調⽤ gcc 實現編譯。
  • gcc-c++也能編譯C源代碼,只不過把會把它當成C++源代碼,後綴爲.c的,gcc把它當作是C程序,⽽g++當作是c++程序;後綴爲.cpp的,兩者都會認爲是c++程序,注意,雖然c++是c的超集,但是兩者對語法的要求是有區別的。
  • automake是⼀個從Makefile.am⽂件⾃動⽣成Makefile.in的⼯具。爲了⽣成Makefile.in,automake還需⽤到perl,由於automake創建的發佈完全遵循GNU標準,所以在創建中不需要perl。libtool是⼀款⽅便⽣成各種程序庫的⼯具。
  • pcre pcre-devel:在Nginx編譯需要 PCRE(Perl Compatible Regular Expression),因爲Nginx 的Rewrite模塊和HTTP 核⼼模塊會使⽤到PCRE正則表達式語法。
  • zlip zlib-devel:nginx啓⽤壓縮功能的時候,需要此模塊的⽀持。
  • openssl openssl-devel:開啓SSL的時候需要此模塊的⽀持。
//進入指定目錄
cd /usr/local/src/

//下載源碼包
wget https://nginx.org/download/nginx-1.16.1.tar.gz

//解包
tar xf nginx-1.16.1.tar.gz

//進入目錄,準備編譯
cd nginx-1.16.1/

#編譯是爲了檢查系統環境是否符合編譯安裝的要求。
#⽐如是否有gcc編譯⼯具,是否⽀持編譯參數當中的模塊。
#並根據開啓的參數等⽣成Makefile⽂件爲下⼀步做準備:

//開始編譯
------------------------------
./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
------------------------------------

//根據Makefile文件生成相應的模塊
make

//創建目錄,並將生成的模塊和文件複製到相應的目錄
make install

//創建服務所需系統用戶
useradd nginx -s /sbin/nologin -u 2000

//授權創建的用戶訪問服務目錄
chown -R nginx.nginx /apps/nginx 

//自定義啓動腳本
vim /usr/lib/systemd/system/nginx.service

-------------------------------------------
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid   #注意這裏的路徑,必須和配置文件裏的PID路徑一致
ExecStartPre=/usr/bin/rm -f /var/run/nginx.pid  #這裏也是
ExecStartPre=/usr/sbin/nginx -t   #主程序的路徑
ExecStart=/usr/sbin/nginx   #主程序的路徑
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
------------------------------------------------
  • 備註:nginx完成編譯安裝後,有四個主要的目錄
    • /apps/nginx/conf:該⽬錄中保存了nginx所有的配置⽂件,其中nginx.conf是nginx服務器的最核⼼最主要的配置⽂件,其他的.conf則是⽤來配置nginx相關的功能的,例如fastcgi功能使⽤的是fastcgi.conf和fastcgi_params兩個⽂件,配置⽂件⼀般都有個樣板配置⽂件,是⽂件名.default結尾,使⽤的使⽤將其複製爲並將default去掉即可。
    • /apps/nginx/html:該⽬錄中保存了nginx服務器的web⽂件,但是可以更改爲其他⽬錄保存web⽂件,另外還有⼀個50x的web⽂件是默認的錯誤⻚⾯提⽰⻚⾯。
    • /apps/nginx/logs:該⽬錄⽤來保存nginx服務器的訪問⽇志錯誤⽇志等⽇志,logs⽬錄可以放在其他路徑,⽐如/var/logs/nginx⾥⾯。
    • /apps/nginx/sbin:該⽬錄⽤來保存nginx⼆進制啓動腳本,可以接受不同的參數以實現不同的功能。

配置Nginx

Nginx的配置⽂件的組成部分: 主配置⽂件:nginx.conf,⼦配置⽂件 include conf.d/*.conf

fastcgi, uwsgi,scgi等協議相關的配置⽂件
mime.types:⽀持的mime類型,MIME(Multipurpose Internet Mail Extensions)多⽤途互聯⽹郵件擴展類
型,MIME消息能包含⽂本、圖像、⾳頻、視頻以及其他應⽤程序專⽤的數據,是設定某種擴展名的⽂件⽤⼀種應⽤程序來
打開的⽅式類型,當該擴展名⽂件被訪問的時候,瀏覽器會⾃動使⽤指定應⽤程序來打開。多⽤於指定⼀些客⼾端⾃定義
的⽂件名,以及⼀些媒體⽂件打開⽅式。
Nginx主配置⽂件的配置指令⽅式:
directive value [value2 ...];
注意:
(1) 指令必須以分號結尾
(2) ⽀持使⽤配置變量
內建變量:由Nginx模塊引⼊,可直接引⽤
⾃定義變量:由⽤⼾使⽤set命令定義
set variable_name value;
引⽤變量:$variable_name

MIME 參考⽂檔:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_Types

main block  # 主配置段,即全局配置段,對http,mail都有效

# 事件驅動相關的配置
event {
    ...
}  

# http/https 協議相關配置段
http {
    ...
}   

# mail 協議相關配置段
mail {
    ...
}   

# stream 服務器相關配置段
stream {
    ...
}   

Nginx默認配置文件

[root@s2 ~]# grep -v "#" /apps/nginx/conf/nginx.conf | grep  -v "^$"
# 全局配置端,對全局⽣效,主要設置nginx的啓動⽤⼾/組,啓動的⼯作進程數量,⼯作模式,Nginx的PID路徑,⽇志路徑等。
user  nginx nginx;
worker_processes  1;   # 啓動⼯作進程數數量
events { # events設置快,主要影響nginx服務器與⽤⼾的⽹絡連接,⽐如是否允許同時接受多個⽹絡連接,使⽤哪種事件驅動模型處理請求,每個⼯作進程可以同時⽀持的最⼤連接數,是否開啓對多⼯作進程下的⽹絡連接進⾏序列化等。
worker_connections  1024;   # 設置單個nginx⼯作進程可以接受的最⼤併發,作爲web服務器的時候最⼤併發數爲worker_connections * worker_processes,作爲反向代理的時候爲(worker_connections *worker_processes)/2
}
http { # http塊是Nginx服務器配置中的重要部分,緩存、代理和⽇志格式定義等絕⼤多數功能和第三⽅模塊都可以在這設置,http塊可以包含多個server塊,⽽⼀個server塊中⼜可以包含多個location塊,server塊可以配置⽂件引⼊、MIME-Type定義、⽇志⾃定義、是否啓⽤sendfile、連接超時時間和單個鏈接的請求上限等。
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on; # 作爲web服務器的時候打開sendfile加快靜態⽂件傳輸,指定是否使⽤sendfile系統調⽤來傳輸⽂件,sendfile系統調⽤在兩個⽂件描述符之間直接傳遞數據(完全在內核中操作),從⽽避免了數據在內核緩衝區和⽤⼾緩衝區之間的拷⻉,操作效率很⾼,被稱之爲零拷⻉,硬盤 >> kernel buffer (快速拷⻉到kernelsocket buffer) >>協議棧。
    keepalive_timeout  65;  # ⻓連接超時時間,單位是秒
    server { # 設置⼀個虛擬機主機,可以包含⾃⼰的全局快,同時也可以包含多個location模塊。⽐如本虛擬機監聽的端⼝、本虛擬機的名稱和IP配置,多個server 可以使⽤⼀個端⼝,⽐如都使⽤80端⼝提供web服務
        listen       80;  # 配置server監聽的端⼝
        server_name  localhost;  # 本server的名稱,當訪問此名稱的時候nginx會調⽤當前serevr內部的配置進程匹配。
        location / { # location其實是server的⼀個指令,爲nginx服務器提供⽐較多⽽且靈活的指令,都是在location中提現的,主要是基於nginx接受到的請求字符串,對⽤⼾請求的UIL進⾏匹配,並對特定的指令進⾏處理,包括地址重定向、數據緩存和應答控制等功能都是在這部分實現,另外很多第三⽅模塊的配置也是在location 塊中配置。
            root   html; # 相當於默認⻚⾯的⽬錄名稱,默認是相對路徑,可以使⽤絕對路徑配置。
            index  index.html index.htm; # 默認的⻚⾯⽂件名稱
        }
        error_page   500 502 503 504  /50x.html; # 錯誤⻚⾯的⽂件名稱
        location = /50x.html { # location處理對應的不同錯誤碼的⻚⾯定義到/50x.html,這個跟對應其
server中定義的⽬錄下。
            root   html; # 定義默認⻚⾯所在的⽬錄
        }
    }

# 和郵件相關的配置
# mail {
#                 ...
#         }          mail 協議相關配置段

# tcp代理配置,1.9版本以上⽀持
# stream {
#                 ...
#         }       stream 服務器相關配置段

# 導⼊其他路徑的配置⽂件
# include /apps/nginx/conf.d/*.conf
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章