部署 nginx

环境

官方文档

速查手册

安装 nginx 需要的包:

sudo apt update
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring

导入 nginx 的签名密钥:

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

配置 nginx 的官方稳定版 apt 源:

echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
https://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

强制使用官方源而非发行版提供的源:

echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
    | sudo tee /etc/apt/preferences.d/99nginx

安装 nginx:

sudo apt update
sudo apt install nginx

启动

可以使用 systemd 管理 nginx:

sudo systemctl start nginx # 启动 nginx
sudo systemctl enable nginx # 开机自启动 nginx

验证

使用 curl 向 localhost 请求内容:

curl localhost

期待得到这样的响应:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>