在Ubuntu上配置LNMP(Linux, Nginx, MySQL, PHP)环境的SSL证书,通常涉及以下几个步骤:
获取SSL证书:
安装Certbot:
sudo apt update
sudo apt install certbot python3-certbot-nginx
获取并安装SSL证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
这个命令会自动配置Nginx以使用SSL,并将证书安装在适当的位置。
测试配置: Certbot在安装证书后会自动测试Nginx配置是否正确。如果有错误,它会提示你修复。
sudo nginx -t
重新加载Nginx: 如果配置测试通过,重新加载Nginx以应用更改。
sudo systemctl reload nginx
设置自动续期: Let’s Encrypt的证书有效期为90天,Certbot可以设置自动续期。
sudo crontab -e
添加以下行以确保每天检查一次续期:
0 */12 * * * certbot renew --post-hook "systemctl reload nginx"
验证SSL配置: 使用SSL检查工具(如SSL Labs)来验证你的SSL配置是否正确和安全。
你可以从Let’s Encrypt免费获取SSL证书。访问Let’s Encrypt网站,注册并获取证书。
Certbot是一个自动化工具,可以帮助你获取和安装SSL证书。使用以下命令安装Certbot及其Nginx插件:
sudo apt update
sudo apt install certbot python3-certbot-nginx
使用Certbot获取并安装SSL证书。替换yourdomain.com
和www.yourdomain.com
为你的实际域名:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Certbot会自动配置Nginx以使用SSL,并将证书安装在适当的位置(通常是/etc/letsencrypt/live/yourdomain.com/
)。
Certbot在安装证书后会自动测试Nginx配置是否正确。如果有错误,它会提示你修复。
sudo nginx -t
如果配置测试通过,重新加载Nginx以应用更改。
sudo systemctl reload nginx
Let’s Encrypt的证书有效期为90天,Certbot可以设置自动续期。编辑crontab文件以确保每天检查一次续期:
sudo crontab -e
添加以下行:
0 */12 * * * certbot renew --post-hook "systemctl reload nginx"
使用SSL检查工具(如SSL Labs)来验证你的SSL配置是否正确和安全。
通过以上步骤,你可以在Ubuntu上成功配置LNMP环境的SSL证书。