温馨提示×

centos apache怎么加密

小樊
40
2026-09-18 02:52:16
栏目: 网络安全

在 CentOS 上给 Apache 做“加密”,通常分为 两种场景,最常见的是 HTTPS(SSL/TLS 加密),也可能是 目录/文件访问加密(密码保护)。我分别说明:


一、Apache 启用 HTTPS(SSL/TLS 加密,最常用)

1. 安装必要组件

yum install httpd mod_ssl openssl -y

2. 生成证书(自签名示例)

openssl req -new -x509 -days 365 -nodes \
-keyout /etc/pki/tls/private/apache.key \
-out /etc/pki/tls/certs/apache.crt

按提示填写信息即可。

如果是生产环境,建议使用 Let’s Encrypt 免费证书

3. 配置 Apache SSL

编辑或新建:

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

确保关键配置:

SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/apache.crt
SSLCertificateKeyFile /etc/pki/tls/private/apache.key

4. 放行防火墙

firewall-cmd --permanent --add-service=https
firewall-cmd --reload

5. 启动 Apache

systemctl restart httpd

访问:

https://你的服务器IP

二、Apache 目录密码加密(Basic Auth)

1. 创建密码文件

htpasswd -c /etc/httpd/.htpasswd user1

2. 配置 Apache

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

3. 重启 Apache

systemctl restart httpd

三、Let’s Encrypt(推荐生产环境)

yum install epel-release -y
yum install certbot python2-certbot-apache -y
certbot --apache

自动配置 HTTPS 并续期。


你可以告诉我:

  • HTTPS 加密 还是 目录密码加密
  • CentOS 版本(6 / 7 / 8 / Stream)
  • 是否有域名

我可以给你 精确配置文件示例

0