Share on Social Media

Problem Statement:

Sometimes we have sections of websites (especially admin panels), that we don’t want to be accessed by public. Most of the web applications have their own authentication methodology but we can also create another layer of security by means of Basic Authentication with Apache HTTP Server.

In this article, we will enable Basic Authentication for the setup directory of phpMyAdmin application with Apache HTTP Server.

Read Also: How to install phpMyAdmin on Linux

System Specification:

We have a preconfigured LAMP Server, and we have deployed phpMyAdmin application on it.
IP Address:
Operating System:CentOS 7.0 Server
Web Server:Apache/2.4.6

Configure HTTP Basic Authentication:

First of all, we must create a password file that Apache can read. For this purpose, we can use htpasswd command. htpasswd command is packaged with httpd-tools package. However, you may found it in apache2-utils package while installing on other Linux distros.

Check if required packages are already installed on the Server.

# rpm -qa | grep httpd

Since, we have a preconfigured LAMP Server, therefore, the httpd-tools package is already installed on our machine.

Create password file and add two users. Omit the –c option while adding the second user to already created .htpasswd file, or it will overwrite the file.

# htpasswd -c /var/www/html/phpmyadmin/setup/.htpasswd ahmer
New password:
Re-type new password:
Adding password for user ahmer
# htpasswd /var/www/html/phpmyadmin/setup/.htpasswd mansoor
New password:
Re-type new password:
Adding password for user mansoor

Check contents of the password file.

# cat /var/www/html/phpmyadmin/setup/.htpasswd

As you know, we can alternatively define Apache directives at various locations like

a) in httpd.conf file,
b) in a separate .conf file created within /etc/httpd/conf.d directory, or
c) override by using a .htaccess file.

If you have set Allow Override for your web site than you can implement Basic Authentication using .htaccess file. One advantage of .htaccess is that, it won’t require the httpd service to restart after configuration. So, for a busy web server, this technique is better.

# cat >> /var/www/html/phpmyadmin/setup/.htaccess << EOF 
> AuthType Basic
> AuthName "Restricted Content"
> AuthUserFile /var/www/html/phpmyadmin/setup/.htpasswd
> Require valid-user

Now, try to access the URL using your browser. it will ask you for authentication.

Apache Basic Authentication Login

Now, you have to login with a valid user and password to get access to this section of website.

phpMyAdmin Homepage

HTTP Basic Authentication with Apache web server has been configured successfully.

Leave a Reply