0

I've enabled Apache user directories on my Ubuntu server. User can now load their /home/username/public_html directory from the browser by visiting http://example.com/~username.

I'm trying to password protect these directories. I was able to get it to work fine using an .htaccess file, but I would prefer to enabled authentication from the Directory context. This is my configuration file for the user directory module, located at /etc/apache2/mods-enabled/userdir.conf:

<IfModule mod_userdir.c>
        UserDir public_html
        UserDir disabled root

        <Directory /home/*/public_html>
                AuthType Basic
                AuthName Private
                Authuserfile /etc/apache2/.htpasswd
                Require valid-user

                Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
                <Limit GET POST OPTIONS>
                        Require all granted
                </Limit>
                <LimitExcept GET POST OPTIONS>
                        Require all denied
                </LimitExcept>
        </Directory>
</IfModule>

When I navigate my browser to one of the user directories, it does not even prompt me for a credentials. When I switch back to the .htaccess method, it works correctly.

I have reloaded apache after every change I make to the user directory configuration file. Why isn't authentication working from the Directory context?

flyingL123
  • 235
  • 3
  • 12
  • 1
    Looks like it's the `` blocks that are overriding my auth directives. – flyingL123 Jun 25 '15 at 20:28
  • Related: https://unix.stackexchange.com/questions/368900/how-to-set-up-authentication-for-the-personal-userdir-in-apache/368920#368920 –  Jun 03 '17 at 00:02

1 Answers1

1

Put the authentication directives to a separate Location directive, like this:

<IfModule mod_userdir.c>
    <Location ~ "/~.*">
        AuthType Basic
        AuthName Private
        Authuserfile /etc/apache2/.htpasswd
        Require valid-user
    </Location>
</IfModule>

This will enable password authentication for every path starting with "/~".

Lacek
  • 6,585
  • 22
  • 28