LinuxPublished on February 6, 20266 min read

Install and secure phpMyAdmin with Nginx on Debian 13

phpMyAdmin with Nginx gives you a web interface to manage your MariaDB or MySQL databases without typing SQL queries. This guide installs phpMyAdmin on Debian 13 behind Nginx, with HTTPS, restricted access and protection against brute-force attacks. Left unprotected, phpMyAdmin quickly draws the bots that scan the web.

You need a VPS running Debian 13 with sudo access and, for HTTPS, a subdomain that points to its IP, for example pma.example.com.


Prepare Debian 13

Update the system, then reboot if the kernel has changed.

sudo apt update && sudo apt -y full-upgrade
sudo reboot

Install Nginx, PHP-FPM and the extensions phpMyAdmin needs. Debian 13 ships PHP 8.4.

sudo apt install -y nginx php-fpm php-cli php-mbstring php-xml php-zip php-curl php-mysql php-gd php-intl php-bcmath
sudo apt install -y php-imagick php-apcu

Check the PHP version and the PHP-FPM socket. You will need them in the Nginx configuration.

php -v
ls -l /run/php/ | grep fpm
# On Debian 13, the socket is called php8.4-fpm.sock

The guide Install Nginx and PHP-FPM on a Linux VPS covers this stack and its tuning.


Install and secure MariaDB

sudo apt install -y mariadb-server mariadb-client
sudo mariadb-secure-installation

On Debian 13, the wizard is called mariadb-secure-installation. The old name, mysql_secure_installation, only exists if the mariadb-client-compat package is installed. Answer the questions as follows.

  • Keep Unix socket authentication for root. The SQL root account is then only reachable with sudo on the VPS.

  • Remove anonymous users.

  • Disallow remote root login.

  • Remove the test database, then reload the privileges.

Create a dedicated user

Do not log in to phpMyAdmin as root. Create a separate admin account. Open the SQL shell.

sudo mariadb

Then type these queries, with your own password.

CREATE USER 'pma_admin'@'localhost' IDENTIFIED BY 'StrongPassword_ChangeMe';
GRANT ALL PRIVILEGES ON *.* TO 'pma_admin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;

This is the account you will use to log in to phpMyAdmin.


Install phpMyAdmin

With apt

The Debian 13 package is the easiest to maintain, as it follows system updates.

sudo apt install -y phpmyadmin

When asked about the web server, tick nothing. Nginx is configured below. Accept dbconfig-common and choose a password for the internal pma user. The files go to /usr/share/phpmyadmin and the configuration to /etc/phpmyadmin/config.inc.php.

With the official archive

The archive gives you the latest version released on phpmyadmin.net. You update it yourself.

sudo mkdir -p /var/www/pma && cd /var/www/pma
sudo wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
sudo tar xzf phpMyAdmin-*-all-languages.tar.gz
sudo mv phpMyAdmin-*-all-languages phpmyadmin
sudo chown -R www-data:www-data /var/www/pma/phpmyadmin

Create the configuration file and generate the cookie secret key. It must be exactly 32 bytes long, which is 64 hexadecimal characters.

sudo -u www-data cp /var/www/pma/phpmyadmin/config.sample.inc.php /var/www/pma/phpmyadmin/config.inc.php
openssl rand -hex 32
sudo -u www-data nano /var/www/pma/phpmyadmin/config.inc.php

Paste the generated value in place of the example one.

$cfg['blowfish_secret'] = sodium_hex2bin('f16ce59f45714194371b48fe362072dc3b019da7861558cd4ad29e4d6fb13851');

With this method, replace /usr/share/phpmyadmin with /var/www/pma/phpmyadmin in the Nginx configuration.


Configure Nginx

A dedicated subdomain is the cleanest option. Access restrictions then apply to all of phpMyAdmin, PHP scripts included.

On a dedicated subdomain

Create the file /etc/nginx/sites-available/pma.conf. It starts out on HTTP. Certbot adds HTTPS in the next step.

server {
    listen 80;
    listen [::]:80;
    server_name pma.example.com;

    root /usr/share/phpmyadmin;
    index index.php;
    access_log /var/log/nginx/pma.access.log;

    add_header X-Frame-Options SAMEORIGIN always;
    add_header X-Content-Type-Options nosniff always;
    add_header Referrer-Policy no-referrer-when-downgrade always;

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

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php8.4-fpm.sock;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_read_timeout 120s;
    }

    location ~* \.(?:css|js|ico|gif|jpe?g|png|svg)$ {
        access_log off;
        expires 7d;
    }
}

Enable the site, test the configuration and reload Nginx.

sudo ln -s /etc/nginx/sites-available/pma.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

On an existing site, under /phpmyadmin

Add these blocks to the site's server block, before its location ~ \.php$ block. Nginx checks regular expression blocks in file order.

location /phpmyadmin {
    alias /usr/share/phpmyadmin;
    index index.php;
    try_files $uri $uri/ /phpmyadmin/index.php?$args;
}

location ~ ^/phpmyadmin/(.+\.php)$ {
    alias /usr/share/phpmyadmin/$1;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_pass unix:/run/php/php8.4-fpm.sock;
    fastcgi_read_timeout 120s;
}

location ~* ^/phpmyadmin/(.+\.(?:png|jpg|jpeg|gif|css|js|ico|svg))$ {
    alias /usr/share/phpmyadmin/$1;
    access_log off;
    expires 7d;
}

With this method, a restriction placed in location /phpmyadmin does not cover PHP scripts, which the next block handles. To restrict access, prefer the subdomain.


Enable HTTPS with Let's Encrypt

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d pma.example.com

Certbot obtains the certificate, adds HTTPS listening to the server block and redirects HTTP to HTTPS. It writes lines like these into pma.conf.

listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/pma.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/pma.example.com/privkey.pem; # managed by Certbot

To enable HTTP/2, add the line http2 on; to this server block, then reload Nginx. Since Nginx 1.25, the http2 option no longer goes on the listen line. Finally, check that automatic renewal is active.

systemctl status certbot.timer

The guide Certbot: install a Let's Encrypt SSL certificate on a VPS covers other cases.


Restrict access to phpMyAdmin

First create the password file for HTTP authentication. It asks for credentials before the phpMyAdmin login page even shows.

sudo apt install -y apache2-utils
sudo htpasswd -c /etc/nginx/.htpasswd-pma admin

In /etc/nginx/nginx.conf, add a request limit zone to the http block.

limit_req_zone $binary_remote_addr zone=pma_limit:10m rate=5r/s;

Then add these lines to the server block of pma.conf that listens on 443, the one Certbot edited. Replace 198.51.100.20 with your fixed IP.

# Block the setup wizard
location ^~ /setup { deny all; }

# Only your IP gets through
allow 198.51.100.20;
deny all;

# HTTP credentials on top of the phpMyAdmin ones
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd-pma;

# Limit the request rate
limit_req zone=pma_limit burst=20 nodelay;

On an existing site, block the wizard with location ^~ /phpmyadmin/setup { deny all; }. Test and reload Nginx after each change.

sudo nginx -t && sudo systemctl reload nginx

Finally, check in the config.inc.php file that phpMyAdmin can only reach the local server. This is the default value, and it prevents SSRF attacks.

$cfg['AllowArbitraryServer'] = false;

Block brute force with Fail2ban and UFW

Fail2ban bans addresses that fail too often. The filter below reads the subdomain's log and counts Nginx refusals, codes 401 (wrong HTTP credentials) and 403 (IP refused). The guide Install and configure Fail2ban introduces the tool.

sudo apt install -y fail2ban

Create the filter /etc/fail2ban/filter.d/nginx-phpmyadmin.conf.

[Definition]
failregex = ^<HOST> -[^"]*"(?:GET|POST) /\S* HTTP/[^"]*" (?:401|403)\s
ignoreregex =

Create the jail /etc/fail2ban/jail.d/nginx-phpmyadmin.local.

[nginx-phpmyadmin]
enabled = true
port    = http,https
filter  = nginx-phpmyadmin
logpath = /var/log/nginx/pma.access.log
maxretry = 8
findtime = 10m
bantime  = 1h

Test the filter against the log, restart Fail2ban and check the jail.

sudo fail2ban-regex /var/log/nginx/pma.access.log /etc/fail2ban/filter.d/nginx-phpmyadmin.conf
sudo systemctl restart fail2ban
sudo fail2ban-client status nginx-phpmyadmin

Finally, close everything else with UFW. The guide How to set up the UFW firewall on Ubuntu or Debian explains these rules.

sudo apt install -y ufw
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status

Fix common errors

502 Bad Gateway

Nginx cannot reach PHP-FPM. Check the socket and the service.

ls -l /run/php/php8.4-fpm.sock
sudo systemctl status php8.4-fpm

403 Forbidden

Your IP is not in the allow list, or the file permissions are wrong. With the official archive, the folder must belong to www-data.

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

Large imports that fail

Raise the PHP limits in /etc/php/8.4/fpm/php.ini.

upload_max_filesize = 512M
post_max_size = 512M
max_execution_time = 300
memory_limit = 512M

Also add client_max_body_size 512M; to the Nginx server block. Then restart PHP-FPM and reload Nginx.

sudo systemctl restart php8.4-fpm
sudo systemctl reload nginx

Invalid CSRF token

Check that the blowfish_secret key is set, that your browser accepts cookies and that the VPS clock is in sync. The timedatectl command shows its status.


Need help?

Open a ticket from Tickets › Open a Ticket, or from the Contact support tile at the bottom of the VPS page. In the Related Service field, pick the VPS so the team saves time. You can also ask your question on the OuiHeberg Discord.

Was this article helpful?

1,096 views
Share this article
Need a Linux VPS?

Root access, your choice of distribution and management from the OuiHeberg client area.

View plans →

Related Articles