在Ubuntu上为域名绑定SSL证书的步骤如下:
安装Certbot和Web服务器软件
如果你使用的是Apache,运行以下命令安装Certbot和Apache模块:
sudo apt update
sudo apt install certbot python3-certbot-apache
如果你使用的是Nginx,运行以下命令安装Certbot和Nginx模块:
sudo apt update
sudo apt install certbot python3-certbot-nginx
获取证书
使用Certbot获取证书。如果你使用的是Apache,运行:
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
如果你使用的是Nginx,运行:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
按照提示完成证书的获取过程。Certbot会自动配置Apache或Nginx以使用新获取的证书。
配置Web服务器
对于Apache:Certbot会自动修改配置文件(通常位于/etc/apache2/sites-available/yourdomain.com-le-ssl.conf
或类似位置),确保以下内容存在:
<VirtualHost *:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
对于Nginx:Certbot会自动修改配置文件(通常位于/etc/nginx/sites-available/yourdomain.com
或类似位置),确保以下内容存在:
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
启用SSL站点
对于Apache:
sudo a2ensite yourdomain.com-le-ssl.conf
sudo systemctl restart apache2
对于Nginx:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
测试配置
打开浏览器并访问https://yourdomain.com
,确保HTTPS连接正常,并且浏览器显示安全锁标志。
自动续期
Let’s Encrypt证书的有效期为90天。为了确保证书始终有效,你可以配置Certbot定期自动更新它们。例如,可以设置一个cron作业来自动执行证书更新:
sudo crontab -e
然后在打开的编辑器中添加以下行(确保将/etc/letsencrypt/live/
替换为你的实际证书路径):
0 0,12 * * * certbot renew --quiet && systemctl reload nginx
这将每天执行两次证书更新检查。如果证书需要更新,Certbot将自动更新它们并重新加载Nginx服务。
通过以上步骤,你应该能够在Ubuntu上成功为你的域名绑定SSL证书。如果有任何问题,请检查日志文件以获取更多信息。