LinuxTutorials

How To Install phpMyAdmin on Nginx

Install phpMyAdmin on Nginx

In this tutorial, we will show you how to install phpMyAdmin on Nginx. phpMyAdmin stands as one of the most popular web-based database administration tools for MySQL and MariaDB databases. When combined with Nginx’s high-performance web server capabilities, it creates a powerful platform for database management. This comprehensive guide walks you through the complete installation and configuration process of phpMyAdmin on Nginx, ensuring both functionality and security.

Unlike Apache installations, setting up phpMyAdmin with Nginx requires manual configuration steps that provide greater control over your server environment. This tutorial covers everything from initial system preparation through advanced security hardening, making it suitable for both beginners and experienced system administrators managing Ubuntu servers.

Prerequisites and System Requirements

Server Requirements

Before beginning the phpMyAdmin installation process, ensure your Ubuntu server meets the following minimum requirements:

  • Ubuntu 18.04, 20.04, or 22.04 LTS server with root or sudo access
  • Minimum 1GB RAM (2GB recommended for larger databases)
  • At least 10GB available disk space
  • Active internet connection for downloading packages
  • Basic firewall configuration knowledge

Required Knowledge

This installation guide assumes familiarity with Linux command-line operations, basic web server concepts, and fundamental MySQL or MariaDB database administration. You should be comfortable navigating terminal environments and editing configuration files using text editors like nano or vim.

Step 1: System Preparation and Updates

Updating the System

Start by updating your Ubuntu system to ensure all packages are current and security patches are applied:

sudo apt update && sudo apt upgrade -y

This command refreshes the package index and upgrades all installed packages to their latest versions. System updates prevent compatibility issues and security vulnerabilities that could affect your phpMyAdmin installation.

Installing Essential Packages

Install necessary utility packages that will be required throughout the installation process:

sudo apt install wget curl nano zip unzip openssl -y

These tools provide essential functionality for downloading files, text editing, and security operations needed for phpMyAdmin configuration.

Step 2: Installing and Configuring MySQL/MariaDB

Database Server Installation

Install either MySQL or MariaDB server to provide the database backend for phpMyAdmin:

sudo apt install mysql-server -y

For MariaDB users, substitute with:

sudo apt install mariadb-server -y

After installation, secure your database server with the security script:

sudo mysql_secure_installation

Creating Database Users

Rather than using the root account, create a dedicated administrative user for phpMyAdmin access. Connect to your MySQL server:

sudo mysql

Create a new administrative user with appropriate privileges:

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

Replace ‘secure_password’ with a strong password following security best practices.

Step 3: Installing and Configuring Nginx Web Server

Nginx Installation

Install Nginx web server using the APT package manager:

sudo apt install nginx -y

Start and enable the Nginx service to ensure it runs automatically at boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Verify the installation by checking the service status:

sudo systemctl status nginx

Basic Nginx Configuration

Nginx stores its configuration files in /etc/nginx/. The main configuration file is located at /etc/nginx/nginx.conf, while site-specific configurations are stored in /etc/nginx/sites-available/.

Test your Nginx installation by visiting your server’s IP address in a web browser. You should see the default Nginx welcome page.

Firewall Configuration

Configure your firewall to allow HTTP and HTTPS traffic:

sudo ufw allow 'Nginx Full'
sudo ufw enable

This opens ports 80 (HTTP) and 443 (HTTPS) while maintaining security for other services.

Step 4: Installing PHP and Required Extensions

PHP Installation

Install PHP with FastCGI Process Manager (FPM) and MySQL extensions:

sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install php8.3-fpm php8.3-mysql php8.3-mbstring php8.3-zip php8.3-gd php8.3-json php8.3-curl -y

PHP Configuration for phpMyAdmin

Edit the PHP configuration file to optimize settings for phpMyAdmin:

sudo nano /etc/php/8.3/fpm/php.ini

Modify the following settings:

max_execution_time = 300
memory_limit = 256M
post_max_size = 64M
upload_max_filesize = 64M
max_input_vars = 3000

Restart PHP-FPM to apply changes:

sudo systemctl restart php8.3-fpm

Step 5: Installing phpMyAdmin

Manual Installation Process

Download the latest phpMyAdmin version from the official website:

cd /tmp
wget -c https://files.phpmyadmin.net/phpMyAdmin/5.2.1/phpMyAdmin-5.2.1-english.tar.gz

Extract the downloaded archive:

tar -xzvf phpMyAdmin-5.2.1-english.tar.gz

Move the extracted directory to the appropriate location:

sudo mv phpMyAdmin-5.2.1-english /usr/share/phpmyadmin

Set proper ownership and permissions:

sudo chown -R www-data:www-data /usr/share/phpmyadmin
sudo chmod -R 755 /usr/share/phpmyadmin

Alternative APT Installation

For users preferring package management, install phpMyAdmin via APT:

sudo apt install phpmyadmin -y

During installation, select “None” when prompted for web server configuration, as we’ll configure Nginx manually.

Step 6: Configuring Nginx for phpMyAdmin

Creating Symbolic Links

Create a symbolic link from phpMyAdmin’s installation directory to Nginx’s document root:

sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

This allows Nginx to serve phpMyAdmin files from the standard web directory structure.

Nginx Virtual Host Configuration

Edit the default Nginx configuration file:

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

Modify the index directive to include PHP files:

index index.php index.html index.htm index.nginx-debian.html;

Add a location block for phpMyAdmin within the server block:

location /phpmyadmin {
    root /usr/share/;
    index index.php index.html index.htm;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

PHP-FPM Integration

Ensure PHP processing is properly configured by adding or modifying the PHP location block:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}

Test the Nginx configuration syntax:

sudo nginx -t

Reload Nginx to apply changes:

sudo systemctl reload nginx

Step 7: Security Implementation

Changing Default URL Path

Enhance security by changing the default phpMyAdmin URL path. Rename the symbolic link to something less obvious:

sudo rm /var/www/html/phpmyadmin
sudo ln -s /usr/share/phpmyadmin /var/www/html/hiddenlink

Update your Nginx configuration to reflect the new path:

location ^~ /hiddenlink {
    root /usr/share/;
    index index.php index.html index.htm;
    # ... rest of configuration
}

HTTP Authentication

Implement HTTP basic authentication for an additional security layer. Create a password file:

openssl passwd

Enter your desired password when prompted. Copy the encrypted output and create the authentication file:

sudo nano /etc/nginx/pma_pass

Add your username and encrypted password:

admin:encrypted_password_here

Modify your Nginx location block to include authentication directives:

location ^~ /hiddenlink {
    auth_basic "Admin Login";
    auth_basic_user_file /etc/nginx/pma_pass;
    root /usr/share/;
    # ... rest of configuration
}

IP-Based Access Control

Restrict access to specific IP addresses for enhanced security:

location ^~ /hiddenlink {
    satisfy all;
    allow 203.0.113.0;  # Replace with your IP
    allow 127.0.0.1;    # Allow localhost
    deny all;
    auth_basic "Admin Login";
    auth_basic_user_file /etc/nginx/pma_pass;
    # ... rest of configuration
}

This configuration requires both valid IP address and authentication credentials.

Step 8: Testing and Verification

Initial Access Testing

Access phpMyAdmin through your web browser using your server’s IP address or domain:

http://your_server_ip/hiddenlink

You should be prompted for HTTP authentication credentials, followed by the phpMyAdmin login screen.

Install phpMyAdmin on Nginx

Database Connection Testing

Log into phpMyAdmin using the database credentials created earlier. Verify that you can:

  • View existing databases
  • Create new databases and tables
  • Execute SQL queries
  • Import and export data

Test various phpMyAdmin features to ensure proper functionality and database connectivity.

Security Best Practices and Hardening

SSL/HTTPS Implementation

Secure your phpMyAdmin installation with SSL certificates. Install Certbot for Let’s Encrypt certificates:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain.com

Configure automatic certificate renewal:

sudo systemctl enable certbot.timer

Additional Security Measures

Implement additional security hardening measures:

  1. Regular Updates: Keep phpMyAdmin, PHP, Nginx, and the operating system updated
  2. Database Backups: Implement automated backup strategies
  3. Log Monitoring: Monitor access logs for suspicious activity
  4. User Privilege Management: Regularly review and limit database user privileges

Configure phpMyAdmin’s security settings by creating a custom configuration file:

sudo nano /usr/share/phpmyadmin/config.inc.php

Add security-focused configurations:

<?php
$cfg['blowfish_secret'] = 'generate_32_character_random_string_here';
$cfg['LoginCookieValidity'] = 3600;
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = false;
?>

Troubleshooting Common Issues

Installation Problems

Package Dependency Conflicts: Resolve dependency issues by updating package repositories and cleaning APT cache:

sudo apt update
sudo apt autoremove
sudo apt autoclean

Permission-Related Errors: Ensure proper file ownership and permissions:

sudo chown -R www-data:www-data /usr/share/phpmyadmin
sudo chmod -R 755 /usr/share/phpmyadmin

Access and Configuration Issues

404 Errors: Verify symbolic link creation and Nginx configuration syntax. Check that the phpMyAdmin path matches your Nginx location block.

PHP Processing Failures: Ensure PHP-FPM is running and properly configured:

sudo systemctl status php8.3-fpm
sudo systemctl restart php8.3-fpm

Authentication Issues: Verify HTTP authentication file permissions and user credentials:

sudo chmod 644 /etc/nginx/pma_pass

Database Connection Problems: Check MySQL/MariaDB service status and user privileges:

sudo systemctl status mysql
mysql -u phpmyadmin_user -p

Performance Optimization

For large databases, optimize PHP settings and Nginx configuration:

client_max_body_size 64M;
fastcgi_read_timeout 300;

Increase PHP memory limits and execution time for handling large imports and exports.

Advanced Configuration Options

SSH Tunnel Setup

For maximum security, configure SSH tunnel access. Users can connect securely using:

ssh -L 8080:localhost:80 user@your_server_ip

Then access phpMyAdmin via http://localhost:8080/hiddenlink.

Multi-Server Configuration

Configure phpMyAdmin to manage multiple database servers by editing the configuration file:

$cfg['Servers'][$i]['host'] = 'server1.example.com';
$cfg['Servers'][$i]['auth_type'] = 'cookie';

$i++;
$cfg['Servers'][$i]['host'] = 'server2.example.com';
$cfg['Servers'][$i]['auth_type'] = 'cookie';

Congratulations! You have successfully installed phpMyAdmin. Thanks for using this tutorial for installing the phpMyAdmin with the Nginx web server on the Linux system. For additional help or useful information, we recommend you check the official phpMyAdmin website.

Nginx Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “Nginx Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button