更新软件包列表

apt update -y

安装必备的依赖包

apt install -y curl gnupg2 ca-certificates lsb-release

添加Nginx官方的签名密钥

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

添加Nginx官方仓库

使用lsb_release -cs命令获取当前操作系统的代号(如buster、bullseye等),并将其赋值给变量DISTRO_CODENAME

DISTRO_CODENAME=$(lsb_release -cs)
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/debian $DISTRO_CODENAME nginx" | tee /etc/apt/sources.list.d/nginx.list

更新软件包列表并安装Nginx

apt update -y
sudo apt install nginx -y

指定版本的格式为:nginx=版本号-1~buster

apt install nginx=1.16.1-1~buster
sudo nano /etc/nginx/nginx.conf

配置文件存放处

cd /etc/nginx/conf.d

配置示例文件

sudo nano /etc/nginx/sites-enabled/default
systemctl status nginx
sudo nginx -t
sudo service nginx stop
sudo service nginx restart
sudo nginx -s reload