How to Add an Extra Layer of Security on PhpMyAdmin Login Interface

MySQL is the world’s most used open source database management system on the Linux ecosystem and at the same time Linux newbies find it difficult to manage from the MySQL prompt.

PhpMyAdmin was created, is a web based MySQL database manage application, which provides a easy way for Linux newbies to interact with MySQL through a web interface. In this article, we’ll share how to secure phpMyAdmin interface with password protect on Linux systems.

Before you move forward with this article, we assume that you have completed a LAMP (Linux, Apache, MySQL/MariaDB, and PHP) and PhpMyAdmin installation on your Linux server. If not, you can follow our below guides to install LAMP stack on your respective distributions..

  1. Install LAMP and PhpMyAdmin in Cent/RHEL 7
  2. Install LAMP and PhpMyAdmin in Ubuntu 16.04
  3. Install LAMP and PhpMyAdmin in Fedora 22-24

If you want to install latest version of PhpMyAdmin, you can follow this guide on installing Latest PhpMyAdmin on Linux systems.

Once you are done with all these above steps, you are ready to get started with this article.

Just by adding the following lines to /etc/apache2/sites-available/000-default.conf in Debian or /etc/httpd/conf/httpd.conf in CentOS will require basic authentication AFTER confirming the security exception but BEFORE accessing the login page.

Thus, we will be adding an extra layer of security, also protected by the certificate.

Suggested Read: Setup HTTPS (SSL Certificates) to Secure PhpMyAdmin

Add these lines to the Apache configuration file (/etc/apache2/sites-available/000-default.conf or /etc/httpd/conf/httpd.conf):

/etc/apache2/sites-available/000-default.conf – On Ubuntu
<Directory /usr/share/phpmyadmin>
    AuthType Basic
    AuthName "Restricted Content"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
</Directory>
/etc/httpd/conf/httpd.conf – On CentOS
 
<Directory /usr/share/phpmyadmin>
    AuthType Basic
    AuthName "Restricted Content"
    AuthUserFile /etc/httpd/.htpasswd
    Require valid-user
</Directory>

Then use htpasswd to generate a password file for an account that will be authorized to access the phpmyadmin login page. We will use /etc/apache2/.htpasswd and tecmint in this case:

---------- On Ubuntu/Debian Systems ---------- 
# htpasswd -c /etc/apache2/.htpasswd tecmint

---------- On CentOS/RHEL Systems ---------- 
# htpasswd -c /etc/httpd/.htpasswd tecmint

Enter password twice and then change the permissions and ownership of the file. This is to prevent anyone not in the www-data or apache group from being able to read .htpasswd:

# chmod 640 /etc/apache2/.htpasswd

---------- On Ubuntu/Debian Systems ---------- 
# chgrp www-data /etc/apache2/.htpasswd 

---------- On CentOS/RHEL Systems ---------- 
# chgrp apache /etc/httpd/.htpasswd 

Go to http://<ip address>/phpmyadmin and you’ll see the authentication dialog before accessing the login page.

Suggested Read: Change and Secure Default PhpMyAdmin Login URL

You will need to enter the credentials of a valid account in /etc/apache2/.htpasswd or /etc/httpd/.htpasswd in order to proceed:

Secure PhpMyAdmin with Password Protect
Secure PhpMyAdmin with Password Protect

If the authentication is successful, you will be taken to the phpmyadmin login page.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Gabriel Cánepa
Gabriel Cánepa
Contributor
About the author

Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

4 Comments

Leave a Reply
  1. Good stuff!

    Did’nt quite understands that last bit at the finish on.

    “You will need to enter the credentials of a valid account in /etc/apache2/.htpasswd or /etc/httpd/.htpasswd in order to proceed:”

    Kindly explain,
    thanks in advance!

    Reply
  2. Firstly, thank you so much for the great instruction.
    Please add to your note that httpd needs to be restarted for the changes to come in effect.

    Cheers

    Reply
  3. Is it possible to create a log for the login attempts on this? Then, we can use fail2ban to ban bad IPs. That adds another level of security too.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.