温馨提示×

nginxssl在debian上怎么装

小樊
42
2026-10-08 14:10:41
栏目: 云计算

在 Debian 上配置 Nginx + SSL 通常分为两部分:
1️⃣ 安装 Nginx
2️⃣ 配置 SSL(可以用自签证书或 Let’s Encrypt 免费证书)

下面给你两种常用方案。


一、安装 Nginx(Debian)

sudo apt update
sudo apt install nginx -y

安装完成后:

sudo systemctl enable nginx
sudo systemctl start nginx

浏览器访问服务器 IP,能看到 Nginx 欢迎页就说明成功。


二、方案 A:使用 Let’s Encrypt 免费 SSL(推荐)

1. 安装 certbot

sudo apt install certbot python3-certbot-nginx -y

2. 申请证书(需已解析域名)

假设你的域名是 example.com:

sudo certbot --nginx -d example.com -d www.example.com

按提示操作,certbot 会:

  • 自动申请证书
  • 自动修改 Nginx 配置
  • 配置 HTTP → HTTPS 跳转

3. 测试自动续期

sudo certbot renew --dry-run

三、方案 B:使用自签 SSL 证书(测试用)

1. 生成证书

sudo openssl req -x509 -nodes -days 365 \
  -newkey rsa:2048 \
  -keyout /etc/ssl/private/nginx.key \
  -out /etc/ssl/certs/nginx.crt

2. 配置 Nginx

编辑站点配置:

sudo nano /etc/nginx/sites-available/default

示例配置:

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate     /etc/ssl/certs/nginx.crt;
    ssl_certificate_key /etc/ssl/private/nginx.key;

    root /var/www/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

3. 测试并重载

sudo nginx -t
sudo systemctl reload nginx

四、防火墙(如有)

sudo ufw allow 'Nginx Full'

五、常见问题

  • ❌ 证书不生效 → 检查域名 A 记录
  • ❌ 443 无法访问 → 检查云服务器安全组
  • ✅ 生产环境强烈建议用 Let’s Encrypt

如果你愿意,可以告诉我:

  • Debian 版本(11 / 12?)
  • 域名是否已解析
  • 是本地测试还是公网服务器

我可以给你更精确的配置。

0 踩