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.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.