ubuntu系统nginx免费SSL证书 certbot+letsencrypt方案
发布日期:2021-05-24 17:30:45 浏览次数:19 分类:精选文章

本文共 2140 字,大约阅读时间需要 7 分钟。

安装Nginx并配置SSL证书(Ubuntu系统)

安装好Ubuntu系统后,以下是一套简洁的Nginx和SSL证书配置指南,帮助你在几分钟内完成配置。

第一步:安装certbot

安装certbot前,需要先安装必要的软件包。运行以下命令:

sudo apt-get updatesudo apt-get install software-properties-commonsudo add-apt-repository universesudo add-apt-repository ppa:certbot/certbotsudo apt-get update

安装certbot及其依赖:

sudo apt-get install certbot python-certbot-nginx

第二步:生成SSL证书

运行certbot获取证书:

certbot --nginx

根据提示输入您的电子邮箱地址:

Enter email address (used for urgent renewal and security notices) (Enter ‘c’ to cancel): frank@pazzn.com

阅读并接受Let’s Encrypt的服务条款并完成验证:

Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org...Congratulations! You have successfully enabled https://pazzn.com

配置SSL证书

certbot会自动安装生成的证书到 /etc/letsencrypt/live/pazzn.com/。默认配置可以直接使用以下Nginx配置文件:

server {    listen 80;    server_name pazzn.com;    root /var/www/pazzn.com;        location / {        index index.php index.html index.htm;    }    location ~* \.php$ {        fastcgi_index index.php;        fastcgi_pass 127.0.0.1:9000;        include fastcgi_params;        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;    }    error_page 404 /404.html;    error_page 500 502 503 504 /50x.html;        listen 443 ssl;    ssl_certificate /etc/letsencrypt/live/pazzn.com/fullchain.pem;    ssl_certificate_key /etc/letsencrypt/live/pazzn.com/privkey.pem;    include /etc/letsencrypt/options-ssl-nginx.conf;    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;}

某些额外配置(可选)

如果需要添加多个域名,请在命令中添加参数:

sudo certbot certonly --webroot -w /var/www/op.jiu.ho -d shop.jiu.ho -d op.jiu.ho -w /var/www/op.jiu.ho -d shop.jiu.hu -d shop.jiu.ho

第三步:启动Nginx并测试访问

在Nginx配置文件修改后,重启Nginx服务:

nginx -tsudo service nginx restart

检查配置是否正确,可以通过访问 https://www.ssllabs.com/ssltest/analyze.html?d=pazzn.com 进行全面测试。

自动更新证书(可选)

为确保证书的及时更新,可以使用cron脚本:

crontab -e@reboot cronjob.sh && 15 * * * *

添加自动更新脚本:

#!/bin/bashcertbot renew |_journalBirthday="`date +'%Y-%m-%d'`"

记得定期检查更新日志:/var/log/letsencrypt/letsencrypt.log

注意事项

  • SSL证书有效期为90天,建议设置自动更新任务。
  • 配置chef、Ansible或其他自动化工具可进一步简化管理。
  • 帮助支持开发者,可以选择加入Let’s Encrypt的捐赠计划。
  • 通过这些步骤,你可以轻松在Ubuntu系统中配置Nginx服务器并部署SSL证书。如果有任何问题或需要进一步优化,请参考Let’s Encrypt的官方文档或社区资源。

    上一篇:如何在将本地代码提交到码云
    下一篇:宝塔如何配置一个IP多个端口访问不同项目

    发表评论

    最新留言

    能坚持,总会有不一样的收获!
    [***.219.124.196]2025年04月18日 15时09分40秒