How to Install phpMyAdmin with Nginx, MariaDB, PHP7 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, MariaDB, 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.

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

Step 1: Download and Install phpMyAdmin

phpMyAdmin is included in Ubuntu 16.04 software repository, so we can easily install it with the command below

sudo apt update
sudo apt 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.

install phpmyadmin

Next, select Yes to create a new database.

phpmyadmin install

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

phpmyadmin password

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/conf.d/your-site.conf

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.

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.com/phpmyadmin/

Login with MariaDB root user and password.

phpmyadmin ubuntu

The advantage of accessing phpMyAdmin from sub-directory rather than sub-domain is that if you have HTTPS enable on your main domain name, then you don’t have to install new TLS certificate to secure phpMyAdmin.

phpmyadmin url

If you are worried about security, you can change the phpMyAdmin URL to something else like

your-domain/secret-path

You can also prevent unauthorized access to the login page by implementing password authentication in Nginx.

That’s it!

I hope this article helped you to install phpMyAdmin with Nginx on Ubuntu 16.04. As always, if you found this post useful, then subscribe to our free newsletter. You can also follow us on Google+ Twitter or like our Facebook page.

Rate this tutorial
[Total: 12 Average: 3.4]

10 Responses to “How to Install phpMyAdmin with Nginx, MariaDB, PHP7 on Ubuntu 16.04

  • Allen Jarvis
    6 months ago

    Lots of poor configuration tutorials out there. This is not one of them. Worked a charm right from the get-go. I used a vhost in nginx’s sites-available configuration directory. No issues whatsoever.

  • Bombano Agency
    6 months ago

    You pixelated the domain in the address field but you didn’t in the window XD, you should double check. Excellent article by the way!

  • Turncoat Tony
    6 months ago

    So what is fastcgi-php.conf;? You want to include it but don’t say what’s in there? Or am I a fucking idiot?

    • It’s a file in /etc/nginx/snippets, which comes with Nginx installed from Ubuntu repository. This file contains FastCGI configurations, which are

      # regex to split $uri to $fastcgi_script_name and $fastcgi_path
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      
      # Check that the PHP script exists before passing it
      try_files $fastcgi_script_name =404;
      
      # Bypass the fact that try_files resets $fastcgi_path_info
      # see: http://trac.nginx.org/nginx/ticket/321
      set $path_info $fastcgi_path_info;
      fastcgi_param PATH_INFO $path_info;
      
      fastcgi_index index.php;
      include fastcgi.conf;
      
  • HelloWorld
    6 months ago

    Hello, I have this error during the configuration ERROR 1045 (28000): Access denied for user ‘root’@’localhost’
    Previously I followed the instructions for “How to Install Nginx, MariaDB and PHP7 (LEMP Stack) on Ubuntu 16.04 LTS”
    Then, “How to Fix MariaDB Plugin ‘unix_socket’ is not loaded Error” because I had the Access denied for user ‘root’@’localhost’ issue.
    Could you help me ?

  • All good until this:

    /etc/nginx/conf.d/your-site.conf

    what is “your-site”.. ?

    • Hi, this Nginx configuration is used for sub-directory installation. That is, phpMyAdmin is installed as a sub-directory of an existing Nginx virtual host. (www.example.com/phpmyadmin).

      I recently wrote a tutorial on Ubuntu 18.04 for sub-domain installation (phpmyadmin.example.com). You can check it out here: https://www.linuxbabe.com/ubuntu/install-phpmyadmin-nginx-lemp-ubuntu-18-04-lts

    • You need to replace /etc/nginx/conf.d/your-site.conf with your existing Nginx virtual host file in /etc/nginx/conf.d directory or /etc/nginx/sites-enabled/ directory.. If you don’t have such files, you can just use the /etc/nginx/conf.d/default.conf or /etc/nginx/sites-enabled/default file.

  • anil patel
    5 months ago

    I wanted a secureurl instead of phpmyadmin.
    In the niginx conf file, I replaced “phpmyadmin” with “secureurl”. But I get 404 page.
    please explain how to modify conf file for secreturl

    Thanks

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • If my answer helped you, please consider supporting this site. Thanks :)