Install LAMP Stack on Rocky Linux 9 | Best Setup – OrcaCore

Posted on

Install LAMP Stack on Rocky Linux 9 | Best Setup - OrcaCore

Install LAMP Stack on Rocky Linux 9 | Best Setup – OrcaCore

This guide provides a comprehensive walkthrough on How To Install LAMP Stack on Rocky Linux 9. LAMP is an acronym that represents a popular open-source web development stack, consisting of Linux, Apache, MariaDB or MySQL, and PHP. This combination of technologies provides a robust and versatile platform for building and deploying web applications.

A LAMP stack is primarily used for backend or server-side development. A backend application is the engine that powers the frontend, processing data and logic behind the scenes, invisible to the end-user. The backend handles crucial tasks such as:

  • Data storage and retrieval
  • User authentication and authorization
  • Business logic execution
  • API endpoint management

The webpage displayed in your browser is the frontend application. When you interact with the page, for example, by clicking a button or submitting a form, your browser sends requests to the backend application to retrieve or process the necessary information.

Developers leverage a LAMP stack to create a wide range of web content, from static websites to dynamic, data-driven web applications. The following steps will guide you through the Install LAMP Stack on Rocky Linux 9 process.

Steps To Install LAMP Stack on Rocky Linux 9 Blue Onyx

Before you begin, ensure you have the following prerequisites:

  • A server running Rocky Linux 9.
  • A non-root user with sudo privileges. You can refer to our guide on Initial Server Setup with Rocky Linux 9 for assistance.
  • A basic firewall configured and enabled.

Now, follow the steps below to complete the Install LAMP Stack on Rocky Linux 9 guide.

1. LAMP Stack Rocky Linux 9: Install Apache Web Server

Apache serves as the web server, responsible for handling HTTP requests and serving web content. First, update your local package index using the following command:

sudo dnf update -y

Next, install Apache with the following command:

sudo dnf install httpd -y

Once the installation is complete, start and enable the Apache service on Rocky Linux 9 using these commands:

sudo systemctl enable httpd
sudo systemctl start httpd

Verify that the Apache service is active and running on your server with the following command:

sudo systemctl status httpd

The output should resemble the following:

**Output**
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor prese>
Active: **active** (**running**) since Wed 2022-10-26 09:00:37 EST; 34s ago
Docs: man:httpd.service(8)
Main PID: 89138 (httpd)
Status: "Running, listening on: port 80"
...

Configure Firewall For Apache Web Server

Assuming you have enabled firewalld as per the prerequisites, you now need to allow traffic for Apache through the Rocky Linux firewall. Use the following command:

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

Reload the firewall to apply the new rules:

sudo firewall-cmd --reload

Now, you can access the Apache default page by typing your server’s IP address in your web browser:

http://server-IP-address

You should see the Apache test page, confirming successful installation.

Apache test page-LAMP Stack

2. LAMP Stack Rocky Linux 9: Install MariaDB

Next, you need to set up MariaDB as the database server component of the LAMP stack on your Rocky Linux 9 system. To install MariaDB, run the following command:

sudo dnf install mariadb-server mariadb -y

After the installation completes, start and enable the MariaDB service using the following commands:

sudo systemctl enable mariadb
sudo systemctl start mariadb

Then, secure your MariaDB installation on Rocky Linux 9 with the following command:

sudo mysql_secure_installation

You will be asked a series of questions. Answer them as shown below:

Enter current password for root (enter for none):
Set root password? [Y/n] Y
New password:
Re-enter new password:
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

To log in to your MariaDB console, you can use the following command:

sudo mysql -u root -p

3. LAMP Stack Rocky Linux 9: Set up PHP

The default PHP version available in the Rocky Linux 9 repositories is PHP 8.0. At the time of writing, the latest version of PHP is PHP 8.1.

To install the latest PHP on Rocky Linux 9, run the command below to enable the Remi repository first:

sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm

Then, list the available PHP modules on Rocky Linux 9 with the command below:

sudo dnf module list php

The output will display the available PHP versions and profiles:

**Output**
Rocky Linux 9 - AppStream
Name    Stream    Profiles                      Summary
php     7.2     common [d], devel, minimal    PHP scripting language
php     7.3     common [d], devel, minimal    PHP scripting language
php     7.4     common [d], devel, minimal    PHP scripting language
Remi's Modular repository for Enterprise Linux 9 - x86_64
Name    Stream    Profiles                      Summary
php     remi-7.2  common [d], devel, minimal    PHP scripting language
php     remi-7.3  common [d], devel, minimal    PHP scripting language
php     remi-7.4  common [d], devel, minimal    PHP scripting language
php     remi-8.0  common [d], devel, minimal    PHP scripting language
php     remi-8.1  common [d], devel, minimal    PHP scripting language

The default module is often PHP 7.2 or 7.4. Reset the default module with the command below:

sudo dnf module reset php

Then, enable the latest PHP module by using the following command:

sudo dnf module enable php:remi-8.1

Now you can install the latest PHP and its dependencies on Rocky Linux 9 with the command below:

sudo dnf install php php-fpm php-curl php-cli php-gd -y

Once the installation is complete, verify your PHP installation by checking its version:

php -v

The output should confirm the installed PHP version:

**Output**
PHP 8.1.8 (cli) (built: Dec 15 2021 02:00:45) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies

Test PHP From Web Server

After you have finished installing the LAMP stack on Rocky Linux 9, you can test your PHP on your web server. To do this, create and open a info.php file in the web server’s document root with your favorite text editor (here we use vi):

sudo vi /var/www/html/info.php

Add the following content to the file:

<?php
phpinfo();
?>

When you are done, save and close the file.

Restart Apache to apply the changes:

sudo systemctl restart httpd

Now, in your web browser, type your server’s IP address followed by /info.php:

http://server-IP/info.php

You should see your PHP information displayed in detail.

php info rocky linux 9

After you have viewed your PHP info, for security reasons, it’s advisable to remove the info.php file with the command below:

sudo rm /var/www/html/info.php

Conclusion

At this point, you have successfully learned how to install LAMP Stack on Rocky Linux 9. The LAMP stack on Rocky Linux 9 provides a versatile and powerful environment for hosting websites and web applications, processing PHP scripts, and managing databases with MySQL/MariaDB. It provides a secure, stable, and open-source foundation for web development.

Hope you enjoyed this guide. Please subscribe to us on Facebook, Instagram, and YouTube.

You may also like these articles:

Install LAMP Stack on AlmaLinux 9

How To Install LAMP Stack on Ubuntu 22.04

LAMP Stack Installation on Ubuntu 24.04

LAMP Stack Setup on Fedora 39

Alternative Solutions for Installing a LAMP Stack on Rocky Linux 9

While the above guide provides a solid method for installing a LAMP stack, here are two alternative approaches you can consider:

1. Using Docker Compose:

Docker Compose allows you to define and manage multi-container Docker applications. This approach provides a highly portable and reproducible environment. Instead of directly installing each component on the host operating system, you create Docker containers for Apache, MariaDB, and PHP, and then use Docker Compose to orchestrate them.

  • Explanation: This method encapsulates each component of the LAMP stack within its own container. This ensures isolation, consistency, and ease of deployment. Docker Compose simplifies the management of these containers, handling networking, volume mounting, and dependency management.

  • Code Example:

First, create a docker-compose.yml file:

version: "3.8"
services:
  db:
    image: mariadb:10.6
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: your_root_password
      MYSQL_DATABASE: your_database_name
      MYSQL_USER: your_database_user
      MYSQL_PASSWORD: your_database_password
    volumes:
      - db_data:/var/lib/mysql

  web:
    image: php:8.1-apache
    restart: always
    ports:
      - "80:80"
    volumes:
      - ./html:/var/www/html
    depends_on:
      - db
    environment:
      PHP_MYSQLI_EXT_HOST: db

volumes:
  db_data:

Create a folder called html in the same directory as the docker-compose.yml file. Put your PHP files in this folder.

To start the LAMP stack, navigate to the directory containing the docker-compose.yml file and run:

docker-compose up -d

This command will download the necessary images, create the containers, and start them in detached mode. You can then access your web application through your server’s IP address.

2. Using a Pre-built Virtual Machine Image (Vagrant):

Vagrant allows you to create and manage virtual development environments. You can use a pre-built Vagrant box (VM image) that already contains a LAMP stack.

  • Explanation: This is the fastest way to get a LAMP stack running. Vagrant provides a consistent environment across different operating systems, ensuring that your development environment closely resembles your production environment.

  • Code Example:

First, install Vagrant and VirtualBox (or another virtualization provider). Then, create a Vagrantfile in an empty directory:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64" # Choose an Ubuntu or other Linux box
  config.vm.network "forwarded_port", guest: 80, host: 8080 # Forward port 80 to host port 8080
  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get install -y apache2 mariadb-server php libapache2-mod-php php-mysql
    # Configure Apache, MariaDB, and PHP as needed
  SHELL
end

Then, run:

vagrant up

This command will download the specified Vagrant box, start the virtual machine, and provision it with the necessary software (Apache, MariaDB, and PHP). You can then access your web application through http://localhost:8080 (or the port you forwarded).

These alternative solutions offer different advantages depending on your specific needs. Docker Compose is ideal for portability and consistency, while Vagrant provides a quick and easy way to set up a development environment. Understanding these options expands your toolkit for managing web development environments on Rocky Linux 9.

Leave a Reply

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