Operating System Ubuntu

How to Install and Secure phpMyAdmin on Ubuntu 24.04

Install phpMyAdmin from the Ubuntu 24.04 repositories and harden it with HTTPS, a non-default URL, HTTP authentication, an IP allow-list and disabled root logins.

This guide installs phpMyAdmin from the Ubuntu repositories on Ubuntu 24.04 LTS with Apache, creates a database account to log in with, and then locks the installation down: HTTPS, a non-default URL, an extra password prompt in front of the login page, an IP allow-list and no root logins. The steps are the same on Ubuntu 22.04.

Older tutorials on this topic were written for Ubuntu 14.04 and 16.04, which are both past their standard support. Commands from that era such as php5enmod mcrypt no longer apply: PHP 5 and the mcrypt extension are gone, and current phpMyAdmin does not use them.

Requirements

  • Ubuntu 24.04 with Apache, PHP and MySQL or MariaDB already installed (a LAMP stack).
  • A user with sudo rights.
  • Preferably a domain name pointing to the server, so that you can use a TLS certificate. The examples use example.com.

Ubuntu 24.04 packages phpMyAdmin 5.2.1 in the universe component. The upstream project's current release line is also 5.2, which it lists as fully supported, and Ubuntu's package is what this guide uses because it is updated through apt.

Step 1: Install phpMyAdmin

sudo apt update
sudo apt install -y phpmyadmin php-mbstring php-zip php-gd php-curl

The installer asks three questions:

  1. Web server to reconfigure automatically: move to apache2, press Space so an asterisk appears, then Tab and Enter. Highlighting the entry without pressing Space selects nothing, and Apache will not be configured.
  2. Configure database for phpmyadmin with dbconfig-common: answer Yes. This creates the phpmyadmin database that stores bookmarks, history and other settings.
  3. MySQL application password for phpmyadmin: leave it empty to have a random one generated. This is the internal control account, not your login.

Restart Apache:

sudo systemctl restart apache2

If http://203.0.113.10/phpmyadmin returns "Not Found", the Apache question was skipped. Enable the packaged configuration by hand:

sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
sudo a2enconf phpmyadmin
sudo systemctl reload apache2

Step 2: Create a database user for phpMyAdmin

On Ubuntu the MySQL root account authenticates through the local socket and cannot log in with a password from a web application. Create a separate administrative account instead of weakening root:

sudo mysql
CREATE USER 'dbadmin'@'localhost' IDENTIFIED BY 'Use-A-Long-Random-Password';
GRANT ALL PRIVILEGES ON *.* TO 'dbadmin'@'localhost' WITH GRANT OPTION;
EXIT;

For day-to-day work, prefer accounts that are limited to one database, for example GRANT ALL PRIVILEGES ON shop.* TO 'shop'@'localhost';.

Step 3: Serve phpMyAdmin over HTTPS only

The phpMyAdmin documentation's first hardening advice is to serve it over HTTPS only. Without TLS your database password crosses the network in clear text. If the site does not have a certificate yet, get one from Let's Encrypt with Certbot's Apache plugin:

sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache -d example.com

Choose the redirect option when Certbot offers it, so that HTTP requests are sent to HTTPS. From here on, use https://example.com/phpmyadmin.

Step 4: Move phpMyAdmin to a non-default URL

Bots request /phpmyadmin on every server they find. Changing the alias does not replace authentication, but it removes most of that noise from your logs. Edit the packaged Apache configuration:

sudo nano /etc/phpmyadmin/apache.conf

Change the Alias line near the top to a path of your own:

Alias /dbadmin-7f3k /usr/share/phpmyadmin

Step 5: Add an Apache password prompt and an IP allow-list

A second authentication layer in the web server means an attacker cannot even reach the phpMyAdmin login form, or any bug in it, without another password. The old approach used a .htaccess file with AllowOverride All; putting the directives into Apache's own configuration does the same without enabling overrides.

Create the password file. htpasswd is part of apache2-utils, which is normally installed with Apache.

sudo apt install -y apache2-utils
sudo htpasswd -c /etc/phpmyadmin/.htpasswd deploy

Use -c only for the first user; it overwrites an existing file. Now create a separate configuration file, so that package upgrades do not touch it:

sudo nano /etc/apache2/conf-available/phpmyadmin-access.conf
<Directory /usr/share/phpmyadmin>
    AuthType Basic
    AuthName "Restricted"
    AuthUserFile /etc/phpmyadmin/.htpasswd
    <RequireAll>
        Require valid-user
        Require ip 198.51.100.25
    </RequireAll>
</Directory>

Replace 198.51.100.25 with your own static address, or add several addresses or a network such as 198.51.100.0/24 on the same line. If you do not have a fixed address, remove the Require ip line and keep the password prompt. Enable the file and test the configuration before reloading:

sudo a2enconf phpmyadmin-access
sudo apache2ctl configtest
sudo systemctl reload apache2

Step 6: Disable root logins in phpMyAdmin

The Ubuntu package reads extra settings from /etc/phpmyadmin/conf.d/. Create a file there:

sudo nano /etc/phpmyadmin/conf.d/security.php
<?php
$cfg['Servers'][$i]['AllowRoot'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = false;
$cfg['LoginCookieValidity'] = 1800;

AllowRoot refuses logins as root, AllowNoPassword refuses accounts with an empty password, and LoginCookieValidity logs idle sessions out after 30 minutes. No restart is needed.

Step 7: Verify

  1. Open https://example.com/phpmyadmin. It should now return "Not Found".
  2. Open https://example.com/dbadmin-7f3k. The browser asks for the Apache user and password first, then phpMyAdmin shows its own login form.
  3. Log in as dbadmin. Logging in as root must be refused.
  4. From an address that is not on the allow-list, the URL returns "Forbidden".

Maintenance and troubleshooting

  • Updates: phpMyAdmin is a frequent target, so apply security updates promptly with sudo apt update && sudo apt upgrade. Packages in universe get security fixes from the community unless the machine is attached to Ubuntu Pro.
  • Two-factor authentication: after logging in, open Settings and the Two-factor authentication tab to add a second factor to your phpMyAdmin account. It requires the configuration storage created in step 1.
  • Failed logins: phpMyAdmin can log failed authentication attempts (the AuthLog setting), which lets tools such as Fail2ban block repeat offenders.
  • "The mbstring extension is missing": run sudo phpenmod mbstring and restart Apache.
  • Re-run the installer questions: sudo dpkg-reconfigure phpmyadmin.

Reference: phpMyAdmin installation and security documentation.

Frequently asked questions

Should I use the Ubuntu package or the upstream archive?

The package is easier to keep patched because apt updates it, and it keeps configuration in /etc/phpmyadmin. The upstream archive from phpmyadmin.net gives you the newest release, but you have to create config.inc.php with a 32-byte blowfish_secret yourself and repeat the download for every update.

Does this work with Nginx?

The package only auto-configures Apache and lighttpd. With Nginx, leave the web server question empty during installation, then add a location block that serves /usr/share/phpmyadmin through PHP-FPM and protect it with auth_basic and allow/deny rules.

Is it safer not to expose phpMyAdmin at all?

Yes. If only you use it, bind access to Require ip 127.0.0.1 and reach it through an SSH tunnel such as ssh -L 8443:127.0.0.1:443 deploy@example.com, or use a desktop client over SSH. Nothing is then reachable from the public Internet.

Why can't I log in as root?

On Ubuntu, MySQL's root uses socket authentication, which only works for the system root user on the command line. That is intentional. Use a dedicated account as in step 2 rather than giving root a password.

Share story:

Continue reading other guides on How VPS.