在 Ubuntu 上通过 Apache2 配置 HTTPS,通常有两种常见方式:
下面分别说明。
sudo apt update
sudo apt install apache2
sudo apt install certbot python3-certbot-apache
确保你的域名(如 example.com)已解析到服务器公网 IP。
sudo a2enmod ssl
sudo a2enmod rewrite
sudo systemctl restart apache2
sudo certbot --apache -d example.com -d www.example.com
按提示选择:
2)sudo certbot renew --dry-run
✅ 配置完成后,访问 https://example.com 即可。
sudo openssl req -x509 -nodes -days 365 \
-newkey rsa:2048 \
-keyout /etc/ssl/private/apache-selfsigned.key \
-out /etc/ssl/certs/apache-selfsigned.crt
按提示填写信息(Common Name 填你的域名或 IP)。
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 a2enmod ssl
sudo a2ensite ssl.conf
sudo systemctl restart apache2
在站点配置中添加:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
然后:
sudo systemctl reload apache2
sudo ufw allow 'Apache Full'
如果你愿意,可以告诉我:
我可以给你更精确的配置示例。