How to protect directory with a password in Apache2?

A usual scenario encountered by every web server is administrator is to password protect specific directory and all it’s resources. We need to add some lines in the configuration file where our web server / virtual host is defined. This is how we do it:

Creating the password file: (the system will ask you to enter the password which will be saved in encrypted format)

sudo htpasswd -c [filename] [username]

Apache BASIC HTTP Authentication using the created password file

AuthUserFile [the path to the password file]
AuthType Basic
AuthName "RESTRICTED ACCESS"
Require valid-user

Publish Date: 2014-10-27