阿里云Ubuntu 24.04.2 LTS 部署Nginx
Ubuntu 24.04 64 位系统上安装 Nginx 的步骤
更新系统软件包
运行以下命令更新系统软件包:
sudo apt update
安装 Nginx
使用以下命令安装 Nginx:
sudo apt install nginx -y
安装完成后,可以通过以下命令查看 Nginx 版本,确认安装成功:
nginx -v
输出类似 nginx version: nginx/1.24.0 (Ubuntu)
。
配置防火墙
如果启用了防火墙(如 ufw
),需要允许 HTTP 和 HTTPS 流量通过:
- 允许 HTTP 流量(端口 80):
sudo ufw allow 'Nginx HTTP'
- 允许 HTTPS 流量(端口 443):
sudo ufw allow 'Nginx HTTPS'
- 允许所有 Nginx 流量:
sudo ufw allow 'Nginx Full'
然后重新加载防火墙规则。
启动和管理 Nginx 服务
- 启动 Nginx 服务:
sudo systemctl start nginx
- 停止 Nginx 服务:
sudo systemctl stop nginx
- 重启 Nginx 服务:
sudo systemctl restart nginx
- 查看 Nginx 服务状态:
sudo systemctl status nginx
如果服务正常运行,会看到类似以下的输出:
● 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 ...
测试 Nginx 是否正常工作
打开浏览器并访问服务器的 IP 地址或域名,如果 Nginx 正常运行,你会看到默认的欢迎页面。
服务器执行记录
Welcome to Ubuntu 24.04.2 LTS (GNU/Linux 6.8.0-63-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/pro
System information as of Mon Jul 28 06:25:38 PM CST 2025
System load: 0.48 Processes: 121
Usage of /: 6.6% of 39.01GB Users logged in: 0
Memory usage: 20% IPv4 address for eth0: 172.16.140.118
Swap usage: 0%
* Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
just raised the bar for easy, resilient and secure K8s cluster deployment.
https://ubuntu.com/engage/secure-kubernetes-at-the-edge
Expanded Security Maintenance for Applications is not enabled.
33 updates can be applied immediately.
To see these additional updates run: apt list --upgradable
Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status
1 updates could not be installed automatically. For more details,
see /var/log/unattended-upgrades/unattended-upgrades.log
*** System restart required ***
Welcome to Alibaba Cloud Elastic Compute Service !
Last login: Mon Jul 28 18:25:39 2025 from 100.104.86.12
root@iZbp10tggfja3e91n9bskuZ:~# sudo apt update
Hit:1 http://mirrors.cloud.aliyuncs.com/ubuntu noble InRelease
Get:2 http://mirrors.cloud.aliyuncs.com/ubuntu noble-updates InRelease [126 kB]
Get:3 http://mirrors.cloud.aliyuncs.com/ubuntu noble-backports InRelease [126 kB]
Get:4 http://mirrors.cloud.aliyuncs.com/ubuntu noble-security InRelease [126 kB]
Get:5 http://mirrors.cloud.aliyuncs.com/ubuntu noble-updates/main amd64 Packages [1,282 kB]
Get:6 http://mirrors.cloud.aliyuncs.com/ubuntu noble-updates/main amd64 Components [163 kB]
Get:7 http://mirrors.cloud.aliyuncs.com/ubuntu noble-updates/restricted amd64 Components [212 B]
Get:8 http://mirrors.cloud.aliyuncs.com/ubuntu noble-updates/universe amd64 Packages [1,113 kB]
Get:9 http://mirrors.cloud.aliyuncs.com/ubuntu noble-updates/universe amd64 Components [376 kB]
Get:10 http://mirrors.cloud.aliyuncs.com/ubuntu noble-updates/multiverse amd64 Components [940 B]
Get:11 http://mirrors.cloud.aliyuncs.com/ubuntu noble-backports/main amd64 Components [7,088 B]
Get:12 http://mirrors.cloud.aliyuncs.com/ubuntu noble-backports/restricted amd64 Components [216 B]
Get:13 http://mirrors.cloud.aliyuncs.com/ubuntu noble-backports/universe amd64 Components [28.4 kB]
Get:14 http://mirrors.cloud.aliyuncs.com/ubuntu noble-backports/multiverse amd64 Components [212 B]
Get:15 http://mirrors.cloud.aliyuncs.com/ubuntu noble-security/main amd64 Components [21.6 kB]
Get:16 http://mirrors.cloud.aliyuncs.com/ubuntu noble-security/restricted amd64 Components [212 B]
Get:17 http://mirrors.cloud.aliyuncs.com/ubuntu noble-security/universe amd64 Components [52.3 kB]
Get:18 http://mirrors.cloud.aliyuncs.com/ubuntu noble-security/multiverse amd64 Components [208 B]
Fetched 3,424 kB in 2s (1,656 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
35 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@iZbp10tggfja3e91n9bskuZ:~# sudo apt install nginx -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
nginx-common
Suggested packages:
fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
nginx nginx-common
0 upgraded, 2 newly installed, 0 to remove and 35 not upgraded.
Need to get 564 kB of archives.
After this operation, 1,596 kB of additional disk space will be used.
Get:1 http://mirrors.cloud.aliyuncs.com/ubuntu noble-updates/main amd64 nginx-common all 1.24.0-2ubuntu7.4 [43.4 kB]
Get:2 http://mirrors.cloud.aliyuncs.com/ubuntu noble-updates/main amd64 nginx amd64 1.24.0-2ubuntu7.4 [521 kB]
Fetched 564 kB in 0s (10.2 MB/s)
Preconfiguring packages ...
Selecting previously unselected package nginx-common.
(Reading database ... 85192 files and directories currently installed.)
Preparing to unpack .../nginx-common_1.24.0-2ubuntu7.4_all.deb ...
Unpacking nginx-common (1.24.0-2ubuntu7.4) ...
Selecting previously unselected package nginx.
Preparing to unpack .../nginx_1.24.0-2ubuntu7.4_amd64.deb ...
Unpacking nginx (1.24.0-2ubuntu7.4) ...
Setting up nginx-common (1.24.0-2ubuntu7.4) ...
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
Setting up nginx (1.24.0-2ubuntu7.4) ...
* Upgrading binary nginx [ OK ]
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for ufw (0.36.2-6) ...
Scanning processes...
Scanning candidates...
Scanning linux images...
Running kernel seems to be up-to-date.
Restarting services...
Service restarts being deferred:
/etc/needrestart/restart.d/dbus.service
systemctl restart getty@tty1.service
systemctl restart networkd-dispatcher.service
systemctl restart serial-getty@ttyS0.service
systemctl restart systemd-logind.service
systemctl restart unattended-upgrades.service
No containers need to be restarted.
No user sessions are running outdated binaries.
No VM guests are running outdated hypervisor (qemu) binaries on this host.
root@iZbp10tggfja3e91n9bskuZ:~# nginx -v
nginx version: nginx/1.24.0 (Ubuntu)
root@iZbp10tggfja3e91n9bskuZ:~# sudo ufw allow 'Nginx HTTP'
Rules updated
Rules updated (v6)
root@iZbp10tggfja3e91n9bskuZ:~# sudo ufw allow 'Nginx HTTPS'
Rules updated
Rules updated (v6)
root@iZbp10tggfja3e91n9bskuZ:~# sudo ufw allow 'Nginx Full'
Rules updated
Rules updated (v6)
root@iZbp10tggfja3e91n9bskuZ:~# sudo systemctl start nginx
root@iZbp10tggfja3e91n9bskuZ:~# sudo systemctl status nginx