如何在 Apache httpd 虚拟主机中配置基本身份验证?

Jad*_*ias 53 httpd authentication mercurial http-authentication apache-2.2

我正在尝试使用 Apache http 配置 mercurial 访问。它需要身份验证。我的/etc/apache2/sites-enabled/mercurial看起来像这样:

NameVirtualHost *:8080

<VirtualHost *:8080>
    UseCanonicalName Off
    ServerAdmin  webmaster@localhost
    AddHandler cgi-script .cgi
    ScriptAliasMatch ^(.*) /usr/lib/cgi-bin/hgwebdir.cgi/$1
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

我在互联网上阅读的每个教程都告诉我插入这些行:

AuthType Basic
AuthUserFile /usr/local/etc/httpd/users
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,我收到以下错误:

# /etc/init.d/apache2 reload
Syntax error on line 8 of /etc/apache2/sites-enabled/mercurial:
AuthType not allowed here
Run Code Online (Sandbox Code Playgroud)

我的发行版是一个定制的 Ubuntu,称为 Turnkey Linux Redmine

小智 80

你应该把它放在一个 Location 指令中:

<VirtualHost *:8080>

<Location /> #the / has to be there, otherwise Apache startup fails
            Deny from all
            #Allow from (You may set IP here / to access without password)
            AuthUserFile /usr/local/etc/httpd/users
            AuthName authorization
            AuthType Basic
            Satisfy Any # (or all, if IPs specified and require IP + pass)
                        # any means neither ip nor pass
            require valid-user
</Location>
...
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

  • 为什么`&lt;Location /&gt;` 被编辑为`&lt;Location&gt;`,内部日志消息为“已修复......以避免很多麻烦”,但在答案本身中却没有说明真正的原因?在 Apache 中没有像`&lt;Location&gt;` 指令(即一个*没有*位置的指令)这样的东西。*那*现在肯定会引起麻烦。;)(例如见上文。) (3认同)

小智 11

我在 ubuntu 10.04 上运行 Apache2 — 同样的问题,感谢您的解决方案。我发现我必须把配置放进去/etc/apache2/apache2.conf

您可以使用 htpasswd 生成用户名和密码。新文件:

$ htpasswd -c /srv/auth/.htpasswd squire
Run Code Online (Sandbox Code Playgroud)

要附加到现有文件:

$ htpasswd -b /srv/auth/.htpasswd squire2 tickleme2
Run Code Online (Sandbox Code Playgroud)


Dan*_*tta 8

您可以保护位置或目录。对于目录添加如下内容:

<Directory /some/dir/cgi-bin/>
    Options +ExecCGI
    AddHandler cgi-script .cgi
    AuthType Basic
    AuthName 'Private scripts'
    AuthUserFile '/some/other/dir/.htpasswd'
    Require valid-user
</Directory>
Run Code Online (Sandbox Code Playgroud)

您还可以添加DenyAllow指令以进行更精细的控制。