我如何密码保护一个Apache网站?

我添加了.htaccess

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

和.htpasswd

用户名密码

但是,它不工作。 它不会提示我login。

你的.htaccess文件看起来不错。 它不工作的唯一原因就是Apacheconfiguration为忽略它。

在你的apacheconfiguration文件中,最好在sites- * config文件中添加:

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

AllowOverride指令允许htaccess文件起作用。 默认情况下,大多数Apache设置已closures(实际设置为“ 无” ),包括Ubuntu / Debian。

.htpasswd文件中,密码必须encryption。 您可以使用mezganibuild议的htpasswd工具轻松完成此操作。 它看起来像这样,如果你阅读文件:

 bob:abJnggxhB/yWI 

通过将AllowOverride指令设置为至lessAuthConfig ,您的.htaccess文件就位,并且您的.htpasswd包含具有encryption密码的用户,您应该很好。

首先,将以下内容添加到您的apacheconfiguration中:

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

之后,您可以使用htpasswd命令创build包含您的login名和密码的.htpasswd文件,而不是手动

 htpasswd -c /path/to/the/directory/you/are/protecting/.htpasswd username 

我认为将.htpasswd文件移动到与您正在保护的目录path不同的地方会更安全。

此外,出于安全原因,请阅读这篇关于securityfocus的文章

在Snow Leopard(OS X 10.6)中,更改httpd.conf中的AllowOverride仅在主网页(/ Library / WebServer)中启用.htaccess保护。 为单个用户的〜/站点文件夹启用它由每个用户的单独的.conf文件控制。 在我的系统上这是在/ etc / apache / users下。 杰夫·斯奈德(Jeff Snider)对“站点中的*”的引用可能是在他的系统上configuration了个人用户页面的文件的地方。