Home

Gallery

Archives

Blog

About

Contact Me

"Personal thoughts, ramblings, and nonsense from Drew, himself."

How-To: Password Protect Apache Directories with .htaccess

Have you ever wanted to let someone download a file or image from your website, but didn’t want to have the whole world know about, such as Google’s great search engine spiders? The robot.txt file only goes so far, personally I’d rather password protect my directories if I don’t want them crawled. It’s a great way to set up realms that only the privileged few know about. I’ll walk you through the very simple steps of doing so, and showing you what gets added on the server side.

Grab Your Tools

We will need a couple of things to allow us to set up a password protected directory using Apache. First off we need an operating system. I am going to presume that you already have Apache 2.x installed on your server (operating system). If you are like me, you will also need to download PuTTY, and a network connection to your server. Of course, you may use the direct system console if you would like also. I am using Debian 3.1; you can download it here. Debian syntax is alot like Fedora Core’s, so it should seem pretty straight forward if you are familiar with Fedora Core or Red Hat Linux.

Our Goal

First off, let’s summarize what we have, and what we want. We have a webserver called localhost and we want to password-protect the directory secret. The problem is, when we go to http://localhost/secret we get right in, no password and Google (and other search engines) can crawl your directory. Note: This isn’t really an article on how to avoid search engine crawling, it’s really about setting up password protective directories using Apache (as the title states); and yes I know that you can use robot.txt.

Dig Into Apache

Apache on Debian is usually found in /etc/apache2 (this is where Apache is installed) and the www root is usually found in /var/www (this is where your HTML/CSS/PHP files go). This article will only go into the default installation of Apache 2.x, so if you go to /etc/apache2/sites-enabled you should see a file called 000-default; this is the file we need to edit:

Apache Site Configuration

<virtualhost *>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/
        <directory />
                Options FollowSymLinks
                AllowOverride none

        <directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride AuthConfig
                Order allow,deny
                allow from all
                # This directive allows us to have apache2's default start page
                # in /apache2-default/, but still have / go to the right place
                # RedirectMatch ^/$ /apache2-default/

</virtualhost>

After editing 000-default, if you aren’t familiar with the Nano editor, hold down Ctrl+O and press enter to write the file and then hold down Ctrl+X to exit.

After changing any part of Apache’s core files, or configuration files, you must restart Apache:

BASH

/etc/init.d/apache2 restart

Configuring .htaccess and .htpasswd

Before we actually configure .htaccess and .htpasswd, we need to create the secret directory. So, go to /var/www and create the directory called secret and add a default index.html file. To do this:

BASH

cd /var/www
mkdir secret
touch index.html

Now, still before we configure .htaccess and .htpasswd, we need to add some text to index.html. Open index.html with Nano:

BASH

nano index.html

Now add this XHTML code to index.html:

XHTML

< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <title>Password-Protected Area</title>
</head>
<body>
   <h1>Password-Protected Area</h1>
   <p>You are authorized.</p>
</body>
</html>

Now exit out of Nano (don’t forget to save your work!!). It’s now time to configure .htaccess and .htpasswd.

Configuring .htaccess

.htaccess is the file that tells apache what name, type of authentication, where the password file is located at, and what users are allowed to access the directory. So let’s create the .htaccess file (note the dot (.) is required):

BASH

touch .htaccess

Now let’s add the following to the file using Nano (you should know how to open a file with Nano now by yourself):

Apache Code

AuthName "My Password-Protected Area - Authorized Users Only"
AuthType Basic
AuthUserFile /var/www/secret/.htpasswd
require user secretuser

AuthName is used to create a realm name. Anyone sees this; it’s a descriptive way of letting you’re users know what this protected area is. Note: If spaces are used, you must encapsulate the whole AuthName value in quotes.

AuthType is the authentication type used for the current directory. Basic authentication is the only one that is currently implemented.

AuthUserFile is the absolute path to the .htpasswd file. This can be any name really, but I have seen the standard set to be .htpasswd.

require user [username] is the set of users that can be used to access the directory. You actually define the usernames and passwords in the .htpasswd file.

Configuring .htpasswd

.htpasswd is the file that tells Apache what the username and corresponding password of each user is. By default, the passwords are encrypted using the CRYPT algorithm. Also note that we will use -c option to create the file. If you use this each time to add a user, it will wipe out preexisting users, so please be careful. Now, let’s create the .htpasswd file (note the dot (.) is required):

BASH

htpasswd -cs .htpasswd secretuser

We have just created a .htpasswd file with the username of secretuser (-c) and we are using the SHA encryption algorithm for the password (-s). You actually create the password after you run this command; the system will prompt you for a fresh password and then have you re-confirm your password for verification. After you have added your new password, your .htpasswd file should look something similar to this (but not quite, since I am probably using a different password than you are):

.htpasswd File

secretuser:{SHA}E/A6F/9rt3w1dIBCIjsm3wbqutk=

Congrats, you have now successfully configured your .htaccess and .htpasswd files. You should now get a password prompt upon going to the URL http://localhost/secret. You should login using a corresponding user and password that you defined with the htpasswd command:

Password-Protected Prompt Dialog

Discussion always soothes thy heart.

1 Britt says: Quote this comment

I don’t know what this is about it looks good to me

2 britt says: Quote this comment

i miss you

3 White Kong. says: Quote this comment

Great instructions.. I haven’t set this up in years and it was a snap using your howto.

Thanks man.

4 Drew says: Quote this comment

Great! Glad I could help. It’s not the most “secure” soution, but sure does work for a temporary solution.

Either way, its a great way of password protecting directories, without having root access to the server.

Again, thanks!

5 keith says: Quote this comment

Nice how-to. I have a question… what if I want to let some users get into a directory but just one of them into another directory inside?

Thanks for the how-to, anyway ;)

6 jeremy says: Quote this comment

this one didnt work for me. i got a password prompt but when trying to login i got internal server errors.

7 Drew says: Quote this comment

Hello Jeremy,

This can easily be due to Apache configuration, that is probably not due to my little tutorial here. I make sure all of my articles are pretty straight forward and very wordy to insure that I get all the detail out.

What OS are you running, and what version of Apache? If you could contact me via the Contact Me page with your Apache configuration, I might be able to help you. Internal Server Errors (500) are usually due to a configuration on the actual web server, and might not be due to my article, especially since you stated that you get the password prompt, and once it lets you in, it gives you internal server errors. This statement actually indicates that my article deemed a success and it did work.

Please let me know how I can further help you out with these errors. Drop me a line or something; I’d love to help.

Regards,
Drew

8 MrGroove says: Quote this comment

Nice read. Thnx for the detailed write-up.

9 Drew says: Quote this comment

Nice read. Thnx for the detailed write-up.

Thanks for the kinda words. Feel free to send me other ideas. I have plenty more to come, actually, but I’ve been rather busy. I’ll see if I can get some of my ideas off the paper and onto the site.

Thanks,
Drew

10 Me says: Quote this comment

Finally, a useful article. I always edited httpd.conf, but not sites-enabled. Thanks a lot!

11 DeckerD: Drew’s Personal Corner of the Web » Splunk Free: .htaccess Protection using Apache says: Quote this comment

[...] configuration, you told Apache to use the .htpasswd.users file in the /var/www directory. You can follow my other article on how to configure .htaccess. If you plan on storing your .htaccess/.htpasswd files somewhere else, [...]

Go ahead, say somethin'





The Notebook

Windows Fix: "Insufficient system resources exist to complete the API"
Ugh. Another Windows error. Another Windows error that really doesn’t tell you what is really wrong; some (most) people aren’t nerds. Don’t worry, I’ve got your fix right here. This has to do with memory on your Windows XP system. Have you tried Hibernating or putting your system into standby after upgrading your RAM? If you have, keep on reading. More...

Curing 'Symbolic link not allowed' (Apache 2.0)
Awhile back when trying to add a directory to Apache on my Ubuntu 6.06 LTS server, I ran into some issues. These issues, of course were the infamous 403 “Forbidden” error that your web barfs up when you try to access a server that is not publicly viewable, because of permission issues. I’m going to give you my scenario and explain how you can fix this issue, so that you don’t have to go through an hour of throwing your hands up in disgust. More...

VMWare: "Failed to create named-pipe directory" error
Have you ever received the Failed to create named-pipe directory error message via the vmware-mui error log files, and not able to access the VMWare Management Interface? If so, here’s the fix. More...

/bin/rm: Argument list too long
Removing gigs and gigs, or just alot, of unwanted “opened” email that often clutters the system. More...