温馨提示×

centos中apache2怎样加密

小樊
40
2026-10-11 20:11:11
栏目: 网络安全

在 CentOS 里,Apache 通常叫 httpd(不是 apache2,apache2 是 Debian/Ubuntu 的叫法)。下面按最常见的“网站 HTTPS 加密”来说明,如果你指的是目录密码加密或通信内容加密,我后面也补充。


一、使用 HTTPS 对网站加密(最常用)

1. 安装 Apache

yum install httpd mod_ssl -y
systemctl enable httpd
systemctl start httpd

mod_ssl 提供 HTTPS 支持。


2. 获取 SSL 证书

方式一:自签名证书(测试用)

openssl req -x509 -nodes -days 365 \
  -newkey rsa:2048 \
  -keyout /etc/pki/tls/private/server.key \
  -out /etc/pki/tls/certs/server.crt

方式二:Let’s Encrypt(推荐,免费)

yum install certbot python2-certbot-apache -y
certbot --apache

Certbot 会自动:

  • 申请证书
  • 配置 HTTPS
  • 设置自动续期

3. 配置 HTTPS(手动方式)

编辑配置文件:

vi /etc/httpd/conf.d/ssl.conf

关键配置:

<VirtualHost *:443>
    ServerName www.example.com
    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/server.crt
    SSLCertificateKeyFile /etc/pki/tls/private/server.key
</VirtualHost>

重启 Apache:

systemctl restart httpd

4. 强制 HTTP 跳转到 HTTPS(可选)

<VirtualHost *:80>
    ServerName www.example.com
    Redirect permanent / https://www.example.com/
</VirtualHost>

二、目录加密(用户名密码)

如果你指的是访问某个目录需要密码:

1. 创建密码文件

htpasswd -c /etc/httpd/.htpasswd user1

2. 配置目录

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

三、总结

需求 方法
网站 HTTPS 加密 mod_ssl + 证书
免费证书 Let’s Encrypt
目录访问加密 .htpasswd
CentOS Apache 名 httpd

如果你说的“加密”是:

  • 数据库通信
  • 内部接口
  • 特定文件加密

可以告诉我具体场景,我给你更精确的方案。

0 踩