在 Debian 上配置 LAMP(Linux + Apache + MySQL/MariaDB + PHP) 并使用 SSL(HTTPS),通常有两种常见方式:
下面分别说明。
sudo apt update
sudo apt upgrade -y
sudo apt install apache2 mariadb-server php libapache2-mod-php php-mysql -y
example.com)已解析到服务器 IPsudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d example.com -d www.example.com
按提示输入邮箱、同意条款
推荐选择 Redirect(强制 HTTPS)
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
sudo a2enmod ssl
sudo a2ensite default-ssl
编辑配置文件:
sudo nano /etc/apache2/sites-available/default-ssl.conf
修改以下两行:
SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
sudo systemctl restart apache2
访问:
https://你的服务器IP
(浏览器会提示不安全,属正常)
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
<Directory /var/www/html>
AllowOverride All
</Directory>
</VirtualHost>
启用站点:
sudo a2ensite example.conf
sudo systemctl reload apache2
ufw allow 443在 .htaccess 或 Apache 配置中添加:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
如果你需要:
可以告诉我你的具体环境,我可以进一步细化。