在Ubuntu上为域名安装SSL证书的步骤如下:
安装Certbot和Web服务器软件如果你使用的是Apache:
- 更新包列表:
sudo apt update
安装Certbot和Apache模块:sudo apt install certbot python3-certbot-apache
如果你使用的是Nginx:
- 更新包列表:
sudo apt update
安装Certbot和Nginx模块: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@localhostdocumentRoot /var/www/htmlSSLEngine onSSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pemSSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pemInclude /etc/letsencrypt/options-ssl-apache.conf</VirtualHost>
启用SSL站点:
sudo a2ensite yourdomain.com-le-ssl.confsudo systemctl restart apache2
对于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站点:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/sudo nginx -tsudo 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服务。