Debian Apache SSL证书怎么配置

604
2025/3/20 3:31:54
栏目: 云计算
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Debian系统上配置Apache以使用SSL证书的步骤如下:

  1. 安装Certbot和Apache SSL模块
sudo apt update
sudo apt install certbot python3-certbot-apache
  1. 生成2048位DH密钥(如果尚未生成):
sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
  1. 获取Let’s Encrypt SSL证书
sudo certbot certonly --agree-tos --email admin@example.com --webroot -w /var/lib/letsencrypt/ -d example.com -d www.example.com

其中,example.com应替换为您的域名。

  1. 配置Apache
  • 创建或编辑虚拟主机配置文件,通常位于 /etc/apache2/sites-available/ 目录下,例如 example.com.conf

  • 在配置文件中添加以下内容:

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/example.com/public_html

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem

    # 可选:启用HTTP/2
    SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite HIGH:!aNULL:!MD5:!ADH:!RC4:!DH:!RSA
    SSLHonorCipherOrder on

    # 其他安全头设置
    Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
    Header always set X-Frame-Options "DENY"
    Header always set X-Content-Type-Options "nosniff"
    SSLCompression off
    SSLUseStapling on
    SSLStaplingCache "shmcb:logs/stapling-cache(150000)"
    SSLSessionTickets off
</VirtualHost>
  • 替换 example.com 为您的域名,并确保 DocumentRoot 指向您的网站根目录。
  1. 启用新的虚拟主机配置文件
sudo a2ensite example.com.conf
  1. 重新加载Apache配置
sudo systemctl reload apache2
  1. 配置防火墙(如果使用 ufw):
sudo ufw allow 'Apache Full'
  1. 测试SSL证书

在浏览器中访问 https://example.com,确保显示安全连接。

请注意,上述步骤假设您已经拥有了一个域名,并且该域名已经解析到运行Apache的服务器IP地址上。如果您使用的是Let’s Encrypt证书,证书通常是免费的,并且会自动更新,但您需要定期使用Certbot进行续期。此外,根据您的具体需求,您可能需要调整防火墙规则和其他安全设置。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: cpustat如何帮助优化Debian服务器