Httpd – How to configure basic authentication in Apache httpd virtual hosts

apache-2.2authenticationhttp-authenticationhttpdmercurial

I'm trying to configure mercurial access using Apache http.
It requires authentication. My /etc/apache2/sites-enabled/mercurial looks like this:

NameVirtualHost *:8080

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

Every tutorial I read on the internet tells me to insert these lines:

AuthType Basic
AuthUserFile /usr/local/etc/httpd/users

But when I do it I get the following error:

# /etc/init.d/apache2 reload
Syntax error on line 8 of /etc/apache2/sites-enabled/mercurial:
AuthType not allowed here

My distro is a customized Ubuntu called Turnkey Linux Redmine

Best Answer

You should place this inside a Location directive:

<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>
Related Topic