下面以 Ubuntu/Debian 为例,说明 Apache2 配置 SSL 证书(HTTPS) 的完整流程。如果你用的是 CentOS/RHEL,命令会略有不同,我也可以单独给你说明。
通常你会有以下文件之一:
自动申请 + 自动续期,免费。
一般有:
example.com.crtexample.com.keyca_bundle.crtsudo apt update
sudo apt install apache2
sudo a2enmod ssl
sudo systemctl restart apache2
✅ 启用 SSL 模块
sudo mkdir -p /etc/apache2/ssl
将证书上传到该目录,例如:
/etc/apache2/ssl/
├── example.com.crt
├── example.com.key
└── ca_bundle.crt
⚠️ 私钥权限必须严格
sudo chmod 600 /etc/apache2/ssl/example.com.key
sudo nano /etc/apache2/sites-available/example-ssl.conf
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/example.com.crt
SSLCertificateKeyFile /etc/apache2/ssl/example.com.key
SSLCertificateChainFile /etc/apache2/ssl/ca_bundle.crt
<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
✅ 如果是 Let’s Encrypt,路径通常是:
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
sudo a2ensite example-ssl.conf
sudo systemctl reload apache2
编辑 HTTP 配置:
sudo nano /etc/apache2/sites-available/000-default.conf
添加:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
然后:
sudo systemctl reload apache2
https://example.com
ss -lntp | grep 443
sudo ufw allow 443/tcp
apache2ctl configtest
如果你还没证书,可以直接用:
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com
✅ 自动配置 + 自动续期
如果你愿意,可以告诉我:
我可以给你 完全针对你环境的配置方案。