TechBuzzPro

Install and Secure phpMyAdmin with Nginx

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks you through installing phpMyAdmin on an Nginx web server and locking it down so your database management interface isn't an easy target. You'll end up with a working setup that requires a web server, PHP, and MySQL/MariaDB already installed. If you haven't done that yet, the steps below assume you have a basic LEMP stack (Linux, Nginx, MySQL/MariaDB, PHP) running.

What You'll Need

  • A server running Linux (Ubuntu/Debian or CentOS/RHEL family) with sudo access.
  • Nginx installed and running.
  • MySQL or MariaDB installed and running, with root access or a user that can create databases.
  • PHP installed with the PHP-FPM service (e.g., php-fpm) and the necessary extensions (php-mysql, php-mbstring, etc.).
  • An SSL certificate (or the ability to get one via Let's Encrypt) to enable HTTPS.

Step-by-Step

  1. Install phpMyAdmin
    On Debian/Ubuntu: sudo apt update && sudo apt install phpmyadmin. On CentOS/RHEL: sudo dnf install epel-release && sudo dnf install phpmyadmin. During installation, you may be asked to choose a web server – select none (or apache2 if forced, but we'll configure Nginx manually). When prompted to configure a database, choose Yes to set up the phpMyAdmin database and user.
  2. Link phpMyAdmin into Nginx's web root
    Create a symlink so Nginx can serve the phpMyAdmin files. On Debian/Ubuntu: sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin. On CentOS/RHEL: sudo ln -s /usr/share/phpMyAdmin /usr/share/nginx/html/phpmyadmin. Adjust the paths if your web root differs.
  3. Create an Nginx server block for phpMyAdmin
    Create a new configuration file, e.g., /etc/nginx/conf.d/phpmyadmin.conf (or a site in sites-available). Include a server block that listens on port 443 (HTTPS) and optionally redirects HTTP to HTTPS. A minimal example:
    server {
        listen 443 ssl;
        server_name phpmyadmin.example.com;
    
        ssl_certificate /etc/letsencrypt/live/phpmyadmin.example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/phpmyadmin.example.com/privkey.pem;
    
        root /usr/share/nginx/html;
        index index.php;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;  # adjust to your PHP version
        }
    
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
        }
    }
    
    Adjust the socket path and certificate paths.
  4. Restart Nginx
    Test the configuration with sudo nginx -t and then reload: sudo systemctl reload nginx (or sudo service nginx reload).
  5. Secure the installation
    Now apply security measures. First, restrict access by IP address. In the Nginx location / block, add allow 192.168.1.0/24; deny all; to only allow your local network or specific IPs. Replace with your actual allowed IPs.
  6. Set up authentication
    Create an additional password prompt using HTTP basic auth. Generate a password file: sudo htpasswd -c /etc/nginx/.htpasswd admin (install apache2-utils if needed). Then in the Nginx config, inside the server block (or a location), add:
    auth_basic "Restricted Access";
    auth_basic_user_file /etc/nginx/.htpasswd;
    
  7. Enable HTTPS with Let's Encrypt
    If you haven't already, obtain a free SSL certificate using certbot: sudo certbot --nginx -d phpmyadmin.example.com. This will automatically update your Nginx config. If you're using a self-signed certificate for testing, skip this step.
  8. Harden phpMyAdmin configuration
    Edit the phpMyAdmin config file, usually at /etc/phpmyadmin/config.inc.php. Set $cfg['ForceSSL'] = true; to force HTTPS. Also, set $cfg['LoginCookieValidity'] = 3600; to limit session time. Consider disabling root login by setting $cfg['Servers'][$i]['AllowRoot'] = false; and create a dedicated database user for phpMyAdmin.
  9. Restart services
    After all changes, restart PHP-FPM and Nginx: sudo systemctl restart php7.4-fpm (or your version) and sudo systemctl restart nginx.
  10. Test the setup
    Open your browser and navigate to https://phpmyadmin.example.com. You should see the HTTP auth prompt, then the phpMyAdmin login page. Log in with a database user.

Common Mistakes

  • Using the wrong PHP-FPM socket path – Check your PHP version and the actual socket file location (e.g., /run/php/php8.1-fpm.sock).
  • Forgetting to install PHP extensions – phpMyAdmin needs php-mysql (or php-mysqli), php-mbstring, and php-json. Without them, you'll get errors.
  • Leaving the default root login enabled – This is a huge security risk. Always create a limited user.
  • Not enabling HTTPS – Sending passwords over plain HTTP is unacceptable.
  • Misconfigured Nginx location blocks – If the PHP location block is missing, Nginx will return a blank page or download the PHP file.

Tips

  • Use a dedicated subdomain like db.example.com so you can apply strict security policies without affecting your main site.
  • Change the default URL path from /phpmyadmin to something less predictable (e.g., /dbadmin) to reduce automated scans.
  • Set up a firewall (UFW or firewalld) to allow only SSH, HTTP, and HTTPS, and block everything else.
  • Regularly update phpMyAdmin and your server packages to patch known vulnerabilities.
  • Consider using a VPN or SSH tunnel to access phpMyAdmin instead of exposing it to the internet at all.

Troubleshooting

  • Blank page or 404 – Check Nginx error logs (sudo tail -f /var/log/nginx/error.log). Often it's a missing PHP-FPM socket or wrong root path.
  • 403 Forbidden – This usually means the index file isn't found or permissions are wrong. Ensure the phpMyAdmin directory is readable by the web server user (e.g., www-data).
  • "Cannot load mysqli extension" – Install the PHP MySQL extension: sudo apt install php-mysql and restart PHP-FPM.
  • Login page shows but login fails – Verify your database credentials. If you disabled root, use a different user. Check MySQL logs for access denied errors.
  • HTTPS not working – Ensure your SSL certificate paths are correct and that port 443 is open in the firewall.
  • After changing Nginx config, site breaks – Run sudo nginx -t to test before reloading. If it fails, revert the last change.

Frequently Asked Questions

Do I need to install phpMyAdmin if I already have MySQL?

No, phpMyAdmin is a web-based client for MySQL/MariaDB. You can manage your databases via the command line instead, but phpMyAdmin provides a convenient graphical interface.

Can I use phpMyAdmin with MySQL 8?

Yes, recent versions of phpMyAdmin support MySQL 8. Make sure you have the latest phpMyAdmin and the php-mysql extension.

Is it safe to expose phpMyAdmin to the internet?

It's risky. Even with security measures, it's a common attack target. It's safer to restrict access by IP, use a VPN, or tunnel via SSH.

How do I change the phpMyAdmin URL path?

Simply rename the symlink or change the location in Nginx. For example, create a symlink named dbadmin and access it at /dbadmin.

What's the difference between HTTP and HTTPS for phpMyAdmin?

HTTPS encrypts the connection, protecting your login credentials and data from being intercepted. Always use HTTPS.

How do I update phpMyAdmin?

On Debian/Ubuntu, use sudo apt update && sudo apt upgrade phpmyadmin. On CentOS/RHEL, use sudo dnf update phpmyadmin. Always back up your config file before upgrading.