ubuntu

LNMP在Ubuntu上的SSL证书配置

小樊
32
2025-06-23 04:29:18
栏目: 云计算

在Ubuntu上配置LNMP(Linux, Nginx, MySQL, PHP)环境的SSL证书,通常涉及以下几个步骤:

  1. 获取SSL证书

    • 你可以从Let’s Encrypt免费获取SSL证书。
    • 使用Certbot工具可以自动化这个过程。
  2. 安装Certbot

    sudo apt update
    sudo apt install certbot python3-certbot-nginx
    
  3. 获取并安装SSL证书

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

    这个命令会自动配置Nginx以使用SSL,并将证书安装在适当的位置。

  4. 测试配置: Certbot在安装证书后会自动测试Nginx配置是否正确。如果有错误,它会提示你修复。

    sudo nginx -t
    
  5. 重新加载Nginx: 如果配置测试通过,重新加载Nginx以应用更改。

    sudo systemctl reload nginx
    
  6. 设置自动续期: Let’s Encrypt的证书有效期为90天,Certbot可以设置自动续期。

    sudo crontab -e
    

    添加以下行以确保每天检查一次续期:

    0 */12 * * * certbot renew --post-hook "systemctl reload nginx"
    
  7. 验证SSL配置: 使用SSL检查工具(如SSL Labs)来验证你的SSL配置是否正确和安全。

详细步骤说明

1. 获取SSL证书

你可以从Let’s Encrypt免费获取SSL证书。访问Let’s Encrypt网站,注册并获取证书。

2. 安装Certbot

Certbot是一个自动化工具,可以帮助你获取和安装SSL证书。使用以下命令安装Certbot及其Nginx插件:

sudo apt update
sudo apt install certbot python3-certbot-nginx

3. 获取并安装SSL证书

使用Certbot获取并安装SSL证书。替换yourdomain.comwww.yourdomain.com为你的实际域名:

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

Certbot会自动配置Nginx以使用SSL,并将证书安装在适当的位置(通常是/etc/letsencrypt/live/yourdomain.com/)。

4. 测试配置

Certbot在安装证书后会自动测试Nginx配置是否正确。如果有错误,它会提示你修复。

sudo nginx -t

5. 重新加载Nginx

如果配置测试通过,重新加载Nginx以应用更改。

sudo systemctl reload nginx

6. 设置自动续期

Let’s Encrypt的证书有效期为90天,Certbot可以设置自动续期。编辑crontab文件以确保每天检查一次续期:

sudo crontab -e

添加以下行:

0 */12 * * * certbot renew --post-hook "systemctl reload nginx"

7. 验证SSL配置

使用SSL检查工具(如SSL Labs)来验证你的SSL配置是否正确和安全。

通过以上步骤,你可以在Ubuntu上成功配置LNMP环境的SSL证书。

0
看了该问题的人还看了