Introduction
Managing your MySQL or MariaDB databases can be a daunting task, especially without a graphical interface. phpMyAdmin provides an intuitive web-based solution that simplifies database administration. When paired with the high-performance Nginx web server, you get a robust and efficient setup for managing your databases on Ubuntu 24.04. This guide will walk you through the process of installing and configuring phpMyAdmin with Nginx, ensuring a secure and functional environment.
Prerequisites
Before you begin, ensure you have a fresh Ubuntu 24.04 server with a non-root sudo user configured. You will also need Nginx and PHP (specifically PHP-FPM) installed and configured. If you haven’t already, you can follow our guide on How to Install PHP 8.3 on Ubuntu 24.04 (for Nginx and Apache). Additionally, a database server like MariaDB or MySQL is required. Refer to our Tutorial on Installing MariaDB on Ubuntu 24.04: A Step-by-Step Database Setup Guide or How to Install MySQL 8 on Ubuntu 24.04 if you need to set one up.
Step 1: Install phpMyAdmin
Start by updating your package list and installing phpMyAdmin. While the system might prompt you to choose a web server (Apache2 or Lighttpd), neither Nginx nor PHP-FPM are listed directly. You can simply hit TAB to select “Ok” without choosing one, as we will configure Nginx manually later.
sudo apt update
sudo apt install phpmyadmin
During the installation, you’ll be asked to configure phpMyAdmin for a web server. Select “None” or simply press ENTER to proceed without selecting any, as we’ll manually integrate it with Nginx.
Next, you’ll be asked if you want to configure the database for phpMyAdmin with dbconfig-common. It’s generally a good idea to select Yes to allow the installer to create the necessary database and user for phpMyAdmin automatically.
You’ll then be prompted to enter the password for the phpMyAdmin MySQL application user. Choose a strong password and remember it.
Step 2: Configure PHP-FPM for phpMyAdmin
phpMyAdmin requires several PHP extensions to function correctly. Install them if you haven’t already:
sudo apt install php-mbstring php-zip php-gd php-json php-curl php-mysql
After installing the extensions, restart PHP-FPM to load the new configurations:
sudo systemctl restart php8.3-fpm
Step 3: Configure Nginx for phpMyAdmin
Now, we need to create an Nginx server block (or virtual host) configuration file for phpMyAdmin. For simplicity, we’ll configure it to be accessible via a subdomain or a specific path on your existing Nginx site. Let’s assume you want to access it via your_domain.com/phpmyadmin.
First, create a symbolic link from the phpMyAdmin installation directory to your Nginx web root. Assuming your Nginx web root is /var/www/html and your domain is your_domain.com:
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
Next, edit your Nginx server block configuration for your_domain.com. This file is typically located at /etc/nginx/sites-available/your_domain.com (replace your_domain.com with your actual domain):
sudo nano /etc/nginx/sites-available/your_domain.com
Inside the server block, add a location block for /phpmyadmin/:
server {
listen 80;
server_name your_domain.com www.your_domain.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}
# phpMyAdmin configuration
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
try_files $uri $uri/ /phpmyadmin/index.php?$args;
location ~ ^/phpmyadmin(.+\.php)$ {
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~* /(libraries|setup/frames|doc) {
deny all;
return 404;
}
}
# Deny access to .htaccess files, if Apache's document root
# concurs with Nginx's one
location ~ /\.ht {
deny all;
}
}
Save and close the file. Test your Nginx configuration for syntax errors:
sudo nginx -t
If the test is successful, restart Nginx to apply the changes:
sudo systemctl restart nginx
Step 4: Access phpMyAdmin
You should now be able to access phpMyAdmin by navigating to http://your_domain.com/phpmyadmin in your web browser. Log in using your database root user credentials or the credentials of a specific database user you’ve set up.
Step 5: Enhance Security (Optional but Recommended)
To further secure your phpMyAdmin installation, consider these steps:
- Implement SSL/TLS: Always use HTTPS for phpMyAdmin to encrypt traffic. You can easily set this up with Let’s Encrypt. Follow our guide on Fortify Your Nginx Server: Let’s Encrypt SSL on Ubuntu 24.04.
- Restrict Access: Limit access to phpMyAdmin by IP address in your Nginx configuration.
- Use a Custom URL: Change
/phpmyadminto a less obvious URL in your Nginx configuration and symbolic link. - Install Fail2Ban: Protect your server from brute-force attacks. Learn How to Install and Configure Fail2Ban to Protect SSH on Ubuntu 24.04.
- Regular Updates: Keep your system and phpMyAdmin up to date. You can also explore How to Set Up Automatic Security Updates on Ubuntu 24.04.
Further Reading
If you found this guide helpful, you might also want to explore:
- How to Install Redis Server on Ubuntu 24.04 – Add a powerful caching layer to your stack
- How to Install Composer (PHP Dependency Manager) on Ubuntu 24.04 – Essential for modern PHP development
- How to Create a Sudo User on Ubuntu 24.04 – Best practice for server security
By following these steps, you will have successfully installed and configured phpMyAdmin with Nginx on your Ubuntu 24.04 server, providing you with a powerful tool for database management. Remember to always prioritize security when exposing administrative interfaces to the web.
