How to Install PhpMyAdmin with Nginx and Let's Encrypt SSL on Ubuntu 24.04
phpMyAdmin on Ubuntu is a web-based interface that simplifies the management of MySQL or MariaDB databases. It allows users to perform database operations like creating, editing, and deleting databases, tables, and records without interacting directly with the command line. Designed with ease of use in mind, phpMyAdmin is especially useful for users who are not comfortable with SQL commands. On Ubuntu, it can be installed as part of the LAMP (Linux, Apache, MySQL, PHP) stack, providing a graphical interface that can be accessed through a web browser, making it a popular choice for developers and administrators managing databases in a Linux environment.
In this tutorial, we'll show you how to install phpMyAdmin with Nginx on Ubuntu 24.04 and include some additional tips for optimal setup.
Prerequisites
- A server running Ubuntu 24.04.
- A valid domain name is pointed to your server.
- SSH access with a non-root user having
sudo
privileges.
Install Nginx, MariaDB, and PHP
Before we begin, ensure your package list is up to date:
sudo apt update && sudo apt upgrade -y
Next, install Nginx, MariaDB, PHP, and essential PHP extensions:
sudo apt install nginx mariadb-server php php-cli php-mysql php-mbstring php-zip php-gd php-json php-curl php-fpm -y
Verify that each service is running:
systemctl status nginx
systemctl status mariadb
Tip: Install unattended-upgrades
to ensure security updates are applied automatically:
sudo apt install unattended-upgrades -y
Install phpMyAdmin
The phpMyAdmin package is available in the default Ubuntu 24.04 repository. Install it with:
sudo apt install phpmyadmin -y
Ensure "No web server" is selected unless explicitly using Apache on the same server.
Configure phpMyAdmin Manually for Nginx
Create a symbolic link in the Nginx web directory:
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
Secure MariaDB
Run the security script to improve MariaDB's default settings:
sudo mysql_secure_installation
Follow the on-screen prompts:
- Set a root password.
- Remove anonymous users.
- Disallow root login remotely.
- Remove the test database.
Create a dedicated database user for phpMyAdmin:
sudo mysql -u root -p
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
Configure Nginx for phpMyAdmin
Create an Nginx configuration file for phpMyAdmin:
sudo nano /etc/nginx/conf.d/phpmyadmin.conf
Add the following configuration:
server {
listen 80;
server_name phpmyadmin.example.com;
root /usr/share/phpmyadmin;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Check the configuration for syntax errors:
sudo nginx -t
Reload Nginx to apply the changes:
sudo systemctl reload nginx
Secure phpMyAdmin with Let's Encrypt SSL
Ensure Certbot is installed for managing SSL certificates:
sudo apt install certbot python3-certbot-nginx -y
Use Certbot to obtain and install a certificate:
sudo certbot --nginx -d phpmyadmin.example.com
Follow the steps to configure SSL, including selecting the option to redirect all HTTP traffic to HTTPS.
Automatic Certificate Renewal
Set up a cron job to automatically renew SSL certificates:
echo "0 3 * * * /usr/bin/certbot renew --quiet" | sudo tee /etc/cron.d/certbot-renew
Final Steps
Change ownership permissions:
sudo chown -R www-data:www-data /usr/share/phpmyadmin
sudo chmod -R 755 /usr/share/phpmyadmin
Access phpMyAdmin
Open your web browser and navigate to https://phpmyadmin.example.com
. Log in using your admin credentials. You should see the phpMyAdmin dashboard.
Conclusion
Congratulations! You've successfully installed and secured phpMyAdmin with Nginx on Ubuntu 24.04. Remember to regularly update your server packages and back up your databases.
Advanced Tip: For enhanced security, consider setting up two-factor authentication (2FA) for sensitive operations in phpMyAdmin.