Back to tutorials
Tutorial

Install phpMyAdmin on a VPS Behind Nginx

Install phpMyAdmin on Nginx with TLS, IP allowlists, and hardening steps for safer DB access on your VPS.

By Anurag Singh
Updated on Dec 05, 2026
Category: Tutorial
Share article
Install phpMyAdmin on a VPS Behind Nginx

You don’t need phpMyAdmin on the open internet to use it well. This walkthrough shows how to install phpMyAdmin on Nginx on a VPS, serve it over HTTPS, restrict it to known IPs, and add an extra login gate. The goal is a usable admin tool that is much harder to abuse.

The steps assume a single VPS (or dedicated server) on Ubuntu/Debian. Where AlmaLinux/Rocky differ, you’ll see notes.

If you want a clean baseline for this setup, start with a stable VM such as a HostMyCode VPS. It gives you full control of firewall rules and Nginx config.

What you’ll build (and why this is safer)

  • Nginx serving phpMyAdmin over HTTPS (Let’s Encrypt)
  • IP allowlist so only your office/home/VPN can reach it
  • Extra auth gate (HTTP Basic Auth) before phpMyAdmin even loads
  • Hardened phpMyAdmin settings + database user restrictions
  • Logging and quick verification so you can confirm it’s working and protected

Prerequisites and assumptions

  • A VPS with root (or sudo) access
  • A domain name pointing to the server (for TLS). If you don’t have one yet, register and manage DNS via HostMyCode domains.
  • Nginx installed and running
  • PHP-FPM installed (PHP 8.2+ recommended; PHP 8.3 works well in 2026)
  • MariaDB or MySQL reachable locally on the same server (common in VPS hosting)

If you still need PHP-FPM on Nginx, follow this HostMyCode tutorial first: Install PHP 8.3 on Ubuntu with Nginx and PHP-FPM.

Step 1: Update packages and install required software

On Ubuntu/Debian:

sudo apt update
sudo apt -y upgrade
sudo apt -y install nginx certbot python3-certbot-nginx \
  php-fpm php-mysql php-mbstring php-zip php-gd php-json php-curl php-xml php-bz2 \
  unzip apache2-utils

Confirm Nginx and PHP-FPM are running:

systemctl status nginx --no-pager
systemctl status php*-fpm --no-pager

On AlmaLinux/Rocky, package names differ. phpMyAdmin also often comes from EPEL on RHEL-family distros.

If you want the least hassle on RHEL-family, consider using a managed environment via managed VPS hosting. It avoids the usual repo and PHP version friction when you just want a reliable admin interface.

Step 2: Install phpMyAdmin (repeatable, distro-agnostic)

Repo packages can lag. They also ship with different defaults between distros.

To keep this predictable, install phpMyAdmin from the official release zip into /var/www/phpmyadmin. Then run it through PHP-FPM behind Nginx.

Pick a current phpMyAdmin 5.2.x release (stable in 2026). Check the official site for the latest minor. Example:

cd /tmp
PMA_VER="5.2.1"
wget -O phpmyadmin.zip "https://files.phpmyadmin.net/phpMyAdmin/${PMA_VER}/phpMyAdmin-${PMA_VER}-all-languages.zip"

sudo mkdir -p /var/www/phpmyadmin
sudo unzip -q phpmyadmin.zip -d /var/www/phpmyadmin
sudo mv "/var/www/phpmyadmin/phpMyAdmin-${PMA_VER}-all-languages" /var/www/phpmyadmin/public
sudo rm -f phpmyadmin.zip

Set ownership so Nginx/PHP-FPM can read the files:

sudo chown -R www-data:www-data /var/www/phpmyadmin

Create the temp directory phpMyAdmin uses. Then lock down permissions:

sudo mkdir -p /var/www/phpmyadmin/tmp
sudo chown -R www-data:www-data /var/www/phpmyadmin/tmp
sudo chmod 700 /var/www/phpmyadmin/tmp

Step 3: Create a strong blowfish secret and basic phpMyAdmin config

phpMyAdmin uses a blowfish secret to protect cookie-based sessions. Generate a long random value:

openssl rand -base64 48

Create /var/www/phpmyadmin/public/config.inc.php:

sudo nano /var/www/phpmyadmin/public/config.inc.php
<?php

declare(strict_types=1);

$cfg['blowfish_secret'] = 'PASTE_YOUR_RANDOM_STRING_HERE';

$i = 0;
$i++;
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['host'] = '127.0.0.1';
$cfg['Servers'][$i]['compress'] = true;
$cfg['Servers'][$i]['AllowNoPassword'] = false;

// Hardening
$cfg['ForceSSL'] = true;
$cfg['ExecTimeLimit'] = 300;
$cfg['LoginCookieValidity'] = 1800; // 30 minutes
$cfg['ShowAll'] = false;

// Where to store temp files
$cfg['TempDir'] = '/var/www/phpmyadmin/tmp';

Common mistake: using an empty or short blowfish secret. phpMyAdmin will warn you. It also weakens session cookie protection.

Step 4: Add an Nginx server block for an internal-only admin URL

Use a dedicated hostname such as dbadmin.example.com. This keeps rules, logs, and certificates scoped to one tool.

Create the Nginx site:

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

Paste this (adjust the domain):

server {
    listen 80;
    server_name dbadmin.example.com;

    root /var/www/phpmyadmin/public;
    index index.php;

    # Block any direct access until HTTPS is enabled (we'll replace this after certbot)
    location / {
        return 301 https://$host$request_uri;
    }
}

Enable it and test the config:

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

Step 5: Issue a Let’s Encrypt certificate and force HTTPS

Before running Certbot, confirm dbadmin.example.com resolves to your VPS IP.

sudo certbot --nginx -d dbadmin.example.com

If Certbot offers an HTTP→HTTPS redirect option, take it.

Then confirm renewal is enabled and working:

sudo systemctl status certbot.timer --no-pager
sudo certbot renew --dry-run

Step 6: Add IP allowlisting and HTTP Basic Auth (two gates)

Think of this as layered defense.

The allowlist blocks drive-by scanners. Basic Auth adds another credential check before phpMyAdmin loads.

Create Basic Auth credentials

sudo htpasswd -c /etc/nginx/.htpasswd-phpmyadmin admin

Use a long password. Store it in a password manager.

Update the HTTPS server block

Certbot typically adds a new 443 server block. Edit the resulting file.

On Ubuntu/Debian it’s usually still /etc/nginx/sites-available/phpmyadmin.

Replace the HTTPS block with the one below. Keep the SSL lines Certbot added. If you’re not sure, re-run Certbot.

sudo nano /etc/nginx/sites-available/phpmyadmin
server {
    listen 443 ssl http2;
    server_name dbadmin.example.com;

    root /var/www/phpmyadmin/public;
    index index.php;

    # Certbot-managed lines (example paths)
    ssl_certificate /etc/letsencrypt/live/dbadmin.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dbadmin.example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # Security headers (sane defaults)
    add_header X-Content-Type-Options nosniff always;
    add_header X-Frame-Options SAMEORIGIN always;
    add_header Referrer-Policy strict-origin-when-cross-origin always;

    # 1) IP allowlist (edit these)
    allow 203.0.113.10;  # office static IP
    allow 198.51.100.25; # home static IP
    allow 10.0.0.0/8;    # private network (VPN / internal)
    deny all;

    # 2) Basic Auth gate
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd-phpmyadmin;

    # Hide dotfiles
    location ~ /\. {
        deny all;
    }

    # Main app
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # PHP handler
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # Adjust for your PHP-FPM version:
        # Ubuntu often uses /run/php/php8.3-fpm.sock
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

        # phpMyAdmin imports can take time
        fastcgi_read_timeout 300;
    }

    # Block access to setup scripts
    location ~* /(setup|doc|examples)/ {
        deny all;
    }

    # Limit upload size for SQL imports (adjust)
    client_max_body_size 64m;
}

server {
    listen 80;
    server_name dbadmin.example.com;
    return 301 https://$host$request_uri;
}

Test and reload:

sudo nginx -t
sudo systemctl reload nginx

Verify both gates:

  • From a non-allowed IP/network, you should get 403 Forbidden.
  • From an allowed IP, you should get a browser auth prompt.
curl -I https://dbadmin.example.com/

If your IP is allowed, you should see HTTP/2 401 (the Basic Auth challenge).

If you see 403 even though you “should” be allowed, you’re likely exiting through a different public IP. Common causes are mobile carrier NAT (CGNAT) and corporate proxies.

Step 7: Tighten the database side (least privilege and safer auth)

phpMyAdmin is only a client. The real risk is the database account you use inside it.

Create a dedicated user. Give it only the privileges required for the database(s) you manage.

Log into MariaDB/MySQL locally:

sudo mysql

Create a user that can manage a single database (example: appdb):

CREATE USER 'appdb_admin'@'localhost' IDENTIFIED BY 'REPLACE_WITH_LONG_RANDOM_PASSWORD';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE VIEW, SHOW VIEW
ON appdb.* TO 'appdb_admin'@'localhost';
FLUSH PRIVILEGES;

Common mistake: logging in as root or granting ALL PRIVILEGES on *.*. Avoid it. If your session gets phished or a browser profile leaks, you lose the whole database server.

If you need occasional administrative actions (creating databases/users), create a separate “admin” user. Use it only when needed. Keep that password out of day-to-day workflows.

Step 8: Lock down phpMyAdmin behavior and reduce attack surface

A couple of small choices make day-to-day use safer:

  • Don’t normalize root logins. Keep root out of routine work and use least-privilege accounts.
  • Keep sessions short, especially if you leave tabs open.

You already set $cfg['LoginCookieValidity'] to 30 minutes. If you want a stricter default, set it to 10 minutes (600).

Also confirm PHP limits are reasonable for imports. You want “works when needed,” not “wide open.” Check:

php -i | egrep "upload_max_filesize|post_max_size|max_execution_time"

On Ubuntu, you’ll typically change FPM values in:

  • /etc/php/8.3/fpm/php.ini
  • /etc/php/8.3/fpm/pool.d/www.conf (if you’re setting per-pool limits)

After changes:

sudo systemctl restart php8.3-fpm

Step 9: Add firewall rules so only HTTP/HTTPS/SSH are exposed

Nginx and Certbot need ports 80 and 443. Keep SSH as tight as your workflow allows. Ideally, limit it to your admin IPs.

Using UFW on Ubuntu/Debian:

sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose

If you haven’t hardened SSH yet, do that next. This HostMyCode tutorial fits well with any admin UI setup: set up SSH keys and disable password login.

Step 10: Logging, quick diagnostics, and common breakages

If phpMyAdmin shows a blank page or you hit a login loop, go straight to logs. The cause usually appears in Nginx errors or PHP-FPM output.

Check Nginx logs for 403/401 issues

sudo tail -n 100 /var/log/nginx/access.log
sudo tail -n 100 /var/log/nginx/error.log

If you see access forbidden by rule, the allowlist is blocking you. Confirm your current public IP:

curl -s https://ifconfig.me; echo

Update the allow lines, then reload Nginx.

Check PHP-FPM for missing extensions

phpMyAdmin often warns you in the UI. Some extension errors show up only in logs, though. Check:

sudo journalctl -u php8.3-fpm --since "30 min ago" --no-pager

Common missing pieces: mbstring, zip, or xml. Install what’s missing, then restart FPM.

Confirm TLS is actually used

curl -I http://dbadmin.example.com/ | head
curl -I https://dbadmin.example.com/ | head

HTTP should redirect to HTTPS. HTTPS should return 401 (Basic Auth) from allowed IPs.

Optional: Put phpMyAdmin on a private network (VPN) instead of public DNS

If your workflow allows it, the safest model is simple: no public DNS. Run phpMyAdmin on an internal hostname. Reach it only through a VPN (WireGuard/Tailscale).

Keep HTTPS and Basic Auth anyway. You’re removing exposure, not removing controls.

If you do need public DNS, the allowlist + Basic Auth pairing above gets you most of the benefit without adding more components.

Operational checklist (printable)

  • HTTPS works and auto-renews (certbot renew --dry-run)
  • Nginx allowlist blocks non-approved IPs (verify with a mobile hotspot)
  • Basic Auth enabled and credentials stored in a password manager
  • No database root login used in daily work
  • Least-privilege DB users per app/database
  • Firewall allows only SSH/80/443
  • Nginx/PHP-FPM logs checked after initial login and after imports

Related HostMyCode tutorials you’ll actually use

Summary: a usable phpMyAdmin without the usual exposure

You now have phpMyAdmin running behind Nginx with HTTPS, an IP allowlist, and Basic Auth in front of the login screen. This stops most automated probing. It also makes credential stuffing much harder.

Long-term safety comes down to habits. Stick to least-privilege DB users and keep session timeouts short.

If you want a stable place to run admin tools like this—where you control Nginx, firewall rules, and backups—use a HostMyCode VPS. If you prefer to hand off OS updates, web stack tuning, and baseline security, managed VPS hosting is the practical upgrade.

If phpMyAdmin is part of your day-to-day work, run it on infrastructure you trust. A properly sized HostMyCode VPS gives PHP-FPM and your database consistent performance, plus the control you need to restrict access correctly. If your team would rather not babysit patching and hardening, managed VPS hosting keeps admin endpoints safer with less ongoing maintenance.

FAQ

Should I install phpMyAdmin on the same server as MySQL/MariaDB?

For small deployments, yes—local-only database access (127.0.0.1) is simpler and safer. If your database is on a separate server, use TLS and strict grants, and avoid opening DB ports to the public internet.

Can I hide phpMyAdmin behind a URL path instead of a subdomain?

You can, but a subdomain makes it easier to apply dedicated security rules and logs. If you must use a path (like /pma), keep the same allowlist and Basic Auth protections.

Why use Basic Auth if I already have phpMyAdmin login?

Basic Auth blocks automated scanners before phpMyAdmin loads. It also prevents anyone who guesses (or steals) DB credentials from reaching the login form unless they pass the extra gate.

I’m locked out after adding the IP allowlist. What’s the fastest fix?

Comment out the deny all; line temporarily, reload Nginx, confirm your real public IP with curl https://ifconfig.me, then add the correct allow entry and re-enable deny all;.

How do I uninstall phpMyAdmin cleanly?

Remove the Nginx site config and the directory:

sudo rm -f /etc/nginx/sites-enabled/phpmyadmin
sudo rm -f /etc/nginx/sites-available/phpmyadmin
sudo rm -rf /var/www/phpmyadmin
sudo nginx -t && sudo systemctl reload nginx

If you issued a certificate just for this hostname, you can remove it with sudo certbot delete.

Install phpMyAdmin on a VPS Behind Nginx | HostMyCode