, updated:

How To Install and Secure phpMyAdmin with Nginx on an Ubuntu 16.04 Server

Tutorial on Digitalocean.com did’t finish part about how to install and secure phpMyAdmin with Nginx on Ubuntu 16.04 so here it is.

Prerequisites

I assume that you have go through following steps:

…and now you have HTTPS domain pointing to your Nginx Ubuntu 16.04 server running MySQL, MariaDB or similar with PHP7 and access to sudo user.

1. Install phpMyAdmin form official repositories:

I will user Digitalocean.com older guide called How To Install and Secure phpMyAdmin with Nginx on an Ubuntu 14.04 Server. Install phpMyAdmin form official repositories:

sudo apt-get install php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc
sudo apt-get update
sudo apt-get install phpmyadmin

During installation you will asked which web server you would like to have automatically configured. Since Nginx is not an option choose none (Use TAB and ENTER keys to navigate).

Next configuration is “dbconfig-common” . Select “Yes” to continue. You will be asked for you MySQL root password you have created during database configuration.

Create symbolic link into Nginx root folder so the Nginx will server myPHPadmin files correctly

sudo ln -s /usr/share/phpmyadmin /var/www//html

Restart PHP7 layer and first step is done:

sudo systemctl restart php7.0-fpm

You should be able to admin databases from url https://www.mydomain.com/phpmyadmin

2. Fix Ningx default configuration file if cgi.fix_pathinfo=0 in your php.ini

If you followed this tutorial and disabled cgi.fix_pathinfo in php.ini you will need you fix your /etc/nginx/sites-available/default configuration file in order to run phpMyAdmin correctly. See the problem here. Make changes similar to this code:

# Phpmyadmin Configurations
    location /phpmyadmin {
       root /usr/share/;
       index index.php index.html index.htm;
       location ~ ^/phpmyadmin/(.+.php)$ {
               try_files $uri =404;
               root /usr/share/;
               fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
               fastcgi_index index.php;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               include fastcgi_params;
       }
       location ~* ^/phpmyadmin/(.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
               root /usr/share/;
       }
   }

3.Secure your phpMyAdmin Instance

3.1 Add web server authentication password

Type into shell:

openssl passwd

You will be prompted to type password (2x), After this you will see string like this “YaGaeWsdkpaonvgju” so copy it. Create new text file

sudo nano /etc/nginx/npass

with following content:

username:YaGaeWsdkpaonvgju

Save. Now edit with nano command NSD:

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

and add these 2 lines:

# Phpmyadmin Configurations
location /phpmyadmin {
       auth_basic "Admin Login";
       auth_basic_user_file /etc/nginx/npass;
       root /usr/share/;

Save nano (ctrl + o, ctrl + x).

3.2 Change application access address from phpmyadmin to myuniqueurl

Edit NSD ones again:

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

and change your config to this:

        # Phpmyadmin Configurations
        location /myuniqueurl {
                auth_basic "Admin Login";
                auth_basic_user_file /etc/nginx/npass;
                root /usr/share/;
                index index.php index.html index.htm;
                location ~ ^/myuniqueurl/(.+.php)$ {
                        try_files $uri =404;
                        root /usr/share/;
                        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        include fastcgi_params;
                }
                location ~* ^/myuniqueurl/(.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                        root /usr/share/;
                }
        }

Save and in /usr/share/ create symbolic link pointing from myuniqueurl to phpmyadmin:

sudo ln -s /usr/share/phpmyadmin/ /usr/share/myuniqueurl

Restart Nginx:

sudo service nginx restart

And that’s it. If something went wrong try to check nginx error or access log:

sudo tail -1 /var/log/nginx/error.log

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Up