How to Install phpMyAdmin with Apache (LAMP) on Ubuntu 20.04
This tutorial will be showing you how to install phpMyAdmin with Apache, MariaDB, PHP7.4 (LAMP stack) on Ubuntu 20.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.
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 20.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 20.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 20.04
phpMyAdmin is included in Ubuntu 20.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.
In the next screen, select Yes
to configure a database for phpMyAdmin with dbconfig-common
.
Then set a password for the phpmyadmin
user in MariaDB/MySQL.
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:
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 20.04 server, then simply add /phpmyadmin
after your domain name in the browser address bar.
example.com/phpmyadmin
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 commands 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 80,443/tcp
Now You can use MariaDB/MySQL username and password to log into phpMyAdmin. If you can’t login as root, go to step 5 to troubleshoot this problem.
Step 3: Access phpMyAdmin From a Sub-domain
If your Ubuntu 20.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
This file lacks <VirtualHost>
tags, so we need to 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 to close the <VirtualHost>
tag.
</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
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 20.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]
Where:
- –apache: Use the Apache authenticator and installer
- –agree-tos: Agree to Let’s Encrypt terms of service
- –redirect: Enforce HTTPS by adding 301 permanent 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.
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'
or
mysqli_real_connect(): (HY000/1698): Access denied for user 'root '@'localhost'
If you log in 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.
Wrapping Up
I hope this tutorial helped you install phpMyAdmin with LAMP on Ubuntu 20.04. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂
Another great tutorial!
thank u very much
Hat alles so geklappt, wie beschrieben. Vielen Dank
Hi,
Very good tutorial.
Do you have also done something about security of the LAMP Server ?
Best regards,
If you need to protect your LAMP web applications from hacking, you can use ModSecurity.
How to Set Up ModSecurity with Apache on Debian/Ubuntu
For general Linux server security, read the following tutorials.
How do we restrict access to /phpmyadmin to a local ip?
I’ve tried modifying both the phpmyadmin.conf & phpmyadmin-le-ssl.conf by adding:
Require host localhost
Require ip 127.0.0.1
Require ip 192.168
Require ip 10
Are you able to help out ??
The exact code I’ve added –
Require host localhost
Require ip 127.0.0.1
Require ip 192.168
Require ip 10
Use the local ip address for the machine running phpmyadmin. For example use http://192.168.1.100/phpmyadmin from another system on your local network. localhost/phpmyadmin will only work on the machine that has phpmyadmin installed on it.
Nevermind, I didn’t read your original post well enough. My apologies.
Hi,
Excelent Tutorial, Congratulations!
How to create a DNS A record for this sub-domain (pma.example.com)
Thank You
You can read this tutorial: How to Create DNS Records in NameCheap
First class tutorial!
Thank you
Great tutorial! Concise and well explained.
Really Nice!
Thank You.
Hi,
Another excellent tutorial.
Once the subdomain for phpMyAdmin has been set up, how to disable the domain.com/phpmyadmin URL? That address gets plenty of brute force attacks.
Thank You.
Simply remove the
/etc/apache2/conf-enabled/phpmyadmin.conf
file and restart Apache.Truly excellent tutorial: thank you
It would be even better if you used the phpmyadmin installation steps from the original website, since those steps allow for the setup of 2FA.
I love your tutorials, have used them several times with great results, however on tyhis one I find I have a problem.
I don’t have a domain for this web server – it is local only – my in-house network.
What do i put in for domain when requesting the cert?
Or do I not need a cert for my local network ?
Thanks
Always so good–thanks!