How to Install phpMyAdmin with Nginx on Ubuntu 16.04

How to Install phpMyAdmin with Nginx on Ubuntu 16.04

phpMyAdmin is a free and open-source web-based database management tool written in PHP. It provides a graphical web interface for users to manage MySQL or MariaDB database. The latest stable version available is 4.6.5.2 , released on December 5, 2016. In this tutorial, we will discuss how to install phpMyAdmin with Nginx, PHP7 (LEMP) on a Ubuntu 16.04 VPS or dedicated server.

Prerequisites

It is assumed that you have already installed LEMP stack on Ubuntu 16.04. If not, please check out the following tutorial.
How To Install LEMP Stack On Ubuntu LTS 16.04

With that out of the way, let’s get started with installation.

Step 1: Download and Install phpMyAdmin

sudo apt-get update
sudo apt-get install phpmyadmin

Note: The above command will install all necessary dependencies including PHP7 extensions. If however the command suggests installing PHP5 extensions, then you might have a broken software repository. You should change your software sources in /etc/apt/sources.list file. I encountered this bug once.

During the installation, it will prompt you to select a web server to configure. Nginx isn’t in the list, so press the Tab key and hit OK to skip this step.

PhpMyAdmin Figure1

Next, select Yes to create a new database.

PhpMyAdmin Figure2

This will also create a new database user named phpmyadmin. Give this user a password.

PhpMyAdmin Figure3

Once done, a new database named phpmyadmin is created and the database user phpmyadmin has necessary privileges to manage this database.

Step 2: Configure Nginx

To be able to access the phpMyAdmin web interface, we need to configure Nginx. We will
configure Nginx so that phpMyAdmin is a sub-directory of the existing website. Open your existing server block file of your website.

sudo nano /etc/nginx/sites-available/default

Add the following lines in the server section.

location /phpmyadmin {
  root /usr/share/;
  index index.php;
  try_files $uri $uri/ =404;

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

  location ~ /phpmyadmin/(.+\.php)$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
  }
 }

Your phpMyAdmin files are in /usr/share/phpmyadmin/ directory. The above configuration tells Nginx that if visitors enter your-domain.com/phpmyadmin in browser address bar, then find index.php file in /usr/share/phpmyadmin/ directory and display the web page.

See Below the complete server block

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name your-domain-or-ip;

location /phpmyadmin {
  root /usr/share/;
  index index.php;
  try_files $uri $uri/ =404;

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

  location ~ /phpmyadmin/(.+\.php)$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
  }
 }
}

Save and close the file. Then test configuration and reload.

sudo nginx -t
sudo systemctl reload nginx

Now you should be able to access phpMyAdmin web interface via
your-domain-or-ip/phpmyadmin/
phpmyadmin page

Subscribe

Stay up to date! Get all the latest & greatest posts delivered straight to your inbox

You've successfully subscribed to Salehin Khan
Great! Next, complete checkout for full access to Salehin Khan
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.