Tutorial

How To Install and Secure phpMyAdmin on Ubuntu

How To Install and Secure phpMyAdmin on Ubuntu
Not using Ubuntu 22.04?Choose a different version or distribution.
Ubuntu 22.04

Introduction

While many users need the functionality of a database management system like MySQL, they may not feel comfortable interacting with the system solely from the MySQL prompt.

phpMyAdmin was created so that users can interact with MySQL through a web interface. In this guide, we’ll discuss how to install and secure phpMyAdmin so that you can safely use it to manage your databases on an Ubuntu system.

How To Install and Secure phpMyAdmin on Ubuntu

  1. Installing phpMyAdmin
  2. Adjusting User Authentication and Privileges
  3. Securing Your phpMyAdmin Instance

Prerequisites

In order to complete this guide, you will need:

  • An Ubuntu server. This server should have a non-root user with administrative privileges and a firewall configured with ufw. To set this up, follow our initial server setup guide for Ubuntu.
  • A LAMP (Linux, Apache, MySQL, and PHP) stack installed on your Ubuntu server. If this is not completed yet, you can follow this guide on installing a LAMP stack on Ubuntu.

Additionally, there are important security considerations when using software like phpMyAdmin since it:

  • Communicates directly with your MySQL installation
  • Handles authentication using MySQL credentials
  • Executes and returns results for arbitrary SQL queries

For these reasons, and because it is a widely deployed PHP application that is frequently targeted for attack, you should never run phpMyAdmin on remote systems over a plain HTTP connection.

If you do not have an existing domain configured with an SSL/TLS certificate, you can follow this guide on securing Apache with Let’s Encrypt on Ubuntu. This will require you to register a domain name, create DNS records for your server, and set up an Apache Virtual Host.

Step 1 — Installing phpMyAdmin

You can use APT to install phpMyAdmin from the default Ubuntu repositories.

As your non-root sudo user, update your server’s package index if you haven’t done so recently:

  1. sudo apt update

Following that you can install the phpmyadmin package. Along with this package, the official documentation also recommends that you install a few PHP extensions onto your server to enable certain functionalities and improve performance.

If you followed the prerequisite LAMP stack tutorial, several of these modules will have been installed along with the php package. However, it’s recommended that you also install these packages:

  • php-mbstring: A module for managing non-ASCII strings and convert strings to different encodings
  • php-zip: This extension supports uploading .zip files to phpMyAdmin
  • php-gd: Enables support for the GD Graphics Library
  • php-json: Provides PHP with support for JSON serialization
  • php-curl: Allows PHP to interact with different kinds of servers using different protocols

Run the following command to install these packages onto your system. Please note, though, that the installation process requires you to make some choices to configure phpMyAdmin correctly. We’ll walk through these options shortly:

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

Here are the options you should choose when prompted in order to configure your installation correctly:

  • For the server selection, choose apache2

Warning: When the prompt appears, “apache2” is highlighted, but not selected. If you do not hit SPACE to select Apache, the installer will not move the necessary files during installation. Hit SPACE, TAB, and then ENTER to select Apache.

  • Select Yes when asked whether to use dbconfig-common to set up the database
  • You will then be asked to choose and confirm a MySQL application password for phpMyAdmin

Note: Assuming you installed MySQL by following Step 2 of the prerequisite LAMP stack tutorial, you may have decided to enable the Validate Password plugin. As of this writing, enabling this component will trigger an error when you attempt to set a password for the phpmyadmin user:

phpMyAdmin password validation error

To resolve this, select the abort option to stop the installation process. Then, open up your MySQL prompt:

  1. sudo mysql

Or, if you enabled password authentication for the root MySQL user, run this command and then enter your password when prompted:

  1. mysql -u root -p

From the prompt, run the following command to disable the Validate Password component. Note that this won’t actually uninstall it, but just stop the component from being loaded on your MySQL server:

  1. UNINSTALL COMPONENT "file://component_validate_password";

Following that, you can close the MySQL client:

  1. exit

Then try installing the phpmyadmin package again and it will work as expected:

  1. sudo apt install phpmyadmin

Once phpMyAdmin is installed, you can open the MySQL prompt once again with sudo mysql or mysql -u root -p and then run the following command to re-enable the Validate Password component:

  1. INSTALL COMPONENT "file://component_validate_password";

The installation process adds the phpMyAdmin Apache configuration file into the /etc/apache2/conf-enabled/ directory, where it is read automatically. To finish configuring Apache and PHP to work with phpMyAdmin, the only remaining task in this section of the tutorial is to is explicitly enable the mbstring PHP extension, which you can do by typing:

  1. sudo phpenmod mbstring

Afterwards, restart Apache for your changes to be recognized:

  1. sudo systemctl restart apache2

phpMyAdmin is now installed and configured to work with Apache. However, before you can log in and begin interacting with your MySQL databases, you will need to ensure that your MySQL users have the privileges required for interacting with the program.

Step 2 — Adjusting User Authentication and Privileges

When you install phpMyAdmin onto your server, it automatically creates a database user called phpmyadmin, which performs certain underlying processes for the program. Rather than logging in as this user with the administrative password you set during installation, it’s recommended that you log in as either your root MySQL user or as a user dedicated to managing databases through the phpMyAdmin interface.

Configuring Password Access for the MySQL Root Account

In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program — like phpMyAdmin — to access the user.

In order to log in to phpMyAdmin as your root MySQL user, you will need to switch its authentication method from auth_socket to one that makes use of a password, if you haven’t already done so. To do this, open up the MySQL prompt from your terminal:

  1. sudo mysql

Next, check which authentication method each of your MySQL user accounts use with the following command:

  1. SELECT user,authentication_string,plugin,host FROM mysql.user;
Output
+------------------+------------------------------------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ | debian-sys-maint | $A$005$I:jOry?]Sy<|qhQRj3fBRQ43i4UJxrpm.IaT6lOHkgveJjmeIjJrRe6 | caching_sha2_password | localhost | | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | phpmyadmin | $A$005$?#{Z?`gN!c2az)}V-INCWXSuVdqB9zWteH1IkZfTe/rOLgVhSzEMM9R3G6K9 | caching_sha2_password | localhost | | root | | auth_socket | localhost | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ 6 rows in set (0.00 sec)

This example output indicates that the root user does in fact authenticate using the auth_socket plugin. To configure the root account to authenticate with a password, run the following ALTER USER command. Be sure to change password to a strong password of your choosing:

  1. ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'password';

Note: The previous ALTER USER statement sets the root MySQL user to authenticate with the caching_sha2_password plugin. Per the official MySQL documentation, caching_sha2_password is MySQL’s preferred authentication plugin, as it provides more secure password encryption than the older, but still widely used, mysql_native_password.

However, some versions of PHP don’t work reliably with caching_sha2_password. PHP has reported that this issue was fixed as of PHP 7.4, but if you encounter an error when trying to log in to phpMyAdmin later on, you may want to set root to authenticate with mysql_native_password instead:

  1. ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Then, check the authentication methods employed by each of your users again to confirm that root no longer authenticates using the auth_socket plugin:

  1. SELECT user,authentication_string,plugin,host FROM mysql.user;
Output
+------------------+------------------------------------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ | debian-sys-maint | $A$005$I:jOry?]Sy<|qhQRj3fBRQ43i4UJxrpm.IaT6lOHkgveJjmeIjJrRe6 | caching_sha2_password | localhost | | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | phpmyadmin | $A$005$?#{Z?`gN!c2az)}V-INCWXSuVdqB9zWteH1IkZfTe/rOLgVhSzEMM9R3G6K9 | caching_sha2_password | localhost | | root | $A$005$3y�y|Z?'_[} ZyVHuESVwNmjKWOH/ndETwS.Kty0IH7UfiXjOfVvyWroy4a. | caching_sha2_password | localhost | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ 6 rows in set (0.00 sec)

This output shows that the root user will authenticate using a password. You can now log in to the phpMyAdmin interface as your root user with the password you’ve set for it here.

Configuring Password Access for a Dedicated MySQL User

Alternatively, some may find that it better suits their workflow to connect to phpMyAdmin with a dedicated user. To do this, open up the MySQL shell once again:

  1. sudo mysql

If you have password authentication enabled for your root user, as described in the previous section, you will need to run the following command and enter your password when prompted in order to connect:

  1. mysql -u root -p

From there, create a new user and give it a strong password:

  1. CREATE USER 'sammy'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'password';

Note: Again, depending on what version of PHP you have installed, you may want to set your new user to authenticate with mysql_native_password instead of caching_sha2_password:

  1. ALTER USER 'sammy'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Then, grant your new user appropriate privileges. For example, you could grant the user privileges to all tables within the database, as well as the power to add, change, and remove user privileges, with this command:

  1. GRANT ALL PRIVILEGES ON *.* TO 'sammy'@'localhost' WITH GRANT OPTION;

Following that, exit the MySQL shell:

  1. exit

You can now access the web interface by visiting your server’s domain name or public IP address followed by /phpmyadmin:

https://your_domain_or_IP/phpmyadmin

phpMyAdmin login screen

Log in to the interface, either as root or with the new username and password you just configured.

When you log in, you’ll be taken to phpMyAdmin’s user interface:

phpMyAdmin user interface

Now that you’re able to connect and interact with phpMyAdmin, all that’s left to do is harden your system’s security to protect it from attackers.

Step 3 — Securing Your phpMyAdmin Instance

Because of its ubiquity, phpMyAdmin is a popular target for attackers, and you should take extra care to prevent unauthorized access. One way of doing this is to place a gateway in front of the entire application by using Apache’s built-in .htaccess authentication and authorization functionalities.

To do this, you must first enable the use of .htaccess file overrides by editing your phpMyAdmin installation’s Apache configuration file.

Use your preferred text editor to edit the phpmyadmin.conf file that has been placed in your Apache configuration directory. Here, we’ll use nano:

  1. 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:

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

When you have added this line, save and close the file. If you used nano to edit the file, do so by pressing CTRL + X, Y, and then ENTER.

To implement the changes you made, restart Apache:

  1. sudo systemctl restart apache2

Now that you have enabled the use of .htaccess files for your application, you need to create one to actually implement some security.

In order for this to be successful, the file must be created within the application directory. You can create the necessary file and open it in your text editor with root privileges by typing:

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

Within this file, enter the following information:

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

Here is what each of these lines mean:

  • AuthType Basic: This line specifies the authentication type that you are implementing. This type will implement password authentication using a password file.
  • AuthName: This sets the message for the authentication dialog box. You should keep this generic so that unauthorized users won’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. We will create this file shortly.
  • Require valid-user: This specifies that only authenticated users should be given access to this resource. This is what actually stops unauthorized users from entering.

When you are finished, save and close the file.

The location that you selected for your password file was /etc/phpmyadmin/.htpasswd. You can now create this file and pass it an initial user with the htpasswd utility:

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

You will be prompted to select and confirm a password for the user you are creating. Afterwards, the file is created with the hashed password that you entered.

If you want to enter an additional user, you need to do so without the -c flag, like this:

  1. sudo htpasswd /etc/phpmyadmin/.htpasswd additionaluser

Then restart Apache to put .htaccess authentication into effect:

  1. sudo systemctl restart apache2

Now, when you access your phpMyAdmin subdirectory, you will be prompted for the additional account name and password that you just configured:

https://domain_name_or_IP/phpmyadmin

phpMyAdmin apache password

After entering the Apache authentication, you’ll be taken to the regular phpMyAdmin authentication page to enter your MySQL credentials. By adding an extra set of non-MySQL credentials, you’re providing your database with an additional layer of security. This is desirable since phpMyAdmin has been vulnerable to security threats in the past.

Conclusion

You should now have phpMyAdmin configured and ready to use on your Ubuntu server. Using this interface, you can create databases, users, and tables, as well as perform the usual operations like deleting and modifying structures and data.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar

Manager, Developer Education

Technical Writer @ DigitalOcean


Default avatar

Sr Technical Writer

Sr. Technical Writer@ DigitalOcean | Medium Top Writers(AI & ChatGPT) | 2M+ monthly views & 34K Subscribers | Ex Cloud Consultant @ AMEX | Ex SRE(DevOps) @ NUTANIX


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Nicely done! I was able to install and to secure phpmyadmin in no time. Thanks!

I did not have good results using this tutorial for Ubuntu 22 LAMP.

The mbstring installation attached it to PHP CLI which was PHP 8.2. The Apache version was 8.1. This caused an error.

The version of PhpMyAdmin that was loaded did not work. Specifically, the router was screwed up somehow. I spent a stupid amount of time trying to fix it.

I used wget to download the latest PhpMyAdmin zip and was up in twenty minutes. It won’t update with the package version system but I really wish I had done that early yesterday.

In Step #1 when running the command to install the php extensions. I get this output … The following packages have unmet dependencies: php8.1-curl : Depends: php8.1-common (= 8.1.7-1ubuntu3.3) but 8.1.17-1+ubuntu22.04.1+deb.sury.org+1 is to be installed php8.1-gd : Depends: php8.1-common (= 8.1.7-1ubuntu3.3) but 8.1.17-1+ubuntu22.04.1+deb.sury.org+1 is to be installed php8.1-mbstring : Depends: php8.1-common (= 8.1.7-1ubuntu3.3) but 8.1.17-1+ubuntu22.04.1+deb.sury.org+1 is to be installed php8.1-mysql : Depends: php8.1-common (= 8.1.7-1ubuntu3.3) but 8.1.17-1+ubuntu22.04.1+deb.sury.org+1 is to be installed php8.1-zip : Depends: php8.1-common (= 8.1.7-1ubuntu3.3) but 8.1.17-1+ubuntu22.04.1+deb.sury.org+1 is to be installed E: Unable to correct problems, you have held broken packages.

I’m running Ubuntu 22.1 and PHP 8.2

On localhost/phpmyadmin 80

For mysql 8.0 the command to disable password validation component is:

UNINSTALL COMPONENT ‘file://component_validate_password’; To install it back again, the command is:

INSTALL COMPONENT ‘file://component_validate_password’; If you just want to change the policy of password validation plugin:

SET GLOBAL validate_password.policy = 0; # For LOW SET GLOBAL validate_password.policy = 1; # For MEDIUM SET GLOBAL validate_password.policy = 2; # For HIGH

UNINSTALL COMPONENT “file://component_validate_password”; UNINSTALL COMPONENT ‘file://component_validate_password’; uninstall plugin validate_password;

None of these work. Ubuntu 22, WP package.

ERROR 1045 (28000): Access denied for user ‘debian-sys-maint’@‘localhost’ (using password: YES

mysql> UNINSTALL COMPONENT “file://component_validate_password”; ERROR 3537 (HY000): Component specified by URN ‘file://component_validate_password’ to unload has not been loaded before.

I had to add the line below to apache2.conf file in order to reach phpmyadmin.

Include /etc/phpmyadmin/apache.conf

It was 404 without/before this. so the last part of Step 1:

The installation process adds the phpMyAdmin Apache configuration file into the /etc/apache2/conf-enabled/ directory, where it is read automatically.

didn’t work as expected on a fresh installed ubuntu in my case.

I am finding this error when I run PHPMyAdmin what could be the issue???

<?php

declare(strict_types=1);

use PhpMyAdmin\Routing;

if (! defined(‘ROOT_PATH’)) { // phpcs:disable PSR1.Files.SideEffects define(‘ROOT_PATH’, DIR . DIRECTORY_SEPARATOR); // phpcs:enable }

global $route, $containerBuilder;

require_once ROOT_PATH . ‘libraries/common.inc.php’;

$dispatcher = Routing::getDispatcher(); Routing::callControllerForRoute($route, $dispatcher, $containerBuilder);

Wow thank you very much!

If anyone’s using MariaDB, I found this command to not work:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'password';

Instead, I did this:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';

Hope it helps.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.