在Ubuntu上为Nginx配置SSL加密,你需要遵循以下步骤:
- 安装Nginx(如果尚未安装):
sudo apt update
sudo apt install nginx
- 获取SSL证书。你可以从Let’s Encrypt免费获取SSL证书,或者购买一个SSL证书。这里以Let’s Encrypt为例:
首先,确保已安装Certbot:
sudo apt install certbot python3-certbot-nginx
然后,运行Certbot以获取并安装SSL证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
将yourdomain.com和www.yourdomain.com替换为你的域名。Certbot将自动配置Nginx并创建一个名为/etc/letsencrypt/options-ssl-nginx.conf的文件,其中包含SSL相关的设置。
- 测试Nginx配置是否正确:
sudo nginx -t
如果没有错误,你将看到“syntax is ok”和“test is successful”的消息。
- 重新加载Nginx以应用更改:
sudo systemctl reload nginx
现在,你的Nginx服务器应该已经使用SSL加密了。你可以通过访问https://yourdomain.com来验证。浏览器应该显示一个绿色的锁图标,表示连接是安全的。
注意:Certbot会自动为你的证书设置一个30天的有效期。在证书到期之前,Certbot会尝试自动续订证书。如果你想手动续订证书,可以运行以下命令:
sudo certbot renew