HOW TO CONFIGURE BASIC AUTHENTICATION IN APACHE WEB SERVER
Introduction
Apache Web Server is a huge application comes with some nice features like SSL Configuration,Website Authentication and so on.In this article we are going to discuss on How to configure basic authentication in Apache Web Server. First thing that is comes in my mind is what is authentication and why we need it. Let me explain you what is authentication. Authentication is nothing but a process by which you have to give you identification to access something.
Let’s take an simple example : Suppose you want to access your gmail account or any other mail account, for that you need to enter your Email ID and Password then only you able to access your mails. That means you shared your personal details ( Name, Address, Mobile Number..etc…) to generated that Email ID and Password with Google. Just because of authentication process we are able to store our valuable and confidential data on web-directories safely. Today all daily basis important works are secure just because of Authentication eg: Bank Transactions.
In this article we are going to configure basic authentication to secure apache web directory which is contains some sensitive and confidential information in Apache web server and will give access to those users who has authorize to use that.
Steps to configure Basic Authentication with Apache Web Server
Step : 1 Install Apache Web Server Package
Install the apache web server package to configure basic authentication. you can install httpd package either by yum repository or by rpm package.
[root@localhost ~]# yum -y install httpd # Install Apache Webserver Package
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: mirror.nbrc.ac.in
* extras: mirrors.vonline.vn
* updates: mirrors.aluhost.com
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.2.15-55.el6.centos.2 will be updated
---> Package httpd.x86_64 0:2.2.15-56.el6.centos.3 will be an update
--> Processing Dependency: httpd-tools = 2.2.15-56.el6.centos.3 for package: httpd-2.2.15-56.el6.centos.3.x86_64
--> Running transaction check
---> Package httpd-tools.x86_64 0:2.2.15-55.el6.centos.2 will be updated
---> Package httpd-tools.x86_64 0:2.2.15-56.el6.centos.3 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================
Updating:
httpd x86_64 2.2.15-56.el6.centos.3 updates 834 k
Updating for dependencies:
httpd-tools x86_64 2.2.15-56.el6.centos.3 updates 79 k
Transaction Summary
==============================================================================================================================
Upgrade 2 Package(s)
Total download size: 913 k
Downloading Packages:
(1/2): httpd-2.2.15-56.el6.centos.3.x86_64.rpm | 834 kB 00:13
(2/2): httpd-tools-2.2.15-56.el6.centos.3.x86_64.rpm | 79 kB 00:00
------------------------------------------------------------------------------------------------------------------------------
Total 56 kB/s | 913 kB 00:16
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Updating : httpd-tools-2.2.15-56.el6.centos.3.x86_64 1/4
Updating : httpd-2.2.15-56.el6.centos.3.x86_64 2/4
Cleanup : httpd-2.2.15-55.el6.centos.2.x86_64 3/4
Cleanup : httpd-tools-2.2.15-55.el6.centos.2.x86_64 4/4
Verifying : httpd-2.2.15-56.el6.centos.3.x86_64 1/4
Verifying : httpd-tools-2.2.15-56.el6.centos.3.x86_64 2/4
Verifying : httpd-tools-2.2.15-55.el6.centos.2.x86_64 3/4
Verifying : httpd-2.2.15-55.el6.centos.2.x86_64 4/4
Updated:
httpd.x86_64 0:2.2.15-56.el6.centos.3
Dependency Updated:
httpd-tools.x86_64 0:2.2.15-56.el6.centos.3
Complete!
Apache web server package installed successfully, now start the httpd service by below command.
[root@localhost ~]# /etc/init.d/httpd start # Start the Apache Webserver Service
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
Now open your web browser and enter http://localhost to confirm if apache web server is working fine or not. Follow the snapshot below.
As we can see on the snapshot above apache web server default page open without any authentication. Here as an example i am taking apache web server default page as my website and I will show you how to configure authentication for this so that it will ask for username & password when we open it.
Step : 2 Create Password File for Apache Basic Authentication
For basic authentication with apache web server you we needs username and password. Here we have a tool that is comes with apache package installation i.e. htpasswd. So let’s create username and password by using htpasswd command. Follow the command below.
After execute the command it will ask to enter password, So enter the password twice to confirm.
Note : This username and password is going to use to access website.
[root@localhost ~]# htpasswd -c /etc/httpd/.htpasswd elinuxbook # Create Password File for Authentication
New password:
Re-type new password:
Adding password for user elinuxbook
Where :
.htpasswd is the password file which is contains username and password for Apache basic authentication which is located at /etc/httpd directory.
elinuxbook is the username.
You can confirm the availability of user in /etc/httpd/.htpasswd file by below command.
[root@localhost ~]# cat /etc/httpd/.htpasswd # Check the Password file
elinuxbook:QmQxg1A7TcQlw
Step : 3 Create a Virtual Host in Apache Web Server
To create a VirtualHost in Apache web server just edit the main configuration file i.e. /etc/httpd/conf/httpd.conf and go to the end of the file and add the below lines.
Note : Here in my practice lab I am taking Apache Web Server default page as my website, So let’s configure basic authentication for that so that it will ask for username and password when open it.
To configure authentication just add below lines to your VirtualHost just befor the </VirtualHost> syntax :
<Directory “/var/www/html”>
AuthType Basic
AuthName “Authentication Required”
AuthUserFile /etc/httpd/.htpasswd
Require valid-user
</Directory>
Refer the configuration below. (Configuration part are highlighted in Blue Color)
# Add VirtualHost in Apache main configuration file with Directory Rescriction ### Apache Authentication ### <VirtualHost *:80> ServerAdmin webmaster@elinuxbook.com DocumentRoot /var/www/html ServerName elinuxbook.com ErrorLog logs/www.google.com-error_log CustomLog logs/elinuxbook.com-access_log common <Directory "/var/www/html"> AuthType Basic AuthName "Authentication Required" AuthUserFile /etc/httpd/.htpasswd Require valid-user </Directory> </VirtualHost> ### Apache Authentication ###
Where :
- <VirtualHost> & </VirtualHost> – Syntax for Start & Close of VirtualHost.
- ServerAdmin – To mention Email of Webmaster.
- DocumentRoot – Document(Files & Directories) location of your Website.
- ServerName – Domain Name of your Website eg: elinuxbook.com
- ErrorLog – To mention log file location for your Apache Web Server errors.
- CustomLog – To mention log file loaction for apache access logs.
- <Directory> & </Directory> – To add some additional features for your VirtualHost for eg: Here we are adding basic authentication for our VirtualHost.
- AuthType – To mention Authentication Type eg: Here we are using Basic Authentication.
- AuthName – To mention the message which will show on your authentication page.
- AuthUserFile – To mention the File Path which is containing Username & Password details eg: Here it is /etc/httpd/.htpasswd
- Require – valied-user – So that it will take only that user which is available in .htpasswd file.
Now restart the Apache Service to take effect the changes.
[root@localhost ~]# /etc/init.d/httpd restart # Restart the Apache Webserver Service
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
We are done with all required changes, Now its time for testing. So Let’s access the Apache Web Server default page and I am sure it will ask for username & Password.
As we can see above now Apache Web Server asking for Username & Password when we are trying to access Default Web Page. Here enter the Username and Password which we have create using htpasswd command on Step 2 above.
So This is how we can configure Basic Authentication in Apache Web Server. If you found this article useful then like Us, Follow Us, Subscribe our Newsletter to stay updated with all latest and updated linux articles Or if you have something to say then feel free to comment on the comment box below.