Ubuntu

How to Install phpMyAdmin on Ubuntu 20.04

What is phpMyAdmin?

The phpMyAdmin is a free and open-source tool for managing the MySQL or MariaDB database. It’s a web-based tool¬† that allows you to manage and dminister MySQL databases from the web browser.

The phpMyAdmin has become one of the most popular tools for managing the MySQL database. It makes you easier to manage the MySQL databases, especially for the newcomer that wants to learn about the MySQL itself.

In this tutorial, we will show you how to install and secure phpMyAdmin with the Apache webserver on Ubuntu 20.04 Bionic Beaver.

Prerequisite

For this guide, ensure you have the Ubuntu 20.04 with the basic LAMP Stack installed on top of it.

Below is the tutorial about the Ubuntu 20.04 server installation.

Install Ubuntu 20.04 Server

And below is the guide LAMP Stack installation on the Ubuntu 20.04 server.

Install LAMP Stack on Ubuntu 20.04 Server

Before going any further, log in to your Ubuntu server and type the sudo command to get the root privileges on your system.

sudo su

Step 1 – Install phpMyAdmin on Ubuntu 20.04

First of all, we will install the phpMyAdmin on the Ubuntu 20.04. The phpMyAdmin package is available on the Ubuntu repository.

You can check the phpMyAdmin package using the ‘apt show‘ command below.

apt show phpmyadmin

As can be seen, the phpMyAdmin 4.9.5 are available on the default Ubuntu reposotory.

Show phpmyadmin package version

Now install the phpMyAdmin package using the ‘apt install‘ command below.

apt install phpmyadmin

During the phpMyAdmin installation, you will be asked for the webserver and phpMyAdmin database configuration.

For the webserver configuration, choose the ‘Apache2‘ and select ‘OK‘.

Choose Apache2 as Webserver for phpMyAdmin

For the phpMyAdmin database configuration with the dbconfig-common, select ‘Yes‘.

Setup phpMyAdmin database with dbconfig-common

Now type a new MySQL password for the phpMyAdmin and select ‘OK‘.

setup password for phpmyadmin

Repeat the password for phpMyAdmin and select ‘OK‘.

repeat password

Once the installation is complete, restart the Apache service.

systemctl restart apache2

Now the phpMyAdmin installation is completed. The source code of phpMyAdmin located at the ‘/usr/share/phpmyadmin’ directory, and the phpMyAdmin configuration stored at the ‘/etc/phpmyadmin’ directory.

And as a result, you’ve installed the phpMyAdmin on the Ubuntu 20.04 server. Also, you’ve set up the MySQL user and password for phpMyAdmin.

Step 2 – Grant Privileges for phpMyAdmin User

After installing the phpMyAdmin, we will set up the privilege for the default phpMyAdmin user.

We will grant all privileges on the MySQL database and system to the phpMyAdmin user. In effect, you’re able to create, update, and delete the MySQL database from the phpMyAdmin dashboard.

Now log in to the MySQL shell using the MySQL root user.

mysql -u root -p
Type Your Password:

After that, grant all database privileges to the default ‘phpmyadmin’ user.

GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost';
FLUSH PRIVILEGES;
EXIT

Allow phpMyAdmin to access databases

As a result, the ‘phpmyadmin’ user will be able to create, update, and delete databases on the MariaDB server.

Step 3 – Secure phpMyAdmin on Ubuntu 20.04

In this step, we will secure the phpMyAdmin installation by changing the default path of phpMyAdmin and setup the Apache basic auth module for additional authentication.

– Change Default URL Path of phpMyAdmin

Go to the ‘/etc/apache2/conf-available‘ directory and edit the phpMyAdmin configuration ‘phpmyadmin.conf’ using vim editor.

cd /etc/apache2/conf-available/
vim phpmyadmin.conf

Change the default Alias for the phpMyAdmin with your secret path as below.

Alias /secretadm20 /usr/share/phpmyadmin

Save and close.

Now restart the Apache service.

systemctl restart apache2

As a result, the phpMyAdmin is now accessible through the custom path URL ‘http://ipserver/secretadm20‘.

Change URL path phpMyAdmin

– Secure with Apache Basic Auth Module

The Apache Basic Auth is a module that can be used to restrict access to the web page.

In this tutorial, we will enable the Apache basic auth module to restrict access to the phpMyAdmin.

First, install the ‘apache2-utils’ package using the apt command below.

apt install apache2-utils

Now go to the ‘/etc/apache2/conf-available’ directory and edit the ‘phpmyadmin.conf’ file.

cd /etc/apache2/conf-available/
vim phpmyadmin.conf

Add the ‘AllowOverride ALL‘ configuration as below.

<Directory /usr/share/phpmyadmin>
    Options SymLinksIfOwnerMatch
    DirectoryIndex index.php
    AllowOverride ALL

Save and close.

Next, go to the ‘/usr/share/phpmyadmin’ directory and create a new ‘.htaccess’ file.

cd /usr/share/phpmyadmin/
vim .htaccess

Paste the Apache basic auth configuration below.

AuthType Basic
AuthName "Restricted Files"
AuthUserFile /etc/phpmyadmin/.htpasswd
Require valid-user

Save and close.

After that, generate a new user file for the basic authentication using the htpasswd command below.

htpasswd -c /etc/phpmyadmin/.htpasswd dream

Type your password and the user file for the basic authentication has been created.

Additionally, if you want to add a new user into it, use the htpasswd command without a ‘-c’ option.

htpasswd /etc/phpmyadmin/.htpasswd user02

Next, test the Apache configuration and make sure there is no error. Then restart the Apache service.

apachectl configtest
systemctl restart apache2

Enable Apache Basic Authentication

As a result, you’ve changed the default phpMyAdmin URL path and enabled the Apache basic authentication for securing the phpMyAdmin installation.

Step 4 – Testing

Open your web browser and type the phpMyAdmin custom path URL.

http://ipserver/secretadm20/

Now you will get the login page of the Apache basic authentication.

Apache basic authentication

Type your user and password, then click the ‘Sign In’ button.

As can be seen, you will be shown the login page of phpMyAdmin.

Login to phpMyAdmin

Type the ‘phpmyadmin’ user and the password that just set up on top during the installation and click the ‘Login’ button.

Once the user and password are correct, you will get the phpMyAdmin dashboard as below.

Install phpMyAdmin on Ubuntu 20.04

As a result, you’ve installed the phpMyAdmin with the Apache webserver on Ubuntu 20.04. And you’ve successfully secured the phpMyAdmin installation by changing the default URL path and enabled the Apache basic authentication.

Write A Comment