Nginx安裝及啟動全過程
一 Nginx yum安裝
1.1 前置準備
1 [root@nginx01 ~]# systemctl status firewalld.service #檢查防火墻 2 [root@nginx01 ~]# getenforce #檢查SELinux 3 Disabled
提示:建議關閉防火墻,或通過如下方式放通相關80或443端口:
1 firewall-cmd --permanent --add-port=80/tcp 2 firewall-cmd --permanent --add-port=443/tcp
1.2 配置yum源
1 [root@nginx01 ~]# cat > /etc/yum.repos.d/nginx.repo <<EOF 2 [nginx-stable] 3 name=nginx stable repo 4 baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/ 5 gpgcheck=1 6 enabled=1 7 gpgkey=https://nginx.org/keys/nginx_signing.key 8 module_hotfixes=true 9 10 [nginx-mainline] 11 name=nginx mainline repo 12 baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/ 13 gpgcheck=1 14 enabled=0 15 gpgkey=https://nginx.org/keys/nginx_signing.key 16 module_hotfixes=true 17 EOF
1.3 安裝Nginx
1 [root@nginx01 ~]# yum -y install nginx 2 [root@nginx01 ~]# nginx -v 3 nginx version: nginx/1.18.0
提示:如上安裝默認安裝為當前最新穩(wěn)定版,若需要安裝開發(fā)版,可執(zhí)行yum-config-manager --enable nginx-mainline,然后yum安裝,不建議安裝開發(fā)版。
1 [root@nginx01 ~]# systemctl start nginx 2 [root@nginx01 ~]# systemctl enable nginx #啟動服務
1.4 測試訪問
瀏覽器訪問:http://172.24.8.71/

1.5 其他信息
1 [root@nginx01 ~]# nginx -V #查看yum安裝所編譯的模塊及參數 2 nginx version: nginx/1.18.0 3 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 4 built with OpenSSL 1.0.2k-fips 26 Jan 2017 5 TLS SNI support enabled 6 configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' 7 [root@nginx01 ~]# rpm -ql nginx #查看所安裝的文件 8 [root@nginx01 ~]# rpm -qc nginx #查看相關的配置文件
二 Nginx源碼編譯安裝
2.1 依賴組件
1 [root@nginx01 ~]# yum -y install gcc gcc-c++ wget autoconf pcre pcre-devel openssl openssl-devel openssh-clients net-tools vim ntp screen lrzsz bash-completion bash-completion-extras lvm2 make automake epel-release tree zlib zlib-devel libtool
提示:部分依賴包為比如,如:
- zlib庫:zlib庫是ngx_http_gzip_module(gzip壓縮模塊)所必需的
- openssl庫 :--with-http_ssl_module使用該模塊必需裝openssl庫,來實現http支持https協議。
2.2 編譯安裝
1 [root@nginx01 ~]# useradd -s /sbin/nologin -M nginx #提前創(chuàng)建用戶及用戶組 2 [root@nginx01 ~]# wget http://nginx.org/download/nginx-1.17.8.tar.gz 3 [root@nginx01 ~]# tar -xvf nginx-1.17.8.tar.gz 4 [root@nginx01 ~]# cd nginx-1.17.8/ 5 [root@nginx01 nginx-1.17.8]# ./configure \ 6 --conf-path=/usr/local/nginx/conf/nginx.conf \ 7 --error-log-path=/var/log/nginx/error.log \ 8 --group=nginx \ 9 --http-client-body-temp-path=/var/cache/nginx/client_temp \ 10 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ 11 --http-log-path=/var/log/nginx/access.log \ 12 --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ 13 --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ 14 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ 15 --lock-path=/var/run/nginx.lock \ 16 --pid-path=/var/run/nginx.pid \ 17 --prefix=/usr/local/nginx \ 18 --sbin-path=/usr/local/bin/nginx \ 19 --user=nginx \ 20 --with-http_gzip_static_module \ 21 --with-http_realip_module \ 22 --with-http_ssl_module \ 23 --with-http_stub_status_module \ 24 --with-http_sub_module \ 25 --with-http_v2_module \ 26 --with-stream \ 27 --with-stream_realip_module \ 28 --with-stream_ssl_module 29 [root@nginx01 nginx-1.17.8]# make && make install 30 [root@nginx01 ~]# nginx -V #查看安裝版本 31 [root@nginx01 ~]# tree /usr/local/nginx/ #查看目錄結構

目錄 | 作用 |
|---|---|
conf | 用于存儲nginx配置文件 |
html | 用于存放靜態(tài)網頁 |
logs | 存放日志 |
sbin | 用于存放 nginx 執(zhí)行命令 |
2.3 服務管理
1 [root@nginx01 ~]# echo $PATH 2 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin 3 [root@nginx01 ~]# mkdir -p /var/cache/nginx/ 4 [root@nginx01 ~]# ll /usr/local/bin/ 5 total 7.5M 6 -rwxr-xr-x 1 root root 7.5M Mar 5 01:09 nginx
提示:若nginx的prefix未編譯系統PATH中,如/opt/nginx/,需要在PATH中,可通過如下方式添加:
echo 'PATH=/opt/nginx/:\$PATH' > /etc/profile.d/nginx.sh
1 [root@nginx01 ~]# nginx #服務啟動 2 [root@nginx01 ~]# nginx -s stop #服務關閉 3 [root@nginx01 ~]# nginx -s reload #重載配置文件 4 [root@nginx01 ~]# nginx -s reopen #重啟Nginx 5 [root@nginx01 ~]# nginx -s quit #關閉Nginx 6 [root@nginx01 ~]# nginx -t #測試配置文件 7 [root@nginx01 ~]# nginx -t -c 【file】 #使用額外的配置文件測試 8 [root@nginx01 ~]# ps aux | grep nginx #查看進程 9 [root@nginx01 ~]# netstat -ano | grep 80 #查看端口
2.4 開機啟動
1 [root@nginx01 ~]# vi /usr/lib/systemd/system/nginx.service 2 [Unit] 3 Description=nginx - high performance web server 4 Documentation=http://nginx.org/en/docs/ 5 After=network-online.target remote-fs.target nss-lookup.target 6 Wants=network-online.target 7 8 [Service] 9 Type=forking 10 PIDFile=/var/run/nginx.pid 11 ExecStart=/usr/local/bin/nginx -c /usr/local/nginx/conf/nginx.conf 12 ExecReload=/bin/kill -s HUP $MAINPID 13 ExecStop=/bin/kill -s TERM $MAINPID 14 15 [Install] 16 WantedBy=multi-user.target 17 [root@nginx01 ~]# systemctl daemon-reload 18 [root@nginx01 ~]# systemctl start nginx.service #啟動服務 19 [root@nginx01 ~]# systemctl enable nginx.service #開機啟動
說明:
- Description:描述服務
- After:描述服務類別
- [Service]:服務運行參數的設置
- Type=forking:是后臺運行的形式
- ExecStart:為服務的具體運行命令
- ExecReload:為重啟命令
- ExecStop:為停止命令
- PrivateTmp=True:表示給服務分配獨立的臨時空間
注意:[Service]的啟動、重啟、停止命令全部要求使用絕對路徑
[Install]運行級別下服務安裝的相關設置,可設置為多用戶,即系統運行級別為3
- systemctl start nginx.service?。▎觧ginx服務)
- systemctl stop nginx.service?。ㄍV筺ginx服務)
- systemctl enable nginx.service (設置開機自啟動)
- systemctl disable nginx.service (停止開機自啟動)
- systemctl status nginx.service (查看服務當前狀態(tài))
- systemctl restart nginx.service?。ㄖ匦聠臃眨?/li>
- systemctl list-units --type=service (查看所有已啟動的服務)
2.5 測試訪問
瀏覽器訪問:http://172.24.8.71/
2.6 編譯選項
1 [root@nginx01 nginx-1.17.8]# ./configure --help #查看編譯選項
如下為常見編譯選項及其釋義:
編譯選項 | 作用 |
|---|---|
--prefix=/etc/nginx | 程序安裝目錄和路徑 |
--sbin-path=/usr/sbin/nginx | Nginx啟動停止名 |
--modules-path=/usr/lib64/nginx/modules | Nginx模塊路徑 |
--conf-path=/etc/nginx/nginx.conf | Nginx主配置文件路徑 |
--error-log-path=/var/log/nginx/error.log | Nginx錯誤日志路徑 |
--http-log-path=/var/log/nginx/access.log | Nginx訪問日志路徑 |
--pid-path=/var/run/nginx.pid | Nginx Pid路徑 |
--lock-path=/var/run/nginx.lock | Nginx鎖路徑 |
--http-client-body-temp-path=/var/cache/nginx/client_temp | client頭部臨時緩存文件 |
--http-proxy-temp-path=/var/cache/nginx/proxy_temp | proxy臨時緩存文件 |
--http-fastcgi-temp-path=/var/cache/nginx/proxy_temp | fastcgi臨時緩存文件 |
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp | uwsgi臨時緩存文件 |
--http-scgi-temp-path=/var/cache/nginx/scgi_temp | scgi臨時緩存文件 |
--user=nginx | 設置Nginx進程啟動用戶 |
--group=nginx | 設置Nginx進程啟動用戶組 |
--with-cc-opt | 設置額外的參數將被添加到CFLACS變量 |
--with-id-opt | 設置額外的參數,鏈接系統庫 |
三 Nginx目錄及模塊
3.1 相關目錄
如下以Nginx yum安裝后的目錄為例:
路徑 | 類型 | 作用 |
|---|---|---|
/etc/nginx /etc/nginx/nginx.conf /etc/nginx/conf.d /etc/nginx/conf.d/default.conf | 配置文件 | Nginx主配置文件 |
/etc/nginx/fastcgi_params /etc/nginx/scgi_params /etc/nginx/uwsgi_params | 配置文件 | Cgi、Fastcgi、Uwsgi配置文件 |
/etc/nginx/win-utf /etc/nginx/koi-utf /etc/nginx/koi-win | 配置文件 | Nginx編碼轉換映射文件 |
/etc/nginx/mime.types | 配置文件 | http協議的Content-Type |
/etc/rc.d/init.d/nginx /etc/rc.d/init.d/nginx-debug /etc/sysconfig/nginx /etc/sysconfig/nginx-debug | 配置文件 | 配置系統守護進程管理器 |
/etc/logrotate.d/nginx | 配置文件 | Nginx日志輪詢、日志切割 |
/usr/sbin/nginx /usr/sbin/nginx-debug | 命令 | Nginx終端管理器命令 |
/usr/share/doc/nginx-1.xx.x /usr/share/man/man8/nginx.8.gz | 目錄 | Nginx的幫助手冊 |
/var/cache/nginx | 目錄 | Nginx的緩存目錄 |
/var/log/nginx | 目錄 | Nginx的日志目錄 |
/etc/nginx/modules /etc/lib64/nginx /etc/lib64/nginx/modules | 目錄 | Nginx的模塊目錄 |
/usr/share/nginx /usr/share/nginx/html /usr/share/nginx/html/50x.html /usr/share/nginx/html/index.html | 目錄 | Nginx默認站點目錄 |
3.2 Nginx模塊
Nginx模塊分為Nginx官方模塊和Nginx第三方模塊。
Nginx編譯選項 | 模塊作用 |
|---|---|
ngx_http_core_module | 包含一些核心的http參數配置,對應Nginx的配置區(qū)塊部分。 |
ngx_http_access_module | 訪問控制模塊,用來控制網站用戶對Nginx的訪問。 |
ngx_http_gzip_module | 壓縮模塊,對Nginx返回的數據壓縮,屬于性能優(yōu)化模塊。 |
ngx_http_fastcgi_module | fastcgi模塊,和動態(tài)應用相關的模塊,例如PHP。 |
ngx_http_proxy_module | proxy代理模塊。 |
ngx_http_upstream_module | 負載均衡模塊,實現網站的負載均衡功能機健康檢查。 |
ngx_http_rewrite_module | URL地址重寫模塊。 |
ngx_http_limit_conn_module | 限制用戶并發(fā)連接數及請求連接數。 |
ngx_http_limit_req_module | 限制Nginx request processing rate根據定義的key。 |
ngx_http_log_module | 訪問日志模塊,以指定的格式記錄Nginx客戶訪問日志等信息。 |
ngx_http_auth_basic_module | Web認證模塊,設置Web用戶通過賬號密碼訪問Nginx。 |
ngx_http_ssl_module | ssl模塊,用于加密的http連接,如https。 |
四 Nginx變量及狀態(tài)碼
4.1 Nginx變量
ngx_http_core_module的內置變量通常有:http請求變量、Nginx內置變量、自定義變量。
- $uri:當前請求的URI,不帶參數;
- $request_uri:請求的URI,帶完整參數;
- $host:http請求報文中的host首部,如果沒有則以處理此請求的虛擬主機的主機名代替;
- $hostname:Nginx服務運行所在主機的主機名;
- $remote_addr:客戶端IP;
- $remote_prot:客戶端端口;
- $remote_user:使用用戶認證時客戶端用戶輸入的用戶名;
- $request_filename:用戶請求中的URI經過本地root或alias轉換后映射的本地文件路徑;
- $request_method:請求方法,GET、POST、PUT;
- $server_addr:服務器地址;
- $server_name:服務器名稱;
- $server_port:服務器端口;
- $server_protocol:服務器向客戶端發(fā)送響應時的協議,如http/1.1、http/1.0;
- $scheme:在請求中使用scheme。如http://xxx.com中的http;
- $http_HEADER:匹配請求報文中指定的HEADER;
- $http_host:匹配請求報文中的host首部。
4.2 http狀態(tài)碼
http狀態(tài)碼是用以表示網頁服務器HTTP響應狀態(tài)的3位數字代碼。
可通過查看HTTP狀態(tài)碼來判斷服務器狀態(tài),常見的有404、502等。
- 301:永久移動,被請求的資源已被永久移動位置;
- 302:請求的資源限制臨時從不同的URI響應請求;
- 305:使用代理,被請求的資源必須通過指定的代理才能訪問;
- 307:臨時跳轉,被請求的資源在臨時從不同的URL響應請求;
- 400:錯誤請求;
- 402:需要付款,預留狀態(tài)碼,用于將來一些數字貨幣或者微支付;
- 403:禁止訪問,服務器已理解請求,但拒絕執(zhí)行它;
- 404:找不到對象,請求失敗,資源不存在;
- 406:不可接受的,請求的資源內容特性無法滿足請求頭部中的條件,因而無法生成響應實體;
- 408:請求超時;
- 409:沖突,由于和被請求的資源的當前狀態(tài)之間存在沖突,請求無法完成;
- 410:遺失的,被請求的資源在服務器上已經不再可用,而且沒有任何已知的轉發(fā)地址;
- 413:響應實體太大,服務器拒絕處理當前請求,請求超過服務器所能處理和允許的最大值;
- 417:期望失敗。在請求頭 Expect 中指定的預期內容無法被服務器滿足;
- 418:我是一個茶壺。超文本咖啡罐控制協議,但是并沒有被實際的HTTP服務器實現;
- 420:方法失效;
- 422:不可處理的實體。請求格式正確,但是由于含有語義錯誤,無法響應;
- 500:服務器內部錯誤。服務器遇到了一個未曾預料的狀況,導致了它無法完成對請求的處理;
- 502:請求后端失?。?/li>
- 504:請求成功,但是響應超時。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Nginx安裝后/etc/nginx/conf.d下沒有default.conf的解決
nginx.conf是nginx默認加載的配置文件 通過nginx -V可以看nginx默認配置文件路徑,本文主要介紹了Nginx安裝后/etc/nginx/conf.d下沒有default.conf的解決,感興趣的可以了解一下2023-11-11
詳解Nginx靜態(tài)服務配置(root和alias指令)
這篇文章主要介紹了詳解Nginx靜態(tài)服務配置(root和alias指令),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01

