How to Setup Password Authentication with Apache

In some cases, we may need to secure our website so that only authenticated users can access the website. We can protect our website at the server level using Apache Password Authentication.

In, this tutorial we will try to explain how we can enable password authentication with apache web server.

NOTE: The mentioned steps are performed on CentOS 7 server.

1. Create the password file.

sudo htpasswd -c /etc/httpd/.htpasswd httpauth

Now, you’ll be asked to supply and confirm a password for user “httpauth”.
You can leave ” -c ” argument for any additional users.

2. Configure Apache Vhost:

vim /etc/httpd/conf.d/

Add the following lines to the Vhost file:

<Directory "/var/www/html">
      AuthType Basic
      AuthName "Restricted Content"
      AuthUserFile /etc/httpd/.htpasswd
      Require valid-user

Save and exit the file.

Check the config file with this command, before restarting your web server.

sudo apachectl configtest

Now, Restart the Apache webserver:

sudo systemctl restart httpd

Now, to check if you have successfully enabled Password Authentication, Open your website in your browser you’ll be prompted for username & password.

Leave a Reply

Your email address will not be published. Required fields are marked *