在 CentOS 系统中,我们通常使用 Apache HTTP 服务器(httpd)而不是 Apache2
sudo vi /etc/httpd/conf/httpd.conf
<Directory> 标签。这个标签用于定义目录访问权限。例如,如果你想限制对名为 “example” 的目录的访问,你可以这样配置:<Directory "/var/www/example">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory> 标签中的配置以满足你的访问控制需求。以下是一些常用的访问控制指令:Options:设置目录选项,如 FollowSymLinks 或 Indexes。AllowOverride:设置是否允许 .htaccess 文件覆盖主配置文件中的设置。可选值有 None、FileInfo、AuthConfig 和 All。Require:设置访问权限。可选值有 all granted、all denied、user1 user2(指定用户)和 ip1 ip2(指定 IP 地址)等。sudo htpasswd -c /etc/httpd/.htpasswd username
这会提示你输入并确认新用户的密码。-c 参数表示创建一个新的 .htpasswd 文件。如果你已经有一个 .htpasswd 文件,可以省略 -c 参数。
<Directory> 标签中使用 Require user 指令来限制访问:<Directory "/var/www/example">
Options FollowSymLinks
AllowOverride None
Require user username
</Directory>
将 username 替换为你在第 4 步中创建的用户名。
保存并关闭配置文件。
重启 Apache 服务以使更改生效:
sudo systemctl restart httpd
现在,你的 Apache 服务器应该根据你的配置文件设置来限制对特定目录的访问。