Ubuntu 源碼安裝nginx

查看當前系統環境

lsb_release -a
當前系統環境

更新apt源

apt update

1. 安裝gccg++開發類庫

apt install build-essential
apt install libtool

2. 安裝pcre

PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。
nginxhttp 模塊使用 pcre 來解析正則表達式,所以需要在linux 上安裝pcre

apt install libpcre3 libpcre3-dev

3. 安裝zlib

zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用zlibhttp 包的內容進行 gzip

apt install zlib1g-dev

4. 安裝oepnssl

OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,并提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要安裝 OpenSSL

apt install openssl libssl-dev

5. 安裝nginx

  • 切換目錄
cd /usr/local
  • 下載nginx
    我選擇的版本是nginx-1.19.0, 可以自行修改對應版本
wget http://nginx.org/download/nginx-1.19.0.tar.gz
  • 解壓文件
tar -zxvf nginx-1.19.0.tar.gz 
  • 切換目錄
cd nginx-1.19.0
  • 預編譯
./configure --prefix=/usr/local/nginx  --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
  • 安裝
make && make install
  • 創建啟動軟連接
ln -s /usr/local/nginx/sbin/nginx /bin/nginx

6. 部署成系統服務

  • 編輯文件
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
  • 重新加載配置文件
systemctl daemon-reload
  • 開機啟動服務
systemctl enable nginx
  • 啟動服務
systemctl restart nginx

7. 卸載nginx相關服務

用于安裝更新版本的nginx,記得提前備份文件

  • 卸載刪除除了配置文件以外的所有相關文件。
apt remove nginx nginx-common
  • 卸載刪除所有相關文件。
apt purge nginx nginx-common
apt remove nginx-full nginx-common
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。