Introduction

phpMyAdmin is a free application for managing a MySQL server.

Installing phpMyAdmin allows management of MySQL databases through a user-friendly interface, instead of entering commands into the terminal.

This guide will help you install phpMyAdmin on Ubuntu 18.04, with additional security features.

secure and install phpmyadmin on ubuntu 18

Prerequisites

  • A server running Ubuntu 18.04
  • A fully installed LAMP stack (Linux, Apache, MySQL, PHP)
  • Ubuntu user account with sudo privileges

This guide assumes you’re installing to a local system. If you need to set up encryption or security certificates for configuring a remote server, please refer to this guide on open SSL.


Note: If you’re connecting to a remote server, there’s a security risk in running phpMyAdmin over plain HTTP. A plain HTTP connection is unencrypted and can be intercepted. Also, since phpMyAdmin is a popular application, it’s a common vector for attack.


Installing phpMyAdmin on Ubuntu

Step 1: Add and Update Software Repositories

Some Ubuntu 18.04 installations are not configured with the software repositories for phpMyAdmin.

To add them, open a terminal window and enter the following command:

sudo add-apt-repository universe

The system should ask for your password – enter it, then make sure it completes successfully. Once completed, refresh the software lists by entering the following:

sudo apt-get update && sudo apt-get upgrade

Allow the process to finish.

Step 2: Install phpMyAdmin

To install phpMyAdmin, enter the command:

sudo apt-get install phpmyadmin

The system should ask for confirmation – press Y then enter, and the system should download and install the phpMyAdmin software package.

Step 3: Configure phpMyAdmin on Ubuntu

Shortly after you start the installation, the installer will prompt you to choose which web server should be automatically configured.

Select “Apache2” -> Spacebar to choose -> Tab -> Enter

package confirmation of apache2

Next, the installer will note that you have to have a database installed and configured before you can use phpMyAdmin. Select “Yes” -> Enter.

configuring phpmyadmin2


Note: If you’re an advanced user, you can manually configure the database, but doing so is outside the scope of this guide. You can find more information on dbconfig-common here.


Shortly after, the installer will ask you to provide a password. The installer is creating a default user named “phpMyAdmin” and the password you type here will work as an administrator password for this user.

Type a strong password and hit Enter, then confirm the password and hit Enter again.

Leaving the password field blank will cause the system to generate a random password.

configuring phpmyadmin3

Step 4: Enable the mbstring Extension

Mbstring is an extension for PHP. It helps email and .pdf files to work correctly, but it’s not enabled by default.

Enter the following in your terminal to enable mbstring:

sudo phpenmod mbstring

Hit Enter and allow the process to finish.

Restart the Apache server to refresh the changes by entering the following in a terminal:

sudo systemctl restart apache2

Step 5: Create a New MySQL Administrator Account

By default, MySQL 5.7 and later in Ubuntu, authenticate a root user using auth_socket. This compares the socket username against the name specified in the MySQL user table and is a tightly-secured authentication method. However, this can make it difficult to use phpMyAdmin.

It is possible to change the authentication type, but it’s simpler to create a new administrator account for MySQL.

Log in to the MySQL server as follows:

sudo mysql

This should change the terminal prompt to show mysql>, indicating that you’re logged into the MySQL shell.

Enter the following commands, replacing UserName with a username of your choice, and PassWord with a strong password of your choice:

CREATE USER 'UserName'@'localhost' IDENTIFIED BY 'PassWord';
GRANT ALL PRIVILEGES ON *.* TO 'padmin'@'localhost' WITH GRANT OPTION;

Once the operation completes, exit the MySQL shell by entering:

exit

Step 6: Test phpMyAdmin

Open up a browser window and enter your server’s domain name or IP address, followed by /phpMyAdmin:

192.168.0.1/phpMyAdmin
www.domain.com/phpMyAdmin

The browser should display a screen welcoming you to phpMyAdmin, with a login field. Enter the username and password you created. The browser should load the phpMyAdmin dashboard.

Secure phpMyAdmin

Being easy to use – and free – makes phpMyAdmin a target for hackers. Follow these steps to use Apache’s built-in features to restrict access and secure phpMyAdmin directories.

Enable .htaccess

In a terminal window, enter the following command to edit the phpMyAdmin.conf file:

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

In the editor, scroll down to the section labeled <Directory /usr/share/phpMyAdmin>. You should see the following two lines:

Options FollowSymLinks

DirectoryIndex index.php

Just below these entries, add the following text:

AllowOverride All

Save the file and exit. Then apply the changes by restarting Apache with the following terminal command:

sudo systemctl restart apache2

Create .htaccess File

The .htaccess file allows a more detailed configuration of the Apache web server. Enter the following in the terminal:

sudo nano /usr/share/phpMyAdmin/.htaccess

This will create a new file and open it in a text editor.

In the text editor, enter the following lines:

AuthType Basic

AuthName "Restricted Files"

AuthUserFile /etc/phpMyAdmin/.htpasswd

Require valid-user

Save the file and close.

These commands configure .htaccess to use basic authentication, to secure restricted files only to valid users, and where to find a list of valid users.

Setup .htaccess Users

This step creates a username and password to access the phpMyAdmin directory. This will force anyone trying to access the phpMyAdmin directory to enter a username and password.

In the terminal window, enter the following:

sudo htpasswd -c /etc/phpMyAdmin/.htpasswd UserName

Replace UserName with the actual username you want to use to access the phpMyAdmin directory.  The system will prompt you to enter and confirm a password.

If you need to add additional users, use the same command without the –c switch, as follows:

sudo htpasswd  /etc/phpMyAdmin/.htpasswd SecondUser

Test the Login

Go back to your browser window, and open

IP_address/phpMyAdmin.

The system should prompt you with a pop-up that asks for credentials, saying “Restricted files.” Enter the username and password you created earlier.

You should now see the phpMyAdmin login screen. Enter your credentials, and the system should display the phpMyAdmin user interface.

Conclusion

Great job, you now know how to install phpMyAdmin on Ubuntu.

You should now be able to manage your MySQL database from the phpMyAdmin tool. This includes managing users, creating databases, and much more.

PhpMyAdmin replaces the command-line interface used in this guide with a more intuitive utility to simplify database management tasks.