How to Install phpMyAdmin on Ubuntu

How to Install phpMyAdmin on Ubuntu

phpMyAdmin is a free and open-source Graphical User Interface (GUI) tool used to manage MySQL databases on shared and VPS hosting plans. Even though it isn’t technically necessary as you can manage the databases using the command line interface, phpMyAdmin is more user-friendly.

If you’re using a virtual private server, you’ll have to install phpMyAdmin on the server manually. This article will walk you through the process of phpMyAdmin installation on Ubuntu servers versions 18.04 and 20.04.

Before Installing phpMyAdmin

Before you begin, you’ll need to install the LAMP (Linux, Apache, MySQL, PHP) stack on your server. Also, the MySQL database should be up and running.

You’ll also need to use an SSH client to connect to the server. We have a tutorial on how to connect PuTTY to the server if you have trouble with it.

How to Install phpMyAdmin – Ubuntu 18.04 and 20.04

Installing phpMyAdmin on Ubuntu involves five steps. In addition to the installation, you’ll need to grant permissions, create a separate user, and secure phpMyAdmin.

  1. Install phpMyAdmin package
  2. Setup user and grant permissions
  3. Access phpMyAdmin on browser
  4. Create a new user
  5. Secure phpMyAdmin

Step 1. Install phpMyAdmin Package

The installation procedure is relatively easy using an SSH client. However, there are some command differences between Ubuntu 18.04 and Ubuntu 20.04.

Installing phpMyAdmin – Ubuntu 18.04

Run the following command to install the phpMyAdmin package and required PHP extensions:

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

During the installation process, you will have to select a web server to configure phpMyAdmin. Select Apache by pressing Space. A * character will appear in the bracket next to apache2, indicating that you’ve selected it. Press Enter to continue.

Select Apache2 on phpMyAdmin setup

You will then have to configure the database. Select Yes.

Configure database on phpMyAdmin setup

Now, set a MySQL application password for phpmyadmin. Make sure to use a strong and unique password. Verify it when prompted, and the installation will be completed.

Enter database password on phpMyAdmin setup

Installing phpMyAdmin on Ubuntu 20.04

The steps to install phpMyAdmin on Ubuntu 20.04 are quite similar. However, you’ll have to run this command instead:

sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl

Then, you will need to select a web server. Pick Apache2 by pressing the Space key and hit Enter to continue.

Configuring the database follows the same process. Select Yes and set a strong and unique password for phpMyAdmin.

Once completed, enable the PHP Mbstring extension with this command:

sudo phpenmod mbstring

Finally, restart the Apache service by using this command:

sudo systemctl restart apache2

Step 2. Set Up the User and Grant Permissions

The default phpMyAdmin account has minimal options available. You’ll have to grant some permissions to phpMyAdmin to make it a practical solution to create and manage MySQL databases.

To manage MySQL users, log in to the MySQL command line as a root user with this command:

sudo mysql -u root -p

You’ll have to enter the MySQL root password before accessing the command line. Once you’ve accessed the MySQL root account, grant permissions to the phpmyadmin by executing these commands one at a time:

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

Step 3. Access phpMyAdmin on a Browser

When the installation procedure is complete, open the browser and go to http://your-server-IP/phpmyadmin. Replace your-server-IP with the server’s IP address. If you’re using Hostinger VPS, you will find the IP address on the server management panel.

SSH details on hPanel

In case you use localhost, use the http://localhost/phpmyadmin URL.

The phpMyAdmin login page will look like this:

phpMyAdmin login page

Due to security issues, Ubuntu 18.04 and Ubuntu 20.04 don’t support root login. Instead, log in with the phpmyadmin username and the MySQL password you set during Step 1. After that, you will be taken to the phpMyAdmin web interface.

Step 4. Create a Separate phpMyAdmin User (Optional)

If you don’t want to use or work with phpMyAdmin’s default user account for security reasons, create a new dedicated MySQL user with full privileges.

You have to access the MySQL command-line interface as a root user to create a dedicated user. Use this command to do so:

sudo mysql -u root -p

Input the MySQL root password when prompted. Then, enter the following commands to create a new user:

CREATE USER username IDENTIFIED by 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';
FLUSH PRIVILEGES;
EXIT

Replace username and password with the desired MySQL credentials. Remember to use a strong password to secure the account.

Restart the Apache server if you encounter any problems logging in. This is the command to do so:

sudo systemctl restart apache2

We recommend looking at the Status tab on the phpMyAdmin dashboard. It shows the number of connections to the MySQL database server, current MySQL queries running on the server, and how long the server has been operating.

phpMyAdmin dashboard

Step 5. Secure phpMyAdmin (Optional)

phpMyAdmin is susceptible to cyberattacks, so you should implement measures like an extra authentication method to improve its security. Enable an additional login in front of the application using Apache’s built-in .htaccess authentication and authorization features.

To do so, enable .htaccess file override by modifying the phpMyAdmin Apache configuration file. Here’s the command to open and edit the file using the Nano text editor:

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

Now, add an AllowOverride All line in the <Directory /usr/share/phpmyadmin> section.

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

It should look like this:

.htaccess override in phpMyAdmin configuration file

Save and exit the file by pressing CTRL+X. Then, hit Y and Enter when prompted. Restart the Apache web server to implement the changes by using this command:

sudo systemctl restart apache2

The next step is to create a .htaccess file in the phpMyAdmin application directory using the following command:

sudo nano /usr/share/phpmyadmin/.htaccess

The Nano text editor will open, allowing you to edit the .htaccess file. Enter the following text:

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

Save and exit the file. Since .htaccess specifies that the file for the password is /etc/phpmyadmin/.htpasswd, we will have to use the same path.

Use this command to create the password file and pass it using the htpasswd utility:

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

Replace USERNAME with the actual username. You will also need to input and confirm a password. Once that’s done, the file will be created, and the password will be hashed.

You can create an additional username by using the same command but excluding the -c option:

sudo htpasswd /etc/phpmyadmin/.htpasswd ADDITIONAL_USERNAME

Restart Apache to implement the changes:

sudo systemctl restart apache2

A new username and password authentication pop-up will appear when accessing phpMyAdmin. Verify it by visiting http://your-server-IP/phpmyadmin. The sign-in prompt should look like this:

Additional authentication for phpMyAdmin.

Enter the username and password you just created. After signing in, you’ll find the phpMyAdmin login page. Enter the phpmyadmin user credentials to access the phpMyAdmin web interface.

Conclusion

phpMyAdmin helps you manage MySQL databases and perform table maintenance. The process to install the application on Ubuntu 18.04 and Ubuntu 20.04 is similar but uses a few different commands during the early steps.

Don’t forget to implement security measures on phpMyAdmin by using strong passwords and an additional authentication method.

You should now be able to manage databases with ease from phpMyAdmin. Leave a comment below if you have any questions.

Author
The author

Laura Z.

Laura is an experienced Content Production Lead. With 9+ years of experience in marketing under her belt, Laura loves sharing her ideas with the world. In her free time, she enjoys learning about new marketing trends and watching TV shows on Netflix.

Author
The Co-author

Leonardus N.

Leo is a WordPress fanatic and contributor. He likes keeping up with the latest WordPress news and updates, and sharing his knowledge to help people build successful websites. When he's not working, he contributes to WordPress documentation team and pampers his dogs.