如何用密码保护 Apache 网站?

如何用密码保护 Apache 网站?

我将其添加到我的.htaccess文件中:

AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/the/directory/you/are/protecting/.htpasswd
require valid-user

并将其写入.htpasswd文件:

username:password

但是,它不起作用。它没有提示我登录。

答案1

你的.htaccess文件看起来不错。它无法正常工作的唯一原因是 Apache 已配置为忽略它。

在您的 apache 配置中,最好在 sites-* 配置文件中,添加:

<Directory /path/to/the/directory/you/are/protecting/>
      AllowOverride AuthConfig
</Directory>

允许覆盖指令允许 htaccess 文件运行。默认情况下,大多数 Apache 设置都将其关闭(设置为没有任何实际上),包括 Ubuntu/Debian。

在你的密码文件,密码必须加密。您可以使用 mezgani 建议的 htpasswd 工具最轻松地完成此操作。如果您读取该文件,它将如下所示:

bob:abJnggxhB/yWI

随着允许覆盖指令至少设置为验证配置,您的 .htaccess 文件已放置到位,并且您的 .htpasswd 包含具有加密密码的用户,您就可以开始了。

答案2

首先,将以下内容添加到您的 apache 配置中:


<Directory /path/to/the/directory/you/are/protecting/>
        AllowOverride AuthConfig
        Options Indexes MultiViews FollowSymLinks
        deny from all
        Order deny,allow
</Directory>

之后,你可以使用 htpasswd 命令创建包含登录名和密码的 .htpasswd 文件,而无需手动


htpasswd -c /path/to/the/directory/you/are/protecting/.htpasswd username
我认为将 .htpasswd 文件移动到与要保护的目录路径不同的位置会更安全。

此外,出于安全原因,请阅读此文章安全焦点

答案3

在 Snow Leopard (OS X 10.6) 中,更改 httpd.conf 中的 AllowOverride 只会在主网页(默认情况下为 /Library/WebServer)中启用 .htaccess 保护。为各个用户的 ~/Sites 文件夹启用它由每个用户的单独 .conf 文件控制。在我的系统上,它位于 /etc/apache/users 下。Jeff Snider 提到的“在 sites-* 中”可能是在他的系统上配置各个用户页面的文件的地方。

相关内容