How to Install phpMyAdmin with Apache (LAMP) on Ubuntu 18.04

This tutorial will be showing you how to install phpMyAdmin with Apache, MariaDB, PHP7.2 (LAMP stack) on Ubuntu 18.04 LTS. phpMyAdmin is a free and open-source web-based database management tool written in PHP. It provides a graphical web interface for users to manage MySQL or MariaDB database. We are also going to learn how to enable two-factor authentication on phpMyAdmin.

phpMyAdmin allows administrators to:

  • browse through databases and tables;
  • create, copy, rename, alter and drop databases;
  • create, copy, rename, alter and drop tables;
  • perform table maintenance;
  • add, edit and drop fields;
  • execute any SQL-statement, even multiple queries;
  • create, alter and drop indexes;
  • load text files into tables;
  • create and read dumps of tables or databases;
  • export data to SQL, CSV, XML, Word, Excel, PDF and LaTeX formats;
  • administer multiple servers;
  • manage MySQL users and privileges;
  • check server settings and runtime information with configuration hints;
  • check referential integrity in MyISAM tables;
  • create complex queries using Query-by-example (QBE), automatically
  • connecting required tables;
  • create PDF graphics of database layout;
  • search globally in a database or a subset of it;
  • transform stored data into any format using a set of predefined functions, such as displaying BLOB-data as image or download-link;
  • manage InnoDB tables and foreign keys;

Prerequisites

To follow this tutorial, you need a Ubuntu 18.04 OS running on your local computer or on a remote server.

If you are looking for a VPS (Virtual Private Server), then you can create an account at Vultr via my referral link to get $50 free credit (for new users only). And if you need to set up phpMyAdmin with a domain name, I recommend buying domain names from NameCheap because the price is low and they give whois privacy protection for free.

It is assumed that you have already installed LAMP stack on Ubuntu 18.04. If not, please check out the following tutorial.

With that out of the way, let’s get started with installing phpMyAdmin.

Step 1: Download and Install phpMyAdmin on Ubuntu 18.04

phpMyAdmin is included in Ubuntu 18.04 software repository. so we can easily install it with the command below.

sudo apt update
sudo apt install phpmyadmin

The above command will install all necessary dependencies including PHP7 extensions. During the installation, it will prompt you to select a web server to configure. Hit the space bar to select apache2. (An asterisk * indicates the item is selected). Then hit Tab key and Enter to confirm your choice.

phpmyadmin lamp

In the next screen, select Yes to configure a database for phpMyAdmin with dbconfig-common.

phpmyadmin apache config

Then set a password for the phpmyadmin user in MariaDB/MySQL.

install phpmyadmin ubuntu 18.04 apache2

Once this step is done, a new database named phpmyadmin is created and the database user phpmyadmin has necessary privileges to manage this database. If you are curious as I am, you can log into MariaDB and check what privileges phpmyadmin user has been granted.

You can use the following command to log into MariaDB server.

sudo mysql -u root

Then check the privileges of phpmyadmin user.

show grants for phpmyadmin@localhost;

Output:

phpmyadmin database configuration

As you can see, user phpmyadmin has all privileges on database phpmyadmin. Now you can exit by executing:

exit;

Now run the following command to check if the /etc/apache2/conf-enabled/phpmyadmin.conf file exists.

file /etc/apache2/conf-enabled/phpmyadmin.conf

If there’s no error in the installation process, you should see the following command output.

/etc/apache2/conf-enabled/phpmyadmin.conf: symbolic link to ../conf-available/phpmyadmin.conf

If this file doesn’t exist on your server, it’s likely that you didn’t select Apache web server in the phpMyAdmin setup wizard. You can fix it with the following commands.

sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf

sudo a2enconf phpmyadmin

sudo systemctl reload apache2

Step 2: Accessing phpMyAdmin from a Sub-directory

When installing phpMyAdmin, a configuration file is placed at /etc/apache2/conf-enabled/phpmyadmin.conf, which allows us to access phpMyAdmin via sub-directory. If you have installed other web applications like WordPress on the same Ubuntu 18.04 server, then simply add /phpmyadmin after your domain name in the browser address bar.

your-domain.com/phpmyadmin

phpmyadmin ubuntu

If phpMyAdmin is installed on your local Ubuntu computer, then you can access phpMyAdmin web interface by typing in the following text in the browser address bar.

localhost/phpmyadmin

If the connection is refused or failed to complete, there might be a firewall preventing HTTP requests. If you are using iptables firewall, then you need to run the following command to open TCP port 80 and 443.

sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 443 -j ACCEPT

If you are using UFW firewall, then run this command to open TCP port 80 and 443.

sudo ufw allow http
sudo ufw allow https

Now You can use MariaDB/MySQL username and password to log into phpMyAdmin

Step 3: Access phpMyAdmin From a Sub-domain

If your Ubuntu 18.04 server doesn’t have other Apache virtual host, then you may want to use a sub-domain to access phpMyAdmin web interface. This way, you can enable HTTPS to encrypt the traffic.

First, we need to create an Apache virtual host for phpMyAdmin. The existing phpMyAdmin configuration snippet can be used as a template. Let’s copy it to a new file.

sudo cp /etc/apache2/conf-enabled/phpmyadmin.conf /etc/apache2/sites-available/phpmyadmin.conf

Then edit the new file with a command line text editor, such as Nano.

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

Add the following lines at the beginning of this file. Replace pma.example.com with your preferred sub-domain for phpMyAdmin. Don’t forget to create DNS A record for this sub-domain.

<VirtualHost *:80>
    ServerName pma.example.com
    DocumentRoot /usr/share/phpmyadmin

    ErrorLog ${APACHE_LOG_DIR}/pma.error.log
    CustomLog ${APACHE_LOG_DIR}/pma.access.log combined

Add the following line at the end of this file.

</VirtualHost>

Save and close the file. (To save a file in Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X.) Then enable this virtual host.

sudo a2ensite phpmyadmin.conf

Reload Apache web server for this change to take effect.

sudo systemctl reload apache2

Now you should be able to access phpMyAdmin web interface via

pma.example.com

how to install phpmyadmin in lubuntu

Before entering user credentials in the login form, let’s enable HTTPS.

Step 4: Enable HTTPS on phpMyAdmin with Apache

To secure the phpMyadmin web interface, we can install a free Let’s Encrypt TLS certificate. Run the following command to install the Let’s Encrypt client (certbot) from Ubuntu 18.04 software repository.

sudo apt install certbot python3-certbot-apache

Python3-certbot-apache is the Apache plugin for Certbot. Now run the following command to obtain and install TLS certificate.

sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --must-staple -d pma.example.com --email [email protected]

Explanation:

  • –apache: Use the Apache authenticator and installer
  • –agree-tos: Agree to Let’s Encrypt terms of service
  • –redirect: Add 301 redirect.
  • –hsts: Add the Strict-Transport-Security header to every HTTP response.
  • –staple-ocsp: Enables OCSP Stapling.
  • –must-staple: Adds the OCSP Must Staple extension to the certificate.
  • -d flag is followed by a list of domain names, separated by comma. You can add up to 100 domain names.
  • –email: Email used for registration and recovery contact.

You will be asked if you want to receive emails from EFF(Electronic Frontier Foundation). After choosing Y or N, your TLS certificate will be automatically obtained and configured for you, which is indicated by the message below.

ubuntu 18.04 phpmyadmin apache subdomain config

Step 5: Troubleshoot phpMyAdmin Login Error

If you try to login to phpMyAdmin with MariaDB root account, you may see the following error.

#1698 - Access denied for user 'root '@'localhost'

and

mysqli_real_connect(): (HY000/1698): Access denied for user 'root '@'localhost'

If you login with user phpmyadmin, you won’t see the above error. However, user phpmyadmin can only be used to manage the phpmyadmin database. The cause of the error is that by default MariDB root user is authenticated via the unix_socket plugin, instead of using the mysql_native_password plugin. To solve this problem, we can create another admin user and grant all privileges to the new admin user.

Log into MariaDB server from the command line.

sudo mysql -u root

Create an admin user with password authentication.

create user admin@localhost identified by 'your-preferred-password';

Grant all privileges on all databases.

grant all privileges on *.* to admin@localhost with grant option;

Flush privileges and exit;

flush privileges;

exit;

Now you can log into phpMyAmin with the admin account and manage all databases.

mysqli_real_connect HY0001698 Access denied for user 'root '@'localhost'

Step 6: Install Latest Stable Version of phpMyAdmin

Ubuntu 18.04 repository ships with PHP7.2 and phpMyAdmin 4.6.6. You can check your phpMyAdmin version in the browser title bar.

ubuntu 18.04 apache phpmyadmin 4.9

However, PHP7.2 is supported by phpMyAdmin since version 4.7.4. When you use phpMyAdmin 4.6.6 with PHP7.2, you will see the following warning message in the Export and Import tab.

parameter must be an array or an object that implements countable

parameter must be an array or an object that implements countable

To fix the compatibility problem, we can install phpMyAdmin 4.9, which is the latest stable version as of this writing. Download it by using wget.

wget https://files.phpmyadmin.net/phpMyAdmin/4.9.0.1/phpMyAdmin-4.9.0.1-all-languages.zip

Hint: You can always use the above URL format to download the latest stable version of phpMyAdmin. Simply replace 4.9.0.1 with the latest version number.

Then extract it.

sudo apt install unzip

unzip phpMyAdmin-4.9.0.1-all-languages.zip

Back up original phpMyAdmin files.

sudo mv /usr/share/phpmyadmin /usr/share/phpmyadmin-original

Move phpMyadmin 4.9 to /usr/share/phpmyadmin/ directory.

sudo mv phpMyAdmin-4.9.0.1-all-languages /usr/share/phpmyadmin

Edit the vendor config file.

sudo nano /usr/share/phpmyadmin/libraries/vendor_config.php

Find the following line.

define('CONFIG_DIR', '');

Change it to

define('CONFIG_DIR', '/etc/phpmyadmin/');

Save and close the file. Then create the tmp folder to store cache files.

sudo mkdir /usr/share/phpmyadmin/tmp

Change user ownership and group ownership to www-data.

sudo chown www-data:www-data /usr/share/phpmyadmin/tmp

Now you can use phpMyAdmin 4.9 without seeing the PHP errors.

phpmyadmin latest version ubuntu 18.04 apache

Enable Two-Factor Authentication

You can also harden phpMyAdmin by enabling two-factor authentication, which is a feature added in version 4.8. To enable it, log into phpMyAdmin. Then go to Settings -> Two-factor authentication and select Authentication application (2FA).

secure phpmyadmin

After clicking the Configure two-factor authentication button, you will be presented with a QR code, which you need to scan with a two-factor authentication app on your phone.

configure two factor authentication phpmyadmin

Google Authenticator is a popular 2FA app, but I recommend FreeOTP, which is an open-source 2FA app developed by Red Hat. Once you enter the authentication code generated by your 2FA app, two-factor authentication is enabled. If you now log out and log back in, you need to enter the authentication code in addition to username and password.

secure phpmyadmin

Wrapping Up

I hope this tutorial helped you install phpMyAdmin with LAMP on Ubuntu 18.04. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂

Rate this tutorial
[Total: 4 Average: 5]

7 Responses to “How to Install phpMyAdmin with Apache (LAMP) on Ubuntu 18.04

  • Edi Ferreira
    5 years ago

    I got stuck!!! On Step 3: Access phpMyAdmin From a Sub-domain:

    “Add the following lines at the beginning of this file. Replace pma.example.com with your preferred sub-domain for phpMyAdmin. Don’t forget to create DNS A record for this sub-domain.”
    How do I create a DNS A record?
    Because I don’t know how and when try to enter in pma.example.com I get “This site can’t be reached…” page!!!

    • Xiao Guo An (Admin)
      5 years ago

      If you have a domain name, then create DNS A record at your domain registrar’s website.

      • Edi Ferreira
        5 years ago

        So that part it’s only for a registered domain?! I thought that it was for my localhost…

        • Edi Ferreira
          5 years ago

          Should I delete something now or reverse some step?

      • Edi Ferreira
        5 years ago

        Ok!!! I’m silly… now i changed the

        server name

        to

        pma.localhost

        on

        phpmyadmin.conf

        and I have access now…

  • Michelignax
    5 years ago

    Is step 3 valid when phpmyadmin is used locally ?

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here