Home  »  GuidesHow ToSoftwareTechnologyToolsTutorials   »   How to Install the Latest phpMyAdmin on Ubuntu 20.04

How to Install the Latest phpMyAdmin on Ubuntu 20.04

This tutorial will guide you in your quest to install the latest phpMyAdmin on your Ubuntu system.

phpMyadmin is probably the best freely available web-based MySQL and MariaDB administration application. You can download and install on your server running PHP and a server like Apache, Nginx, or IIS among others.

Prerequisites

We are assuming you already have installed the MySQL server on your Ubuntu system. In this case, we will be using Ubuntu 20.04. We will also assume you have Apache, PHP, and a non-root user with sudo privileges, as well as a firewall configured with ufw. This tutorial about the LAMP stack will help you install the necessary prerequisites.

We will also recommend you have an existing domain configured with an SSL/TLS certificate. This is because phpMyAdmin communicates directly with your MySQL installation, uses MySQL credentials to authenticate as well as create users, databases, and their objects, as well as executes and returns results for SQL queries.

If you have the above in place you are ready to proceed.

Default Step (Optional) — Installing phpMyAdmin

PhpMyAdmin can be installed from the default Ubuntu repositories. This will not install the latest available phpMyAdmin version. The steps to install the latest phpMyAdmin will be highlighted later in this tutorial.

The process involves updating your server’s package index and then using apt install them on your Ubuntu system. Use the following commands:

$ sudo apt update
$ sudo apt install phpmyadmin php-mbstring php-gettext

php-mbstring and php-gettext extensions will only be installed if they are not already installed.

Follow the prompts in order to configure your installation correctly with the following hints:

  • For the server selection, choose apache2
  • Select Yes when asked whether to use dbconfig-common to set up the database
  • Choose and confirm a MySQL application password for phpMyAdmin.

The phpMyAdmin Apache configuration file will be added to the /etc/apache2/conf-enabled/ directory.

You may need to explicitly enable the mbstring PHP extension if it has not been previously enabled. This can be done by running the following command:

$ sudo phpenmod mbstring

Restart Apache for your changes to be saved:

$ sudo systemctl restart apache2

phpMyAdmin is now installed and configured. Be sure to make sure MySQL users have the privileges required.

Step 1: Install the Latest phpMyAdmin on Ubuntu 20.04

let’s take a diversion to install the latest phpMyAdmin on your Ubuntu system seeing the above procedure does not give us the latest version.

Download the latest phpMyAdmin archive from the official download page here. Also, visit the page to determine the latest version which is currently phpMyAdmin 5.0.2 at the initial publishing of this tutorial.

Use the following commands to download phpMyAdmin 5.0.2 onto your system.

$ wget https://files.phpmyadmin.net/phpMyAdmin/5.0.2/phpMyAdmin-5.0.2-all-languages.zip
$ unzip phpMyAdmin-5.0.2-all-languages.zip
$ mv phpMyAdmin-5.0.2-all-languages /usr/share/phpmyadmin

If wget or unzip are missing you can install them using the apt install command.

Next, create a tmp directory and set the proper permissions.

$ mkdir /usr/share/phpmyadmin/tmp
$ chown -R www-data:www-data /usr/share/phpmyadmin
$ chmod 777 /usr/share/phpmyadmin/tmp

Step 2: Configure phpMyAdmin

Next, you need to configure webserver to serve phpMyAdmin over the network by creating an Apache configuration file for phpMyAdmin:

$ sudo nano /etc/apache/conf-available/phpmyadmin.conf

Add the content below to the configuration file you have just created.

Alias /phpmyadmin /usr/share/phpmyadmin
Alias /phpMyAdmin /usr/share/phpmyadmin
 
<Directory /usr/share/phpmyadmin/>
   AddDefaultCharset UTF-8
   <IfModule mod_authz_core.c>
      <RequireAny>
      Require all granted
     </RequireAny>
   </IfModule>
</Directory>
 
<Directory /usr/share/phpmyadmin/setup/>
   <IfModule mod_authz_core.c>
     <RequireAny>
       Require all granted
     </RequireAny>
   </IfModule>
</Directory>

Save and close the file, enable the configuration, and restart apache to pick up the new changes using the following commands:

$ sudo a2enconf PHPMyAdmin
$ sudo systemctl restart apache2

You now have the latest phpMyAdmin on your Ubuntu system which can be accessed through the server IP address or domain name.

http://example.com/phpmyadmin

From this point onwards, whether you used the default Ubuntu 20.04 phpMyAdmin or you opted to go for the latest version, everything is pretty much the same from here on.

Sign in with the username and password that you used to access MySQL on the command line. If the user has no privileges then they may have to be set from the MySQL privileged admin user you have. It is important to disable the root user from non-local sources for security reasons seeing phpMyAdmin is generally available off the Internet.

Step 3: Secure Your phpMyAdmin Instance

PhpMyAdmin is a popular target for attackers, and you should take extra care to prevent unauthorized access. We are going to do this by adding a gateway in front of the entire application by using Apache’s built-in .htaccess authentication and authorization functionalities.

Enable the use of .htaccess file overrides by editing your Apache configuration file with this command.

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

Add an AllowOverride All directive within the Directory /usr/share/phpmyadmin section of the configuration file, like this:

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

Save and close the file.

Restart Apache to implement the changes.

$ sudo systemctl restart apache2

Create a password file outside of the directories that are being served directory using the htpasswd utility:

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

You will be prompted to select and confirm a password for the user you are creating.

You can create additional users using the command below. Notice that the -c flag is not used when adding users.

$ sudo htpasswd /etc/phpmyadmin/.htpasswd anotheruser

Now let’s create the .htaccess file in your phpMyAdmin root folder

$ sudo nano /usr/share/phpmyadmin/.htaccess

Type in the following rules into the file:

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

Save and close the file.

These are the options you have for securing the folder:

  • AuthType Basic: This line specifies the authentication type that you are implementing. This type will implement password authentication using a password file.
  • AuthName: This is the message for the authentication dialog box. You should keep this generic so that unauthorized users don’t gain any information about what is being protected.
  • AuthUserFile: This sets the location of the password file that will be used for authentication. This should be outside of the directories that are being served.
  • Require valid-user: This specifies that only authenticated users should be given access to this resource.

This setup adds an additional layer of security with a popup dialog that requires a username and password to access files within the folder.

Conclusion

You now have downloaded, install the latest phpMyAdmin, and configured it on your Ubuntu 20.04 server. You can create databases, users, tables, and other database objects as well as perform the usual operations like selecting, deleting, and modifying structures and data.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.