centos

centos apache2如何部署SSL

小樊
43
2025-04-28 20:40:17
栏目: 云计算

在CentOS上使用Apache2(通常称为httpd)部署SSL,你需要遵循以下步骤:

  1. 安装Apache: 如果你还没有安装Apache,请使用以下命令安装:

    sudo yum install httpd
    
  2. 获取SSL证书: 你可以从Let’s Encrypt免费获取SSL证书,或者购买一个。如果你选择Let’s Encrypt,请确保你的服务器已经安装了Certbot。

  3. 安装并配置Certbot(如果使用Let’s Encrypt): Certbot是一个自动化的工具,可以帮助你获取和更新Let’s Encrypt的证书。安装Certbot和Apache插件:

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

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

    Certbot会自动配置Apache并重启服务。

  5. 手动配置SSL(如果你不使用Certbot): 如果你选择手动配置SSL,你需要编辑Apache的SSL配置文件,通常位于/etc/httpd/conf.d/ssl.conf

    • 确保你已经启用了SSL模块:

      sudo a2enmod ssl
      
    • 编辑SSL配置文件,设置你的证书和私钥路径:

      <VirtualHost *:443>
          ServerName yourdomain.com
          DocumentRoot "/var/www/html"
      
          SSLEngine on
          SSLCertificateFile /path/to/your/certificate.crt
          SSLCertificateKeyFile /path/to/your/private.key
          SSLCertificateChainFile /path/to/your/chainfile.pem
      
          <Directory "/var/www/html">
              Options Indexes FollowSymLinks
              AllowOverride All
              Require all granted
          </Directory>
      
          ErrorLog "/var/log/httpd/ssl_error_log"
          CustomLog "/var/log/httpd/ssl_access_log" combined
      </VirtualHost>
      
    • 保存文件并退出编辑器。

  6. 重启Apache: 无论你是使用Certbot还是手动配置,都需要重启Apache以应用更改:

    sudo systemctl restart httpd
    
  7. 测试SSL配置: 打开浏览器并访问https://yourdomain.com。你应该能够看到你的网站,并且浏览器地址栏应该显示一个锁形图标,表示连接是安全的。

  8. 自动续期(如果你使用Let’s Encrypt): Let’s Encrypt的证书有效期为90天。Certbot会自动尝试续期,但你也可以手动运行以下命令来测试续期过程:

    sudo certbot renew --dry-run
    

请确保将yourdomain.com/path/to/your/certificate.crt/path/to/your/private.key/path/to/your/chainfile.pem替换为你的实际域名和证书文件路径。

0
看了该问题的人还看了