解決在Ubuntu Server 22.04安裝NGINX失敗(2)
在前一篇分享之後,收到社團朋友的提醒,原因是主機停用 IPv6 造成繫結失敗,不是我說的那樣,這要快點再寫一篇釐清一下。
其實安裝當下,提供的日誌寫的很清楚,是自己沒用心看。
nginx[1695]: nginx: [emerg] socket() [::]:80 failed (97: Unknown error)
[::]:80
裡的 [::]
想成 IPv6 裡的 127.0.01 或 localhost。也就是說,日誌有清楚說明繫結不到 IPv6 的 80 Port。結果自己沒用心,往 IPv4 去查。雖然最後改用開源版本 NGINX 有解決我的問題,但這眼花的問題,也是要來好好瞭解學習一下。
IPv6 的 [::]:80 在那裡
首先讓 NGINX 在繫結 [::]:80
失敗。
sudo apt install nginx
非常明顯,不像開源版 NGINX,預設安裝的 NGINX 應該還有個 default 組態檔,會從那裡去繼承一些組態過來。因為我們在預設的 /etc/nginx/nginx.conf
看不到關於 server {}
組態。
接下來要找 NGINX - IPv6 繫結設定在那裡?
網路上有人寫,修改 /etc/nginx/conf.d/nginx.conf
的組態。不過目前我安裝 NGINX 有此目錄,但沒有 nginx.config
。
/etc/nginx/sites-available/default
/etc/nginx/sites-enabled/default
在以上兩個路徑 default
檔都有找到 [::]:80
的組態,那我要改那一個?兩個都改?一個一個試錯?
靜下心來,其實 /etc/nginx/nginx.conf
有給答案:
$ cat /etc/nginx/nginx.conf
# 省略
include /etc/nginx/modules-enabled/*.conf;
http {
# 省略
include /etc/nginx/mime.types;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
提示夠清楚了吧,NGINX 會利用 include
來匯入其他組態。因此 sites-enabled
目錄下的 default
組態檔是我們修改的目標。
server {
listen 80 default_server;
listen [::]:80 default_server;
# 省略
}
將 [::]:80
組態註解或刪除掉。接著測試一下 nginx.conf
:
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
透過前一篇學習的指令來完成啟動:
$ sudo dpkg --configure -a
Setting up nginx-core (1.18.0-6ubuntu14.7) ...
Setting up nginx (1.18.0-6ubuntu14.7) ...
$ systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2025-09-08 14:45:46 CST; 2min 41s ago
Docs: man:nginx(8)
Process: 2244 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 2245 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 2246 (nginx)
Tasks: 3 (limit: 9345)
Memory: 3.3M
CPU: 21ms
CGroup: /system.slice/nginx.service
├─2246 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
├─2247 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
└─2248 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
測試存取 NGINX:
$ curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
存取 HTTP 也沒問題了。
Ubuntu 預設安裝的 NGINX 與 nginx.org 的有何差異?
前面,對於 Ubuntu 裡的 NGINX 又多幾分瞭解了。但一個疑問又浮出心頭,就是 Ubuntu 預設安裝的 NGINX 與 nginx.org 的有何差異?
首先,可以確定不論是系統預設的 NGINX 或 nginx.org 的版本,都不是商業版。在 askubuntu 找到一篇不錯的參考資料,細部說明預設 NGINX 裡的 nginx-core
、nginx-extras
、nginx-full
、nginx-light
有何差異,有興趣的人,可以自行參考一下。
# Ubuntu nginx 安裝的套件
$ apt-cache depends nginx | tail -n+2 | cut -d: -f 2 | sort -u
<libnginx-mod-http-lua>
nginx-core
nginx-extras
nginx-full
nginx-light
# nginx.org 安裝的套件
$ apt-cache depends nginx | tail -n+2 | cut -d: -f 2 | sort -u
libc6
libcrypt1
libpcre2-8-0
libssl3
logrotate
lsb-base
nginx-common
nginx-core
zlib1g
簡單來說,如果你求穩且預設的套件功已夠你使用,那麼 Ubuntu Repository 裡的版本沒有什麼問題。如果你喜歡求新求快,那麼 nginx.org Repository 會是比較好的選擇。以安裝了 nginx.org Repository 的主機為例:
$ apt-cache madison nginx
nginx | 1.28.0-1~jammy | http://nginx.org/packages/ubuntu jammy/nginx amd64 Packages
nginx | 1.26.3-1~jammy | http://nginx.org/packages/ubuntu jammy/nginx amd64 Packages
nginx | 1.26.2-1~jammy | http://nginx.org/packages/ubuntu jammy/nginx amd64 Packages
nginx | 1.26.1-2~jammy | http://nginx.org/packages/ubuntu jammy/nginx amd64 Packages
nginx | 1.26.1-1~jammy | http://nginx.org/packages/ubuntu jammy/nginx amd64 Packages
nginx | 1.26.0-1~jammy | http://nginx.org/packages/ubuntu jammy/nginx amd64 Packages
nginx | 1.24.0-1~jammy | http://nginx.org/packages/ubuntu jammy/nginx amd64 Packages
nginx | 1.22.1-1~jammy | http://nginx.org/packages/ubuntu jammy/nginx amd64 Packages
nginx | 1.22.0-1~jammy | http://nginx.org/packages/ubuntu jammy/nginx amd64 Packages
nginx | 1.20.2-1~jammy | http://nginx.org/packages/ubuntu jammy/nginx amd64 Packages
nginx | 1.18.0-6ubuntu14.7 | http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages
nginx | 1.18.0-6ubuntu14.7 | http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages
nginx | 1.18.0-6ubuntu14 | http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages
目前 Ubuntu Repository 中是 nginx 1.18.0 版,而 nginx.org Repository (我選 stable 分支)是 1.28.0,兩者已相差 5 版(stable 是偶數號、mainline 是單數號)。最後我應該會採用 nginx.org Repository,重點在於 stable 這 5 次進版中,修正了多個 CVE 的漏洞,這就不是穩或快的問題。看你在不在乎,這些 CVE 會不會對你造成影響。
沒有留言:
張貼留言
感謝您的留言,如果我的文章你喜歡或對你有幫助,按個「讚」或「分享」它,我會很高興的。