在 Debian 上给 Apache 做“加密”,通常分为两种情况:
下面分别说明。
sudo apt update
sudo apt install apache2
sudo a2enmod ssl
sudo systemctl restart apache2
sudo apt install certbot python3-certbot-apache
sudo certbot --apache
按提示输入域名,证书会自动配置。
sudo mkdir /etc/apache2/ssl
sudo openssl req -x509 -nodes -days 365 \
-newkey rsa:2048 \
-keyout /etc/apache2/ssl/apache.key \
-out /etc/apache2/ssl/apache.crt
编辑或新建:
sudo nano /etc/apache2/sites-available/example.conf
示例:
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key
</VirtualHost>
启用站点:
sudo a2ensite example.conf
sudo systemctl reload apache2
sudo apt install apache2-utils
sudo htpasswd -c /etc/apache2/.htpasswd user1
<Directory /var/www/html/secret>
AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
sudo systemctl reload apache2
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
如果你能说明是 网站加密、接口加密、还是目录保护,我可以给你更精确的配置。