Protect a Directory with Apache, .htaccess and httpasswd

Apache comes with a command line utility called “htpasswd”. This utility will generate a username and password that you can use to authenticate against using a .htaccess file. Just run the utility like so:

  htpasswd -c /path/to/your/password/directory/and-your-password-filename jane_doe_username

This will prompt you for a password/confirm password.

Keep in mind that this will not create a user on the system. It will just create a password and associate it with a string, that is the username you’ll use to authenticate in your request.

Then you’ll need to add a .htaccess file in the directory that you want to protect, and in that file place the following code.

AuthType Basic
AuthName "Restricted Directory"
AuthUserFile /path/to/your/password/directory/and-your-password-filename
Require user jane_doe_username

Remember that the /path/to/your/password/directory will need to be owned by Apache. on Ubuntu, it’s by default the www-data user. Change ownership like so:

 chown -R www-data:www-data directory

Related posts:

  1. Dot htaccess file for wordpress installation (.htaccess)
  2. Recursively Zip a Directory and Files on Linux
  3. Installing RedMine PM Software on Apache with Phusion and REE and Seeing a 404 Page Not Found Error on Installation
  4. Recursively Zip Up a Directory while Excluding Certain Files Based on File Extension Type
  5. Install and Serve a Rails Application from PHP Subdirectory Using Apache, Phussion Passenger and Ruby Enterprise Edition
This entry was posted in Programming and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. admin
    Posted January 13, 2010 at 12:03 pm | Permalink

    typo alert… i wrote “httpasswd” when it should be “htpasswd”. the httpasswd won’t work because the command doesn’t exist. the post above contains the corrected copy!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>