How to Install phpMyAdmin with Nginx on Ubuntu 22.04

0

phpMyAdmin is a free web-based management tool for managing the MySQL and MariaDB database servers. Web hosting companies widely use it to enable users to manage databases.

phpMyAdmin helps the system administrator perform database activities such as creating, deleting, and querying databases, tables, columns, etc.

This post will show how to install phpMyAdmin with Nginx on Ubuntu 22.04.

Set up Environment

Before installing phpMyAdmin, install MariaDB or MySQL database server and an Nginx web server.

Install Nginx Web Server

First, follow the Nginx installation procedure to install the Nginx server web server.

READ: Install LEMP Stack on Ubuntu 22.04

Install MariaDB / MySQL Server

Then, install MariaDB or MySQL server by following the below tutorials for preparing your system for phpMyAdmin.

READ: Install MariaDB on Ubuntu 22.04  / Install MySQL on Ubuntu 22.04

Install PHP

Next, install PHP and required extensions for phpMyAdmin.

sudo apt install -y php-fpm php-mysql php-json php-mbstring php-xml

Install phpMyAdmin on Ubuntu 22.04

Download phpMyAdmin

The phpMyAdmin package is available in the Ubuntu repository. But, the package available in it is a bit older version. So, we will download the latest version of phpMyAdmin from the official website.

wget https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.tar.gz

Then, extract phpMyAdmin using the tar command.

tar -zxvf phpMyAdmin-5.2.0-all-languages.tar.gz

And then move the phpMyAdmin to the desired location.

sudo mv phpMyAdmin-5.2.0-all-languages /usr/share/phpMyAdmin

Set up phpMyAdmin

First, rename the phpMyAdmin’s sample configuration file for configuring the phpMyAdmin.

sudo mv /usr/share/phpMyAdmin/config.sample.inc.php /usr/share/phpMyAdmin/config.inc.php

Then, edit the configuration file to make a few changes.

sudo nano /usr/share/phpMyAdmin/config.inc.php

Generate a blowfish secret and update the below line with the generated secret in the configuration file.

$cfg['blowfish_secret'] = 'CfX1la/aG83gx1{7rADus,iqz8RzeV8x'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

You may also need to uncomment the phpMyAdmin storage settings.

/**
 * phpMyAdmin configuration storage settings.
 */

/* User used to manipulate with storage */
$cfg['Servers'][$i]['controlhost'] = 'localhost';
// $cfg['Servers'][$i]['controlport'] = '';
$cfg['Servers'][$i]['controluser'] = 'pmauser';
$cfg['Servers'][$i]['controlpass'] = 'mypmapass';

phpMyAdmin requires database tables. So, import the create_tables.sql comes with the phpMyAdmin archive package to create tables for phpMyAdmin.

sudo mysql < /usr/share/phpMyAdmin/sql/create_tables.sql -u root -p

Next, log in to MariaDB.

sudo mysql -u root -p

Then, add the user and grant permission to phpMyAdmin’s database.

CREATE USER 'pmauser'@'localhost' IDENTIFIED BY 'mypmapass';

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

FLUSH PRIVILEGES;

EXIT;

Create Nginx Server Block for phpMyAdmin

Create a server block configuration file for phpMyAdmin under the /etc/nginx/conf.d directory. If you have installed Nginx from the Ubuntu repository, you need to create the file under /etc/nginx/sites-available directory.

sudo nano /etc/nginx/conf.d/phpmyadmin.conf

Use the following server block for phpMyAdmin. You may change the domain name (server_name) as per your requirement (use only the domain name, not the IP address).

server {
   listen 80;
   server_name pma.itzgeek.local;
   root /usr/share/phpMyAdmin;

   location / {
      index index.php;
   }

## Images and static content is treated different
   location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
      access_log off;
      expires 30d;
   }

   location ~ /\.ht {
      deny all;
   }

   location ~ /(libraries|setup/frames|setup/libs) {
      deny all;
      return 404;
   }

   location ~ \.php$ {
      include /etc/nginx/fastcgi_params;
      fastcgi_pass unix:/run/php/php8.1-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   }
}

Next, create a temporary directory for phpMyAdmin and then change the permission.

sudo mkdir /usr/share/phpMyAdmin/tmp

sudo chmod 777 /usr/share/phpMyAdmin/tmp

Then, set the ownership of the phpMyAdmin directory.

sudo chown -R www-data:www-data /usr/share/phpMyAdmin

Finally, restart the Nginx and PHP services.

sudo systemctl restart nginx php8.1-fpm

Create Database & User (Optional)

The MariaDB root user can log in locally via Unix socket by default. So, you will need to create a database and a user to log in to phpMyAdmin.

CREATE DATABASE app_db;

CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'password';

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

FLUSH PRIVILEGES;

EXIT;

Alternatively, you can disable Unix socket authentication and enable native password login.

Access phpMyAdmin

Now, access the phpMyAdmin via browser by going to the phpMyAdmin’s URL.

http://fully-qualified-domain-name

Log in with the database user.

Login to phpMyAdmin
Login to phpMyAdmin

You will get the home page where you can manage databases.

phpMyAdmin with Nginx
phpMyAdmin with Nginx

Conclusion

I hope this post helped you install phpMyAdmin with Nginx on Ubuntu 22.04. Share your feedback and issues you have encountered while setting this up in the comments section.

You might also like