How to Install & Secure phpMyAdmin for Nginx on Ubuntu 24.04

Install phpMyAdmin on Nginx for Ubuntu 24.04 – DevAnswers.net guide

This guide covers Ubuntu 24.04. Read the Ubuntu 26.04 edition.

We may earn a referral fee at no additional cost to you when you shop through the links on this page.

Installing phpMyAdmin on Nginx gives you a friendly database GUI without exposing your MariaDB root credentials to the wider internet. This DevAnswers guide walks through a complete, hardened install on Ubuntu 24.04 LTS (Noble Numbat) with the distro’s stock Nginx 1.24, PHP 8.3, and MariaDB 10.11 from the official repositories, fully supported until May 2029.

The walkthrough installs phpMyAdmin from the distro repositories (skipping the Apache auto-config since nginx isn’t an option in dbconfig), creates a clean nginx server block with PHP-FPM wired in, hardens with URL obfuscation and an HTTP Basic Auth gateway, and ends with a dedicated database login user so you never have to type the root password into a web form.

Prerequisites 

You need a running LEMP stack on Ubuntu 24.04 (Noble Numbat) (Nginx 1.24, PHP 8.3 with PHP-FPM, and MariaDB 10.11) before installing phpMyAdmin. If you haven’t built one yet, follow our complete LEMP stack guide for Ubuntu 24.04 (Noble Numbat) first.

  • A non-root user with sudo privileges (see our initial server setup guide if you haven’t created one).
  • The MariaDB root account reachable via sudo mysql (the unix_socket default on modern Ubuntu 24.04 (Noble Numbat)).
  • Recommended: a domain name pointed at the server. Examples below use devanswers.test; substitute your real domain.

Install phpMyAdmin and Required Extensions 

The distro packages bundle phpMyAdmin as static PHP files plus an apache.conf. We’re using nginx, so we’ll skip the auto-config step and wire up our own server block in a moment. You also need php-mbstring for character-encoding support and php-zip for the import/export feature, both of which phpMyAdmin complains about if missing.

Terminal
sudo apt update
sudo apt install -y phpmyadmin php8.3-mbstring php8.3-zip

The installer drops you into an interactive debconf prompt. Three answers matter:

  1. Web server to configure automatically: press Space to leave nothing ticked (nginx isn’t an option), then Tab and Enter. We’ll set up nginx manually below.
  2. Configure database for phpmyadmin with dbconfig-common: choose Yes. This creates the internal storage database phpMyAdmin uses for bookmarks, query history, and column comments.
  3. MySQL application password for phpmyadmin: enter a strong generated password (or leave blank to let dbconfig generate one). This is the password for the phpmyadmin internal user, not your login account.

The phpMyAdmin package files now live under /usr/share/phpmyadmin. They’re not yet served by nginx; we’ll wire that up next.

Configure Nginx for phpMyAdmin 

Create a small nginx server block — or location block in your existing server — that serves /usr/share/phpmyadmin at the URL of your choice. The example below sets up a dedicated server block for devanswers.test serving phpMyAdmin at /phpmyadmin (which we’ll change to an obfuscated path in the next section).

Terminal
sudo nano /etc/nginx/sites-available/phpmyadmin
/etc/nginx/sites-available/phpmyadmin
server {
    listen 80;
    listen [::]:80;
    server_name devanswers.test;

    access_log /var/log/nginx/phpmyadmin-access.log;
    error_log /var/log/nginx/phpmyadmin-error.log;

    location ^~ /phpmyadmin {
        alias /usr/share/phpmyadmin/;
        index index.php;

        location ~ ^/phpmyadmin/(.+\.php)$ {
            alias /usr/share/phpmyadmin/$1;
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /usr/share/phpmyadmin/$1;
        }

        location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt|svg|woff|woff2|ttf))$ {
            alias /usr/share/phpmyadmin/$1;
        }
    }
}

Save and exit (press CTRL + X, press Y, then press ENTER)

The nested location ~ ^/phpmyadmin/(.+\.php)$ block is what makes this work. alias + SCRIPT_FILENAME together translate the request URI back to the real path on disk so PHP-FPM executes the right file. Without it, nginx would serve index.php as a static download instead of executing it (the “PHP downloads instead of executing” symptom in Troubleshooting).

Enable the site, test the config, and reload nginx:

Terminal
sudo ln -s /etc/nginx/sites-available/phpmyadmin /etc/nginx/sites-enabled/phpmyadmin
sudo nginx -t
sudo systemctl reload nginx

Verify by visiting http://devanswers.test/phpmyadmin/ (or your server’s IP if you don’t have a domain yet). You should see the phpMyAdmin login screen.

You will see the login screen, but you cannot sign in yet. First create a dedicated login user (the next section): on modern systems the root account uses socket authentication and cannot log in through the phpMyAdmin form, so this step is required for every install, whether local or internet-facing. The two sections after it (obfuscating the URL and adding an auth gateway) are only needed if phpMyAdmin will be reachable from the internet. For a local or LAN-only install, you can stop once the login user exists.

Create a Dedicated phpMyAdmin Login User 

Don’t log into phpMyAdmin as root. On modern Ubuntu 24.04 (Noble Numbat), the root account uses unix_socket authentication (which only works for local shell access), so password login as root fails at the phpMyAdmin login screen. This step is required whether or not phpMyAdmin will ever face the internet: without a password-authenticated user, you cannot log in at all. Create a dedicated database user with the privileges you actually need.

Terminal
sudo mysql

At the prompt, create a privileged user. GRANT ALL ... WITH GRANT OPTION makes this an effectively all-powerful administrative account, which suits a single-administrator phpMyAdmin box where you manage every database. If you only ever work with one application database, a narrower grant such as GRANT ALL ON app_db.* is safer. Replace change-this-strong-password with a strong generated password before running:

MySQL
CREATE USER 'pmaadmin'@'localhost' IDENTIFIED BY 'change-this-strong-password';
GRANT ALL PRIVILEGES ON *.* TO 'pmaadmin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;

You’ll use pmaadmin as the phpMyAdmin login username from now on. The phpmyadmin internal user that dbconfig created stays as a system account; never log in with it directly.

Obfuscate the phpMyAdmin URL 

Security through obscurity isn’t a real layer, but it blocks the overwhelming majority of automated scanners, which only ever probe the default path. /phpmyadmin is one of the most heavily scanned URLs on the internet after /wp-admin. Changing it to something random kills automated brute-force attempts before they reach phpMyAdmin’s own authentication layer.

Open the nginx server block:

Terminal
sudo nano /etc/nginx/sites-available/phpmyadmin

Change only the URL path in the three location directives to a random path. Use a passphrase or random string you’ll remember; the example below uses secret-db-92xq7m. These are the three location lines (the outer block plus the two inner blocks); leave the filesystem paths /usr/share/phpmyadmin/, the SCRIPT_FILENAME values, and the log filenames unchanged:

/etc/nginx/sites-available/phpmyadmin
    location ^~ /secret-db-92xq7m {
        alias /usr/share/phpmyadmin/;
        index index.php;

        location ~ ^/secret-db-92xq7m/(.+\.php)$ {
            alias /usr/share/phpmyadmin/$1;
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /usr/share/phpmyadmin/$1;
        }

        location ~* ^/secret-db-92xq7m/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt|svg|woff|woff2|ttf))$ {
            alias /usr/share/phpmyadmin/$1;
        }
    }

Save and exit (press CTRL + X, press Y, then press ENTER)

Save and reload nginx:

Terminal
sudo nginx -t
sudo systemctl reload nginx

The old /phpmyadmin URL now 404s; phpMyAdmin is reachable only at http://devanswers.test/secret-db-92xq7m. Note this URL: you’ll need it again in a moment.

Add an HTTP Basic Auth Gateway 

Nginx’s auth_basic directive puts a password gate in front of phpMyAdmin. An attacker who somehow discovers your obfuscated URL still has to break a separate password before reaching the phpMyAdmin login form. Two independent password hurdles defeats every commodity brute-force tool.

Install the Apache password-file utility (it ships in apache2-utils on Ubuntu and Debian even when you’re not running Apache):

Terminal
sudo apt install -y apache2-utils

Create a password file outside the web root so it can’t be served as static content. Replace gatekeeper with whatever username you want for the gateway:

Terminal
sudo htpasswd -c /etc/nginx/.htpasswd gatekeeper

You’ll be prompted to enter and confirm a password. The -c flag creates the file; omit it when adding additional users later.

Now wire the password file into your obfuscated location block. Open the config:

Terminal
sudo nano /etc/nginx/sites-available/phpmyadmin

Add the two auth_basic lines at the top of the outer location block (just below index index.php;):

/etc/nginx/sites-available/phpmyadmin
    location ^~ /secret-db-92xq7m {
        alias /usr/share/phpmyadmin/;
        index index.php;

        # HTTP Basic Auth gateway (added by us)
        auth_basic "Restricted Access";
        auth_basic_user_file /etc/nginx/.htpasswd;

        location ~ ^/secret-db-92xq7m/(.+\.php)$ {
            # ...existing fastcgi block stays here...
        }
        # ...existing static-asset block stays here...
    }

Save and exit (press CTRL + X, press Y, then press ENTER)

The nested location blocks inherit the auth_basic setting from their parent, so both PHP execution and static assets are protected by the same gate. Save and reload:

Terminal
sudo nginx -t
sudo systemctl reload nginx

Browse to your obfuscated URL again. Before phpMyAdmin’s login form loads, nginx pops a browser-native authentication dialog: enter your gateway username (gatekeeper) and password. Only then do you reach phpMyAdmin’s own login, where you sign in as pmaadmin.

Verify the Setup End-to-End 

Run through the full happy path one more time to confirm both layers work:

  • http://devanswers.test/phpmyadmin returns a 404 (the old URL is gone).
  • http://devanswers.test/secret-db-92xq7m triggers the HTTP Basic Auth dialog. Wrong gateway password returns HTTP 401.
  • After the gateway, the phpMyAdmin login form loads. Logging in as pmaadmin with the correct password lands you on the phpMyAdmin dashboard. Wrong pmaadmin password returns to the login form with an error.
  • Confirm you can see all databases on the left sidebar (your grant covers *.*).

Optional: Harden config.inc.php 

Two more low-cost settings improve the security posture. Edit phpMyAdmin’s local config (created automatically by dbconfig):

Terminal
sudo nano /etc/phpmyadmin/config.inc.php

Add (or set) the following lines near the bottom of the file, before the final ?> if it exists:

/etc/phpmyadmin/config.inc.php
// Disallow login with an empty password.
$cfg['Servers'][$i]['AllowNoPassword'] = false;

// Shorten the admin login session to 15 minutes (default is 1440 seconds, 24 minutes).
$cfg['LoginCookieValidity'] = 900;

Save and exit (press CTRL + X, press Y, then press ENTER)

phpMyAdmin has no built-in force-HTTPS option (the old ForceSSL setting was removed back in version 4.6). Enforce HTTPS at the web server instead: once you have a TLS certificate, add an nginx redirect from port 80 to 443 (return 301 to the https URL) and a Strict-Transport-Security header so the admin session is always encrypted. See Next Steps below.

Reload PHP-FPM one more time so the new config is picked up:

Terminal
sudo systemctl reload php8.3-fpm

Troubleshooting 

Three issues come up often enough to call out:

  • Obfuscated URL still 404s after reload: check that you replaced all three occurrences of /phpmyadmin in the server block (the outer location plus the two nested ones). Run sudo nginx -t and look at the error log: sudo tail -f /var/log/nginx/phpmyadmin-error.log.
  • PHP downloads instead of executing: your fastcgi block isn’t matching. Confirm the inner location ~ ^/your-path/(.+\\.php)$ regex matches the URL you’re actually hitting (don’t forget to update it when you rename the alias).
  • “Cannot start session without errors” on the login form: the session directory is unwritable. On Debian and Ubuntu it ships as /var/lib/php/sessions owned by root:root with mode 1733 (world-writable but deliberately not listable), so PHP-FPM (running as www-data) can write to it. Confirm it exists with sudo ls -ld /var/lib/php/sessions; if a custom session.save_path is set in php.ini, make sure that path is writable by www-data.
  • Forgot the obfuscated URL: grep the nginx config for it: sudo grep '^\\s*location' /etc/nginx/sites-available/phpmyadmin.

Next Steps 

  • Add HTTPS with a free Let’s Encrypt certificate: follow our Secure Nginx with Let’s Encrypt SSL on Ubuntu 24.04 (Noble Numbat) guide. Once HTTPS is working, add an nginx redirect from port 80 to 443 (plus a Strict-Transport-Security header) so admin sessions are always encrypted.
  • Restrict access by IP (paranoid mode): add an allow 1.2.3.4; deny all; pair inside the location block to allow only your own IP. Effectively makes phpMyAdmin a private interface.
  • Set up a daily database backup: see our mysqldump + cron backup guide, or use a managed backup add-on. Install phpMyAdmin without a backup story and the convenience becomes a liability the first time someone clicks “Drop” on the wrong table.

Conclusion 

Your Ubuntu 24.04 server now hosts a hardened phpMyAdmin reachable only at an obfuscated URL, behind an HTTP Basic Auth gateway, with a dedicated database administrator account in place of root. Nginx is configured as a clean location block with PHP-FPM handling all PHP execution.

phpMyAdmin is a sharp tool with a wide blast radius — one careless DROP TABLE on a production database has no undo. The next step is a free Let’s Encrypt certificate so credentials don’t travel in plain text, then automated daily database backups. The convenience of a web GUI for MariaDB becomes a liability the first time someone (likely you) clicks the wrong row delete button without one.

Support DevAnswers
Written, tested and maintained by one developer. Your support keeps these guides accurate and ad-light.
Buy me a coffee

Comments

Leave a reply