Ubuntu 上 Apache 配置 SSL 的完整步骤
一 准备与前置检查
sudo apt update && sudo apt install apache2。如使用防火墙,放行 80/443:sudo ufw allow 80,443/tcp && sudo ufw enable。为自动续期 HTTPS,建议安装 Certbot:sudo apt install certbot python3-certbot-apache。二 手动部署自有证书
sudo mkdir -p /etc/apache2/sslsudo cp YourDomainName_public.crt /etc/apache2/ssl/sudo cp YourDomainName_chain.crt /etc/apache2/ssl/sudo cp YourDomainName.key /etc/apache2/ssl/sudo a2enmod ssl/etc/apache2/ports.conf 是否包含 Listen 443(如未包含可手动添加)<VirtualHost *:443>ServerName example.comDocumentRoot /var/www/example.com/public_htmlSSLEngine onSSLCertificateFile /etc/apache2/ssl/YourDomainName_public.crtSSLCertificateKeyFile /etc/apache2/ssl/YourDomainName.keySSLCertificateChainFile /etc/apache2/ssl/YourDomainName_chain.crt<Directory /var/www/example.com/public_html> ... Require all granted </Directory>sudo a2ensite example.com-ssl.confsudo apache2ctl configtest && sudo systemctl reload apache2三 使用 Let’s Encrypt 自动获取并配置
sudo apt install certbot python3-certbot-apachesudo certbot --apache -d example.com -d www.example.comsudo certbot renew --dry-run四 常见问题与优化
/etc/apache2/ports.conf 包含 Listen 443。sudo apache2ctl configtest,无误再 sudo systemctl reload apache2。sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt