In this tutorial we have step by step instruction on how to install Apache MySQL and PHP on your RHEL 9.0, Red Hat Enterprise Linux release 9.0 (Plow) .

I am using EC2 instance in AWS and launched a RedHat Linux 9 AMI.
Note: By default it installs PHP-FPM, an efficient method to configure PHP  that minimize the memory consumption and rise the performance for websites with heavy traffic. That way you don’t have any dependency with Apache and is decoupled.

Table of Contents

Update Package (Optional)

This will update every currently installed package. DNF will only update the listed packages. While updating packages, DNF will ensure that all dependencies are satisfied. This is optional but I recommend you to make sure that the server is updated with all the latest packages/releases every time.

$ sudo dnf update

Install Apache

At the time of writing this tutorial the latest available version of Apache in the Red Hat Update Infrastructure (RHUI) repo for AWS is 2.4.53-7 . If you would like to get the updates from your on-premises update infrastucture then you need follow the FAQ from AWS https://aws.amazon.com/partners/redhat/faqs/

$ sudo dnf install httpd

When you run the above command these dependencies will also get installed along with the original httpd package

Installed:
apr-1.7.0-11.el9.x86_64                  
apr-util-1.6.1-20.el9.x86_64        
apr-util-bdb-1.6.1-20.el9.x86_64
apr-util-openssl-1.6.1-20.el9.x8         
httpd-core-2.4.53-7.el9.x86_64
httpd-filesystem-2.4.53-7.el9.noarch     
httpd-tools-2.4.53-7.el9.x86_64     
mailcap-2.1.49-5.el9.noarch
mod_http2-1.15.19-2.el9.x86_64           
mod_lua-2.4.53-7.el9.x86_64         
redhat-logos-httpd-90.4-1.el9.noarch

Check the version

$ httpd -v
Server version: Apache/2.4.53 (Red Hat Enterprise Linux)
Server built:   Jul 20 2022 00:00:00
$

Start Apache

$ sudo systemctl start httpd

Check Apache is running

$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
     Active: active (running) since Tue 2022-11-22 16:13:41 UTC; 1min 1s ago
       Docs: man:httpd.service(8)
   Main PID: 44321 (httpd)
     Status: "Total requests: 1; Idle/Busy workers 100/0;Requests/sec: 0.0169; Bytes served/sec:   5 B/sec"
      Tasks: 213 (limit: 5719)
     Memory: 23.5M
        CPU: 95ms
     CGroup: /system.slice/httpd.service
             ├─44321 /usr/sbin/httpd -DFOREGROUND
             ├─44322 /usr/sbin/httpd -DFOREGROUND
             ├─44323 /usr/sbin/httpd -DFOREGROUND
             ├─44324 /usr/sbin/httpd -DFOREGROUND
             └─44325 /usr/sbin/httpd -DFOREGROUND

Also use CURL to confirm the page is loading, it may show 403 but that is ok

$ curl -I localhost
HTTP/1.1 403 Forbidden
Date: Tue, 22 Nov 2022 16:14:45 GMT
Server: Apache/2.4.53 (Red Hat Enterprise Linux)
Last-Modified: Mon, 09 Aug 2021 11:43:42 GMT
ETag: "1715-5c91ee59c9780"
Accept-Ranges: bytes
Content-Length: 5909
Content-Type: text/html; charset=UTF-8

PHP Installation

We are going to install the default package available in Red Hat Update Infrastructure (RHUI) repo for PHP 8.0.20-3 at the time of writing this post.

$ sudo dnf install php

Check PHP Installed

$ php -v
PHP 8.0.20 (cli) (built: Jun  8 2022 00:33:06) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.20, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.20, Copyright (c), by Zend Technologies

Install PHP-MYSQLND

This package is important for you since it comes with all the modules required to connect from PHP to MYSQL

$ sudo dnf install php-mysqlnd

Restart Apache

$ sudo systemctl restart httpd

Restart PHP

$ sudo systemctl restart php-fpm

Test Apache / PHP

In order to make sure that PHP has been configured successfully with Apache, let us change directory to /var/www/html and create a file named index.php.

Add the content below

<?php
phpinfo();
?>

$ pwd
/var/www/html
$ ls -l
total 4
-rw-r--r--. 1 root root 20 Nov 22 16:18 index.php

Load the website in a browser, you can use your public IP address. Make sure port 80 is open in the security group

MySQL / MariaDB Installation

I would recommend using AWS RDS Aurora MySQL (Since it is scalable and completely avoids the complexity of maintaining a database infrastructure) instead of local database. Available version for MYSQL 8.0.30-3 and MariaDB 3:10.5.16-2. If you wish to install MySQL run this command.

$ sudo dnf install mysql-server

If you need MariaDB instead of MYSQL then run,

$ sudo dnf install mariadb-server

Start the MYSQL / MariaDB service

$ sudo systemctl start mysqld

Secure MySQL / MariaDB installation by running the below command. Make sure to provide a password. Select the options that pop up according to your requirement. I have given most of them no since this is just a test installation.

$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: n
Please set the password for root here.

New password:

Re-enter new password:
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

Connect to MYSQL using new password and create a test database

$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.30 Source distribution

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE LAMP_RHEL9;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| LAMP_RHEL9         |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql>

Test LAMP Installation

Let us create a PHP file named test_connection.php under /var/www/html . This file contains the logic to connect to the database we created above. Make sure to replace the $password variable with the new password you created to connect to MySQL server.

$ cat test_connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "YOURPASSWORD";
$dbname = "LAMP_RHEL9";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
else
{
echo "Connected to ".$dbname;
}
?>

Troubleshooting

If you are getting this error in your PHP logs

[22-Nov-2022 16:45:23 UTC] PHP Fatal error:  Uncaught Error: Class “mysqli” not found in /var/www/html/test_connection.php:8

This means that you are missing the mysqli module and have to install php-mysqlnd package. Check the PHP fpm logs for more details under /var/log/php-fpm

Leave a Reply

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