How to install and protect phpMyAdmin on Ubuntu 22.04?

Updated on 2026-10-28 14:01:19 15 visits

Introduction

phpMyAdmin is an open-source web application written in PHP used to administer and manage MySQL databases. It provides a graphical user interface that allows users to create, modify, and delete databases, tables, fields, and records, as well as execute SQL queries and perform other database management tasks.

Before starting the tutorial, don't you have a Cloud Linux Ubuntu 22.04 server where you can install your phpMyAdmin? Don't worry! With just one click, you can easily deploy it with SW Hosting.

To install and protect phpMyAdmin on Ubuntu 22.04, follow these steps:

Step 1: Update the system

Before starting, it is important to ensure that the Ubuntu 22.04 system is updated. To do this, open a terminal and type the following command:

sudo apt-get update && sudo apt-get upgrade

Step 2: Install Apache, MySQL, and PHP

To use phpMyAdmin, it is necessary to install an Apache web server, a MySQL database, and the PHP programming language. To install these packages, type the following command in the terminal:

sudo apt-get install apache2 mysql-server php

Next, we must change / set a new root password for mysql server:

To change the root password in MySQL for Ubuntu 22.04, follow these steps:

Open a terminal on your Ubuntu system. Log in to MySQL as the root user by running the following command and press enter:

sudo mysql -u root -p

Once you have logged into MySQL, switch to the 'mysql' database by running the following command:

USE mysql;

You can change the root password by running the following command. Replace <nueva-contraseña> with the new password you wish to set:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'nueva-contraseña.';

If you wish to change the root password for a host other than localhost, adjust the query accordingly using the correct host instead of 'localhost'. Then, run the following command to apply the changes and reload privileges:

FLUSH PRIVILEGES;

You can exit the MySQL console by running:

EXIT;

Once this is done, you can improve the security of your DB server using the following command:

 mysql_secure_installation 

This command performs a series of basic security configurations in MySQL to protect it against potential threats.

Step 3: Install phpMyAdmin

Now, it is necessary to install phpMyAdmin on the server. For this, type the following command in the terminal:

sudo apt-get install phpmyadmin

During installation, you will be asked to configure some options. Select apache2 as the web server and Yes when asked if you wish to configure the phpMyAdmin database with dbconfig-common.

Step 4: Configure Apache for phpMyAdmin

Now it is necessary to configure Apache so that phpMyAdmin works correctly. Type the following command in the terminal:

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

This will open the Apache configuration file for phpMyAdmin. Inside this file, add the following lines just below the line starting with Alias:

file phpmyadmin.conf

<Directory /usr/share/phpmyadmin>
   AddDefaultCharset UTF-8
   <IfModule mod_authz_core.c>
      # Apache 2.4
      Require ip 127.0.0.1
      Require ip ::1
   </IfModule>
   <IfModule !mod_authz_core.c>
      # Apache 2.2
      Order Deny,Allow
      Deny from All
      Allow from 127.0.0.1
      Allow from ::1
   </IfModule>
</Directory>

Once you have added these lines, you must save and close the file.

Step 5: Enable phpMyAdmin configuration

Type the following command in the terminal:

sudo a2enconf phpmyadmin

This will enable the phpMyAdmin configuration in Apache.

Step 6: Restart Apache

To apply the changes in Apache, it is necessary to restart the web server. Type the following command in the terminal:

sudo systemctl restart apache2

Step 7: Protect phpMyAdmin with a password

By default, phpMyAdmin is not password-protected, which means anyone who has access to the phpMyAdmin URL will be able to access the database. To protect phpMyAdmin with a password, follow these steps:

  • Create a password file for Apache. Type the following command in the terminal:
sudo htpasswd -c /etc/apache2/.htpasswd <username>

Where <username> is the username you wish to use to access phpMyAdmin.

  • Edit the phpMyAdmin configuration file. Type the following command in the terminal:
sudo nano /etc/phpmyadmin/apache.conf

Inside this file, add the following lines just below the line starting with Alias /phpmyadmin:

file apache.conf

<Directory /usr/share/phpmyadmin>
   Options FollowSymLinks
   DirectoryIndex index.php
     <IfModule mod_authz_core.c>
        # Apache 2.4
      <RequireAny>
           Require ip 127.0.0.1
           Require ip ::1
           AuthType Basic
           AuthName 'Restricted Content'
           AuthUserFile /etc/apache2/.htpasswd
           Require valid-user
      </RequireAny>
     </IfModule>
     <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Deny from All
        Allow from 127.0.0.1
        Allow from ::1
        AuthType Basic
        AuthName 'Restricted Content'
        AuthUserFile /etc/apache2/.htpasswd
        Require valid-user
     </IfModule>
  </Directory>

After adding these lines, you must save and close the file.

  • Then, you have to restart Apache. For this, type the following command in the terminal:
sudo systemctl restart apache2

Step 8: Access phpMyAdmin

Now, you can access phpMyAdmin by entering the URL 'http://localhost/phpmyadmin' in your web browser. You will be asked to enter the username and password you created in the previous step to access phpMyAdmin.

success With these steps, you have installed and protected phpMyAdmin on Ubuntu 22.04. It is important to keep in mind that phpMyAdmin is a powerful tool and that an incorrect configuration could compromise the security of your server and your database, so it is recommended to use it with caution and always ensure that the latest updates and security patches are installed.

Remember that if you do not yet have a Cloud server with a Linux operating system, you can easily deploy it with SW Hosting.