How to Install the LAMP Stack on CentOS and Rocky Linux

November 14, 2024

Introduction

The LAMP stack is a web development stack consisting of a Linux operating system, an Apache web server, a MySQL (or MariaDB) database, and the PHP programming language. Each layer is an open-source tool that contributes to developing web applications.

This guide will teach you how to install the LAMP stack on CentOS and Rocky Linux.

How to Install the LAMP Stack on CentOS and Rocky Linux

Note: If you are running CentOS 8, refer to LAMP stack CentOS 8 instalation guide.

Prerequisites

  • A user account with sudo or root privileges.
  • A terminal window or command line.

Step 1: Update Package Repository Cache

Before you start building the stack, update the package repository:

sudo yum update

If there are updates, confirm the installation with y and wait for the update to complete.

Note: By default, Rocky Linux uses the DNF package manager. However, the yum command is an alias for dnf in newer Rocky versions. Read more about the differences between DNF vs. YUM.

Step 2: Install the Apache Web Server

Install the Apache web server first. The simplest way is via the package manager:

1. Install Apache on Rocky Linux or CentOS with:

sudo yum install httpd
sudo yum install httpd terminal output

Confirm the installation when prompted.

2. Start Apache with:

sudo systemctl start httpd.service

3. Check whether the service is running by accessing the server's public IP through a web browser.

HTTP server test page Rocky Linux

Alternatively, check via the terminal with:

sudo systemctl status httpd.service
sudo systemctl status httpd.service terminal output

The service shows as active (running).

4. Set up Apache to start at boot:

sudo systemctl enable httpd.service
sudo systemctl enable httpd.service terminal output

The command creates a symlink, and the service automatically starts when rebooted. In case of restarts, the web server also automatically boots.

Step 3: Install MySQL (h)

To organize and store data in a database, install MariaDB or MySQL. The section below shows how to install MariaDB, which is an open-source fork of the MySQL DBMS. It is a backward-compatible and binary drop-in replacement for the original MySQL.

Follow these steps:

1. Install MariaDB with the command:

sudo yum install mariadb-server mariadb
sudo yum install mariadb terminal output

Confirm with y when prompted.

2. Start the MariaDB service:

sudo systemctl start mariadb

The command runs the service and changes its status to active.

Step 4: Run MySQL Security Script

MariaDB does not have secure settings by default. Configure the settings before getting started with the database:

1. Run the MySQL secure installation script:

sudo mysql_secure_installation

2. Since the password is not set yet, press Enter to continue the configuration without a password.

sudo mysql_secure_installation script root password terminal output

3. The script asks a series of questions. To ensure your database is protected, answer the questions as follows:

  • Set root password - press Y. Provide a secure password and re-enter it.
  • Remove anonymous users - press Y.
  • Disallow root login remotely - press Y.
  • Remove the test database and access to it - press Y.
  • Reload privilege tables now - press Y.

4. After answering the questions, the script applies the set configuration.

mysql_secure_script mariadb done terminal output

5. Enable MariaDB:

sudo systemctl enable mariadb.service
sudo systemctl enable mariadb.service terminal output

The command enables MariaDB to start up when the system boots.

Step 5: Install PHP

PHP is a server-side scripting language that is part of the LAMP stack. It processes code for showing content dynamically. Once connected to the database, PHP helps retrieve information from it, processes it, and sends it to Apache.

To install PHP on CentOS or Rocky Linux:

1. Install PHP from the repository with:

sudo yum install php
sudo yum install php terminal output

Confirm the installation with y and wait for it to finish.

2. For Apache to register the installed PHP, restart the service:

sudo systemctl restart httpd.service 

This resets the web server.

Step 6: Test PHP Processing

Apache reads files from the root directory ( /var/www/html/ by default) to locate and serve web pages. Use the nano editor to create a test script for PHP on the CentOS or Rocky server.

1. Create a PHP script using nano:

sudo nano /var/www/html/index.php

2. Copy and paste the following code:

<?php
phpinfo ();
?>
index.php test page code

The function phpinfo (); generates the default PHP info page.

3. Save the changes and close nano.

4. Check whether PHP is working by visiting the page via browser:

http://[IP_address]/index.php
index.php page test browser

The PHP info page shows.

6. If firewalld is enabled, allow HTTP and HTTPS traffic. Use the following commands to open the ports and reload the firewalld service:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
sudo firewalld enable http https terminal output

Alternatively, disable firewalld and use a different firewall to protect the web server.

Step 7: Install PHP Modules

To enhance PHP's capabilities, use optional modules. Search for modules with the command:

yum search php-
yum search php- terminal output

To get detailed, plain-language information about what each module does, use:

yum info [module_name]
yum info php-mysqlnd terminal output

To install an optional package, run:

sudo yum install [module_name]

The command adds the PHP module to the system and is ready to use.

Step 8: Restart Apache

When making changes to the server, the changes only apply once the Apache service is restarted. To restart the Apache service, use:

sudo systemctl restart httpd.service

If there are any new configurations, they apply during the restart.

Conclusion

This guide showed how to install the LAMP stack on CentOS and Rocky Linux. The LAMP stack enables open-source web development on various Linux distributions.

Next, see how to set up and configure ModSecurity on Apache to monitor and secure the server. For an alternate stack, compare MEAN vs. LAMP.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to Install LAMP Stack on CentOS 8
July 19, 2020

The LAMP stack consists of open-source software required for web development. Install and configure the LAMP ...
Read more
How To Install XAMPP On CentOS/RHEL 7.0
September 5, 2019

The XAMPP stack is an open-source Apache distribution of a PHP development environment consisting of ...
Read more
How to Install XAMPP on Ubuntu 18.04
February 22, 2024

The XAMPP stack is an open-source Apache distribution of a PHP development environment consisting of...
Read more
What is a LAMP Stack?
February 7, 2024

The LAMP stack is a set of open source software used for web application development. For a web application ...
Read more