1. How do I protecting a directory in Apache on linux?
A. There are many ways you can password protect directories under Apache web server.
This is important to keep your file privates from both unauthorized users and search
engines (when you do not want to get your data indexed). Here you will see the basics
of password protecting a directory on your server. You can use any one of the following method:
1) Putting authentication directives in a <Directory> section, in your main server configuration
httpd.conf file, is the preferred way to implement this kind of authentication.
2) If you do not have access to Apache httpd.conf file (for example shared hosting)
then with the help of file called .htaccess you can create password protect directories. .htaccess
file provide a way to make configuration changes on a per-directory basis.
2. In order to create apache password protected directories you need:
1. Password file
2. And Directory name which you would like to password protect (/var/www/html)
Step # 1: Make sure Apache is configured to use .htaccess file
You need to have AllowOverride AuthConfig directive in httpd.conf file in order for these
directives to have any effect. Look for DocumentRoot Directory entry. In this example,
our DocumentRoot directory is set to /var/www/html. Therefore, my entry in httpd.conf looks like as follows:
<Directory /var/www/html>
Options Indexes Includes FollowSymLinks MultiViews
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>
2.restart apache:-
service httpd restart
Step # 3: Create a password file with htpasswd
htpasswd command is used to create and update the flat-files (text file) used to store usernames
and password for basic authentication of Apache users. General syntax:
htpasswd -c password-file username
Where,
-c : Create the password-file. If password-file already exists, it is rewritten and truncated.
username : The username to create or update in password-file. If username does not exist in this
file, an entry is added. If it does exist, the password is changed.
Create directory outside apache document root, so that only Apache can access password file.
The password-file should be placed somewhere not accessible from the web.
This is so that people cannot download the password file:
mkdir -p /home/secure/
4. Add new user called tushar
htpasswd -c /home/secure/password tushar
Make sure /home/secure/password file is readable by Apache web server. If Apache cannot
read your password file, it will not authenticate you. You need to setup a correct
permission using chown command. Usually apache use www-data user. Use the following command to find out Apache username. If you are using Debian Linux use pache2.conf, type the following
If you are using RedHat and Fedora core, type the following commands :
# grep -e '^User' /etc/httpd/conf/httpd.conf
Output:
apache
5. Now allow apache user apache to read our password file:
# chown apache:apache /home/secure/password
# chmod 0660 /home/secure/password
6.Create .htaccess file using text editor:
cd /var/www/html
vim .htaccess
Add following text:
AuthType Basic
AuthName "Restricted Access"
AuthUserFile /home/secure/password
Require user tushar
7. Test your configuration
http://localhost
Nice article, thank you for sharing the informative article. I have also an article about htaccess
ReplyDelete. Thank you!!!
This comment has been removed by the author.
ReplyDelete