在 Ubuntu 上给 Apache2 配置 SSL(HTTPS),通常有两种常见方式:
sudo apt update
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d example.com -d www.example.com
把
example.com换成你的域名
Certbot 会:
sudo certbot renew --dry-run
sudo openssl req -x509 -nodes -days 365 \
-newkey rsa:2048 \
-keyout /etc/ssl/private/apache-selfsigned.key \
-out /etc/ssl/certs/apache-selfsigned.crt
sudo a2enmod ssl
sudo systemctl restart apache2
sudo nano /etc/apache2/sites-available/ssl.conf
示例配置:
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
<Directory /var/www/html>
AllowOverride All
</Directory>
</VirtualHost>
sudo a2ensite ssl.conf
sudo systemctl reload apache2
在 /etc/apache2/sites-available/000-default.conf 中添加:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
然后:
sudo systemctl reload apache2
sudo ufw allow 443
sudo ufw allow 80
如果你愿意,可以告诉我:
我可以给你更精确的步骤。