網站架設

Ubuntu Linux 安裝最新版 Nginx 網頁伺服器教學與範例

介紹如何在 Ubuntu Linux 中透過官方或社群套件庫,安裝最新版的 Nginx 網頁伺服器。

Nginx 官方套件庫

Nginx 官方有提供適用於 Ubuntu Linux 發行版的套件庫,而 Ubuntu Linux 各版的所對應的 code name 可以從 Ubuntu Wiki 網站上查詢,或是透過 lsb_release 指令查詢目前 Ubuntu Linux 的 code name:

# 查詢 Ubuntu Linux 版本的 code name
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:        20.04
Codename:       focal

得知自己的 Ubuntu Linux 版本所對應的 code name 之後,在 /etc/apt/sources.list.d/ 目錄之下新增一個 Nginx 套件庫的設定檔,檔案名稱可以取名為 /etc/apt/sources.list.d/nginx.list(或是其他容易識別的檔名亦可),然後依據自己的 Ubuntu Linux 版本新增 Nginx 套件庫設定:

# Nginx 套件庫設定($release 為 Ubuntu Linux 的 code name)
deb https://nginx.org/packages/ubuntu/ $release nginx
deb-src https://nginx.org/packages/ubuntu/ $release nginx

以下是一些實際的範例:

# Nginx 套件庫設定(Ubuntu Linux 21.10)
deb https://nginx.org/packages/ubuntu/ impish nginx
deb-src https://nginx.org/packages/ubuntu/ impish nginx

# Nginx 套件庫設定(Ubuntu Linux 21.04)
deb https://nginx.org/packages/ubuntu/ hirsute nginx
deb-src https://nginx.org/packages/ubuntu/ hirsute nginx

# Nginx 套件庫設定(Ubuntu Linux 20.04)
deb https://nginx.org/packages/ubuntu/ focal nginx
deb-src https://nginx.org/packages/ubuntu/ focal nginx

# Nginx 套件庫設定(Ubuntu Linux 18.04)
deb https://nginx.org/packages/ubuntu/ bionic nginx
deb-src https://nginx.org/packages/ubuntu/ bionic nginx

除了新增 /etc/apt/sources.list.d/ 目錄之下的設定檔,也可以直接將 Nginx 套件庫設定寫在 /etc/apt/sources.list 檔案中,設定的語法都相同。

設定好 Nginx 官方的套件庫之後,即可使用 apt 安裝最新版的 Nginx 伺服器:

# 更新 Ubuntu Linux 套件庫資訊
sudo apt update

# 安裝 nginx 套件
sudo apt install nginx

安裝好 nginx 套件之後,即可啟動 Nginx 伺服器:

# 啟動 nginx 伺服器
sudo systemctl start nginx

常見問題

有時候在安裝 nginx 套件時,可能會遇到類似以下這樣的 GPG 找不到公開金鑰的錯誤訊息:

W: GPG error: https://nginx.org/packages/ubuntu bionic InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY ABF5BD827BD9BF62
E: The repository 'https://nginx.org/packages/ubuntu bionic InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

若遇到這樣的錯誤,可以透過 apt-key 指令更新公開金鑰:

# 更新公開金鑰
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ABF5BD827BD9BF62

其中 ABF5BD827BD9BF62 就對應錯誤訊息中的金鑰。接著再重新安裝 Nginx 即可:

# 更新 Ubuntu Linux 套件庫資訊
sudo apt update

# 安裝 nginx 套件
sudo apt install nginx

Ubuntu PPA

除了 Nginx 官方的套件庫之外,我們也可以利用社群維護的 NGINX PPA 來安裝 Nginx 伺服器。

# 新增 Nginx PPA 套件庫
sudo add-apt-repository ppa:nginx/stable

# 更新 Ubuntu Linux 套件庫資訊
sudo apt update

# 安裝 nginx 套件
sudo apt install nginx

安裝好 nginx 套件之後,即可啟動 Nginx 伺服器:

# 啟動 nginx 伺服器
sudo systemctl start nginx

常見問題

若遇到 add-apt-repository 指令不存在,可以安裝 software-properties-common 套件:

# 安裝 software-properties-common 套件
sudo apt install software-properties-common

參考資料

Share
Published by
Office Guide

Recent Posts

Python 使用 PyAutoGUI 自動操作滑鼠與鍵盤

本篇介紹如何在 Python ...

9 個月 ago

Ubuntu Linux 以 WireGuard 架設 VPN 伺服器教學與範例

本篇介紹如何在 Ubuntu ...

9 個月 ago

Linux 網路設定 ip 指令用法教學與範例

本篇介紹如何在 Linux 系...

9 個月 ago

Windows 使用 TPM 虛擬智慧卡保護 SSH 金鑰教學與範例

本篇介紹如何在 Windows...

10 個月 ago

Linux 以 Shamir’s Secret Sharing 分割保存金鑰教學與範例

介紹如何在 Linux 中使用...

11 個月 ago

Linux 以 Cryptsetup、LUKS 加密 USB 隨身碟教學與範例

介紹如何在 Linux 系統中...

11 個月 ago