Skip to the fix
PHPSolvedThe fix first, then the reason

Newest fixes

Sanna Ketola in phpMyAdmin, nginx and Apache | | 7 min

Install phpMyAdmin on Ubuntu with nginx and PHP-FPM, the manual way

Download and verify the archive, unpack it outside the web root, write a server block with a PHP location, add config.inc.php and lock the address down.

An open metal toolbox with neatly arranged tools on a white bench
On this page
  1. Package or archive
  2. The seven steps
  3. 1. PHP-FPM and the extensions
  4. 2. Download and verify
  5. 3. Unpack and set permissions
  6. 4. The server block
  7. 5. config.inc.php
  8. 6. A database account
  9. 7. Close the door behind you
  10. When it doesn't work

Ubuntu has a phpMyAdmin package, and its automatic setup covers Apache and lighttpd. The phpMyAdmin documentation says so, and nginx isn't on that list. You can install the package and write the nginx part yourself, or take the release from the project. This page does the second: you get the version you choose, every file in one folder, and nothing that configures itself.

The shell steps that need root, the nginx blocks and phpMyAdmin itself were not run for this page. They follow the official documents linked along the way. The PHP and SQL blocks marked as run were run.

Package or archive

Both are sound choices, and they fail in different ways later on.

The package is updated together with the rest of the system, which is worth a lot on a server nobody looks at for months. In return you take the version your Ubuntu release carries, the configuration is split over several files in /etc/phpmyadmin, and it may differ from what the documentation describes. Its code lives in /usr/share/phpmyadmin, so the server blocks below work with that path as well: change root, skip steps 2, 3 and 5, and make your own settings in the config.inc.php that the package keeps in /etc/phpmyadmin.

The archive has one folder, one configuration file and the version you picked. You have to update it yourself. If you know that you won't, take the package.

A third answer is not to install it on the server. phpMyAdmin on your own computer can reach a remote MySQL through an SSH tunnel, and then there's no login page on the internet at all:

bash
ssh -N -L 3307:127.0.0.1:3306 deploy@db.example.com

Point the local copy at host 127.0.0.1 and port 3307. The port counts only with an address, because MySQL programs ignore it for localhost and use the socket.

The seven steps

  1. PHP-FPM and the extensions
  2. Download and verify
  3. Unpack and set permissions
  4. The server block
  5. config.inc.php
  6. A database account
  7. Close the door behind you

1. PHP-FPM and the extensions

bash
sudo apt update
sudo apt install nginx php-fpm php-mysql php-mbstring php-zip php-gd php-curl

The packages without a version number pull in Ubuntu's default PHP. Note which one it is, because the socket and the service carry the number. On Ubuntu 24.04 the service is php8.3-fpm. Ask the machine:

bash
systemctl list-units --type=service 'php*-fpm*'
ls /run/php/

The requirements page asks for PHP 7.2.5 or newer with sessions, SPL, hash, ctype and JSON, and strongly recommends mbstring. A zip extension is needed for zip imports. One line of PHP lists what's missing:

phpran on PHP 8.2.8
<?php
$need = ['mysqli', 'session', 'SPL', 'hash', 'ctype', 'json', 'mbstring', 'openssl', 'sodium', 'zip'];
$missing = array_filter($need, fn ($name) => !extension_loaded($name));
echo $missing ? 'missing: ' . implode(', ', $missing) . "\n" : "all there\n";
real output
all there

Run it through the web server later as well, since the command line can load a different set.

2. Download and verify

Pick the version on the downloads page and put it into a variable, so that the commands below don't age:

bash
V=5.2.3
cd /tmp
wget https://files.phpmyadmin.net/phpMyAdmin/$V/phpMyAdmin-$V-all-languages.tar.xz
wget https://files.phpmyadmin.net/phpMyAdmin/$V/phpMyAdmin-$V-all-languages.tar.xz.asc
wget https://files.phpmyadmin.net/phpmyadmin.keyring
gpg --import phpmyadmin.keyring
gpg --verify phpMyAdmin-$V-all-languages.tar.xz.asc

Look for "Good signature" and compare the fingerprint with the one in the documentation's section on verifying releases. The release manager's key there is 3D06 A59E CE73 0EB7 1B51 1C17 CE75 2F17 8259 BD92. The warning that the key isn't certified with a trusted signature is normal when you've just imported it. The documentation also asks you to look at the date of the signature, which is how you notice an old release being passed off as a new one. If gpg can't reach its agent at all, there's a short note about that message.

3. Unpack and set permissions

bash
sudo mkdir -p /srv/phpmyadmin /var/lib/phpmyadmin/tmp
sudo tar -xf phpMyAdmin-$V-all-languages.tar.xz -C /srv/phpmyadmin --strip-components=1
sudo chown -R root:root /srv/phpmyadmin
sudo chown www-data:www-data /var/lib/phpmyadmin/tmp
sudo chmod 700 /var/lib/phpmyadmin/tmp

The program files belong to root, so a hole in some other PHP application running as www-data can't change them. The one folder that PHP must write to is the temporary one, and it stands outside the folder that nginx serves.

Two folders in the archive have no place on a server in use. The security section of the documentation says to remove setup after the first configuration and test unless you develop phpMyAdmin:

bash
sudo rm -rf /srv/phpmyadmin/setup /srv/phpmyadmin/test

Check the path before you press Enter. Nothing else on this page deletes anything.

4. The server block

A host name of its own is simpler than a folder under another site, and it lets you restrict the whole name at once. Create /etc/nginx/sites-available/phpmyadmin:

nginx
server {
    listen 80;
    server_name db.example.com;
    root /srv/phpmyadmin;
    index index.php;

    client_max_body_size 64m;

    allow 203.0.113.10;
    deny all;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ ^/(libraries|templates|sql|vendor)/ {
        deny all;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }
}

Replace 203.0.113.10 with your own address. Four lines deserve a remark:

  • The libraries and templates folders are named in the documentation as ones to deny. Order matters: nginx takes the first regular expression that matches, so the denying location stands above the PHP one.
  • try_files $uri =404 in the PHP location stops requests for scripts that don't exist before PHP sees them.
  • SCRIPT_FILENAME is the parameter PHP uses to find the script, as the FastCGI module page explains. The stock fastcgi_params file doesn't contain it on every system, so it's written out.
  • client_max_body_size decides how large an import may be before PHP is asked. PHP has limits of its own behind it: upload_max_filesize, post_max_size and the time a script may run.

Turn it on, test, reload:

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

If the answer is a 502, the socket path in fastcgi_pass doesn't match the one in /run/php/. The 502 page reads that line of the error log with you.

Under a folder of an existing site

If it has to be example.com/pma/, use a prefix location with an alias inside the existing server block:

nginx
location ^~ /pma/ {
    alias /srv/phpmyadmin/;
    index index.php;

    location ~ ^/pma/(libraries|templates|sql|vendor)/ { deny all; }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }
}

$request_filename takes the place of the document root here, because that root belongs to the other site. An odd folder name keeps the noise of automated scans out of your log. It isn't protection.

5. config.inc.php

Make the encryption key with the blowfish secret generator or on the server itself. This script writes a complete file:

phpran on PHP 8.2.8
<?php
$key = bin2hex(random_bytes(32));
$config = <<<PHP
<?php
\$cfg['blowfish_secret'] = sodium_hex2bin('$key');
\$cfg['TempDir'] = '/var/lib/phpmyadmin/tmp';
\$i = 0;
\$i++;
\$cfg['Servers'][\$i]['auth_type'] = 'cookie';
\$cfg['Servers'][\$i]['host'] = 'localhost';
\$cfg['Servers'][\$i]['AllowNoPassword'] = false;
\$cfg['Servers'][\$i]['AllowRoot'] = false;

PHP;
file_put_contents('config.inc.php', $config);

$cfg = [];
include 'config.inc.php';
echo strlen($cfg['blowfish_secret']), " bytes, ", $cfg['Servers'][1]['auth_type'], " login\n";
real output
32 bytes, cookie login

Move the file to /srv/phpmyadmin/config.inc.php, owned by root, readable by the group of the PHP process and by nobody else:

bash
sudo install -o root -g www-data -m 640 config.inc.php /srv/phpmyadmin/config.inc.php
rm config.inc.php

AllowRoot set to false is optional and in the documentation's list of ways to tighten an installation. On Ubuntu it costs nothing, because the MySQL root account there logs in through the system user and not with a password.

The key has to be exactly 32 bytes. A shorter one is replaced by a temporary key for each session, a longer one is cut.

6. A database account

phpMyAdmin has no accounts. It passes your login to MySQL, so you need a MySQL account with a password. Give it the databases it should work on:

sqlran on MySQL 8.0.33
CREATE DATABASE IF NOT EXISTS lab_shop_pma;
CREATE USER 'lab_paul'@'localhost' IDENTIFIED BY 'a long random password';
GRANT ALL PRIVILEGES ON lab_shop_pma.* TO 'lab_paul'@'localhost';
SHOW GRANTS FOR 'lab_paul'@'localhost';
DROP USER 'lab_paul'@'localhost';
DROP DATABASE lab_shop_pma;
real output
+--------------------------------------------------------------------+
| Grants for lab_paul@localhost                                      |
+--------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `lab_paul`@`localhost`                       |
| GRANT ALL PRIVILEGES ON `lab_shop_pma`.* TO `lab_paul`@`localhost` |
+--------------------------------------------------------------------+

The two DROP lines only clean up after the run; leave them out. If the form rejects the login, the message contains the number 1045, and that error has its own page.

After the first login phpMyAdmin may say that the configuration storage isn't completely set up. That's about extra features such as bookmarks and history. The documentation describes a zero-configuration mode in which the Operations tab of a database offers to create the tables.

7. Close the door behind you

TLS. The login and every result cross the network, so serve the name over https only. The security section asks for HTTPS and prefers HSTS on top.

A second lock. An address list, as in the block above, is the strongest. If your address changes, use a password in front of the login. nginx checks it before PHP runs:

nginx
auth_basic "db";
auth_basic_user_file /etc/nginx/pma.htpasswd;

The module page says that openssl passwd can make the entry for that file.

A tunnel for the page itself. The strictest version of the block above listens on the machine only. Replace the listen line with listen 127.0.0.1:8081;, drop the address rules, and open http://localhost:8081/ on your computer while a tunnel is running: ssh -N -L 8081:127.0.0.1:8081 deploy@db.example.com. Nobody without an SSH login can even see that phpMyAdmin is installed, and you need neither a certificate nor a password file for it.

Failed logins. They go to syslog if it's available, not to the nginx error log, which the documentation points out because tools like fail2ban have to read the right file.

Updates. A manual install isn't updated by apt. Follow the project's security announcements. To update, unpack the new version into a second folder, copy config.inc.php over, change the root line, test, reload, and remove the old folder a week later.

When it doesn't work

You seeLook at
403 everywhereThe allow line has a different address from the one nginx sees
404 for every pageroot, or the link in sites-enabled
502The socket path and the FPM service
"File not found."SCRIPT_FILENAME
A page without stylesA location from another server block is catching /themes/
A temporary key messageconfig.inc.php can't be read, or the key is too short

The longer list, starting from what the browser shows, is in localhost/phpmyadmin does not open.

Same subject