Install LAMP (Linux, Apache, MariaDB, PHP) on CentOS 8 / RHEL 8

LAMP stands for Linux, Apache, MySQL and PHP. Most of the websites used LAMP stack and it’s very easy to setup. In this lesson, I’m going to install LAMP on CentOS 8 server. The commands are the same for RHEL 8.

Table of Contents

  1. Install Apache Web Server
  2. Install MariaDB Server
  3. Install Latest PHP
  4. Check PHP Info

Step 1 : Install Apache Web Server

On CentOS / RHEL 8, we will use the dnf command to install any package. Before installing Apache, let’s upgrade our operating system by this command:

sudo yum update

Now, let’s install Apache:

sudo dnf install httpd

We need to enable the Apache server to start after reboot:

sudo systemctl enable httpd

To start the Apache server, run this command:

sudo systemctl start httpd

If firewall is enabled on your server, you need to open HTTP (80) port. Let’s open HTTP and HTTPS (443) ports:

 # open http
sudo firewall-cmd --zone=public --permanent --add-service=http
# open https
sudo firewall-cmd --zone=public --permanent --add-service=https
# reload firewall
sudo firewall-cmd --reload

We’ve installed Apache web server on our server. Let’s print a welcome message. Just run this command:

echo "Welcome to CentOS/RHEL 8 Server" > /var/www/html/index.html

Open browser and visit the website using the IP address or hostname. Here’s the example http://ip-address/. You’ll see the welcome message.

Step 2 : Install MariaDB Server

We are going to install MariaDB on our server. Run this command to install MariaDB:

sudo dnf install -y mariadb mariadb-server

We have to enable the MariaDB server to start after each reboot:

sudo systemctl enable mariadb

Start the MariaDB server:

sudo systemctl start mariadb

Let’s check the status of the MariaDB server:

sudo systemctl status mariadb

Here’s the console output:

Let’s secure our MariaDB server. Enter this command:


You’ll be asked some questions. The questions and answers are:

1. Enter current password for root (enter for none):
Ans: just press Enter

2. Set root password? [Y/n]
Ans: Y and then set your password.

3. Remove anonymous users? [Y/n]
Ans: Y

4. Disallow root login remotely? [Y/n]
Ans: Y

5. Remove test database and access to it? [Y/n]
Ans: Y

6. Reload privilege tables now? [Y/n]
Ans: Y

Step 3 : Install Latest PHP

By default, CentOS/RHEL 8 has PHP 7.2 version. We can install by this command:

sudo dnf install -y php php-mysqlnd

To install the latest PHP, we have to add two repositories on our server. The first one is the EPEL repository:

sudo dnf install

The second one is the Remi repository. Mainly we need this one. Remi repo requires EPEL repo.

sudo dnf install

See all available PHP version:

dnf module list php

You’ll find PHP 7.2, 7.3, and 7.4 versions. Let’s install PHP 7.4:

sudo dnf module install php:remi-7.4

Install PHP 7.4 with all necessary modules:

sudo dnf install -y php-cli php-common php-zip php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json php-pdo

Now restart the webserver:

sudo systemctl restart httpd

Step 4 : Check PHP Info

Check the PHP version:

# command
php -v

# output
PHP 7.4.0RC4 (cli) (built: Oct 15 2019 11:28:21) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0-dev, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.0RC4, Copyright (c), by Zend Technologies

Let’s test on the live site. Enter this command to create a PHP file and echo phpinfo() function.

echo "<?php phpinfo()?>" > /var/www/html/info.php

Now visit your website by entering the IP or hostname of your server.


You’ll see a page like this:

The article is over. Thank you. ?

Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.