1.Install apache2
#apt-get install apache2
2.Create a Password file
We now have access to the htpasswd command. We can use this to create a password file that Apache can use to authenticate users. We will create a hidden file for this purpose called .htpasswd within our /etc/apache2 configuration directory.
#htpasswd -c /etc/apache2/.htpasswd arun
Leave out the -c argument for any additional users you wish to add:
#htpasswd /etc/apache2/.htpasswd Rahul
If we view the contents of the file, we can see the username and the encrypted password for each record:
#cat /etc/apache2/.htpasswd
arun:$apr1$zgRuzA6/$RSvqTSJPxikIXEKxgYu4f0
rahul:$apr1$yX/HaM9G$S0vY8PfVef0JhG66xsCFq.
#apt-get install apache2
2.Create a Password file
We now have access to the htpasswd command. We can use this to create a password file that Apache can use to authenticate users. We will create a hidden file for this purpose called .htpasswd within our /etc/apache2 configuration directory.
#htpasswd -c /etc/apache2/.htpasswd arun
Leave out the -c argument for any additional users you wish to add:
#htpasswd /etc/apache2/.htpasswd Rahul
If we view the contents of the file, we can see the username and the encrypted password for each record:
#cat /etc/apache2/.htpasswd
arun:$apr1$zgRuzA6/$RSvqTSJPxikIXEKxgYu4f0
rahul:$apr1$yX/HaM9G$S0vY8PfVef0JhG66xsCFq.
3.Configuring Access Control with apache2 configuration file:
#vi /etc/apache2/apach2.conf
Add the following entries
<Directory /var/www/html/test>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
Before restarting the web server, you can check the configuration with the following command:
#apache2ctl configtest
Create a test folder inside /var/www/html/ and create a index file inside it
#echo “this is test page” > /var/www/html/test/index.html
#systemctl restart apache2
Try to acess test folder from browser. http://Your ip/test.We can see we can access without any authentication
4.Now create a .htaccess file inside test folder
#vi /var/www/html/test/.htaccess
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
5.Save the file and restart the apache 2 service
# systemctl restart apache2
Now access test folder http://yourip/test