65.9K
CodeProject 正在变化。 阅读更多。
Home

如何设置SSL:一步一步指南

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2024年9月7日

CPOL

2分钟阅读

viewsIcon

3266

设置SSL(安全套接层)对于保护网站与其访问者之间的通信至关重要。本指南将引导您完成整个过程的每一步,从购买SSL证书到在各种服务器上配置它。

1. 了解SSL及其重要性

SSL证书对服务器和用户之间传输的数据进行加密,确保敏感信息(如登录凭据和付款详细信息)保持安全。SSL对于建立用户信任和提高网站的SEO排名至关重要。

2. 购买SSL证书

2.1 选择SSL证书提供商

有几家信誉良好的SSL证书提供商,包括:
  • Let’s Encrypt(免费)
  • DigiCert
  • Comodo
  • GeoTrust
在本指南中,我们将使用Let’s Encrypt,因为它提供免费证书并且被广泛接受。

2.2 生成证书签名请求 (CSR)

在购买或获取SSL证书之前,您需要生成一个CSR。以下是在基于Unix的系统上执行此操作的方法:
运行以下命令以生成私钥和CSR:
openssl req -new -newkey rsa:2048 -nodes -keyout yourdomain.key -out yourdomain.csr
填写所需的信息,包括您的域名、组织和联系方式。

3. 在不同服务器上配置SSL

3.1 Nginx

安装Certbot(Let’s Encrypt客户端)
sudo apt update
sudo apt install certbot python3-certbot-nginx
获取SSL证书
sudo certbot --nginx -d yourdomain.com
配置Nginx
您的Nginx配置文件(/etc/nginx/sites-available/yourdomain)应包含以下行:
server {
    listen 443 ssl;
    server_name yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;

    location / {
        proxy_pass https://:8080;
    }
}
测试并重新加载Nginx
sudo nginx -t
sudo systemctl reload nginx

3.2 Tomcat

将证书转换为Java密钥库
openssl pkcs12 -export -in yourdomain.crt -inkey yourdomain.key -out yourdomain.p12 -name tomcat
将密钥库导入Tomcat
编辑位于$CATALINA_HOME/conf中的server.xml
<Connector port="8443" protocol="HTTP/1.1" 
           maxThreads="150" SSLEnabled="true" 
           scheme="https" secure="true" 
           clientAuth="false" sslProtocol="TLS" 
           keystoreFile="/path/to/yourdomain.p12" 
           keystorePass="password" />
重启Tomcat
sudo systemctl restart tomcat

3.3 Apache

安装Certbot
sudo apt update
sudo apt install certbot python3-certbot-apache
获取SSL证书
sudo certbot --apache -d yourdomain.com
验证Apache配置
确保您的Apache配置(/etc/apache2/sites-available/yourdomain.conf)包含:
<VirtualHost *:443>
    ServerName yourdomain.com
    DocumentRoot /var/www/yourdomain

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem
</VirtualHost>
重启Apache
sudo systemctl restart apache2

3.4 XAMPP

生成CSR和密钥(如上所示)。
从Let’s Encrypt或其他提供商处获取SSL证书
在XAMPP中配置SSL
将您的证书文件(.crt.key)分别放置在xampp/apache/conf/ssl.crtxampp/apache/conf/ssl.key目录中。
<VirtualHost _default_:443>
    DocumentRoot "C:/xampp/htdocs"
    ServerName yourdomain.com:443

    SSLEngine on
    SSLCertificateFile "conf/ssl.crt/yourdomain.crt"
    SSLCertificateKeyFile "conf/ssl.key/yourdomain.key"
</VirtualHost>
重启XAMPP。

4. 验证SSL配置

为了确保您的SSL设置正常工作,请使用https://yourdomain.com访问您的网站,并检查浏览器地址栏中的锁形图标。您还可以使用SSL Labs的SSL Test等在线工具来验证您的配置。

5. 结论

设置SSL是保护您的网站和增强用户信任的关键步骤。通过遵循本指南,您可以确保您的SSL证书在流行的服务器(如Nginx、Tomcat、Apache和XAMPP)上正确配置。请记住保持您的SSL证书是最新的,并在到期前续订它,以保持安全的通信。

更多文章阅读请访问: 如何设置SSL:分步指南

© . All rights reserved.