Apache服务器部署SSL证书详细步骤
一、准备工作
1.1 所需文件准备
证书文件:your_domain.crt(或 server.crt)
私钥文件:your_domain.key(或 server.key)
中间证书:intermediate.crt(CA证书,可选但建议包含
1.2 安装必要模块
bash
Ubuntu/Debian系统
sudo apt update
sudo apt install apache2 openssl
sudo a2enmod ssl
sudo a2enmod headers
CentOS/RHEL系统
sudo yum install httpd mod_ssl openss
二、证书文件放置
bash
创建SSL证书目录
sudo mkdir -p /etc/apache2/ssl # Ubuntu/Debian
或
sudo mkdir -p /etc/httpd/ssl # CentOS/RHEL
复制证书文件到相应位置
sudo cp your_domain.crt /etc/apache2/ssl/
sudo cp your_domain.key /etc/apache2/ssl/
sudo cp intermediate.crt /etc/apache2/ssl/
# 设置正确的权限
sudo chmod 600 /etc/apache2/ssl/your_domain.key
sudo chmod 644 /etc/apache2/ssl/your_domain.crt
sudo chown root:root /etc/apache2/ssl/your_domain.key
三、Apache配置
3.1 配置SSL虚拟主机
创建或编辑配置文件:
Ubuntu/Debian:**
bash
sudo nano /etc/apache2/sites-available/your_domain-ssl.conf
CentOS/RHEL
bash
sudo nano /etc/httpd/conf.d/ssl.conf
3.2 SSL配置文件示例
apache
<VirtualHost *:443>
ServerName www.your_domain.com
ServerAlias your_domain.com
DocumentRoot /var/www/html
# SSL配置
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/your_domain.crt
SSLCertificateKeyFile /etc/apache2/ssl/your_domain.key
# 如果有中间证书
SSLCertificateChainFile /etc/apache2/ssl/intermediate.crt
# 或者使用这个(新版本Apache)
# SSLCACertificateFile /etc/apache2/ssl/intermediate.crt
# 日志文件
ErrorLog ${APACHE_LOG_DIR}/ssl_error.log
CustomLog ${APACHE_LOG_DIR}/ssl_access.log combined
# 安全增强设置
SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLHonorCipherOrder on
SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
SSLCompression off
# HSTS(强制HTTPS)
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
# 其他配置...
</VirtualHost>
3.3 启用站点(Ubuntu/Debian)
bash
启用SSL站点
sudo a2ensite your_domain-ssl.conf
禁用默认HTTP站点(可选)
sudo a2dissite 000-default.conf
测试配置
sudo apache2ctl configtest
四、HTTP重定向到HTTPS
4.1 方法一:使用.htaccess(需要启用mod_rewrite)
在网站根目录创建或编辑`.htaccess`文件:
apache
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
4.2 方法二:虚拟主机配置中重定向
创建HTTP虚拟主机配置文件:
apache
<VirtualHost *:80>
ServerName www.your_domain.com
ServerAlias your_domain.com
# 重定向到HTTPS
Redirect permanent / https://www.your_domain.com/
# 或者使用Rewrite规则
# RewriteEngine On
# RewriteCond %{HTTPS} off
# RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>
五、重启Apache服务
bash
Ubuntu/Debian
sudo systemctl restart apache2
CentOS/RHEL
sudo systemctl restart httpd
查看状态
sudo systemctl status apache2
六、验证安装
6.1 使用OpenSSL验证
bash
openssl s_client -connect your_domain.com:443 -servername your_domain.com
6.2 使用在线工具验证
[SSL Labs SSL Test](https://www.ssllabs.com/ssltest/)
[SSL Checker](https://www.sslshopper.com/ssl-checker.html)
6.3 检查Apache错误日志bash
# Ubuntu/Debian
sudo tail -f /var/log/apache2/ssl_error.log
# CentOS/RHEL
sudo tail -f /var/log/httpd/ssl_error_log
七、常见问题解决
7.1 权限问题
bash
确保私钥文件权限正确
sudo chmod 600 /etc/apache2/ssl/your_domain.key
7.2 证书链不完整
bash
合并证书文件(如果需要)
cat your_domain.crt intermediate.crt > combined.crt
然后使用combined.crt作为SSLCertificateFile
7.3 配置语法错误
bash
测试配置
sudo apachectl configtest
查看详细错误
sudo apachectl -t -D DUMP_VHOSTS
7.4 SELinux问题(CentOS/RHEL)
bash
查看SELinux上下文
ls -Z /etc/httpd/ssl/
设置正确的上下文
sudo semanage fcontext -a -t httpd_cert_t "/etc/httpd/ssl(/.*)?"
sudo restorecon -Rv /etc/httpd/ssl/
八、自动续签配置(Let's Encrypt)
8.1 安装Certbot
bash
Ubuntu/Debian
sudo apt install certbot python3-certbot-apache
CentOS/RHEL 8+
sudo dnf install certbot python3-certbot-apache
8.2 获取并安装证书
bash
sudo certbot --apache -d your_domain.com -d www.your_domain.com
8.3 测试自动续签
bash
tbot renew --dry-run
九、性能优化建议
apache
启用HTTP/2
Protocols h2 http/1.1
启用SSL会话缓存
SSLSessionCache shmcb:/var/run/apache2/ssl_scache(512000)
SSLSessionCacheTimeout 300
OCSP Stapling
SSLUseStapling on
SSLStaplingCache shmcb:/var/run/ocsp(128000)
十、安全配置检查清单
1. ✅ 使用TLS 1.2或更高版本
2. ✅ 禁用弱密码套件
3. ✅ 启用HSTS
4. ✅ 配置正确的证书链
5. ✅ 私钥文件权限设置为600
6. ✅ 定期更新证书
7. ✅ 监控SSL/TLS漏洞
8. ✅ 配置合适的加密套件
完成以上步骤后,用户的Apache服务器应该已成功配置SSL证书,并通过HTTPS安全地提供服务。