Install PhpMyAdmin with Apache on CentOS, RHEL 8

phpMyAdmin allows you to administer and manage your MySQL/MariaDB database using the web interface. This tutorial will walk you through the steps to install and configure phpMyAdmin on CentOS, or RHEL 8 Linux

Note: With CentOS 8 release, yum command has been replaced with dnf and in near future yum package manager will be discontinued. It is now recommended to use dnf for installing packages but if you still wish to use yum you can use it.

Prerequisites

You will need one CentOS/RHEL8 server installed on (physical or virtual) machine with root or sudo non-root user privileges

Set Timezone

You can correct your server timezone by typing the following command but make sure you replace Asia/Karachi with yours
sudo timedatectl set-timezone Asia/Karachi

Adding EPEL Repository

It is always recommended to add extra packages for enterprise Linux on your fresh CentOS/RHEL server:
sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo dnf config-manager --set-enabled PowerTools
sudo dnf -y upate
If you are on RHEL 8, execute the following command as well:
sudo ARCH=$( /bin/arch )
sudo subscription-manager repos --enable "codeready-builder-for-rhel-8-${ARCH}-rpms"
sudo dnf config-manager --set-enabled PowerTools

Installing PHP

You can install latest PHP and its commonly used extensions with the below command:
sudo dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
sudo dnf -y install php php-common php-process php-xmlrpc php-xml php-soap php-snmp php-recode php-bcmath php-cli php-dba php-dbg php-mbstring php-odbc php-pecl-apcu-devel php-pecl-zip php-pgsql php-pecl-apcu php-pear php-pdo php-opcache php-devel php-embedded php-enchant php-gd php-fpm php-gmp php-intl php-ldap php-json php-mysqlnd php-pdo php-gd php-mbstring zip unzip tar wget

Installing Apache

Type the following command to install Apache web server:
sudo dnf -y install httpd httpd-devel mod_ssl openssl
Now starting Apache service and making persistent even when system reboots:
sudo systemctl start httpd
sudo systemctl enable httpd

Installing MariaDB

If you would like to user MariaDB as your database, type the below command to install:
sudo dnf -y install mariadb-server
Starting MariaDB service and making it persistent even when system reboots:
sudo systemctl start mariadb
sudo systemctl enable mariadb
Or if you still wish to use MySQL as your database, type the below command to install:
sudo dnf -y install mysql-server mysql

Securing MariaDB/MySQL

By default MariaDB/MySQL has no root password and anyone can intrude into your database, so run the below script and follow on screen instruction to secure it:
sudo mysql_secure_installation
Response to the following prompts on your server like below:
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Testing Apache, PHP

Now create info.php page under /var/www/html to test your Apache and PHP functionality:
sudo vi /var/www/html/info.php
Add following code into it:
<?php
phpinfo();
?>
Save and close file.

Now disable default welcome page from Apache like below:
sudo mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/bkpwelcome
Reload Apache to take changes into effect:
sudo systemctl reload httpd
Add firewall rules to allow HTTP traffic:
sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --reload
Open up your preferred web browser and navigate to http://your_server_name_or_ip/info.php

If you can see the below page, then your Apache and PHP working as expected.


Enabling SSL

It is always recommended to access your web services over secure protocol HTTPS. As you have already installed mod_ssl while installing Apache in earlier steps, you just need to add the below firewall rules to allow HTTPS traffic over default port 443 and deny access to HTTP on port 80.
sudo firewall-cmd --zone=public --permanent --remove-service=http
sudo firewall-cmd --zone=public --permanent --add-service=https
sudo firewall-cmd --reload

Downloading phpMyAdmin

You can check the current release on phpMyAdmin website, then copy the downloadable link:

Set phpMyAdmin version to your CentOS/RHEL server environment variable:
export VER="5.0.0"
Now download the latest release of phpMyAdmin and extract it like below:
curl -o phpMyAdmin-5.0.0-all-languages.zip https://files.phpmyadmin.net/phpMyAdmin/5.0.0/phpMyAdmin-5.0.0-all-languages.zip
sudo unzip -q phpMyAdmin*.zip
sudo mv phpMyAdmin-5.0.0-all-languages /usr/share/phpmyadmin
Copy config.sample.inc.php configuration file like below:
sudo cp /usr/share/phpmyadmin/config.sample.inc.php  /usr/share/phpmyadmin/config.inc.php
Now edit config.inc.php file:
sudo vi /usr/share/phpmyadmin/config.inc.php
Set a secret passphrase should be 32 chars long as well as set tmp directory like below:
$cfg['blowfish_secret'] = '$2a$07$H6V9J74bK5S5qez6CRXt7OviIqRlFwJiniEFAaBsGXoz8MCukudia'; 
$cfg['TempDir'] = '/var/lib/phpmyadmin/tmp';
Save and close file when you are finished.

Now create tmp directory and set appropriate permission like below:
sudo mkdir /var/lib/phpmyadmin
sudo mkdir /var/lib/phpmyadmin/tmp
sudo chown -R apache:apache /var/lib/phpmyadmin/tmp

Configuring Apache

At this point, you need to create phpmyadmin.conf file under Apache:
sudo vi /etc/httpd/conf.d/phpmyadmin.conf
Add below parameters into it:
Alias /phpMyAdmin /usr/share/phpmyadmin/
Alias /phpmyadmin /usr/share/phpmyadmin/
 
<Directory /usr/share/phpmyadmin/>
   AddDefaultCharset UTF-8
 
   <IfModule mod_authz_core.c>
     # Apache 2.4
     Require all granted
   </IfModule>
   <IfModule !mod_authz_core.c>
     # Apache 2.2
     Order Deny,Allow
     Deny from All
     Allow from 192.168.137.1
     Allow from ::1
   </IfModule>
</Directory>
Save and close file when you are finished

For security reason, we have restricted phpMyAdmin access to a specific IP with Allow from 192.168.137.1 parameter in above file.

Type the below commands to validate configuration, then restart Apache service to take changes into effect:
sudo apachectl configtest
sudo systemctl restart httpd

Configuring SELinux

If SELinux is in Enforcing mode on your CentOS/RHEL server, you’ll get permission denied error when you try to access phpMyAdmin page.

Type the below command to allow it pass-throw selinux:
sudo semanage fcontext -a -t httpd_sys_content_t "/usr/share/phpmyadmin(/.*)?"
sudo restorecon -Rv /usr/share/phpmyadmin
Or type just below command to disable selinux enforcing:
sudo setenforce 0

Access phpMyAdmin Web interface

Open up web browser on the host you allowed to access phpMyAdmin web interface in above Apache configuration file, then navigate to https://your_server_name_or_ip/phpmyadmin and you will see the below phpMyAdmin login page.

For the first time you can log in with database user root and password whatever you set up during mysql_secure_installation script.


Once you log in, you will see the below man page


We will create a test database, user and privileges to give you an example:


Creating user for testdb


Set the user's credentials


User created with database specific privileges


Now logout from the root user


Login with newly created database user testdb


You are log in to testdb database.



Wrapping up

You have successfully set up phpMyAdmin on your CentOS/RHEL 8 server. Now you can create, administer and manage your MariaDB/MySQL database using phpMyAdmin web interface.

No comments:

Powered by Blogger.