温馨提示×

centos apache如何加密

小樊
38
2026-09-11 21:09:08
栏目: 网络安全

在 CentOS 上给 Apache 做“加密”,通常有两种常见含义,我分别说明,你可以按需选择:


一、HTTPS 加密(最常用)

目的:让网站通过 https:// 访问,数据传输加密

1. 安装 Apache(如未安装)

yum install httpd -y
systemctl start httpd
systemctl enable httpd

2. 安装 mod_ssl

yum install mod_ssl -y

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

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

certbot --apache -d yourdomain.com

按提示操作,证书会自动配置

4. 或修改 SSL 配置文件(手动证书)

编辑:

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

关键配置:

SSLEngine on
SSLCertificateFile /path/to/cert.pem
SSLCertificateKeyFile /path/to/privkey.pem
SSLCertificateChainFile /path/to/chain.pem

重启 Apache:

systemctl restart httpd

二、.htaccess 密码加密(访问保护)

目的:访问目录时需要账号密码

1. 创建密码文件

htpasswd -c /etc/httpd/.htpasswd user1

2. 配置目录保护

在网站目录创建 .htaccess

AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user

3. 允许 .htaccess

编辑 Apache 配置:

<Directory "/var/www/html">
    AllowOverride All
</Directory>

重启:

systemctl restart httpd

三、强制 HTTP 跳转 HTTPS(可选)

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

如果你能说明:

  • 网站传输加密 还是 目录密码保护
  • CentOS 版本(6 / 7 / 8)
  • 是否有域名

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

0