Install phpMyAdmin Manually with Nginx server on Ubuntu

Harish Kumar · · 1736 Views

In this guide, I will show you how to install and configure phpMyAdmin with Nginx, MySQL, and PHP8.0 (LEMP) on an Ubuntu system. phpMyAdmin is a free and open-source database management tool written in PHP. It provides a web-based interface for users to manage the MySQL database.

Prerequisites

To install phpMyAdmin on the Nginx server, you will require:

  1. A LEMP (Linux, Nginx, MySQL, and PHP) stack is installed on your system. If you have not installed it yet, you can follow this guide on installing a LEMP stack on your Ubuntu server.

  2. Or Valet Linux Plus if you are working on the local development system. This Valet Linux Plus tool is to manage the LEMP stack for local development and provides many additional features. You can follow this guide to install Valet Linux Plus on Ubuntu for a local development environment.

Step 1: Download the latest phpMyAdmin

 First, we will download the latest phpMyAdmin version from its official website. You can visit the following link and copy the link of the latest stable phpMyAdmin version:

  1. https://www.phpmyadmin.net/files/

As of the time of writing this post, the most recent phpMyAdmin was v5.0.4. So to download the files, run the following commands:

#1: Then download the zip file
wget https://files.phpmyadmin.net/phpMyAdmin/5.0.4/phpMyAdmin-5.0.4-all-languages.zip

#2: Unzip the archive
unzip phpMyAdmin-5.0.4-all-languages.zip

#3: Move the phpMyAdmin folder to your document root:
mv phpMyAdmin-5.0.4-all-languages /var/www/html/phpmyadmin

#4: update ownership to add support for webserver
sudo chown -R www-data:www-data /var/www/html/phpmyadmin

Step 2: Configure phpMyadmin

There are a few configurations needed on phpMyAdmin. To begin with, copy the sample config file, then open that config file:

cd /var/www/html/phpmyadmin

# rename the sample config file
mv config.sample.inc.php config.inc.php

# open that config file
vi config.inc.php

And then update the following line to the correct database host:

$cfg['Servers'][$i]['host'] = 'localhost'

Next, we need to configure phpMyAdmin tmp directory. So, first create tmp directory for phpMyAdmin:

mkdir -p /var/www/html/phpmyadmin/tmp

Now, open the config.inc.php file and configure $cfg['TempDir']:

$cfg['TempDir'] = '/var/www/html/phpmyadmin/tmp';

Step 3: Authentication method for MySQL 8 User

To sign in to phpMyAdmin as your root MySQL user, you should change its authentication method from auth_socket to mysql_native_password if you haven’t already done so. To implement this, open up the MySQL prompt from your terminal:

sudo mysql

Now, in the MySQL shell run the following ALTER USER command:

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

Alternatively, create a new MySQL user, if you prefer to connect with phpMyAdmin with a dedicated MySQL user instead of the root user.

So, for that open up the MySQL shell once again and run the following commands:

# create `phpmyadmin` user
CREATE USER 'phpmyadmin'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; 

# grant all permissions to `phpmyadmin` user
GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost' WITH GRANT OPTION;

At that point, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:

FLUSH PRIVILEGES;

Step 4: Create an Nginx Server Block

To be able to access the phpMyAdmin web interface, we need to make an Nginx server block. So, create a phpmyadmin.conf file:

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

Paste the following code into this file, and forget to replace phpmyadmin.test with your preferred domain.

server {
  listen 80;
  listen [::]:80;
  server_name phpmyadmin.test;
  root /var/www/html/phpmyadmin/;
  index index.php index.html index.htm index.nginx-debian.html;

  access_log /var/log/nginx/phpmyadmin_access.log;
  error_log /var/log/nginx/phpmyadmin_error.log;

  location / {
    try_files $uri $uri/ /index.php;
  }

  location ~ ^/(doc|sql|setup)/ {
    deny all;
  }

  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    # include snippets/fastcgi-php.conf;
  }

  location ~ /\.ht {
    deny all;
  }
}

Save and close the file. Then test Nginx configurations.

sudo nginx -t

If the test is successful, reload Nginx for the changes to take effect.

sudo systemctl reload nginx

Now you should be able to access phpMyAdmin web interface via phpmyadmin.test.

Conclusion

You have successfully installed phpMyAdmin and you should now have phpMyAdmin configured and prepared to use on your Ubuntu server. I hope this is a helpful tutorial.

4

Please login or create new account to add your comment.

4 comments
Gabriel Filippi
Gabriel Filippi ·

when i run sudo nginx -t appears the following error

nginx: [emerg] open() "/etc/nginx/snippets/fastcgi-php.conf" failed (2: No such file or directory) in /etc/nginx/conf.d/phpmyadmin.conf:23 nginx: configuration file /etc/nginx/nginx.conf test failed

no have snippets directory inside nginx

Harish Kumar
Harish Kumar ·

The error is in the /etc/nginx/conf.d/phpmyadmin.conf file at line number 23. I think that is include snippets/fastcgi-php.conf;. Remove this line and try again.

Gabriel Filippi
Gabriel Filippi ·

Tks its work, but now i have another problem. when i try access phpmyadmin.subdomain.domain.com not worked, is redirected to my main page.

`server { listen 80; listen [::]:80; server_name phpmyadmin.wpflori; root /var/www/html/phpmyadmin/; index index.php index.html index.htm index.nginx-debian.html;

access_log /var/log/nginx/phpmyadmin_access.log; error_log /var/log/nginx/phpmyadmin_error.log;

location / { try_files $uri $uri/ /index.php; }

location ~ ^/(doc|sql|setup)/ { deny all; }

location ~ .php$ { fastcgi_pass unix:/run/php8.0-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }

location ~ /.ht { deny all; } }`

Harish Kumar
Harish Kumar ·

Take a look at this line: server_name phpmyadmin.wpflori;

Your phpmyadmin is accessible at this domain phpmyadmin.wpflori.

You may also like:

Install Laravel Valet Linux+ development environment on Ubuntu System

The official Laravel Valet development environment is great if you are an Apple user. But there is no official Valet for Linux or Window system.
Harish Kumar

How to create Stored Procedures in PostgreSQL with example?

In this post, you will learn how to use the CREATE PROCEDURE statement to create new stored procedures in PostgreSQL.
Sumit Talwar

How to Install phpMyAdmin with Nginx on Ubuntu Server?

The phpMyAdmin is an open-source PHP-based tool for handle MySQL and MariaDB databases over a web-based interface. 
Harish Kumar

How to install Laravel with LEMP stack on Ubuntu

Laravel is one of the most popular open-source PHP framework, and Laravel allows you to build scalable and flexible web applications. Because of its outstanding features, for example, (...)
Harish Kumar

How To Install LEMP stack (Linux, Nginx, MySQL, PHP) on Ubuntu System

The term LEMP is an acronym of the four open-source projects:
Harish Kumar

How to create PHP Pagination using PDO with example?

In this example, I am going to show you how to create pagination in PHP using PDO.First, create a DatabaseConnection.php to create a database connection.
Razet