How to Install and Secure phpMyAdmin on Ubuntu

December 5, 2018

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 or 20.04, with additional security features.

How to Install and Secure phpMyAdmin on Ubuntu

Prerequisites

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 attacks.

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

If the repository is already enabled, skip to the next step. If not, the system asks for your password – enter it, then make sure the process completes successfully. Once completed, refresh the software lists by entering the following commands:

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

Allow the operation to finish.

Step 2: Install phpMyAdmin

To install phpMyAdmin, enter the command:

sudo apt-get install phpmyadmin

The system asks for confirmation – press Y then Enter, and the system downloads and installs the phpMyAdmin software package.

Step 3: Configure phpMyAdmin on Ubuntu

Shortly the installation starts, the installer prompts to choose the web server to automatically configure.

Select apache2 (use spacebar to choose), press Tab to navigate to <ok> and hit Enter.

package confirmation of apache2

Next, the installer will notes that phpMyAdmin needs a database installed and configured before starting use. Select <Yes> and hit 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. Find more information on dbconfig-common on Debian’s official site.

Shortly after, the installer asks to provide a password. The program creates a default user named "phpMyAdmin" and the password you type here works 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 causes 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

Press Enter and allow the process to finish.

Restart the Apache service 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 makes using phpMyAdmin difficult.

It is possible to change the authentication type. However, a simpler method to avoid auth_socket is to create a new administrator account for MySQL.

Log in to the MySQL server as follows:

sudo mysql

This changes 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 'UserName'@'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 localhost, your server’s IP address or domain name, followed by /phpMyAdmin:

localhost/phpMyAdmin
192.168.0.1/phpMyAdmin
www.domain.com/phpMyAdmin

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

Secure phpMyAdmin on Ubuntu

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, find the section labeled <Directory /usr/share/phpMyAdmin>. You should see the following two lines:

Options SymLinksIfOwnerMatch
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 creates a new file and opens it in a text editor.

In the text editor, add 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.

Note: For additional configuration options refer to this detailed guide on configuring .htaccess on Apache.

Setup .htaccess Users

This step creates a username and password to access the phpMyAdmin directory. The process generates a prompt, forcing 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 asks 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 activates 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 displays 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.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How To Install PhpMyAdmin On CentOS 8
June 2, 2020

The phpMyAdmin software is used for working with MySQL databases through a graphical interface. This guide shows you how to run a CentOS 8 server with Apache, MySQL, and PHP.
Read more
How To Install PhpMyAdmin On Debian 10 (Buster)
December 5, 2018

This clear-cut tutorial explains how to download and configure phpMyAdmin on Debian 10. Also, find out how to thoroughly prepare your server by installing a LAMP stack.
Read more
How To Enable & Set Up .Htaccess File On Apache
April 9, 2019

The .htaccess file in Apache is a tool that allows configurations at the directory and subdirectory level. Using a .htaccess file will let you configure website details without altering server configuration files.
Read more
How To Install The LAMP Stack On Ubuntu 18.04
December 5, 2018

A LAMP stack is a set of open source software used for web application development. Learn how to install and set up a LAMP Stack in Ubuntu.
Read more