使用.htaccess和.htpasswd为网站设置密码保护 - 错误 -“无法打开密码文件:”

3
我们通过Charter的Web托管使用Plesk网站生成器来创建一个简单的开发网站。
我正在尝试使用.htaccess / .htpasswd密码保护该网站。
无论我做什么,我都会收到以下错误提示:
[Wed Apr 06 09:02:57 2016] [error] [client 66.169.84.65] (2)No such file or directory: Could not open password file: /.htpasswd
[Wed Apr 06 09:02:57 2016] [error] [client 66.169.84.65] access to / failed, reason: verification of user id 'heather' not configured
[Wed Apr 06 09:03:52 2016] [error] [client 66.169.84.65] (2)No such file or directory: Could not open password file: /etc/httpd/.htpasswd
[Wed Apr 06 09:03:52 2016] [error] [client 66.169.84.65] access to / failed, reason: verification of user id 'admin' not configured
[Wed Apr 06 09:03:56 2016] [error] [client 66.169.84.65] (2)No such file or directory: Could not open password file: /etc/httpd/.htpasswd
[Wed Apr 06 09:03:56 2016] [error] [client 66.169.84.65] access to / failed, reason: verification of user id 'heather' not configured
[Wed Apr 06 09:05:11 2016] [error] [client 66.169.84.65] (2)No such file or directory: Could not open password file: /etc/httpd/HTTP/.htpasswd
[Wed Apr 06 09:05:11 2016] [error] [client 66.169.84.65] access to / failed, reason: verification of user id 'admin' not configured
[Wed Apr 06 09:05:15 2016] [error] [client 66.169.84.65] (2)No such file or directory: Could not open password file: /etc/httpd/HTTP/.htpasswd
[Wed Apr 06 09:05:15 2016] [error] [client 66.169.84.65] access to / failed, reason: verification of user id 'heather' not configured

我不太熟悉Parallels Plesk,而且联系他们的支持就像拔牙一样困难。

  • 我已经检查了.htpasswd文件的路径,并进行了双重检查;看起来是正确的。
  • 我已经将它和.htaccess文件移动到其他位置,看看是否放错了地方。

我不确定问题出在哪里。我收到了身份验证框(但我注意到它没有我的“请登录”消息......

enter image description here

以下是.htaccess文件的代码:

#
#           AUTHENTICATION
#
### BASIC PASSWORD PROTECTION
AuthUserFile /etc/httpd/HTTP/.htpasswd
AuthName "Please Login"
AuthType basic

<Limit GET POST>
Require valid-user
</Limit>

非常感谢您的帮助或提供正确的方向。提前致谢!

6个回答

3

只需使用以下命令生成 /etc/httpd/HTTP/.htpasswd 文件:

# htpasswd /etc/httpd/HTTP/.htpasswd UserNameYouWant

它会要求您输入密码,并生成一个名为UserNameYouWant的用户密码文件。


3
你确定在 /etc/httpd/HTTP/ 目录下有 .htpasswd 文件吗?如果没有,请使用以下命令创建一个。
sudo htpasswd -c /etc/apache2/.htpasswd heather

点击这里查看逐步配置说明:


3

如果您使用的是 CentOS,那么如果该文件位于 Web 服务器根目录之外,则可能会被 SELinux 阻止访问(这就是发生在我身上的情况)。

解决方案

将文件移动到 Web 服务器根目录,然后使用以下命令恢复文件的 SELinux 上下文:

mv /path/to/.httpasswd /var/www/html/
restorecon -v /var/www/html/.htpasswd

2

如果您认为文件已放置在正确的位置,则可能是权限问题。

请使用以下命令检查文件所有权和权限:

ls -la /path/to/file

请确保您的Apache进程运行的文件/组能够读取该文件。

您可以通过运行以下命令来检查进程的所有者:

ps -ef | grep apache

在最左侧的列中,您应该看到进程的所有者。

如果文件所有权是问题,则使用chown更改所有权:

sudo chown username: /path/to/file

您需要以root/sudo身份执行此操作,因为非root用户通常没有更改所有权的权限。

用户名后面的 : 将组更改为与该用户名相同


-1

我遇到了同样的问题。对我来说,.htpasswd 存储在 /home/username/.htpasswds 目录下。我通过递归地确保该目录可执行来解决了这个错误:

chmod +x -R /home/username/.htpasswds

这个解决方案安全吗?没有副作用吗? - Thamer
这也会使文件可执行,这是不可取的。 - Rumbles

-2

我在一台Rackspeed服务器上遇到了同样的问题。原因是/home/company/.htpasswds/project/目录下缺少passwd文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接