How to Install LAMP Stack on Rocky Linux 8

LAMP is a popular stack used in development circles for hosting both static and dynamic web applications. It’s an acronym for Linux, Apache, MySQL (or MariaDB) & PHP. Notably, it comprises the Apache web server, MySQL or MariaDB database server, and PHP.

[ You might also like: How to Install LEMP Stack on Rocky Linux 8 ]

In this guide, we walk you through the installation of LAMP on Rocky Linux 8.

Prerequisites

Before you get started, ensure you have the following:

Let’s get started…

Step 1: Install Apache on Rocky Linux

The first component that we are going to start installing is the Apache webserver. This is provided by the httpd software package. When installed, the httpd daemon runs in the background awaiting incoming HTTP requests from client devices.

To install Apache, launch your terminal and run the command.

$ sudo dnf install httpd

This installs the httpd package along with other dependencies.

Install Apache on Rocky Linux
Install Apache on Rocky Linux

Thereafter, enable the webserver to start at boot time.

$ sudo systemctl enable httpd

And then start the Apache httpd daemon as shown.

$ sudo systemctl start httpd

To verify that Apache is running on Rocky Linux 8, issue the command:

$ sudo systemctl status httpd
Check Apache Status on Rocky Linux
Check Apache Status on Rocky Linux

Another nifty way of verifying that Apache is active and running is by browsing the server’s IP address or Fully Qualified Domain Name (FQDN) as shown.

http://server-IP
OR
http://domain.com

This should give you the Apache HTTP test page, an indicator that all is well.

Check Apache Page on Rocky Linux
Check Apache Page on Rocky Linux

If you are getting an error on your browser when trying to access the page, then perhaps the firewall is blocking HTTP traffic. Run the commands below to allow HTTP traffic and reload the firewall.

$ sudo firewall-cmd --add-service=http --permanent
$ sudo firewall-cmd --reload

Step 2: Install MariaDB on Rocky Linux

Next, we need to install a database server. Two options can suffice – MariaDB and MySQL. We will focus on MariaDB thanks to its numerous enhancements including faster and safer replication, numerous high-performance storage engines, backward compatibility with MySQL, and better overall performance when compared to MySQL.

The AppStream repository for Rocky Linux provides MariaDB 10.3 by the time of writing down this guide.

To install MariaDB, run the command:

$ sudo dnf install mariadb-server mariadb

When the installation is complete, enable MariaDB to start upon a reboot or during booting.

$ sudo systemctl enable --now mariadb

And finally, start the MariaDB service.

$ sudo systemctl start mariadb

To confirm that MariaDB daemon is running, run the command:

$ sudo systemctl status mariadb
Check MariaDB Status
Check MariaDB Status

MariaDB’s default settings are weak and pose a few vulnerabilities that can be leveraged by hackers to breach the database server. As such, we need to take additional steps to harden the database server.

To achieve this, run the script shown.

$ sudo mysql_secure_installation

The first step is to set the root password. So press ENTER since no Root password is configured by default, and then ‘Y’ to set a root password. Provide a strong password and confirm it.

Set MariaDB Root Password
Set MariaDB Root Password

Type ‘Y’ for the remaining configurations. This will effectively purge or remove any anonymous users, block remote root login and remove the Test database which is not needed in a production environment.

Secure MariaDB Installation
Secure MariaDB Installation

The database server is now fully configured and secure.

Step 3: Install PHP on Rocky Linux

Finally, the last component to install will be PHP. PHP, a backronym for PHP Hypertext Preprocessor, is a scripting language used in the development of dynamic web pages.

[ You might also like: How to Install Latest PHP 8.0 on Rocky Linux 8 ]

Rocky Linux AppStream provides multiple versions of PHP. To check the available versions, run the command:

$ sudo dnf module list php

This provides a list of PHP modules and Streams.

List PHP Module Stream
List PHP Module Stream

The default PHP stream is PHP 7.2. To install the latest module Stream from the repository, reset the PHP streams.

$ sudo dnf module reset php

Then enable the preferred PHP stream. For example, to enable PHP 7.4, execute:

$ sudo dnf module install php:7.4

This installs PHP 7.4 and associated extensions.

Install PHP on Rocky Linux
Install PHP on Rocky Linux

Moreover, you can install additional PHP extensions. Here, we are installing php-curl, and php-zip extensions.

$ sudo dnf install php-curl php-zip

Once installed, confirm the version of PHP installed as shown.

$ php -v
Check PHP Version
Check PHP Version

Another way of testing the version of PHP installed is to create a test PHP file in the /var/www/html path.

$ sudo vim /var/www/html/info.php

Paste the configuration shown.

<?php

phpinfo();

?>

Save the changes and restart the webserver.

$ sudo systemctl restart httpd

Then head back to your browser and follow the URL shown

http://server-ip/info.php

A page displaying the PHP version among other parameters such as details of PHP extensions enabled will be displayed.

Check PHP Info
Check PHP Info

You can now remove the test PHP file.

$ sudo rm -f /var/www/html/info.php

And there you have it. We have successfully installed the LAMP stack on Rocky Linux 8.4. You can proceed and host your web applications or configure apache to run multiple virtual hosts in the same server.

Also, if you want to secure an Apache web server with SSL Certificate, do check out our guide that explains How to Secure Apache with Let’s Encrypt Certificate on Rocky Linux.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

James Kiarie
This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I'm not running commands on the terminal, I'm taking listening to some cool music. taking a casual stroll or watching a nice movie.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.