In the first part of the Red Hat Server series, we configured a web server using Apache (and optionally Nginx) and enabled a firewall. The tutorials are based on the Rocky Linux 9.5 installation, which is fully compatible with Red Hat and can also be used with CloudLinux, AlmaLinux, EuroLinux, CentOS Stream, MIRACLE Linux, etc.
In this part, we’ll install PHP and a database system based on MariaDB. PHP is used to generate web pages and build real-time web applications, while SQL is used to create and modify relational databases, as well as to insert and retrieve data from them.
Installing PHP
dnf install php php-fpm php-opcache php-gd php-xml php-mbstring
Activating PHP-FPM:
systemctl start php-fpm
systemctl enable php-fpm
systemctl status php-fpm
We need to run the following command to inform SELinux to allow Apache to execute PHP code via PHP-FPM.
setsebool -P httpd_execmem 1
To check the PHP configuration on your server, place the file:
info.php
in the shared directory for websites:
/var/www/html/
by pasting into the file:
<?php phpinfo(); ?>
Reload Apache and PHP:
systemctl restart httpd php-fpm
Open the following page in your web browser (instead of 111.222.33.44, enter your server’s IP or domain):
http://111.222.33.44/info.php
or enter localhost if you’re running the server on a local machine:
localhost/info.php
MariaDB Installation
dnf install mariadb-server php-mysqlnd
Service status and startup:
systemctl start mariadb
systemctl enable mariadb
systemctl status mariadb
After installation, MariaDB does not include a password for the root user, so you need to set one immediately. Execute the command:
mysql_secure_installation
When prompted for the root password, simply press ENTER:
Enter current password for root (enter for none):
Now you can define the MariaDB authentication protocol:
If you already have your root account protected, you can safely answer ‘n’.
Switch to unix_socket authentication [Y/n]
Specify Y (Yes) to change the root password:
Change the root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!Next, a series of questions about MariaDB configuration and security appear. You can answer each one with Y:
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access it? [Y/n] Y
Reload privilege tables now? [Y/n] YThe script will then complete, and MariaDB will be ready for use.
First login as root:
mysql -u root -p
Enter password:
MariaDB [(none)]>
Now you can create a new database user, which will be needed to configure the web application.
Create a user named ‘pawel0’:
CREATE USER ‘pawel0’@’localhost’ IDENTIFIED BY ‘mypassword0’;
Create a new database named database0:
CREATE DATABASE database0;
Grant user ‘pawel0’ access to database ‘base0’:
GRANT ALL PRIVILEGES ON database0.* TO ‘pawel0’@’localhost’;
To exit MariaDB:
exit;
To check the MariaDB version:
mysql -v
And that’s all for the installation and basic configuration of PHP and MariaDB on a server running Red Hat and its forks.
In the next Part 3, we will install the SSL certificate using the free tool Let’s Encrypt and configure cron to automatically renew it.
