How To Install phpMyAdmin for Apache on Ubuntu 20.04

How To Install & Secure phpMyAdmin for Apache on Ubuntu 20.04

Last updated on

In this guide we will install and secure phpMyAdmin to work with Apache on Ubuntu Server 20.04.

Prerequisites

You should be using a non-root user with sudo privileges as explained in Ubuntu 20.04 Initial Server Setup.

You should also have your LAMP stack for Ubuntu 20.04 already installed and serving web pages before continuing with this guide.

1. Install phpMyAdmin

Let’s begin by updating the package lists and installing phpMyAdmin on Ubuntu 20.04.

Below we have two commands separated by &&. The first command will update the package lists to ensure you get the latest version and dependencies for phpMyAdmin.  I am also including the PHP extension php-mbstring, which is strongly recommended for performance reasons.

sudo apt update && sudo apt install phpmyadmin php-mbstring

Press y and ENTER when asked to continue.

1.1. Select apache2

If you are prompted to choose a web server, press SPACE to put a star [*] beside apache2, then press TAB to highlight OK and press ENTER.

1.2. Configure Database

Select Yes and press ENTER to install and configure the database.

1.3. Application Password

The MySQL application password is used internally by phpMyAdmin to communicate with MySQL and it is not recommended that you use it to log into phpMyAdmin. You can leave this blank and a password will be generated automatically.

Press ENTER to continue.

2. Test phpMyAdmin

You should now be able to access the phpMyAdmin web interface by visiting your server’s domain name or public IP address followed by /phpmyadmin. e.g. http://example.com/phpmyadmin or http://192.168.1.10/phpmyadmin

If you don’t have a domain name yet or don’t know your IP, you can find out with:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

MySQL root Account and phpMyAdmin

In MySQL 5.7 and above, you will not be able to log into phpMyAdmin using the MySQL root account and will get an error “Access denied for user ‘root’@’localhost'”. Instead, you should create a new superuser account just for phpMyAdmin. If you want force MySQL to allow root login via phpMyAdmin, see: Can’t log into phpMyAdmin with root

3. Create MySQL Superuser

In terminal, log into MySQL using your MySQL root password.

You may have created a root password when you installed MySQL for the first time or the password could be blank. If you have forgotten your MySQL root password, see: Reset MySQL Root Password

sudo mysql -u root -p

Once logged in, add a new MySQL superuser with the username of your choice. In this example we are calling it pmauser. Click here to generate a strong password and replace password_here below with it.

CREATE USER 'pmauser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password_here';

Now grant superuser privileges to our new user pmauser.

GRANT ALL PRIVILEGES ON *.* TO 'pmauser'@'localhost';

Exit MySQL.

exit

You should now be able to access phpMyAdmin using this new user account.

phpMyadmin login Ubuntu 20.04

It is strongly recommended that you set up some additional security for phpMyAdmin in the steps below.

4. Obscure phpMyAdmin URL

Bots continuously scan web servers for the default phpMyAdmin login page, so it is recommended that you change the URL to something else.

In this example we are going to change it from example.com/phpmyadmin to example.com/pmahidden.

Open the phpMyAdmin configuration file for Apache using the nano text editor.

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

Change the Alias from /phpmyadmin to /pmahidden – you can change this to whatever you want.

/etc/apache2/conf-available/phpmyadmin.conf
# phpMyAdmin default Apache configuration

Alias /pmahidden /usr/share/phpmyadmin

Save and exit (press CTRL + X, press Y and then press ENTER)

Now you must reload the Apache service for changes to take effect.

sudo service apache2 reload

You should now be able to access phpMyAdmin at example.com/pmahidden

5. Protect with .htpasswd

We can further protect the phpMyAdmin login page with .htpasswd. This adds another line of defence against bots and attackers.

5.1.  Allow .htaccess Overrides

To set up .htpasswd, we must first change the phpMyadmin Apache configuration file to allow .htaccess Overrides.

Open the config file in nano text editor.

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

Add AllowOverride All underneath DirectoryIndex index.php

/etc/apache2/conf-available/phpmyadmin.conf
<Directory /usr/share/phpmyadmin>
    Options SymLinksIfOwnerMatch
    DirectoryIndex index.php
    AllowOverride All

Save and exit (press CTRL + X, press Y and then press ENTER)

Now reload the Apache service.

sudo service apache2 reload

5.2. Set up .htpasswd

We are going to create a new .htaccess file in the phpMyAdmin install directory using the nano text editor.

sudo nano /usr/share/phpmyadmin/.htaccess

Paste in the following. (Use the right mouse button to paste if using PuTTY on Windows)

/usr/share/phpmyadmin/.htaccess
AuthType Basic
AuthName "Restricted Access"
AuthUserFile /etc/phpmyadmin/.htpasswd
Require valid-user

Save and exit (press CTRL + X, press Y and then press ENTER)

We can now generate the .htpasswd file using the htpasswd tool.

In this example, we are creating a new user called pmauser (php my admin user), though you can change this to whatever you want.

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

You will be asked to enter a new password twice (Click here to generate a strong password).

That’s it, you’re done! Visit phpMyAdmin in your browser and you should now be prompted to enter login details.

What Next?

By now you will have successfully implemented your LAMP stack (Apache/MySQL/PHP) for Ubuntu 20.04 and can administer MySQL through phpMyAdmin.

You may now want to configure SSL for you domain or set up an FTP server.

Manually Update phpMyAdmin (Optional)

The repositories often don’t give you the latest releases of phpMyAdmin. If you would like to manually upgrade to the the latest version, see:

1 Star2 Stars3 Stars4 Stars5 Stars 5.00 (4 votes)

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

p.s. I increased my AdSense revenue by 68% using AI 🤖. Read my Ezoic review to find out how.

18 replies

avatar
  Subscribe  
newest oldest
Notify of
xiaowan
Guest
xiaowan
xiaowan
3 months ago

Hello sir! I installed phpmyadmin according to your method but the phpmyadmin login page could not be opened after installation. Show’Not Found’ how to fix it, please

Mamat
Guest
Mamat
Mamat
24 days ago

Same problem,
I follow all tutorial in this blog (installing apache2, mysql, Multi PHP).

when access phpmyadmin page showing 404 not found.
how to solve this issue?

cabbage
Guest
cabbage
cabbage
4 days ago

I have the same problem but my issue is in step 1.1, i miss out the step “press the space to highlight an asterisk beside apache2”. Instead i press enter straight away.
I found a way to reconfigure the phpmyadmin, but i also waiting for the author to brief on more appropriate way to solve this if this is the scenario

Srikant kumar
Guest
Srikant kumar
Srikant kumar
4 months ago

Sir, small correction

GRANT ALL PRIVILEGES ON *.* TO 'pmauser'@'%' WITH GRANT OPTION;

this is giving error: you are not allowed create user with grant

Solution

GRANT ALL PRIVILEGES ON *.* TO 'pmauser'@'%';

I am using latest version mysql on ubuntu 20

John
Guest
John
John
4 months ago

Thanks sir!

John
Guest
John
John
4 months ago

ERROR 1410 (42000): You are not allowed to create a user with GRANT, hello DevAnswers eu can help me

Deepesh Dhakal
Guest
Deepesh Dhakal
Deepesh Dhakal
4 months ago

Hello writer,
I have been struggling to set up the LAMP stack on my ubuntu. I went to so many sites and failed. Specifically to set up the phpmyadmin. Thank you from the bottom of my heart for setting up these pages for Ubuntu’s latest OS version. God bless you !

John
Guest
John
John
4 months ago

GRANT ALL PRIVILEGES ON . TO ‘pmauser’@’%’ WITH GRANT OPTION; HELP ME ERROR 1410 (42000): You are not allowed to create a user with GRANT

david
Guest
david
david
4 months ago

Hi, when trying to install it gives me error in php-gettext (sudo apt update && sudo apt install phpmyadmin php-mbstring php-gettext)

alex
Guest
alex
alex
4 months ago

php-gettext is not released yet in Focal servers https://launchpad.net/ubuntu/focal/+source/php-gettext