loading

Loading

请输入关键字开始搜索
    首页 技术实践网站部署

    【2】wordpress申请https证书

    分类:网站部署
    字数: (2276)
    阅读: (142)
    0

    默认wordpress是http进行访问,http其实不安全,但是使用https访问,如果没申请证书会提示警告,这个时候就需要申请有效的证书并配置。默认在腾讯云控制台是可以申请域名证书的,但是有控制台申请的域名证书每次只有3个月,每次都需要手动下载替换。在linux中可以直接使用certbot机器人进行申请和自动替换,好处有2个。

    • 可以申请https的ssl证书
    • 可以自动续期
      申请域名的前提条件:
    • 域名:需要已备案并有效的域名,并且该域名指向服务器。
    • 服务器:1台VPS 或云服务器,并且上面安装了 Nginx 或 Apache。
    • 操作系统: Debian、Ubuntu、CentOS系统
      本次以ubuntu系统为例,安装的时nginx。

    安装与申请

    1. 更新软件源

    sudo apt update

    2. 安装Certbot

    在ubuntu中,安装cerbot

    sudo apt install certbot python3-certbot-nginx

    3. 申请ssl证书

    替换yourdomain为你的域名,比如我申请rrdsceo.cn,那么就一次性申请:rrdsceo.cn,www.rrdsceo.cn, blog.rrdsceo.cn;

    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com -d blog.yourdomain.com

    Certbot 会请求 Let's Encrypt 验证域名的控制权,通常 Certbot 会自动处理这个过程。验证成功后,它会安装证书并更新你的 web 服务器配置,比如查看:

    sudo vim /etc/nginx/sites-enabled/default
    sudo vim /etc/nginx/sites-enabled/yourdomain.com

    都能看到有对应的443的https,则说明已经自动添加了

    server {
        listen 443 ssl;
        listen [::]:443 ssl;
    
        server_name yourdomain.com;
    
        # SSL configuration
        ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
        root /var/www/html;  # 确保这个目录存在并有可访问的文件
        index index.html index.htm index.nginx-debian.html;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }

    这个时候访问https://yourdomain.com,确保 HTTPS 正常工作:

    https://yourdomain.com

    4. 自动续期

    Let's Encrypt 的证书有效期为 90 天,因此建议设置一个 Cron 任务自动续期。
    `注意:默认情况下,Certbot 会在安装时为你设置自动续期。
    查看当前的所有证书

    sudo certbot certificates

    你可以运行以下命令来测试自动续期。

    sudo certbot renew --dry-run

    也可以查看是否正在进行后台任务运行

    systemctl list-timers | grep certbot

    能看到一条,则表明后台自动运行

    Sun 2024-11-10 04:34:35 CST 2h 33min left Sat 2024-11-09 13:12:10 CST 12h ago      certbot.timer                certbot.service
    

    5. 手动续期

    有时候很奇怪,nginx启动如果443端口没有证书,无法启动,但是certbot又需要网站启动,所以可以手动启动。

    sudo systemctl stop nginx
    sudo certbot certonly --standalone \
      -d blog.askerlab.com \
      --cert-name blog.askerlab.com
    sudo systemctl start nginx

    如果有证书残留,一般证书放在以下三个位置,对应删除即可

    sudo rm /etc/letsencrypt/live/git.askerlab.com -rf
    sudo rm /etc/letsencrypt/archive/git.askerlab.com -rf
    sudo rm /etc/letsencrypt/renewal/git.askerlab.com.conf -f
    本文发布于2024年11月10日01:42,已经过了414天,若内容或图片失效,请留言反馈
    文章出处: 求索空间
    文章链接: https://blog.askerlab.com/wps_add_https
    评论列表:
    empty

    暂无评论