Set Apache Password Protected Directories With .htaccess File

by Vivek Gite on January 26, 2006 · 21 comments

Q. How do I protecting a directory in Apache on linux?

A. There are many ways you can password protect directories under Apache web server. This is important to keep your file privates from both unauthorized users and search engines (when you do not want to get your data indexed). Here you will see the basics of password protecting a directory on your server. You can use any one of the following method:

  1. Putting authentication directives in a <Directory> section, in your main server configuration httpd.conf file, is the preferred way to implement this kind of authentication.
  2. If you do not have access to Apache httpd.conf file (for example shared hosting) then with the help of file called .htaccess you can create password protect directories. .htaccess file provide a way to make configuration changes on a per-directory basis.

In order to create apache password protected directories you need:

  • Password file
  • And Directory name which you would like to password protect (/var/www/docs)

Step # 1: Make sure Apache is configured to use .htaccess file

You need to have AllowOverride AuthConfig directive in httpd.conf file in order for these directives to have any effect. Look for DocumentRoot Directory entry. In this example, our DocumentRoot directory is set to /var/www. Therefore, my entry in httpd.conf looks like as follows:

<Directory /var/www>
Options Indexes Includes FollowSymLinks MultiViews
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>

Save the file and restart Apache
If you are using Red Hat /Fedora Linux:

# service httpd restart

If you are using Debian Linux:

# /etc/init.d/apache-perl restart

Step # 2: Create a password file with htpasswd

htpasswd command is used to create and update the flat-files (text file) used to store usernames and password for basic authentication of Apache users. General syntax:
htpasswd -c password-file username

Where,

  • -c : Create the password-file. If password-file already exists, it is rewritten and truncated.
  • username : The username to create or update in password-file. If username does not exist in this file, an entry is added. If it does exist, the password is changed.

Create directory outside apache document root, so that only Apache can access password file. The password-file should be placed somewhere not accessible from the web. This is so that people cannot download the password file:

# mkdir -p /home/secure/

Add new user called vivek

# htpasswd -c /home/secure/apasswords vivek

Make sure /home/secure/apasswords file is readable by Apache web server. If Apache cannot read your password file, it will not authenticate you. You need to setup a correct permission using chown command. Usually apache use www-data user. Use the following command to find out Apache username. If you are using Debian Linux use pache2.conf, type the following command:
# grep -e '^User' /etc/apache2/apache2.conf

Output:

www-data

Now allow apache user www-data to read our password file:
# chown www-data:www-data /home/secure/apasswords
# chmod 0660 /home/secure/apasswords

If you are using RedHat and Fedora core, type the following commands :
# grep -e '^User' /etc/httpd/conf/httpd.conf

Output:

apache

Now allow apache user apache to read our password file:
# chown apache:apache /home/secure/apasswords
# chmod 0660 /home/secure/apasswords

Now our user vivek is added but you need to configure the Apache web server to request a password and tell the server which users are allowed access. Let us assume you have directory called /var/www/docs and you would like to protect it with a password.

Create a directory /var/www/docs if it does not exist:
# mkdir -p /var/www/docs

Create .htaccess file using text editor:
# cd /var/www/docs
# vi .htaccess

Add following text:

AuthType Basic
AuthName "Restricted Access"
AuthUserFile /home/secure/apasswords
Require user vivek

Save file and exit to shell prompt.

Step # 3: Test your configuration

Fire your browser type url http://yourdomain.com/docs/ or http://localhost/docs/ or http://ip-address/docs


When prompted for username and password please supply username vivek and password. You can add following lines to any file <Diretory> entry in httpd.conf file:

AuthType Basic
AuthName "Restricted Access"
AuthUserFile /home/secure/apasswords
Require user vivek

To change or setup new user use htpasswd command again.

Troubleshooting

If password is not accepted or if you want to troubleshoot authentication related problems, open and see apache access.log/error.log files:

Fedora Core/CentOS/RHEL Linux log file location:
# tail -f /var/log/httpd/access_log
# tail -f /var/log/httpd/error_log

Debian Linux Apache 2 log file location:
# tailf -f /var/log/apache2/access.log
# tailf -f /var/log/apache2/error.log

See also:

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 21 comments… read them below or add one }

1 Anonymous August 13, 2006

is there any similar resource for setting this up on windows?

Reply

2 nixcraft August 14, 2006

Do you want information for Apache or IIS server?

Reply

3 alka March 22, 2007

When i provide the username & password, it works fine. But when i try to again access something from the same location in the same browser , it does not promt fot the username & password.
How can i do that?

Reply

4 nixcraft March 22, 2007

alka,

Browser remembers your password/username for current running session. If you close browser it will again prompt for the same.

HTH

Reply

5 Jo June 30, 2007

I want use the Password Protect Directories for add user in automaticly whit a simple FORM PHP … Tanks for your good services :P

Reply

6 MrGroove August 28, 2007

Nice write-up. From the windows server standpoint running apache, check out http://www.groovypost.com/howto/apache/password-protect-apache-website/

Reply

7 jason December 29, 2007

Thank you for great tips.
I did as said in here and it works great.
But, I have a question.
The password length that works is only max 8 char.
In other words, all I have to enter is the first 8 char for the password. After 8 ch, the characters are ignored.
Is there any way to increase the password length?

Thank you very much

Reply

8 vivek December 30, 2007

jason,

You need to change password backend to mysql or ldap.

Reply

9 Mikhail February 29, 2008

Have the same problem with 8-char password, but didn’t found the solution. Is there any way to fix it, please, explain more detailed.

Reply

10 Joshua K August 17, 2008

I’ve gone through these steps on Ubuntu Hardy twice and it still doesn’t work for me. Is there something I’m missing?

Reply

11 Marian Vlad October 9, 2008

try htpasswd -m … ;) (and man htpasswd for more)

Reply

12 robert January 18, 2009

Sweet! Instructions worked great on my freebsd box. I am curious though: Why did you choose put the file in /home/secure/, instead of a etc directory? I followed your recommendation and made /home/secure/ and it works fine. I was just wondering if there was a reason one should not put it in /etc or /usr/local/etc/ ?

Reply

13 sim January 23, 2009

ThanXx very much m8 ..its work great ..

Reply

14 Raymond August 14, 2009

Change directive
Require user vivek
to
Requre valid-user

will allow all users in the password file to access.

Reply

15 3.grosz October 12, 2009

And now I figure it out. Thanks :)

Reply

16 etoshx October 23, 2009

ive followed the above instructions but when i try to open the site its giving me errors like “The website declined to show this webpage ,This website requires you to log in.” this what am getting in the httpd error logs “[error] [client XX.XX.XX.XX] client denied by server configuration: /var/www/html/site1/”

Reply

17 Jose August 18, 2010

Just a quick note to say thanks for the post. I’m developing a website on a new VPS with Rackspace. My existing host provider, HostGator, provides a web interface for configuring .htaccess files in protected directories. However, after I installed the my chosen Linux distro (Ubuntu 10.04) I had to install LAMP. So, basically, I’m working from the command line in a bare bones server.

This post helped me configure password protection on my new bare bones server. Thanks again!

Reply

18 Junior December 4, 2010

awesome!!!! this works so well for me. I made multiply accounts for Secured directory and now are secured. Vivek you are the man thank you!

Reply

19 Junior December 4, 2010

Vivek everything works well on my box now… i made multiplier accoutn for diffrent directory with out a problem…my question is how can now make multiplier user id to access the same directory? I like to make about 5 user id in case one of the users id needs to be deleted or disable etc…

Thank yuo for your help!

Reply

20 JB January 27, 2011

Set up SSL on apache2 Ubuntu. WOrks fine and prompts for password using http: but when you go the web server using https it does not prompt for a password. What configuration setting could be doing this?

Reply

21 Ritesh July 21, 2011

Awesome article. Saved hours for me !

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 2 + 8 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: