温馨提示×

Apache2 在 CentOS 上如何实现 SSL 加密

小樊
45
2025-11-17 21:31:31
栏目: 云计算

在 CentOS 上为 Apache2 实现 SSL 加密,你需要遵循以下步骤:

  1. 安装 Apache 和 mod_ssl 模块:

    首先,确保你已经安装了 Apache。如果没有,请使用以下命令安装:

    sudo yum install httpd
    

    接下来,安装 mod_ssl 模块,它提供了对 SSL 的支持:

    sudo yum install mod_ssl
    
  2. 获取 SSL 证书:

    你可以从证书颁发机构(CA)购买 SSL 证书,或者使用 Let’s Encrypt 提供的免费证书。以下是使用 Certbot 获取 Let’s Encrypt 证书的示例:

    sudo yum install certbot python2-certbot-apache
    sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
    

    在安装过程中,Certbot 会自动创建一个名为 /etc/letsencrypt 的目录,其中包含证书文件。

  3. 配置 Apache 使用 SSL:

    编辑 Apache 配置文件(通常位于 /etc/httpd/conf/httpd.conf/etc/apache2/apache2.conf),并添加以下内容:

    LoadModule ssl_module modules/mod_ssl.so
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem
    

    请确保将 yourdomain.com 替换为你的域名。

  4. 重启 Apache 以应用更改:

    sudo systemctl restart httpd
    

    现在,你的 Apache 服务器应该已经配置为使用 SSL 加密。你可以通过访问 https://yourdomain.com 来验证 SSL 配置是否正确。你应该看到一个绿色的锁图标,表示连接已加密。

  5. (可选)设置自动续订证书:

    Let’s Encrypt 证书有效期为 90 天。为了确保证书始终有效,你可以设置 Certbot 自动续订证书。编辑 /etc/letsencrypt/renewal-hooks/deploy/post 文件,并添加以下内容:

    systemctl reload httpd
    

    然后,运行以下命令测试自动续订功能:

    sudo certbot renew --dry-run
    

    如果一切正常,Certbot 将在证书到期前自动续订证书。

0