在Ubuntu上部署SSL证书以启用HTTPS连接,通常涉及以下几个步骤。以下是使用Let’s Encrypt免费证书的详细指南:
Certbot是Let’s Encrypt的官方客户端工具,用于自动获取和续订SSL证书。
sudo apt update
sudo apt install certbot python3-certbot-nginx # 如果使用Nginx
# 或者
sudo apt install certbot python3-certbot-apache # 如果使用Apache
根据你使用的Web服务器,运行相应的Certbot命令。
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
Certbot会自动配置你的Web服务器以使用SSL,并创建一个临时文件来测试配置是否正确。如果一切正常,它会将证书和私钥文件移动到适当的位置,并更新你的Web服务器配置文件。
Certbot会自动验证你的域名所有权,并在验证成功后重启Web服务器以应用新的SSL配置。
sudo systemctl restart nginx
sudo systemctl restart apache2
Let’s Encrypt证书通常有效期为90天,因此需要设置自动续订。
Certbot会在安装时创建一个cron任务或systemd定时器来自动续订证书。你可以手动测试续订过程以确保一切正常。
sudo certbot renew --dry-run
如果一切正常,Certbot会输出续订成功的信息。
为了提高安全性,可以启用HTTP Strict Transport Security (HSTS)。
在Nginx配置文件中添加以下行:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
在Apache配置文件中添加以下行:
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
然后重启Web服务器。
通过以上步骤,你可以在Ubuntu上成功部署SSL证书,并启用HTTPS连接。确保定期检查和续订证书,以保持网站的安全性。