I have a folder that I want to host files privately on my webserver.

How do I go about making it password protected using .htaccess?

Note: Nothing monolithic, I want something that is simple/easy so setup so me and a few colleagues can download a few files.

2 Answers 2


You'll need to create a file called .htpasswd with the content of username:password (you should encrypt that.

Then append this to the htaccesss file:

AuthUserFile /usr/local/you/safedir/.htpasswd
AuthGroupFile /dev/null
AuthName EnterPassword
AuthType Basic

require user username

Follow this guide.

Also, here is a tool for generating the encrypted passwords: http://tools.dynamicdrive.com/password (Thanks to the original poster for the link)


install htpasswd (password generator)

install apache2-utils through

sudo apt-get install apache2-utils

This above package has htpasswd command.

configure basic authentication

go to apache2.conf file and edit

  1. change "AllowOverride none" to "AllowOverride All" to .htaccess enable and working in the directory.

  2. add a new line below the above edit lines "AllowOverride AuthConfig" to enable apache2 to handle authentication. keep in mind these modules should be enables

    1. authn_core_module
    2. authz_core_module

you can check these modules (enable or not) through this command

sudo apache2ctl -M 

create password file(create new user and password)

  1. create your folder in /var/www/html/

    sudo mkdir -m /var/www/html/my_protected_folder

  2. htpasswd -c /var/www/html/my_protected_folder/.htpassword username and enter prompted password. .htpassword file created with username and encrypted password.

    1. crate .htaccess file with these content AuthType Basic AuthName "Restricted Access" AuthUserFile /var/www/html/my_protected_folder/.htpassword Require user username

change permission and ownership

  1. sudo chown -R www-data:www-data /var/www/html/my_protected_folder
  2. sudo chmod 660 /var/www/html/my_protected_folder/.htpassword

visit your page and enter username "username" and password which you wrote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.