In this tutorial, we'll explain how to install phpMyAdmin on Ubuntu 24.04 server.
You will learn how to install and secure phpMyAdmin on an Ubuntu 24.04 server. phpMyAdmin is a popular web-based database management tool for managing MySQL/MariaDB databases. It's easy to use and provides a graphical interface to handle tasks such as creating databases, running SQL queries, and managing tables.
Prerequisites
- A dedicated server or KVM VPS running Ubuntu 24.04.
- A non-root user with sudo privileges.
How to Install phpMyAdmin on Ubuntu 24.04
Step 1: Install LAMP Stack
The LAMP stack includes Apache, MySQL/MariaDB, and PHP. Follow these steps to set up each component.
1.1 Install Apache
First, install Apache, the web server that will host your phpMyAdmin instance:
sudo apt update
sudo apt install apache2
Enable Apache to start at boot and check its status:
sudo systemctl enable apache2
sudo systemctl status apache2
Open your browser and visit http://your-server-ip
. You should see the default Apache welcome page, indicating the web server is working.
1.2 Install MySQL or MariaDB
Next, install MySQL or MariaDB. In this example, we’ll install MySQL:
sudo apt install mysql-server
After installation, secure MySQL by running the following script:
sudo mysql_secure_installation
You will be prompted with several security questions:
- Enable Validate Password Plugin: Optional but recommended.
- Remove Anonymous Users: Yes.
- Disallow Root Login Remotely: Yes.
- Remove Test Database: Yes.
- Reload Privilege Tables: Yes.
To check if MySQL is running, use:
sudo systemctl status mysql
Create a MySQL User. Login into MySQL:
sudo mysql
or
mysql -u root -p
create a new user and give it a strong password:
CREATE USER 'your_username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Note: Replace your_username
and password
with your values.
Grant your new user appropriate privileges.
GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'localhost' WITH GRANT OPTION;
Exit the MySQL session.
exit
1.3 Install PHP
Install PHP and necessary extensions that will allow PHP to interact with MySQL and Apache:
sudo apt install php libapache2-mod-php php-mysql
After the installation, restart Apache to load PHP:
sudo systemctl restart apache2
1.4 Test PHP
To test PHP, create a phpinfo file in the Apache root directory:
sudo nano /var/www/html/info.php
Add the following code to the file:
<?php
phpinfo();
?>
Save and close the file. Then, open your browser and go to http://your-server-ip/info.php
. If PHP is installed correctly, you will see the PHP information page. For security, delete this file after confirming that PHP is working:
sudo rm /var/www/html/info.php
At this point, your LAMP stack is ready, and you can proceed to install phpMyAdmin.
Step 2: Install phpMyAdmin
With the LAMP stack in place, install phpMyAdmin to manage your databases.
2.1 Install phpMyAdmin
Install phpMyAdmin using the package manager:
sudo apt install phpmyadmin
During installation, you will be prompted to configure phpMyAdmin:
- Select Web Server: Choose Apache.
- Configure database: Select Yes to automatically configure the database.
- MySQL Application Password: Enter a password for phpMyAdmin to register with the database.
After installation, phpMyAdmin will automatically create a database for itself and configure the necessary permissions.
2.2 Enable PHP Extensions
Make sure the required PHP extensions are installed for phpMyAdmin to work properly:
sudo apt install php-mbstring php-zip php-gd
Enable the mbstring extension and restart Apache:
sudo phpenmod mbstring
sudo systemctl restart apache2
2.3 Configure Apache for phpMyAdmin
Link the phpMyAdmin configuration to Apache's configuration folder:
sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
Enable the configuration and reload Apache:
sudo a2enconf phpmyadmin
sudo systemctl reload apache2
Now, you can access phpMyAdmin by navigating to http://your-server-ip/phpmyadmin
.
Step 3: Secure phpMyAdmin
phpMyAdmin is a powerful tool, but it needs additional security measures to prevent unauthorized access.
3.1 Create an Apache Authentication Gateway
To protect phpMyAdmin with an additional layer of authentication, install the htpasswd utility:
sudo apt install apache2-utils
Create a .htpasswd
file to store the credentials for the authentication gateway:
sudo htpasswd -c /etc/phpmyadmin/.htpasswd your_username
Enter a strong password and confirm.
3.2 Configure Apache for Authentication
Edit the phpMyAdmin Apache configuration file:
sudo nano /etc/apache2/conf-available/phpmyadmin.conf
Add the following configuration inside the <Directory /usr/share/phpmyadmin>
section:
AuthType Basic
AuthName "Restricted Access"
AuthUserFile /etc/phpmyadmin/.htpasswd
Require valid-user
Save and close the file, then reload Apache to apply the changes:
sudo systemctl reload apache2
3.3 Restrict Access by IP Address (Optional)
If you want to allow access to phpMyAdmin only from certain IP addresses, modify the same <Directory>
block and add the following lines:
Require ip your_ip_address
Require ip your_secondary_ip_address
Replace your_ip_address
with your actual IP address. Reload Apache after saving the file.
3.4 Disable Root Login
It’s not safe to allow logging in as the root database user via phpMyAdmin. To disable root login, open the phpMyAdmin configuration file:
sudo nano /etc/phpmyadmin/config.inc.php
Add the following line at the end of the file:
$cfg['Servers'][$i]['AllowRoot'] = false;
Save and close the file.
3.5 Enable SSL Encryption
Securing phpMyAdmin with SSL is essential to protect data transmitted between the browser and the server. You can use Let's Encrypt to configure SSL. Install certbot for Let's Encrypt:
sudo apt install certbot python3-certbot-apache
Generate the SSL certificate:
sudo certbot --apache -d your_domain -d www.your_domain
Follow the prompts to configure the SSL certificate. Once done, ensure that your users access phpMyAdmin via https://your-domain/phpmyadmin
.
Step 4: Test phpMyAdmin
Now that phpMyAdmin is installed and secured, open your web browser and go to:
http://your-server-ip/phpmyadmin
Log in using the user we have created in step 1.2
. If everything is set up correctly, you should see the phpMyAdmin interface, where you can manage your databases.
Conclusion
In this guide, you learned how to install a complete LAMP stack (Linux, Apache, MySQL, PHP) and set up phpMyAdmin on an Ubuntu 24.04 server. Additionally, you secured your phpMyAdmin installation by enabling password protection, restricting access by IP, disabling root login, and enabling SSL encryption. This setup allows you to manage your databases securely using a user-friendly web interface.