温馨提示×

ubuntu apache如何加密

小樊
35
2026-10-22 17:15:50
栏目: 网络安全

在 Ubuntu 上,Apache 的“加密”通常分为几种常见场景。下面按最常见 → 进阶给你说明,你可以按需选择。


一、最常用:HTTPS 加密(SSL/TLS)

这是网站访问加密,防止数据被窃听。

1. 安装 Apache 和 SSL 模块

sudo apt update
sudo apt install apache2
sudo a2enmod ssl
sudo systemctl restart apache2

2. 使用 Let’s Encrypt 免费证书(推荐)

sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com

✅ 自动配置 HTTPS
✅ 自动续期

3. 强制 HTTPS(可选但推荐)

sudo a2enmod rewrite

在站点配置中:

<VirtualHost *:80>
    ServerName example.com
    RewriteEngine On
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

二、使用自签名证书(测试用)

sudo openssl req -x509 -nodes -days 365 \
  -newkey rsa:2048 \
  -keyout /etc/ssl/private/apache-selfsigned.key \
  -out / /etc/ssl/certs/apache-selfsigned.crt

然后配置:

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
    SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
</VirtualHost>

⚠️ 浏览器会警告,仅适合内网/测试


三、目录加密(访问密码)

如果你说的是访问网页需要输密码:

1. 创建密码文件

sudo apt install apache2-utils
sudo htpasswd -c /etc/apache2/.htpasswd user1

2. 配置目录保护

<Directory /var/www/html/secret>
    AuthType Basic
    AuthName "Restricted"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
</Directory>

四、文件/磁盘级加密(系统层)

如果你说的是服务器数据安全:

  • 磁盘加密:LUKS
  • 文件加密:GPG / encfs
  • 数据库:MySQL/PostgreSQL 自带加密

五、简单总结

需求 方法
网站 HTTPS Let’s Encrypt
内网测试 自签名证书
页面访问密码 htpasswd
服务器数据 LUKS 磁盘加密

如果你能说清楚:

  • 是 网站访问加密 还是 文件/目录加密
  • 是 公网还是内网
  • Ubuntu 版本(20.04 / 22.04 / 24.04)

我可以给你更精确的配置示例。

0 踩