keepcalculator

06 Numbers and Powers

Install phpMyAdmin with Nginx on CentOS

Card No. 06·1370 · Updated · Standard formula, shown below

This guide walks you through installing phpMyAdmin on a CentOS server running Nginx, giving you a web-based interface to manage MySQL or MariaDB databases. You’ll need root or sudo access to your server, a working Nginx installation, and PHP with the necessary extensions (php-fpm, php-mysqlnd, etc.) already configured. By the end, you’ll have phpMyAdmin accessible via HTTPS on your domain or IP.

What You’ll Need

  • A CentOS server (7, 8, or 9) with Nginx installed and running.
  • PHP 7.4 or newer with php-fpm and the mysqli or pdo_mysql extension enabled.
  • MySQL or MariaDB server installed and running.
  • Root or sudo access to the command line.
  • Optional but recommended: a domain name pointing to your server and an SSL certificate (Let’s Encrypt).

Step-by-Step

  1. Update your system
    Run sudo yum update -y (or sudo dnf update -y on CentOS 8+) to ensure all packages are current.
  2. Install EPEL repository
    phpMyAdmin is not in the default CentOS repos. Install EPEL: sudo yum install epel-release -y (or sudo dnf install epel-release -y).
  3. Install phpMyAdmin
    Run sudo yum install phpmyadmin -y (or sudo dnf install phpmyadmin -y). This installs the package and its dependencies, including PHP extensions if needed.
  4. Configure phpMyAdmin for Nginx
    The default configuration is for Apache. Create an Nginx server block or edit an existing one to serve phpMyAdmin. Typically, you’ll add a location block inside your server block. For example, create /etc/nginx/conf.d/phpmyadmin.conf with:

    server {
        listen 80;
        server_name your_domain_or_IP;
        root /usr/share/phpMyAdmin;
        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:/run/php-fpm/www.sock; # Adjust to your php-fpm socket
            fastcgi_index index.php;
        }
    
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
        }
    }
      

    Adjust the fastcgi_pass to match your php-fpm configuration (check /etc/php-fpm.d/www.conf for the listen directive).

  5. Set proper file ownership and permissions
    Ensure the web server can read the phpMyAdmin files: sudo chown -R root:nginx /usr/share/phpMyAdmin (or use apache if that’s your nginx user) and sudo chmod -R 755 /usr/share/phpMyAdmin.
  6. Configure phpMyAdmin to connect to MySQL
    Edit /etc/phpMyAdmin/config.inc.php (or /etc/phpmyadmin/config.inc.php). Ensure the $cfg['Servers'][$i]['host'] is set to localhost or 127.0.0.1. If you use a socket, set it accordingly. For most setups, the default works.
  7. Test and restart Nginx and PHP-FPM
    Run sudo nginx -t to check the configuration. Then restart services: sudo systemctl restart nginx and sudo systemctl restart php-fpm.
  8. Access phpMyAdmin
    Open your browser and go to http://your_domain_or_IP/. You should see the phpMyAdmin login page. Log in with a MySQL user (e.g., root).
  9. Secure with HTTPS (recommended)
    Use Let’s Encrypt to get a free SSL certificate: sudo yum install certbot python3-certbot-nginx -y then sudo certbot --nginx -d your_domain. Follow the prompts.

Common Mistakes

  • Wrong php-fpm socket path – If the socket path in your Nginx config doesn’t match the one in php-fpm, you’ll get a 502 Bad Gateway error.
  • Missing PHP extensions – phpMyAdmin requires the mysqli extension. If it’s missing, you’ll see a blank page or a warning. Install with sudo yum install php-mysqlnd.
  • File permissions too restrictive – If Nginx can’t read the phpMyAdmin files, you’ll get a 403 Forbidden error. Ensure the ownership and permissions are correct.
  • Using Apache’s .htaccess directives – Nginx ignores .htaccess files. If you copied an Apache config, you’ll need to translate those rules into Nginx syntax.
  • Not restarting services after changes – Always restart Nginx and php-fpm after modifying configs.

Tips

  • Use a dedicated subdomain – Serve phpMyAdmin on something like db.example.com to avoid exposing it on the main domain and to simplify SSL configuration.
  • Restrict access by IP – Add allow YOUR_IP; deny all; in the server block to limit who can reach phpMyAdmin. This is a strong security measure.
  • Enable two-factor authentication – phpMyAdmin supports plugins like the TOTP (time-based one-time password) to add a second layer of security.
  • Keep it updated – Regularly run sudo yum update phpmyadmin to get security patches.
  • Backup your config – Before editing config files, copy them: sudo cp /etc/phpMyAdmin/config.inc.php /etc/phpMyAdmin/config.inc.php.bak.

Troubleshooting

  • 502 Bad Gateway – This usually means Nginx can’t reach php-fpm. Check the socket path in your Nginx config and the listen directive in /etc/php-fpm.d/www.conf. Also verify php-fpm is running: sudo systemctl status php-fpm.
  • 403 Forbidden – Likely permission issues. Check that the root directive points to the correct directory and that the user running Nginx has read access. Also check SELinux: sudo setsebool -P httpd_can_network_connect 1 or sudo chcon -R -t httpd_sys_content_t /usr/share/phpMyAdmin.
  • Blank page after login – This often indicates a PHP error. Check the PHP error log: sudo tail -f /var/log/php-fpm/error.log. Make sure the mysqli extension is enabled in /etc/php.ini.
  • “Cannot load mysqli extensionâ€� – Install it with sudo yum install php-mysqlnd and restart php-fpm.
  • Access denied for user – You’re using wrong credentials or the MySQL user is not allowed to connect from localhost. Check your MySQL user permissions.
  • SSL certificate not working – If using Let’s Encrypt, ensure port 80 is open and that your domain resolves to your server. Run sudo certbot renew --dry-run to test.

Frequently Asked Questions

Can I install phpMyAdmin without EPEL?

Yes, you can download the tarball from the official phpMyAdmin site and place it in your web root, but EPEL is the easiest method on CentOS.

Is it safe to use phpMyAdmin on a public server?

It can be safe if you use HTTPS, strong passwords, restrict access by IP, and keep it updated. Avoid using the root MySQL account for web access.

How do I change the default login timeout?

Edit config.inc.php and set $cfg['LoginCookieValidity'] = 3600; (or your desired seconds). Also ensure PHP’s session.gc_maxlifetime is at least as high.

Why do I get a 404 when accessing phpMyAdmin?

Your Nginx server block may not be pointing to the correct root, or the location block is misconfigured. Double-check the root and try_files directives.

Can I use phpMyAdmin with MariaDB?

Yes, phpMyAdmin works with both MySQL and MariaDB. No special configuration is needed.

How do I uninstall phpMyAdmin?

Run sudo yum remove phpmyadmin and delete any Nginx config files you created for it.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →