Never Ending Security

It starts all here

How To Install and Secure phpMyAdmin on Linux

How To Install and Secure phpMyAdmin

Relational database management systems like MySQL are needed for a significant portion of web sites and applications. However, not all users feel comfortable administering their data from the command line.

To solve this problem, a project called phpMyAdmin was created in order to offer an alternative in the form of a web-based management interface. In this guide, we will demonstrate how to install and secure a phpMyAdmin configuration on an Ubuntu 14.04 server. We will build this setup on top of the Nginx web server, which has a good performance profile and can handle heavy loads better than some other web servers.

Before we begin, there are a few requirements that need to be settled.

  • Ubuntu Server
  • LEMP (Linux, Nginx, MySQL, and PHP) stack on your Ubuntu server.

Install phpMyAdmin

With our LEMP platform already in place, we can begin right away with installing the phpMyAdmin software.

sudo apt-get update
sudo apt-get install phpmyadmin

During the installation, you will be prompted for following information.

  • Which web server you would like the software to automatically configure.
  • The next prompt will ask if you would like dbconfig-common to configure a database for phpmyadmin to use. Select “Yes” to continue.
  • You will also need to enter the database administrative password that you configured during the MySQL installation to allow these changes.

When the installation is complete. you will need to create a symbolic link from the installation files to our Nginx document root directory:

sudo ln -s /usr/share/phpmyadmin /usr/share/nginx/html

A final item that we need to address is enabling the mcrypt PHP module, which phpMyAdmin relies on.

sudo php5enmod mcrypt
sudo service php5-fpm restart

With that, our phpMyAdmin installation is now operational. To access the interface, go to your server’s domain name or public IP address followed by /phpmyadmin, in your web browser:



To sign in, use a username/password pair of a valid MySQL user.

Secure your phpMyAdmin Instance

The phpMyAdmin instance installed on our server should be completely usable at this point. However, by installing a web interface, we have exposed our MySQL system to the outside world. Even with the included authentication screen, this is quite a problem.

We will implement two simple strategies to lessen the chances of our installation being targeted and compromised. We will change the location of the interface from /phpmyadmin and also create an additional, web server-level authentication gateway that must be passed before even getting to the phpMyAdmin login screen.

Changing the Application’s Access Location

In order for our Nginx web server to find and serve our phpMyAdmin files, we created a symbolic link from the phpMyAdmin directory to our document root. To change the URL where our phpMyAdmin interface can be accessed, we simply need to rename the symbolic link.

cd /usr/share/nginx/html
ls -l
total 8
-rw-r--r-- 1 root root 537 Nov  17 18:46 50x.html
-rw-r--r-- 1 root root 612 Nov  17 18:46 index.html
lrwxrwxrwx 1 root root  21 Nov  17 19:50 phpmyadmin -> /usr/share/phpmyadmin

As you can see, we have a symbolic link called phpmyadmin in this directory. We can change this link name to whatever we would like. This will change the location where phpMyAdmin can be accessed from a browser, which can help obscure the access point from hard-coded bots.

Choose a name that does not indicate the purpose of the location. In this guide, we will name our access location /hiddenadmin. To accomplish this, we will just rename the link:

sudo mv phpmyadmin hiddenadmin
ls -l
total 8
-rw-r--r-- 1 root root 537 Nov  17 18:46 50x.html
-rw-r--r-- 1 root root 612 Nov  17 18:46 index.html
lrwxrwxrwx 1 root root  21 Nov  17 20:25 hiddenadmin -> /usr/share/phpmyadmin

Now, if you go to the previous location of your phpMyAdmin installation, you will get a 404 error


However, your phpMyAdmin interface will be available at the new location we selected:


Setting up a Web Server Authentication Gate

The next feature we wanted for our installation was an authentication prompt that a user would be required to pass before ever seeing the phpMyAdmin login screen.

Fortunately, most web servers, including Nginx, provide this capability natively. We will just need to modify our Nginx configuration file with the details.

Before we do this, we will create a password file that will store our the authentication credentials. Nginx requires that passwords be encrypted using the crypt() function. The OpenSSL suite, which should already be installed on your server, includes this functionality.

To create an encrypted password, type:

openssl passwd

You will be prompted to enter and confirm the password that you wish to use. The utility will then display an encrypted version of the password that will look something like this:


Copy this value, as you will need to paste it into the authentication file we will be creating.

Now, create an authentication file. We will call this file pma_pass and place it in the Nginx configuration directory:

sudo nano /etc/nginx/pma_pass

Within this file, you simply need to specify the username you would like to use, followed by a colon (:), followed by the encrypted version of your password you received from the openssl passwd utility.

We are going to name our user demo, but you should choose a different username. The file for this guide looks like this:


Save and close the file when you are finished.

Now, we are ready to modify our Nginx configuration file. Open this file in your text editor to get started:

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

Within this file, we need to add a new location section. This will target the location we chose for our phpMyAdmin interface (we selected /hiddenadmin in this guide).

Create this section within the server block, but outside of any other blocks. We will put our new location block below the location / block in our example:

server {
    . . .

    location / {
        try_files $uri $uri/ =404;

    location /hiddenadmin {

    . . .

Within this block, we need to set the value of a directive called auth_basic to an authentication message that our prompt will display to users. We do not want to indicate to unauthenticated users what we are protecting, so do not give specific details. We will just use “Admin Login” in our example.

We then need to use a directive called auth_basic_user_file to point our web server to the authentication file that we created. Nginx will prompt the user for authentication details and check that the inputted values match what it finds in the specified file.

After we are finished, the file should look like this:

server {
    . . .

    location / {
        try_files $uri $uri/ =404;

    location /hiddenadmin {
       auth_basic "Admin Login";
       auth_basic_user_file /etc/nginx/pma_pass;

    . . .

Save and close the file when you are finished.

To implement our new authentication gate, we must restart the web server:

sudo service nginx restart

Now, if we visit our phpMyAdmin location in our web browser (you may have to clear your cache or use a different browser session if you have already been using phpMyAdmin), you should be prompted for the username and password you added to the pma_pass file:


Once you enter your credentials, you will be taken to the normal phpMyAdmin login page. This added layer of protection will help keep your MySQL logs clean of authentication attempts in addition to the added security benefit.

You can now manage your MySQL databases from a reasonably secure web interface. This UI exposes most of the functionality that is available from the MySQL command prompt. You can view databases and schema, execute queries, and create new data sets and structures.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s