首先准备私钥、证书和中间证书文件,然后根据Web服务器选择配置方式:Apache需启用mod_ssl模块并配置虚拟主机,Nginx需编辑服务器块启用SSL并设置证书路径,推荐使用Certbot工具从Let's Encrypt获取免费证书,自动完成部署与续期,最后通过SSL测试工具验证配置完整性与安全性。

在Linux系统中配置SSL证书是保障Web服务安全通信的关键步骤。无论你使用Apache、Nginx还是其他Web服务器,正确部署SSL证书都能实现HTTPS加密,提升用户数据的安全性。本文将带你一步步完成从生成证书请求到启用HTTPS的完整流程。
要配置SSL,你需要以下文件:
如果你还没有证书,可以使用OpenSSL生成CSR(证书签名请求):
openssl req -new -newkey rsa:2048 -nodes \ -keyout your_domain.key \ -out your_domain.csr
将生成的CSR提交给CA,下载签发的证书和中间证书,保存到服务器指定目录,例如 /etc/ssl/certs/ 和 /etc/ssl/private/。
确保已安装mod_ssl模块:
sudo a2enmod ssl sudo systemctl restart apache2
编辑虚拟主机配置文件(通常位于 /etc/apache2/sites-available/default-ssl.conf):
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerName www.yourdomain.com
DocumentRoot /var/www/html
<pre class='brush:php;toolbar:false;'> SSLEngine on
SSLCertificateFile /etc/ssl/certs/your_domain.crt
SSLCertificateKeyFile /etc/ssl/private/your_domain.key
SSLCACertificateFile /etc/ssl/certs/ca-bundle.crt
<Directory "/var/www/html">
AllowOverride All
Require all granted
</Directory>
</VirtualHost></IfModule>
启用SSL站点并重启Apache:
sudo a2ensite default-ssl sudo systemctl reload apache2
编辑Nginx服务器块配置文件(如 /etc/nginx/sites-available/your_site):
server {
listen 443 ssl http2;
server_name yourdomain.com;
<pre class='brush:php;toolbar:false;'>ssl_certificate /etc/ssl/certs/your_domain.crt;
ssl_certificate_key /etc/ssl/private/your_domain.key;
ssl_trusted_certificate /etc/ssl/certs/ca-bundle.crt;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}}
如果需要强制HTTP跳转HTTPS,添加另一个server块:
server {
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}
测试配置并重载Nginx:
sudo nginx -t sudo systemctl reload nginx
Let's Encrypt提供免费、自动化的SSL证书。使用Certbot工具一键部署:
sudo apt install certbot python3-certbot-nginx # Nginx # 或 sudo apt install certbot python3-certbot-apache # Apache
为Nginx自动生成并配置证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
对于Apache:
sudo certbot --apache -d yourdomain.com
Certbot会自动完成证书申请、配置HTTPS和设置自动续期(通过cron或systemd timer),有效期为90天。
基本上就这些。只要证书文件路径正确、权限设置合理(私钥应为600权限),Web服务器就能正常启用HTTPS。定期检查证书到期时间,确保服务不中断。使用在线工具如SSL Labs的SSL Test可验证配置安全性。整个过程不复杂但容易忽略细节,比如中间证书合并或协议版本限制。
以上就是Linux中如何配置SSL证书_LinuxSSL证书配置的完整指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号