How To Install And Configure phpMyAdmin

How To Install And Configure phpMyAdmin

Introduction

This guide will show you how to install and configure phpMyAdmin on Ubuntu 22.04.

What is phpMyAdmin?

phpMyAdmin is a free and open-source web-based database management tool written in PHP. It is used to manage MySQL and MariaDB databases through a web browser. You can perform various database operations such as creating, deleting, modifying, importing, exporting, and much more.

Prerequisites

  • A server running Nginx on Ubuntu 22.04.
  • A non-root user with sudo privileges.
  • PHP installed on your server.
  • MySQL installed on your server. Learn how.
  • A domain name pointed to your server IP. Learn how.
    • In this guide, we will use phpmyadmin.example.com.

Installation

Before starting, it is recommended to update your system packages to the latest version. You can update them using the following command:

sudo apt update

Once all the packages are updated, you can proceed to install phpMyAdmin.

Step 1 - Install phpMyAdmin

You can install phpMyAdmin with the following command:

sudo add-apt-repository ppa:phpmyadmin/ppa

Once the repository is added, update the repository and install phpMyAdmin with the following command:

sudo apt update
sudo apt install phpmyadmin

During the installation, you will be asked to choose the web server.

Since we're using Nginx as our web server, we shouldn't select any web server and press TAB to select OK and hit ENTER.

Step 2 - Create Admin User

Next, you will need to create an admin user for phpMyAdmin by creating a new MySQL user.

You can create it with the following command:

mysql -u root -p
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';

Then, grant all privileges to the admin user with the following command:

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

Finally, flush the privileges and exit from the MariaDB shell with the following command:

FLUSH PRIVILEGES;
EXIT;

Step 3 - SSL/TLS Certificate

Before continuing, you will need to create an SSL/TLS certificate for your phpMyAdmin domain.

See the following blogs to create an SSL/TLS certificate for your domain using Certbot:

Once you have created an SSL/TLS certificate for your domain, you can proceed to the next step.

Step 4 - Configure Nginx for phpMyAdmin

Next, you will need to create an Nginx config file for phpMyAdmin.

You can create it with the following command:

sudo nano /etc/nginx/sites-enabled/phpmyadmin.example.com.conf

Add the following lines:

/etc/nginx/sites-enabled/phpmyadmin.example.com.conf
server {
  listen 80;
  listen [::]:80;
 
  server_name phpmyadmin.example.com;
 
  # Redirect all HTTP traffic to HTTPS
  return 301 https://$host$request_uri;
}
 
server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
 
  root /var/www/phpmyadmin.example.com/html;
  index index.php index.html index.htm index.nginx-debian.html;
 
  server_name phpmyadmin.example.com;
 
  ssl_certificate /etc/letsencrypt/live/phpmyadmin.example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/phpmyadmin.example.com/privkey.pem;
 
  location / {
    try_files $uri $uri/ =404;
  }
 
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
  }
}

Next, you will need to create a symbolic link of phpMyAdmin in the Nginx web root directory.

You can do it with the following command:

sudo ln -s /usr/share/phpmyadmin /var/www/phpmyadmin.example.com/phpmyadmin

Step 5 - Verify phpMyAdmin

You can now verify the phpMyAdmin installation by visiting the following URL:

https://phpmyadmin.example.com

And, you will be redirected to the phpMyAdmin login page where you will need to provide your admin username and password. Which you have created in the previous step.

Conclusion

Congratulations! you have successfully installed and configured phpMyAdmin on Ubuntu 22.04. You can now easily manage your MySQL and MariaDB databases through a web browser.

Resources