CentOS

How to Install phpMyAdmin with Nginx on CentOS 8

What is phpMyAdmin?

The phpMyAdmin is a free and open-source tool for managing the MySQL or MariaDB database. It’s a web-based tool that allows you to manage and administer MySQL databases from the web browser.

PhpMyAdmin has become one of the most popular tools for managing the MySQL database. It makes you easier to manage the MySQL databases, especially for the newcomer that wants to learn about the MySQL itself.

In this tutorial, we will show you how to install and secure phpMyAdmin with the Nginx webserver on the CentOS 8 server.

Prerequisite

For this guide, ensure you have the CentOS 8 with the basic LEMP Stack installed on top of it.

Below is the tutorial about the CentOS 8 server installation.

Install CentOS 8 Server

And below is the guide LEMP Stack installation on the CentOS 8 server.

Install LEMP Stack on CentOS 8 Server

Before going any further, log in to your CentOS server and type the sudo command to get the root privileges on your system.

sudo su

Step 1 – Install PHP Packages

First, we will install additional PHP packages needed for the phpMyAdmin installation.

Install additional PHP packages using the dnf command below.

dnf install php-mysqlnd php-json php-mbstring php-spl php-xml php-ctype php-gd php-zip

After that, change the owner of the ‘/var/lib/php’ directory to the ‘nginx’ user and group.

chown -R root:nginx /var/lib/php

Then restart the PHP-FPM service

systemctl restart php-fpm

Install PHP Packages

As a result, you’ve installed additional PHP packages for the phpMyAdmin installation.

Step 2 – Install and Configure phpMyAdmin

In this tutorial, we will install the phpMyAdmin manually from source code.

To do that, we will download the latest version of the phpMyAdmin source code, setup the phpMyAdmin configuration file, and import the MySQL database scheme.

– Download phpMyAdmin Source Code

Go to the ‘/var/www’ directory and download the latest phpMyAdmin source code using the wget command below.

cd /var/www/
wget https://files.phpmyadmin.net/phpMyAdmin/4.9.4/phpMyAdmin-4.9.4-all-languages.tar.gz

After that, extract the phpMyAdmin source code and rename the extracted directory to ‘phpMyAdmin’.

tar -xf phpMyAdmin-4.9.4-all-languages.tar.gz
mv phpMyAdmin-4.9.4-all-languages/ phpmyadmin/

As a result, you’ve downloaded the phpMyAdmin source code to the ‘/var/www/phpmyadmin’ directory.

– Configure phpMyAdmin

Next, go to the ‘/var/www/phpMyAdmin’ directory and copy the sample configuration file ‘config.sample.inc.php’ to ‘config.inc.php’.

cd /var/www/phpmyadmin/
cp config.sample.inc.php config.inc.php

Edit the ‘config.inc.php’ configuration file using vim editor.

vim config.inc.php

Now go to this site to generate the cookie authentication for the phpMyAdmin.

https://phpsolved.com/phpmyadmin-blowfish-secret-generator/

Copy and paste the blowfish secret as below.

$cfg['blowfish_secret'] = '[3Lbnu,dlSf8-[E/kKOdM2M[Z[fHf;2K'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

Save and close.

– Import Database Scheme for phpMyAdmin

Next, we need to import the MySQL database scheme for phpMyAdmin. The sample of phpMyAdmin database scheme are available at It’s available on the the ‘/var/www/phpmyadmin/sql/’.

Import the phpMyAdmin database scheme to the MySQL server using the command below.

mysql < /var/www/phpmyadmin/sql/create_tables.sql -u root -p

Type your MySQL root password and make sure there is no error.

As a result, you’ve imported the MySQL database scheme for the phpMyAdmin.

– Create a tmp Directory

The ‘tmp’ directory on the phpMyAdmin will be used as the log cache. We will create the tmp directory and make it writable by phpMyAdmin.

Create the ‘tmp’ directory and change the permission for that directory.

mkdir /var/www/phpmyadmin/tmp
chmod 777 /var/www/phpmyadmin/tmp

After that, change the owner of the phpMyAdmin directory to the ‘nginx’ user and group.

chown -R nginx:nginx /var/www/phpmyadmin

Install phpMyAdmin on CentOS 8

As a result, you’ve installed the phpMyAdmin on the CentOS 8 server.

Step 3 – Set Up SELinux Policy for phpMyAdmin

Since we leave the SELinux on the ‘Enforcing’ mode, we need to set up the SELinux policy for phpMyAdmin.

First, install the SELinux policy management tool using the dnf command below.

dnf install policycoreutils-python-utils

After that, add the SELinux object policy for the phpMyAdmin using the semanage command below.

semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/phpmyadmin/'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/phpmyadmin/tmp(/.*)?'

Now restore the phpMyAdmin to default SELinux security context.

restorecon -Rv '/var/www/phpmyadmin/'

As a result, you’ve added the SELinux policy for phpMyAdmin.

Step 4 – Setup Nginx for phpMyAdmin on CentOS 8

In this step, we will add the nginx configuration for the phpMyAdmin.

Go to the ‘/etc/nginx/default.d/’ directory and create a new configuration ‘phpmyadmin.conf’.

cd /etc/nginx/default.d/
vim phpmyadmin.conf

Paste the following configuration.

location /phpmyadmin {

    root /var/www/;
    index index.php index.html index.htm;

    location ~ ^/phpmyadmin/(.+\.php)$ {
        try_files $uri =404;
        root /var/www/;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

    location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
        root /var/www/;
    }
}

Save and close.

Next, test the gninx configuration and make sure there is no error. Then restart the nginx service.

nginx -t
systemctl restart nginx

And you will get the result as below.

Setup Nginx for phpMyAdmin on CentOS 8

As a result, you’ve added the Nginx configuration for phpMyAdmin. The phpMyAdmin is now accessible through the URL below.

http://ipserver/phpmyadmin/

Step 5 – Secure phpMyAdmin with Nginx Basic Authentication

In this step, we will secure the phpMyAdmin installation using the Nginx basic authentication module.

First, install the ‘httpd-tools’ package using the dnf command below.

dnf install httpd-tools

After that, generate a new password file for the basic authentication using the htpasswd command below.

htpasswd -c /etc/nginx/.htpasswd hakase

Type your password and the user file for the basic authentication has been created.

Additionally, if you want to add a new user into it, use the htpasswd command without a ‘-c’ option.

htpasswd /etc/phpmyadmin/.htpasswd user02

Next, go to the ‘/etc/nginx/default.d’ directory and edit the ‘phpmyadmin.conf’ file.

cd /etc/nginx/default.d/
vim phpmyadmin.conf

Add the folliwng configuration under the ‘location /phpmyadmin { ... }‘ blocks.

location /phpmyadmin {

...
    auth_basic "Administrator’s Area";
    auth_basic_user_file /etc/nginx/.htpasswd;
...
}

Save and close.

Now test the nginx configuration and make sure there is no error. Then restart the Nginx service.

nginx -t
systemctl restart nginx

Setup Nginx Basic Authentication for phpMyAdmin on CentOS 8

As a result, you’ve completed the Nginx basic authentication set up for phpMyAdmin. In effect, you will get an additional authentication for accessing the phpMyAdmin.

Step 6 – Testing

Open your web browser and type the phpMyAdmin URL as below.

http://ipserver/phpmyadmin/

Now you will get the login page of the Nginx basic authentication.

Nginx Basic Authentication Login

Type your user and password, then click the ‘Sign In‘ button.

And you will be shown the login page of phpMyAdmin.

phpMyAdmin Login Page

Type the ‘phpmyadmin’ user and the password that just set up on top during the installation and click the ‘Go‘ button.

Once the user and password are correct, you will get the phpMyAdmin dashboard as below.

phpMyAdmin Dashboard

As a result, you’ve installed the phpMyAdmin with the Nginx web server on the CentOS 8 Server. And you’ve successfully secured the phpMyAdmin installation using the Nginx basic authentication.

Write A Comment