debian系統上如何編譯安裝nginx?下面本篇文章帶大家詳解下debian系統上編譯安裝nginx的方法,希望對大家有所幫助!
Nginx
Nginx是一款輕量級的 http 服務器,時常用于服務端的反向代理和負載均衡。
手動編譯安裝Nginx比較復雜,但是平時一般使用最多。原因:
- 便于管理 編譯安裝的Nginx,其安裝地址可控,如果需要卸載,執行反編譯即可。
- 模塊可控 Nginx有其豐富的模塊庫,如:ngx-fancyindex。使用docker或軟件包管理器安裝的Nginx,模塊有時不方便載入。
下次給大家分享,怎么安裝模塊~~~
環境準備
本次安裝Nginx,是在Debian發行版本的linux上安裝,如果是centos發行版本Linux,需要注意:
- 編譯安裝時,需要自行安裝:gcc、pcre、zlib以及openssl
另外,如果你覺得本文的安裝方法過于技術型。其實,也可以試試寶塔面板的一鍵操作。
本次教程使用一臺Debian10 x64服務器:
安裝gcc編譯器
首先,我們需要安裝gcc編譯器用于make編譯,Debian可以通過安裝build-essential來安裝GCC編譯器:
apt?install?-y?build-essential
安裝正則庫
正則庫很關鍵,我們使用Nginx,在配置文件內location進行目錄匹配,就需要正則庫。Debian安裝正則庫,可以:
apt?install?-y?libpcre3?libpcre3-dev
安裝zlib庫
當然,Nginx編譯過程和Http相應過程還需要gzip格式的壓縮,所以我們還需要安裝zlib庫用于對HTTP包的內容做gzip格式的壓縮,可以這樣安裝:
apt?install?-y?zlib1g-dev
安裝OpenSSL庫
最后,現在SSL協議很重要,chrome等主流瀏覽器,都開始默認相應https了,所以OpenSSL編譯環境也很重要:
apt?install?-y?openssl?libssl-dev
依賴都安裝完成,就可以下載源碼來編譯了。
下載Nginx源碼
接下來,我們下載Nginx源碼,我們進入Nginx官網:http://nginx.org/en/download.html
下載最新的stable穩定版本:
在Debian上使用wget下載:
#?下載源碼 wget?http://nginx.org/download/nginx-1.20.2.tar.gz #?解壓源碼 tar?-xf?nginx-1.20.2.tar.gz #?進入源代碼內 cd?cd?nginx-1.20.2
配置和編譯
接下來就是make環節了,編譯時候的參數可以參考官方Nginx文檔:http://nginx.org/en/docs/configure.html
我自己編譯Nginx時候,選擇的參數一般是:
./configure? --prefix=/usr/local/nginx? --user=www? --group=www? --sbin-path=/usr/local/nginx/sbin/nginx? --conf-path=/usr/local/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? --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
其中:
- –prefix:Nginx主要安裝路徑,后續Nginx子目錄依照這個變量展開
- –user:設置Nginx進程啟動時,所屬的用戶
- –group:設置Nginx進程啟動時,所屬的用戶組
如果沒有問題,會提示信息:
Configuration?summary ??+?using?threads ??+?using?system?PCRE?library ??+?using?system?OpenSSL?library ??+?using?system?zlib?library ??nginx?path?prefix:?"/usr/local/nginx" ??nginx?binary?file:?"/usr/local/nginx/sbin/nginx" ??nginx?modules?path:?"/usr/local/nginx/modules" ??nginx?configuration?prefix:?"/usr/local/nginx" ??nginx?configuration?file:?"/usr/local/nginx/nginx.conf" ??nginx?pid?file:?"/var/run/nginx.pid" ??nginx?error?log?file:?"/var/log/nginx/error.log" ??nginx?http?access?log?file:?"/var/log/nginx/access.log" ??nginx?http?client?request?body?temporary?files:?"/var/cache/nginx/client_temp" ??nginx?http?proxy?temporary?files:?"/var/cache/nginx/proxy_temp" ??nginx?http?fastcgi?temporary?files:?"/var/cache/nginx/fastcgi_temp" ??nginx?http?uwsgi?temporary?files:?"/var/cache/nginx/uwsgi_temp" ??nginx?http?scgi?temporary?files:?"/var/cache/nginx/scgi_temp"
沒有報錯信息就可以編譯了:
make
接下來就是安裝了。
安裝
首先是安裝,很簡單:
make?install
我們再創建systemctl守護,管理Nginx:
vim?/usr/lib/systemd/system/nginx.service
[Unit] Description=nginx After=network.target ?? [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx?-s?reload ExecStop=/usr/local/nginx/sbin/nginx?-s?quit PrivateTmp=true ?? [Install] WantedBy=multi-user.target
具體使用
如果你是按我的方法編譯,那么,需要注意。
- /usr/local/nginx:為Nginx編譯安裝的地址。
- /usr/local/nginx/nginx.conf:Nginx默認配置文件。
同時,我們使用systemctl對Nginx進行管理:
- systemctl start nginx:啟動Nginx服務。
- systemctl reload nginx:Nginx配置重載。
- systemctl stop nginx:停止Nginx服務。
更多systemctl操作,可以看這篇教程:《Linux系統服務神器:systemctl的配置與使用》
https://juejin.cn/post/7059029634922315812
最后,我們寫個HelloWorld。
編輯配置文件:
指向目錄/www:
cd?/ mkdir?/www cd?www vim?index.html
重載Nginx配置:
systemctl?reload?nginx
瀏覽器訪問成功:
卸載
最后,如何卸載Nginx呢?其實更簡單:
#?停止Nginx服務 systemctl?stop?nginx #?刪除Nginx服務 rm?-rf?/usr/lib/systemd/system/nginx.service #?重載配置 systemctl?daemon-reload #?刪除Nginx編譯文件 rm?-rf?nginx
這樣就卸載完成了。
END
其實呢?個人是喜歡編譯安裝Nginx。
Nginx確實是個Web服務器神器呢~~~
推薦教程:ngx-fancyindex