centos

centos nginx SSL证书安装

小樊
40
2025-04-28 20:53:16
栏目: 云计算

在CentOS上安装Nginx SSL证书,您需要遵循以下步骤:

  1. 安装Certbot和Nginx: 如果您还没有安装Certbot和Nginx,请先安装它们。可以使用以下命令安装:

    sudo yum install epel-release
    sudo yum install certbot python2-certbot-nginx
    
  2. 获取SSL证书: 使用Certbot获取SSL证书。运行以下命令并按照提示操作:

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

    yourdomain.comwww.yourdomain.com替换为您的域名。Certbot将自动为您的域名生成SSL证书。

  3. 配置Nginx: Certbot将自动修改您的Nginx配置文件以启用HTTPS。您可以在/etc/nginx/conf.d/yourdomain.com.conf(将yourdomain.com替换为您的域名)中找到配置文件。配置文件应类似于以下内容:

    server {
        listen 80;
        server_name yourdomain.com www.yourdomain.com;
        return 301 https://$host$request_uri;
    }
    
    server {
        listen 443 ssl;
        server_name yourdomain.com www.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;
    
        # 其他Nginx配置...
    }
    

    确保ssl_certificatessl_certificate_key指向Certbot生成的证书文件。

  4. 重启Nginx: 保存配置文件并重启Nginx以应用更改:

    sudo systemctl restart nginx
    
  5. 测试SSL证书: 使用浏览器访问您的域名,确保SSL证书已正确安装并且网站可以通过HTTPS访问。

注意:Certbot的免费SSL证书有效期为90天。为了确保证书始终有效,您需要设置自动续期。可以使用以下命令测试自动续期:

sudo certbot renew --dry-run

如果一切正常,您可以使用以下命令设置自动续期:

sudo crontab -e

在打开的编辑器中添加以下行:

0 0,12 * * * certbot renew --post-hook "systemctl reload nginx"

保存并退出编辑器。这将确保Certbot在证书到期前30天尝试续期,并在成功续期后自动重新加载Nginx。

0
看了该问题的人还看了