在Ubuntu上为Apache2启用SSL,您需要首先安装Certbot和Let’s Encrypt证书。以下是详细步骤:
- 更新系统软件包列表:
sudo apt update
- 安装Certbot和Apache2插件:
sudo apt install certbot python3-certbot-apache
- 运行Certbot以获取并安装Let’s Encrypt证书:
sudo certbot --apache
-
Certbot将引导您完成证书安装过程。您需要提供电子邮件地址、同意服务条款,并选择要安装证书的域名。
-
Certbot将自动配置Apache2以使用SSL,并创建一个新的虚拟主机文件(通常位于
/etc/apache2/sites-available/your_domain_name-le-ssl.conf
)。 -
启用新的SSL站点配置:
sudo a2ensite your_domain_name-le-ssl.conf
- 禁用旧的HTTP站点配置(可选,但建议):
sudo a2dissite 000-default.conf
- 重新加载Apache2服务以应用更改:
sudo systemctl reload apache2
- 确保防火墙允许HTTPS流量:
sudo ufw allow 'Apache Full'
现在,您的网站应该已经通过HTTPS提供服务。您可以通过访问https://your_domain_name
来验证。
注意:Let’s Encrypt证书有效期为90天。为了确保证书始终有效,您可以使用Certbot的自动续订功能。Certbot将自动为您续订证书并在到期前安装新证书。要测试自动续订,请运行:
sudo certbot renew --dry-run