· Tutorial ·

How to install and secure phpMyAdmin on Ubuntu 22.04?

Introduction

The phpMyAdmin is an open source web application written in PHP that is used to administer and manage MySQL databases. It provides a graphical user interface that allows users to create, modify and drop 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 on which you can install your phpMyAdmin? Don't worry! With just one click, you can easily deploy it with SW Hosting.

Your Cloud with Ubuntu 22 from $5.60/month

Get the most out of your project with the fastest disks and most powerful CPUs in the Cloud.

To install and secure phpMyAdmin on Ubuntu 22.04, follow the steps below:

Step 1: Upgrade the system

Before starting, it is important to ensure that you have upgraded to Ubuntu 22.04. 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 you will need 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 need to change/set a new root password for the mysql server.

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

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

sudo mysql -u root -p

Once you are logged in to 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 <new-password> with the new password you want to set:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'new-password.';

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

FLUSH PRIVILEGES;

You can exit the MySQL console by running:

EXIT;

Once you have done this, you can increase the security of your DB server by using the following command:

 mysql_secure_installation 

This command performs a number of basic security settings on MySQL to protect it from potential threats.

Step 3: phpMyAdmin installation

Now, you need to install phpMyAdmin on the server. To do this, type the following command in the terminal:

sudo apt-get install phpmyadmin

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

Step 4: Configuring Apache for phpMyAdmin

Now you need to configure Apache for phpMyAdmin to work properly. In a terminal, type the following command:

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 need to save and close the file.

Step 5: Activate the 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 to 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 passwords

By default, phpMyAdmin is not password protected, which means that anyone with access to the phpMyAdmin URL can access the database. To password protect phpMyAdmin, follow the steps below:

  • Create a password file for Apache. In a terminal, type the following command
sudo htpasswd -c /etc/apache2/.htpasswd <username>

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

  • Edit the phpMyAdmin configuration file. In a terminal, type the following command
sudo nano /etc/phpmyadmin/apache.conf

Within 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 need to restart Apache. To do this, type the following command in a terminal
sudo systemctl restart apache2

Step 8: Login to phpMyAdmin

You can now access phpMyAdmin by entering the URL "http://localhost/phpmyadmin" in your web browser. You will be prompted 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 remember that phpMyAdmin is a powerful tool and that incorrect configuration can compromise the security of your server and database, so it is recommended that you use it with care and always make sure you have the latest updates and security patches 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.

Your Cloud with Ubuntu 22 from $5.60/month

Get the most out of your project with the fastest disks and most powerful CPUs in the Cloud.

i