SSL配置设置详细说明

1. 获取SSL证书

  • 自签名证书: 仅用于测试环境。

  • CA签发的证书: 用于生产环境,从受信任的证书颁发机构(CA)获取。

2. 安装SSL证书

  • 将证书文件(如certificate.crt)、私钥文件(如private.key)和CA证书链文件(如ca_bundle.crt)放置在服务器上。

3. 配置Web服务器

  • Apache:

    <VirtualHost *:443>
        ServerName www.example.com
        DocumentRoot /var/www/html
    
        SSLEngine on
        SSLCertificateFile /path/to/certificate.crt
        SSLCertificateKeyFile /path/to/private.key
        SSLCertificateChainFile /path/to/ca_bundle.crt
    
        <Directory /var/www/html>
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>

    重启Apache: sudo systemctl restart apache2

  • Nginx:

    server {
        listen 443 ssl;
        server_name www.example.com;
    
        ssl_certificate /path/to/certificate.crt;
        ssl_certificate_key /path/to/private.key;
        ssl_trusted_certificate /path/to/ca_bundle.crt;
    
        location / {
            root /var/www/html;
            index index.html;
        }
    }

    重启Nginx: sudo systemctl restart nginx

4. 强制HTTPS重定向

  • Apache:

    <VirtualHost *:80>
        ServerName www.example.com
        Redirect permanent / https://www.example.com/
    </VirtualHost>
  • Nginx:

    server {
        listen 80;
        server_name www.example.com;
        return 301 https://$host$request_uri;
    }

5. 测试配置

  • 使用浏览器访问 https://www.example.com,确认连接安全。

  • 使用工具如 SSL Labs 测试SSL配置安全性。

案例

场景: 为域名 www.example.com 配置SSL。

  1. 获取证书:

    • 从CA获取 certificate.crtprivate.keyca_bundle.crt

  2. 安装证书:

    • 将证书文件放置在 /etc/ssl/certs//etc/ssl/private/ 目录。

  3. 配置Web服务器 (以Nginx为例):

    server {
        listen 443 ssl;
        server_name www.example.com;
    
        ssl_certificate /etc/ssl/certs/certificate.crt;
        ssl_certificate_key /etc/ssl/private/private.key;
        ssl_trusted_certificate /etc/ssl/certs/ca_bundle.crt;
    
        location / {
            root /var/www/html;
            index index.html;
        }
    }
    
    server {
        listen 80;
        server_name www.example.com;
        return 301 https://$host$request_uri;
    }
  4. 重启Nginx:

    sudo systemctl restart nginx
  5. 测试:

    • 访问 https://www.example.com,确认SSL配置生效。

本篇文章内容来源于:ssl配置设置详细说明以及案例