Password Protect APACHE Directory with Basic Authentication
In our earlier tutorials, we have discussed ‘how we can create a apache server’ ‘secure it using an SSL certificate’ & also learned ‘some tips for Hardening apache server’. In this tutorial, we are going to learn to password protect apache directory server with Basic Authentication using .htaccess file.
We might want to secure an apache directory for any number of reasons like if we want to share data over internet & keep it private with only limited access to some users etc & there are many ways to password protect a directory in apache but we are going to use .htaccess file to make our directory private.
.htaccess is used to alter configuration of apache server without editing the server configuration file. With .htaccess, we can configure page redirect, image hotlink prevention & in this case, password protect a directory.
So. let’s get started…
Firstly, let’s create a directory that we can password protect,
& then add some files to it
Now we will make changes to main configuration file i.e. ‘/etc/httpd/conf/httpd.conf’ to allow the use of .htaccess. We need to add AllowOverride AuthConfig directive in httpd.conf,
Options Indexes FollowSymLinks MultiViews
Allow from all
Save the file & restart apache services to implement the changes.
We will now create password file with ‘htpasswd’. Command htpasswd is used to create or update text files for storing username & password for apache users authentication. Go to ‘/var/www/html/secret_dir’ & run the following command,
Enter the password that want to identify your user with. Once done, the .htpasswd file will be created & we will now assign the file with proper group & file permissions,
Next step is to create .htaccess file in ‘secret_dir’ directory
& add the following lines,
AuthName “Restricted Access”
Require user dan
Save the file & restart your apache services once more.
We will now test our private directory. Open your browser & enter your url
& press enter. As soon as you press enter, you will be prompted to enter your username & password. You can add as many users as you need using the same process as above.
Thanks for reading this tutorial. If having any issues or questions, please use the comment box down below.