首頁  >  文章  >  運維  >  一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

青灯夜游
青灯夜游轉載
2022-02-17 11:14:428844瀏覽

Debian系統上如何編譯安裝Nginx?以下這篇文章帶大家詳解下Debian系統上編譯安裝Nginx的方法,希望對大家有幫助!

一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

Nginx

Nginx是一款輕量級的HTTP 伺服器,時常用於服務端的反向代理與負載平衡。

手動編譯安裝Nginx比較複雜,但平常一般使用最多。原因:

  • 便於管理 編譯安裝的Nginx,其安裝位址可控,如果需要卸載,執行反編譯即可。
  • 模組可控制 Nginx有其豐富的模組庫,如:ngx-fancyindex。使用Docker或軟體包管理器安裝的Nginx,模組有時不方便載入。

下次給大家分享,怎麼安裝模組~~~

#環境準備

本次安裝Nginx,是在Debian發行版本的Linux上安裝,如果是CentOS發行版本Linux,需要注意:

  • #編譯安裝時,需要自行安裝:gccpcrezlib以及openssl

另外,如果你覺得本文的安裝方法過於技術型。其實,也可以試試寶塔面板的一鍵操作。

本次教學使用一台Debian10 x64伺服器:

一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

#安裝gcc編譯器

一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

  • #首先,我們需要安裝gcc編譯器用於make
  • 編譯,Debian可以透過安裝
  • build-essential來安裝GCC編譯器:
    apt install -y build-essential

一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

#安裝正規庫

正規庫很關鍵,我們使用Nginx,在設定檔內一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)location

進行目錄匹配,就需要正規庫。 Debian安裝正規庫,可以:

apt install -y libpcre3 libpcre3-dev

#安裝zlib函式庫一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

##當然,Nginx編譯過程和Http對應過程還需要1一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)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

1一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

[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

1一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

具体使用

如果你是按我的方法编译,那么,需要注意。

  • /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

编辑配置文件:

1一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

指向目录/www

1一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

cd /
mkdir /www
cd www
vim index.html

1一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)

重载Nginx配置:

systemctl reload nginx

浏览器访问成功:

1一文教你怎麼在Debian上編譯安裝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服务器神器呢~~~

推荐教程:nginx教程

以上是一文教你怎麼在Debian上編譯安裝Nginx(步驟詳解)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:juejin.cn。如有侵權,請聯絡admin@php.cn刪除