Password Protect a Directory in Apache 2

Filed Under (Web Development) by manatarms on 29-12-2008

Tagged Under : ,

For anyone who wants to do this, the steps are as follows:

  • Edit /etc/apach2/sites-available/default
  • Look for the line “AllowOverride” in the section <Directory /var/www>
  • Change the line from “AllowOverride None” to “AllowOverride All”
  • Change to the directory you wish to password protect
  • Create a .htaccess file. The contents of mine is shown below. The only truly important line is AuthUserFile. This parameter should contain the path to the file containing your usernames/passwords. You should be able to figure out what everything else is on your own.
  • Create the .htpasswd file in the directory of your choosing with the following command:
1
sudo htpasswd -c /path/to/.htpasswd username

My .htaccess file:

1
2
3
4
5
6
7
AuthUserFile /var/www/path/to/site/.htpasswd
AuthName "Please Log In"
AuthGroupFile /dev/null
AuthType Basic
<Limit GET POST PUT>
    Require valid-user
<Limit>

Leave a Reply