使用 .htaccess 和 .htpasswd 密码保护网站 - 错误 - "Could not open password file:"

password protect website with .htaccess and .htpasswd - error - "Could not open password file:"

我们通过 Charter 的虚拟主机拥有 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 不是很熟悉 - 获得他们的支持就像拔牙一样。

我不确定是什么问题。我收到了身份验证框(我注意到它没有我的 "Please Login" 消息...

这里是 .htaccess 文件的代码:

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

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

非常感谢任何帮助或引导正确的方向。提前致谢!!

只需通过命令生成/etc/httpd/HTTP/.htpasswd文件:

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

它要求您输入密码并使用用户 UserNameYouWant

生成密码文件

您确定 .htpasswd 文件在 /etc/httpd/HTTP/ 目录中吗?否则使用以下命令创建 1

sudo htpasswd -c /etc/apache2/.htpasswd heather

Here你去一步步配置:

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

检查文件所有权和权限:

ls -la /path/to/file

确保您的 apache 进程运行的 file/group 能够读取该文件。

您可以通过运行查看进程的所有者:

ps -ef | grep apache

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

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

sudo chown username: /path/to/file

您需要以 root/sudo 身份执行此操作,因为非根用户无权正常更改所有权。

: 用户名改组后与用户名相同

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

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

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

解决方案

将文件移动到您的网络服务器根目录,然后使用这些命令恢复文件 SELinux 上下文:

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

我在 Rackspeed 服务器上遇到了同样的问题。这是因为文件 passwd/home/company/.htpasswds/project/

中丢失