How to Password Protect a folder in Linux

By jldurham6,

  Filed under: Linux
  Comments: None

1. Find this file: /etc/httpd/httpd.conf
Edit it with your favorite text editor.
Find the section “DocumentRoot”
or find: “”
Now, edit the section so it looks like the following:
AllowOverride All <— if you do not see ‘All’ put it in.

2. htpasswd -c /dirname/filename
filename and directory is where you want to create the
htpasswd file. Put it in /etc/httpd/ and call it htpasswd.
username is a valid username already on your system.
for example: htpasswd -c /etc/httpd/htpasswd joe
3. Create file ‘.htaccess’ in the directory you want to be protected.
so if you want to proctect everything inĀ www.yoursite.com/private/
then you put the ‘.htaccess’ file in private/
The content of ‘.htaccess’ is:
AuthUserFile /etc/httpd/htpasswd
AuthGroupFile /dev/null
AuthName MyNameIsJohnDoe
AuthType Basic

require valid-user

4. Restart apache.
For example: /etc/rc.d/init.d/httpd restart
You must type in the full path.

Notes: After you have created /etc/httpd/htpasswd with the option -c,
you can add more users by using htpasswd without the -c option.
You also do not have to restart Apache.