是的,Ubuntu Apache2 可以支持 HTTPS
sudo apt update
sudo apt install apache2
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache-selfsigned.key -out /etc/apache2/ssl/apache-selfsigned.crt
按照提示输入相关信息。这将生成一个自签名证书(apache-selfsigned.crt
)和一个私钥(apache-selfsigned.key
)。
sudo mkdir -p /etc/apache2/ssl
sudo mv /etc/apache2/ssl/apache-selfsigned.key /etc/apache2/ssl/
sudo mv /etc/apache2/ssl/apache-selfsigned.crt /etc/apache2/ssl/
/etc/apache2/sites-available/000-default.conf
。在 <VirtualHost *:80>
部分添加以下内容:RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
这将强制将所有 HTTP 请求重定向到 HTTPS。
<VirtualHost *:443>
部分添加以下内容(用你的证书和私钥文件路径替换 /etc/apache2/ssl/apache-selfsigned.key
和 /etc/apache2/ssl/apache-selfsigned.crt
):<VirtualHost *:443>
ServerAdmin webmaster@localhost
ServerName yourdomain.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache-selfsigned.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache-selfsigned.key
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
sudo systemctl restart apache2
现在,你的 Ubuntu Apache2 服务器应该已经支持 HTTPS 了。请注意,由于我们使用的是自签名证书,浏览器可能会警告你连接不安全。在实际生产环境中,建议使用由可信证书颁发机构颁发的证书。