
The Complete Guide to Installing and Securing phpMyAdmin on Ubuntu 24.04
Managing a MySQL or MariaDB database from the command line is powerful, but it’s not always the most efficient way to handle complex tasks. For developers and system administrators looking for a robust graphical interface, phpMyAdmin is the industry-standard solution. This free and open-source tool provides a user-friendly web interface to perform database operations, from creating tables and running queries to managing user privileges.
This comprehensive guide will walk you through the entire process of installing, configuring, and—most importantly—securing phpMyAdmin on a fresh Ubuntu 24.04 server.
Prerequisites
Before you begin, ensure you have the following components set up on your server. This guide assumes you have a standard LAMP (Linux, Apache, MySQL, PHP) stack already running.
- An Ubuntu 24.04 server.
- A non-root user with
sudo
privileges. - Apache2 web server installed and configured.
- MySQL or MariaDB installed. You will need the database root password or credentials for another administrative user.
- PHP installed and configured to work with Apache.
Step 1: Installing phpMyAdmin and Required Extensions
First, it’s always best practice to update your server’s package index to ensure you are downloading the latest available software versions.
sudo apt update
Next, install phpMyAdmin directly from Ubuntu’s official repositories. We will also include several PHP extensions that are commonly required by phpMyAdmin for full functionality, such as managing ZIP files, handling image transformations, and more.
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl
This command will download and install all the necessary packages and their dependencies.
Step 2: Configuring phpMyAdmin During Installation
The installation process is interactive and will prompt you for several key configuration choices. Pay close attention to these steps.
Web Server Selection: The first screen will ask you to choose the web server to automatically reconfigure. Press the
Spacebar
to selectapache2
(an asterisk[*]
will appear next to it). Then, pressTab
to highlight<Ok>
and pressEnter
.Database Configuration: Next, you will be asked whether to configure a database for phpMyAdmin using
dbconfig-common
. Select<Yes>
and pressEnter
. This is the recommended option as it automatically creates the dedicated database that phpMyAdmin needs to store its internal data.MySQL Application Password: You will then be prompted to create a password for the
phpmyadmin
user to register with the database server. You can leave this blank to have a random password generated, but for better management, it is highly recommended to enter a strong, unique password. Confirm the password on the next screen.
Once these steps are complete, the installation will finish. The installer automatically adds the phpMyAdmin configuration file into the Apache configuration directory at /etc/apache2/conf-enabled/phpmyadmin.conf
.
Step 3: Accessing the phpMyAdmin Web Interface
Your phpMyAdmin installation should now be accessible. Open your web browser and navigate to your server’s domain name or public IP address, followed by /phpmyadmin
.
http://your_server_domain_or_ip/phpmyadmin
You will see the phpMyAdmin login page. You can log in using any valid MySQL/MariaDB username and password. For initial setup, you can use the root
user and the password you configured during the database installation.
Important: Securing Your phpMyAdmin Installation
A default phpMyAdmin installation is a common target for malicious bots and attackers. Leaving it in its default state is a significant security risk. Do not skip these critical security hardening steps.
1. Change the Default Access URL
Bots constantly scan for common paths like /phpmyadmin
. Changing this URL is one of the most effective ways to avoid automated attacks.
Open the Apache configuration file for phpMyAdmin with a text editor:
sudo nano /etc/phpmyadmin/apache.conf
Find the line that reads Alias /phpmyadmin /usr/share/phpmyadmin
. Change the /phpmyadmin
alias to something secret and difficult to guess. For example:
# Change this:
# Alias /phpmyadmin /usr/share/phpmyadmin
# To something like this:
Alias /managedb-secret /usr/share/phpmyadmin
Save the file and exit the editor. To apply the change, you must restart Apache:
sudo systemctl restart apache2
Now, you will only be able to access the login page via your new, secret URL (e.g., http://your_server_domain/managedb-secret
).
2. Set Up a Web Server Authentication Gateway
For an additional layer of security, you can protect the entire application with an extra password prompt at the web server level. This forces an attacker to bypass two separate passwords before gaining access.
First, enable the use of .htaccess
file overrides by editing the phpMyAdmin Apache configuration again:
sudo nano /etc/phpmyadmin/apache.conf
Inside the <Directory /usr/share/phpmyadmin>
block, add the line AllowOverride All
.
<Directory /usr/share/phpmyadmin>
Options SymLinksIfOwnerMatch
DirectoryIndex index.php
AllowOverride All
# ... other directives
</Directory>
Save the file and restart Apache again.
sudo systemctl restart apache2
Next, create a .htaccess
file within the phpMyAdmin directory:
sudo nano /usr/share/phpmyadmin/.htaccess
Add the following content to the file. This tells Apache to require authentication for this directory.
AuthType Basic
AuthName "Restricted Files"
AuthUserFile /etc/phpmyadmin/.htpasswd
Require valid-user
Save and close the file. Now, create the password file (.htpasswd
) referenced in the configuration. The -c
flag creates a new file. Only use -c
for the first user you add. For any subsequent users, omit the -c
flag.
sudo htpasswd -c /etc/phpmyadmin/.htpasswd your_username
You will be prompted to create and confirm a password for your_username
. After setting the password, trying to access your phpMyAdmin URL will now present a server-level authentication pop-up before the phpMyAdmin login page even loads.
3. Create a Dedicated phpMyAdmin User
Logging in with the root
MySQL user is convenient but risky. A better practice is to create a separate MySQL user that has the necessary privileges but is not the all-powerful root user. You can do this from within phpMyAdmin itself (while logged in as root) or via the MySQL command line.
By following these installation and security steps, you have deployed a powerful tool for database management while significantly reducing your server’s exposure to common threats.
Source: https://kifarunix.com/how-to-install-phpmyadmin-on-ubuntu-24-04/