在 Linux 上为 Apache 配置 HTTPS 需要以下几个关键步骤,我将分步详细说明:
# Debian/Ubuntu
sudo apt update
sudo apt install apache2 openssl certbot python3-certbot-apache
# RHEL/CentOS
sudo yum install httpd mod_ssl openssl certbot python3-certbot-apache
sudo certbot --apache
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/ssl/private/apache-selfsigned.key \
-out /etc/ssl/certs/apache-selfsigned.crt
/etc/ssl/
目录。# Debian/Ubuntu
sudo nano /etc/apache2/sites-available/default-ssl.conf
# RHEL/CentOS
sudo nano /etc/httpd/conf.d/ssl.conf
修改或确认以下关键参数:
<VirtualHost *:443>
ServerName your-domain.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/your-domain.com/fullchain.pem # Let's Encrypt 证书路径
SSLCertificateKeyFile /etc/letsencrypt/live/your-domain.com/privkey.pem # 私钥路径
# 如果是自签名证书,替换为:
# SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
# SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
</VirtualHost>
sudo a2enmod ssl # 启用 SSL 模块
sudo a2ensite default-ssl # 启用 SSL 站点
sudo systemctl restart apache2
sudo systemctl enable --now httpd
sudo systemctl restart httpd
编辑 HTTP 虚拟主机配置文件(如 /etc/apache2/sites-available/000-default.conf
),添加重定向规则:
<VirtualHost *:80>
ServerName your-domain.com
Redirect permanent / https://your-domain.com/
</VirtualHost>
重启 Apache:
sudo systemctl restart apache2
https://your-domain.com
,确认锁图标正常。bash
sudo apache2ctl configtest # 检查语法
openssl s_client -connect your-domain.com:443 -servername your-domain.com
SSLCertificateFile
和 SSLCertificateKeyFile
路径正确。bash
sudo ufw allow 443/tcp # Debian/Ubuntu
sudo firewall-cmd --add-service=https --permanent && firewall-cmd --reload # RHEL/CentOS
sudo apache2ctl -M | grep ssl
确认 ssl_module
已加载。通过以上步骤,你的 Apache 将成功启用 HTTPS。对于生产环境,推荐使用 Let's Encrypt 自动管理证书,既免费又安全。