AKZN Notes

Archives for My Lazy and Forgetful Mind

HTTP Basic Authentication to secure phpMyAdmin

HTTP Authentication is a method of securing web resources by requiring users to provide valid credentials before accessing them. It operates at the HTTP protocol level and can be implemented using several authentication schemes. One common method for HTTP Authentication is Basic Authentication, which prompts users to enter their username and password in a dialog box when accessing a protected resource.

Here's how you can implement HTTP Basic Authentication to secure phpMyAdmin:

  1. Create a Password File:

    • Create a password file that stores usernames and encrypted passwords. You can use the htpasswd utility to generate encrypted passwords.
    • For example, to create a password file named .htpasswd with a user admin, you can use the following command:
      htpasswd -c /etc/apache2/.htpasswd youruser
    • You'll be prompted to enter and confirm the password for the user admin.
  2. Configure Apache:

    • Open your Apache configuration file (e.g., apache2.conf, httpd.conf, or a virtual host configuration file).
    • Inside the <Directory> or <Location> block for phpMyAdmin, add the following directives:
      < Directory /usr/share/phpmyadmin>
       AuthType Basic
       AuthName "Restricted Access"
       AuthUserFile /etc/apache2/.htpasswd
       Require valid-user
      < /Directory>
    • Replace /usr/share/phpmyadmin with the actual path to your phpMyAdmin directory.
    • AuthType Basic: Specifies the type of authentication.
    • AuthName "Restricted Access": Specifies the authentication realm (the message displayed to users when prompted for credentials).
    • AuthUserFile /etc/apache2/.htpasswd: Specifies the path to the password file created earlier.
    • Require valid-user: Specifies that only authenticated users are allowed to access the resource.
  3. Restart Apache:

    • After making changes to the Apache configuration, restart Apache for the changes to take effect:
      sudo systemctl restart apache2   # for systemd-based systems
      sudo systemctl restart httpd     # for systemd-based systems

With these steps, phpMyAdmin will be protected by HTTP Basic Authentication, and users will need to enter valid credentials to access it. Make sure to choose strong passwords and securely manage the password file to maintain the security of your phpMyAdmin installation.

Leave a Reply

Your email address will not be published.