Installing phpMyAdmin for Nginx on Ubuntu 18.04

Installing phpMyAdmin for Nginx on Ubuntu 18.04 / 19.10

Last updated on | 36 replies

In this guide we will install and configure phpMyAdmin to work with Nginx on Ubuntu Server 18.04 / 19.10. phpMyAdmin is open source free software, designed to handle the administration and management of MySQL databases through a graphic user interface.

Prerequisites

You should be using a non-root user with sudo privileges as explained in Ubuntu 18.04 / 19.10 Initial Server Setup.

You should also have your LEMP stack (Nginx, MySQL and PHP) already installed before continuing with this guide. If you don’t have these installed yet, please see Installing a LEMP Stack (Nginx, MySQL, PHP) on Ubuntu 18.04 / 19.10.

1. Install phpMyAdmin

Let’s begin by updating the package lists and installing phpMyAdmin on Ubuntu 18.04 / 19.10. Below we have two commands separated by &&. The first command will update the package lists to ensure you get the latest version and dependencies for phpMyAdmin . The second command will then download and install phpMyAdmin. Press y and ENTER when asked to continue.

sudo apt update && sudo apt install phpmyadmin

The order of the following screens in the phpMyAdmin Package configuration may vary depending on your setup.

If you are prompted to choose a web server like below, as there is no option for Nginx, press TAB and then ENTER to continue without selecting a web server.

Select Yes and press ENTER to install and configure the database.

The MySQL application password is only used internally by phpMyAdmin to communicate with MySQL. You can leave this blank and a password will be generated automatically. Just press ENTER to continue.

2. Create Symbolic Link

In order for Nginx to serve the phpMyAdmin files correctly, we must create a symbolic link from the phpMyAdmin directory /usr/share/phpmyadmin to the Nginx document root directory.

The default location of the Nginx document root in Ubuntu 18.04 / 19.10 should be /var/www/html/, though it could be different depending on your setup. If you followed a previous guide for setting up multiple domains for Nginx, your document root may be located in somewhere like /var/www/example.com/public_html.

Once you have confirmed your document root, let’s create a symbolic link from the phpMyAdmin directory to your document root. Here we will assume your document root is /var/www/html/ and we will simply add phpmyadmin to the end of it. This will allow us to access phpMyAdmin at example.com/phpmyadmin.

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

3. Test phpMyAdmin

You should now be able to access the phpMyAdmin web interface by visiting your server’s domain name or public IP address followed by /phpmyadmin. e.g. http://example.com/phpmyadmin or http://192.168.1.10/phpmyadmin

If you don’t have a domain name yet and don’t know your IP, you can find out with:

ip a | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

You will have set up the root user and password when installing MySQL for the first time. However, remote login might be disabled for root. If you get an error  “Access denied for user ‘root’@’localhost'”, you should continue to Step 4 to create a superuser just for phpMyAdmin.

4. Create MySQL User

If you weren’t able to log in as root above, you can now create a superuser account just for phpMyAdmin.

In terminal, log into MySQL as root. You may have created a root password when you installed MySQL for the first time or the password could be blank, in which case you can just press ENTER when prompted for a password. If you have forgotten the root password, see: How to Reset the MySQL Root Password on Ubuntu.

sudo mysql -p -u root

Now add a new MySQL user with the username of your choice. In this example we are calling it pmauser (php my admin user). Make sure to replace  password_here with your own (generate a password).

The % symbol tells MySQL to allow this user to log in from anywhere remotely. If you want heightened security, you could replace this with an IP address.

CREATE USER 'pmauser'@'%' IDENTIFIED BY 'password_here';

Now we will grant superuser privileges to our new user pmauser.

GRANT ALL PRIVILEGES ON *.* TO 'pmauser'@'%';

You should now be able to access phpMyAdmin using this new user account.

If you would like to set up some additional security for phpMyAdmin, continue to the next step.

5. Obscure phpMyAdmin URL

Bots and attackers continuously scan web servers for the phpMyAdmin login page, so it is recommended that you change the URL to something else.

In this example we are going to change it from example.com/phpmyadmin to example.com/pma_hidden, though you can change it to whatever you want.

In step 2, we created a symbolic link in the document root /var/www/html/phpmyadmin

All we need to do is to rename this symbolic link, in this example, to: pma_hidden. Make sure you enter the correct document root here. The default is /var/www/html though it may be something like /var/www/example.com/public_html/ on your server.

sudo mv /var/www/html/phpmyadmin /var/www/html/pma_hidden

You should now be able to access phpMyAdmin at example.com/pma_hidden

6. Secure phpMyAdmin (Optional)

To provide an additional layer of security, we can set up authentication in Nginx.

Firstly, generate a strong password and keep it safe.

We will now install apache2-utils, which can generate the .htpasswd file that works with both Nginx and Apache.

sudo apt install apache2-utils

Once installed, we can generate the .htpasswd file. Simply change username to whatever username you want. Generate a password and keep it safe.

sudo htpasswd -c /etc/nginx/.htpasswd username

There should now be a .htpasswd file containing your username and encrypted password. You can check with:

cat /etc/nginx/.htpasswd

You should see something like username:$apr1sdfsdf4435sdtskLfWmmg1sfdsdgg4

We now need to add two directives to our Nginx configuration file. The location of the config file may vary depending on your setup, though the default is usually in /etc/nginx/sites-available/default. If you set up multiple domains in a previous guide, your config file may be located in somewhere like /etc/nginx/sites-available/example.com

This this example, we will assume the config file is in /etc/nginx/sites-available/default. Open the file to edit.

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

Scroll down and look for the location block and paste in a new block underneath it with the name of your obscured phpMyAdmin folder, in this example pma_hidden. (Use the right mouse button to paste if using PuTTY for Windows)

/etc/nginx/sites-available/default
location /pma_hidden {
        auth_basic "Restricted Access";
        auth_basic_user_file /etc/nginx/.htpasswd;
}

Save file and exit (press CTRL + X, press Y and then press ENTER).

Check that the Nginx config file is valid, otherwise the server could crash on restart.

sudo nginx -t

If valid, reload Nginx config.

sudo service nginx reload

Now when visiting example.com/pma_hidden, you should be presented with an authentication window.

You’re all done!

What Next?

By now you will have successfully implemented your LEMP stack (Linux/MySQL/PHP) for Ubuntu 18.04 / 19.10 and can administer MySQL through phpMyAdmin.

You may now want to configure SSL for you domain or set up an FTP.

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

36 replies

Leave a reply

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

  1. After Install phpMyAdmin. current PHP version change 7.2 to 8.0.5 my WP website Error
    How install phpMYAdmin without change current PHP version?

  2. I am running an Ubuntu 19.10 with a LEMP stack installed and working. and cannot follow this guide because there no phpmyadmin package available for 19.10. Any suggestions?

  3. Hello Dev,
    I try to install 2 websites on Nginx Everything works perfectly but after apply this code

    Domain 1:
    sudo ln -s /usr/share/phpmyadmin /var/www/test1.com/public_html/phpmyadmin
    Domain 2:
    sudo ln -s /usr/share/phpmyadmin /var/www/test2.com/public_html/phpmyadmin

    and try to login with http://test1.com/phpmyadmin “A file download automatically” I only can login to phpmyadmin on IP address

    please help me to how i can login with my domain to phpmyadmin.

    Thanks…

  4. My PHP is
    The mbstring extension is missing. Please check your PHP configuration.
    why?

  5. I get to section “2. Create a Symbolic Link” and create the link:

    $ sudo ln -s /usr/share/phpmyadmin /var/www/mytest1.com/public_html/phpmyadmin

    and then when I type into my browser my_IP_address/phpmyadmin I get “403 Error / nginx/1.14.0 (Ubuntu)”.

  6. I installed ngnix on Ubuntu 18.04 on digitalocean. Then installed wordpress. After installing phpmyadmin. there is an error while uploadng mysql database in phpmyadmin the error is:

    CREATE TABLE b2s_posts ( id int(11) NOT NULL, post_id int(11) NOT NULL, blog_user_id int(11) NOT NULL, last_edit_blog_user_id int(11) NOT NULL, user_timezone tinyint(4) NOT NULL DEFAULT '0', sched_details_id int(11) NOT NULL, sched_type tinyint(4) NOT NULL DEFAULT '0', sched_date datetime NOT NULL DEFAULT '0000-00-00 00:00:00', sched_date_utc datetime NOT NULL DEFAULT '0000-00-00 00:00:00', publish_date datetime NOT NULL DEFAULT '0000-00-00 00:00:00', publish_link varchar(255) NOT NULL, publish_error_code varchar(100) NOT NULL, network_details_id int(11) NOT NULL, post_for_relay tinyint(4) NOT NULL DEFAULT '0', post_for_approve tinyint(4) NOT NULL DEFAULT '0', relay_primary_post_id int(11) NOT NULL DEFAULT '0', relay_delay_min int(11) NOT NULL DEFAULT '0', hook_action tinyint(4) NOT NULL DEFAULT '0', hide tinyint(4) NOT NULL DEFAULT '0', v2_id int(11) NOT NULL DEFAULT '0' ) ENGINE=MyISAM DEFAULT CHARS

  7. Hey, I successfully installed phpmyadmin on my ngnix server. while importing mysql database file to the phpmyadmin i get this warning message. how to solve it.

    `Warning in ./libraries/plugin_interface.lib.php#551
    count(): Parameter must be an array or an object that implements Countable
    
    Backtrace
    
    ./libraries/display_import.lib.php#371: PMA_pluginGetOptions(
    string ‘Import’,
    array,
    )
    ./libraries/display_import.lib.php#456: PMA_getHtmlForImportOptionsFormat(array)
    ./libraries/display_import.lib.php#691: PMA_getHtmlForImport(
    string ‘5c485d8151b38’,
    string ‘server’,
    string ”,
    string ”,
    integer 2097152,
    array,
    NULL,
    NULL,
    string ”,
    )
    ./server_import.php#34: PMA_getImportDisplay(
    string ‘server’,
    string ”,
    string ”,
    integer 2097152,
    )`
    
  8. Hi,
    I am installing phpmyadmin on ubuntu 18.04 on windows. I have installed nginx and mysql and both are running. Did a symlink
    sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

    However in my browser http://localhost/phpmyadmin, I get loading of the page for 2 mins and stops with a blank page.
    I ‘ll be grateful if someone can help. Thanks

      1. 2018/08/13 19:32:07 [error] 43#43: *28 upstream timed out (110: Connection timed out) while reading upstream, client: ::1, server: _, request: "GET /phpmyadmin/js/get_scripts.js.php?scripts%5B%5D=jquery/jquery-2.1.4.min.js&scripts%5B%5D=sprintf.js&scripts%5B%5D=ajax.js&scripts%5B%5D=keyhandler.js&scripts%5B%5D=jquery/jquery-ui-1.11.4.min.js&scripts%5B%5D=jquery/jquery.cookie.js&scripts%5B%5D=jquery/jquery.mousewheel.js&scripts%5B%5D=jquery/jquery.event.drag-2.2.js&scripts%5B%5D=jquery/jquery-ui-timepicker-addon.js&scripts%5B%5D=jquery/jquery.ba-hashchange-1.3.js&v=4.6.6deb5 HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "localhost"
        
          1. 2018/08/13 19:52:22 [error] 689#689: *54 upstream timed out (110: Connection timed out) while reading upstream, client: ::1, server: _, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "localhost"
            
          2. According to the docs in the link you have given above, there’s a section to sudo apt install libapache2-mod-php7.2. Is this a correct module to install for nginx? Anyway I did install it, but the problem persist. For localhost/info.php, there is still error in the logs, took a long time to load, sometimes I get full page and sometimes only partial. No page at all for phpmyadmin. Is it something to do with not installing mcrypt module?

          3. You are correct, libapache2-mod-php7.2 is not needed for Nginx. I’ve removed that from the guide. However, it will not interfere with Nginx if installed.

            What happens if you run sudo php-fpm7.2 -t

            Did you enter your IP or domain beside server_name in your Nginx config?

            Also reboot the server.

          4. 13-Aug-2018 22:07:26] NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library './mcrypt.so' (tried: ./mcrypt.so (./mcrypt.so: cannot open shared object file: No such file or directory), /usr/lib/php/20170718/./mcrypt.so.so (/usr/lib/php/20170718/./mcrypt.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 [13-Aug-2018 22:07:26] NOTICE: configuration file /etc/php/7.2/fpm/php-fpm.conf test is successful
            

            I have commented out mcrypt.so in the extensions. I thought phpmyadmin need mcrypt. I tried to install but cannot find the package and also read somewhere that it’s being deprecated. Anyway from forums, tried to build it but probably didn’t quite finish the job.

      2. I have installed ubuntu16.04 on windows and working just fine, able to access phpmyadmin

        I have also installed ubuntu 18.04 because I need to install laravel and it uses php version >7.1. So at the moment doing laravel new my-app on 1804 and using 1604 phpmyadmin and ‘php artisan tinker’ to query the 1604 mysql.

        I have tried to connect to 1804 mysql using php artisan migrate and cannot log in. I suspect php7.2-fpm not configured correctly on 1804 and that’s probably why phpmyadmin on 1804 do not completely load. Any advise please.

        1. I have done a new install of ubuntu 18.04 on windows. Install mysql-server, but could not connect to the database using DataGrip. So I guess the problem is mysql setup. I could log in using the terminal.

  9. Great guide! I just had to add the following lines to prevent error 403:

    location /pma {
    index index.php;
    }

  10. Do you have any video tutorial installing multiple domain in 1 server? dont know how to do it..sorry im beginner.

  11. When in comes to making a symbolic link for phpmyadmin, I can really only do this once correct? I tried to do this for each of 3 domains root folders and only the first one actually works. When I try to so make the link the second time my text for “phpmyadmin” is red instead of the light blue like the first time I did it.

    1. Yes, you need to create a separate symbolic link for each domain. Example:

      Domain 1:

      sudo ln -s /usr/share/phpmyadmin /var/www/test1.com/public_html/phpmyadmin
      

      Domain 2:

      sudo ln -s /usr/share/phpmyadmin /var/www/test2.com/public_html/phpmyadmin
      

      Domain 3:

      sudo ln -s /usr/share/phpmyadmin /var/www/test3.com/public_html/phpmyadmin
      

      And they should all be light blue, not red.

      Run ls -l

      ls -l /var/www/test1.com/public_html
      

      And it will show you the link

      rwxrwxrwx  1 www-data www-data    21 Feb 11 03:13 phpmyadmin -> /usr/share/phpmyadmin
      

      Make sure it’s correct!