phpMyAdmin is a free and open-source administration tool for MySQL and MariaDB. As a portable web application written primarily in PHP, it has become one of the most popular MySQL administration tools, especially for web hosting services. In this guide, we are going to install phpMyAdmin with Apache on Ubuntu 16.04.

Step 1: Install phpMyAdmin
Before proceeding with the installation, we need to update our local package index with the command below:

sudo apt-get update

To install phpMyAdmin from the default Ubuntu repositories, type the command below followed by y to continue:

sudo apt-get install phpmyadmin php-mbstring php-gettext

After installation, a series of questions will be asked in regards to the basic configuration of phpMyAdmin.

    1. Use the tab key on the keyboard for navigation.

  • You can change the Port number for mySQL or leave it blank to use the default

  • Enter the name for the MySQL database to be used by phpmyadmin

  • Enter a phpAdmin password, else a random password will be generated.

  • Type in the name of the database's administrative user

  • We are working with an Apache server here, so we select apache2

If you didn't get any of the steps above after installation of phpmyadmin, you can type the command below:

sudo dpkg-reconfigure phpmyadmin

The next step is to explicitly enable the PHP mcrypt and mbstring extensions, which can be done by typing the commands below:

sudo phpenmod mcrypt  
sudo phpenmod mbstring  

Next, we need to restart Apache with the command:

sudo systemctl restart apache2  

Finally, in order to access PhpMyAdmin web interface, open a browser and type the following network address.

http://your_server_IP/phpmyadmin

Log into the phpmyadmin interface using the root username and the administrative password you set up during the MySQL installation.

After logging in, the user interface looks like the screenshot below:

SECURING YOUR PHPMYADMIN INSTANCE
Securing your phpmyadmin is important in order to prevent unauthorized access. An easy way to do this is to place a gateway in front of the entire application by using Apache's built-in .htaccess authentication and authorization functionalities.

Firstly, we will need to enable the use of .htaccess file overrides by editing our Apache configuration file. Open the phpmyadmin.conf with the command below:

sudo nano /etc/apache2/conf-available/phpmyadmin.conf

Add AllowOverride Allto the bottom of the <Directory /usr/share/phpmyadmin> section of the configuration file as shown below:

Save the file before closing then proceed to restart Apache with the command: sudo systemctl restart apache2

After enabling .htaccess use, we need to create the file to fully implement security by typing the command below:

sudo nano /usr/share/phpmyadmin/.htaccess

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

You can read more on htaccess here.

Type the command below to add the password to the htaccess file:

sudo htpasswd -c /etc/phpmyadmin/.htpasswd username

.

With this, whenever you access phpMyAdmin on your domain, you will be prompted for the additional account name and password that you just configured as shown below:

After typing the Apache authentication password, you'll be redirected to the original phpMyAdmin authentication page to enter your other credentials.