phpMyAdmin is convenient, and that is exactly why bots love it. If your login page sits at the default address with the root account enabled, automated scanners will find it and keep trying passwords around the clock. Learning how to secure phpMyAdmin on your VPS is not about one magic setting. It takes a few simple layers: a hidden address, a second password, an IP check, and a limited MySQL account standing between a bot and your databases.
Apply them one at a time and test your own login after each step, so if something breaks you know which change caused it.
Why phpMyAdmin Draws Bots
phpMyAdmin ships on millions of servers, and its default paths are public knowledge. Scanners sweep whole IP ranges, request /phpmyadmin/ and a handful of common aliases, and log every server that answers. Once your panel answers, the same bot starts guessing passwords.
With that login, an attacker can read user tables, dump customer data, or change WordPress admin passwords. A strong MySQL root password helps, but it is a single point of failure. The layers below remove that single point.
Before You Touch Anything: Back Up First
Take a backup before you start. A quick dump of every database gives you a way back if a permission change goes wrong.
sudo mysqldump --all-databases --single-transaction > /root/db-backup-before-pma-hardening.sql
Check that the file is not empty, then store a copy off the server. To make this automatic, set up a daily MySQL backup with mysqldump and cron.
On Ubuntu, the package install places its files in /usr/share/phpmyadmin and its configuration in /etc/phpmyadmin/config.inc.php. If you installed it by hand, adjust the paths below to match.
Secure phpMyAdmin on Your VPS: Change the Default URL
The cheapest win is to move phpMyAdmin off its default address. Bots scan for /phpmyadmin. They do not scan for a random path only you know, such as /db-panel-9f3k. Never link to it from any public page.
With Nginx, find the location block that serves phpMyAdmin and rename it:
location /db-panel-9f3k/ {
alias /usr/share/phpmyadmin/;
index index.php;
}
With Apache, change the alias in /etc/phpmyadmin/apache.conf:
Alias /db-panel-9f3k /usr/share/phpmyadmin
Reload the web server and test the new address in a private window. The old one should return a 404. Most bots never try a custom path, so this alone cuts the scanning noise in your access log to almost nothing. It is not real security on its own, which is why the next layers still matter.
To change phpMyAdmin url safely, keep the old alias disabled instead of redirecting it. A redirect tells anyone who finds the old address where the panel moved.
Add a Second Password with htpasswd
phpMyAdmin htpasswd protection puts a second login screen in front of the panel. Even if someone guesses a MySQL password, they still need the web-server password first.
First, create a password file outside the web root:
sudo htpasswd -c /etc/nginx/.pma-passwd dbadmin
Skip -c when adding users later, or it overwrites the file. Then require that password in the phpMyAdmin location block. For Nginx:
location /db-panel-9f3k/ {
alias /usr/share/phpmyadmin/;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.pma-passwd;
index index.php;
}
For Apache, add the same two directives inside the phpMyAdmin directory block. Reload the web server and open the panel: a browser password prompt should appear before the login form.
Use a different password here than your MySQL password, and keep the file outside any served directory, with permissions 640.
Restrict phpMyAdmin by IP Address
If you only manage the database from one or two places, an IP allowlist is the strongest simple control. To restrict phpMyAdmin by IP address in Nginx, add allow and deny rules inside the same location block:
location /db-panel-9f3k/ {
alias /usr/share/phpmyadmin/;
allow 203.0.113.42;
deny all;
index index.php;
}
Replace 203.0.113.42 with your own public IP address; list several allow lines for home and office. For Apache, the equivalent is Require ip 203.0.113.42 inside the directory block.
This works best when your IP rarely changes. If your home IP is dynamic, keep an SSH session open while you test, or you will lock yourself out when it changes. As a safety net, a VPN gives you a stable IP to allowlist. If you run your own, you can lock down the firewall with UFW rules at the same time so only the ports you actually use stay open.
One warning: an IP allowlist alone is not enough on a shared office network, because anyone on the same network shares the address. Pair it with the htpasswd layer above.
Turn On phpMyAdmin Two-Factor Authentication
phpMyAdmin two-factor authentication adds a time-based code from an authenticator app to the normal login. Version 5.1 and later ships with it built in. You need the phpmyadmin configuration storage database set up first, because 2FA settings live there. On a Debian or Ubuntu package install, those tables usually already exist.
Open /etc/phpmyadmin/config.inc.php and check that the pmadb lines are uncommented. Then log in, open Settings, go to the Two-factor authentication tab, enable the application option, and scan the QR code with your authenticator app.
From then on, every login asks for the six-digit code after the password. Store the recovery codes somewhere safe. If you lose your phone with no recovery code, you will have to disable 2FA from the command line by editing the configuration storage tables.
Add it to SSH too, since the same server holds the keys to everything. Our guide to setting up two-factor authentication for SSH covers that side.
Stop Logging In as Root
The root MySQL account can do anything, including dropping databases. Your daily admin work does not need that power. Create a separate MySQL user for phpMyAdmin with rights limited to the databases you manage:
CREATE USER 'pma_admin'@'localhost' IDENTIFIED BY 'a-long-random-password'; GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, DROP ON wordpress_db.* TO 'pma_admin'@'localhost'; FLUSH PRIVILEGES;
Then disable root login through the panel. In config.inc.php, add:
$cfg['Servers'][$i]['AllowRoot'] = false;
While you are in that file, set two more options. A long random blowfish secret protects the login cookie, and forcing SSL stops the panel from working over plain HTTP:
$cfg['blowfish_secret'] = 'paste-a-32-character-random-string-here'; $cfg['ForceSSL'] = true; $cfg['LoginCookieValidity'] = 1800;
The last line logs you out after 30 minutes of inactivity, limiting the damage of a session left open on a shared computer. Generate the blowfish secret with openssl rand -base64 24 and never reuse a value copied from a tutorial.
For routine jobs that only read data, go further and use a dedicated read-only account. Our walkthrough on creating a read-only MySQL user for WordPress backups shows the exact grants to use.
Add Fail2Ban and Keep phpMyAdmin Updated
Even with every layer above, failed login attempts will still show up in the logs. Fail2Ban watches those logs and blocks the source IP after a few failures. If you have not installed it yet, follow our Fail2Ban setup guide for SSH and Nginx first, then add a filter that matches phpMyAdmin authentication failures.
Updates matter just as much. phpMyAdmin gets security releases when vulnerabilities are found, and bots scan for old versions. On Ubuntu, update the package regularly:
sudo apt update && sudo apt upgrade phpmyadmin
If you installed phpMyAdmin by hand, check the project download page for new releases and update it yourself. Check the version on the phpMyAdmin home page once a month. An old panel with perfect configuration is still an old panel.
Finally, consider whether you need phpMyAdmin at all. If you open it once a month, manage MySQL from the command line instead. A panel that does not exist cannot be attacked.
Test Your Access Before You Log Out
Every layer here can lock out the wrong person, including you. Test in this order before you close your current session:
- Open the new URL in a private window and confirm the old URL returns a 404.
- Log in with the htpasswd password, then the MySQL user and the 2FA code.
- Try logging in from a different IP or with the wrong password to confirm the blocks work.
- Confirm the root account cannot log in through the panel.
- Check that your site and WordPress admin still work, since a bad Nginx edit can break more than the panel.
Keep one SSH session open while you test. If a rule locks you out, fix the config from that session instead of waiting for your hosting provider to rescue the server. Once every test passes, write down the new URL, the htpasswd username, and where the recovery codes live.
| Layer | What it stops | Effort |
|---|---|---|
| Custom URL | Automated scanners hitting the default path | 5 minutes |
| htpasswd second password | Password guessers | 10 minutes |
| IP allowlist | Logins from other addresses | 10 minutes |
| Two-factor authentication | Stolen passwords | 15 minutes |
| Non-root MySQL user | Full takeover from one login | 10 minutes |
| Fail2Ban and updates | Repeated guessing, known flaws | 30 minutes |
—