How To Install Linux, Apache, MySQL, PHP (LAMP) stack On RHEL 8

How do I set up a LAMP stack on RHEL 8 cloud server or VPS or bare metal server? How can I install Linux, Apache, MySQL/MariaDB, PHP (LAMP) stack On RHEL (Red Hat Enterprise Linux) 8?

Introduction – A LAMP stack is nothing but a group of popular software installed to build and run dynamic websites and apps. Typical examples of LAMP includes WordPress based blog, Wiki coded in Mediawiki, or custom made e-commerce site. LAMP is an acronym for the:

  • Linux OS
  • Apache web server
  • MySQL or MariaDB server to store data
  • PHP for a server-side dynamic web generation programming language

This page shows how to install and set up a LAMP stack on RHEL 8 using the CLI.

You can use either yum command or dnf command to install packages on RHEL 8. The yum is nothing but a soft link to dnf for backward compatibility reasons.

How to set up and install Linux, Apache, MySQL, PHP (LAMP) stack On RHEL 8

The procedure to set up a LAMP stack on a Red Hat Enterprise Linux:

  1. Update your system by running sudo dnf update
  2. Install Apache HTTPD in RHEL 8: sudo dnf install httpd
  3. Set up MariaDB (MySQL clone) on RHEL 8 sudo dnf install mariadb-server
  4. Finally install PHP 7.x to complete a LAMP set up on RHEL 8, run sudo dnf install @php
  5. Test your LAMP setup

Let us see all commands in details.

Step 1 – Update RHEL 8 box

Simply run the following dnf command:
$ sudo dnf update

Step 2 – Install Apache (HTTPD) on RHEL 8

Again use the dnf command:
$ sudo dnf install httpd

How to install HTTPD on RHEL 8

Installing httpd on RHEL 8

How to enable httpd service

he httpd.service disabled by default. To start the httpd service at boot time, run the following systemctl command:
sudo systemctl enable httpd.service
In the default configuration, the httpd daemon will accept connections on port 80 (and, if mod_ssl is installed, TLS connections on port 443) for any configured IPv4 or IPv6 address.

Command to start/stop/restart httpd

sudo systemctl start httpd.service ## <- Start Apache ##
sudo systemctl stop httpd.service ## <- Stop Apache ##
sudo systemctl restart httpd.service ## <- Restart Apache ##
sudo systemctl reload httpd.service ## <- Reload Apache ##
sudo systemctl status httpd.service ## <- Get status of Apache ##

Verify that port 80 open by running the ss command along with grep command:
$ sudo ss -tulpn
$ sudo ss -tulpn | grep :80

How to enable and start HTTPD service on RHEL 8

httpd.service is systemd unit files that controls Apache on RHEL 8

How to open port 80 using firewalld

By default firewalld on RHEL 8 would block access to HTTP TCP port # 80.To open HTTP port 80 on a RHEL 8, run:
$ sudo firewall-cmd --permanent --add-service=http --zone=public
$ sudo firewall-cmd --reload
$ sudo firewall-cmd --list-services --zone=public

How to open Apache port 80 using firewalld command on RHEL 8

Open Apache TCP port # 80 using the firewalld command

Test it

Use the ip command to find out your public IP address on a RHEL 8:
ip a
ip a s ens3
ip a s eth0
ip a s ens3 | grep -w inet | awk '{ print $2}'

Fire a web browser and type url:
OR use FQDN:

The default RHEL 8 Apache web page

The default RHEL 8 Apache web page indicates that our Apache installation completed

Step 3 – Install MariaDB on RHEL 8

Our Apache is running. It is time to install MariaDB which acts as a drop replacement for Oracle MySQL server. Type the following dnf command:
$ sudo dnf install mariadb-server

Installing MariaDB on RHEL 8 using the dnf/yum command

Installing MariaDB on RHEL 8 using the dnf/yum command

Enable and start/stop/restart MariaDB service

Turn on MariDB server at boot time, run:
$ sudo systemctl enable mariadb.service
To stop/stop/restart MariDB service use the following systemctl command:
sudo systemctl start mariadb.service ## <- Start MariaDB server ##
sudo systemctl stop mariadb.service ## <- STOP MariaDB server ##
sudo systemctl restart mariadb.service ## <- RESTART MariaDB server ##
sudo systemctl status mariadb.service ## <- Get status of MariaDB server ##

Finally secure you MariaDB server, run:
$ sudo mysql_secure_installation

Securing MariaDB server on RHEL 8

Securing MariaDB server on RHEL 8

Step 4 – Install PHP 7.x on RHEL 8

Now your Apache server and database systems are up and running. It is time to install the final piece of the puzzle. Type the following dnf command to install PHP 7.2 along with popular PHP modules to access MySQL from PHP, graphics and more:
$ sudo dnf install php php-mysqlnd php-mbstring php-opcache php-gd

How to install PHP 7.2 on RHEL 8

Installing PHP 7.2 and modules on RHEL 8

You must restart httpd service to access PHP:
$ sudo systemctl restart httpd.service

A note about searching and installing additional PHP modules

PHP comes with many modules. You need to install them as per your needs. You can search for modules using the following syntax:
$ sudo dnf search php-
$ sudo dnf search php- | grep -i mysql

Find info about php-mbstring, run
$ dnf info {php_module_package_name_here}
$ sudo dnf info php-mbstring

Install php-mbstring, run:
$ sudo dnf install php-mbstring

Test your PHP installation

Create a file named test.php in /var/www/html/ directory:
$ sudo vi /var/www/html/test.php
Append the following php code:


Save and close the file. Test your PHP. Fire a web browser and type url:
The address you want to visit will be:
Testing PHP 7.2 on RHEL 8 LAMP setup
If you see the output as above, then your PHP is installed and working correctly. You can delete the file using rm command:
$ sudo rm /var/www/html/test.php

How do I secure PHP?

See my “Linux: 25 PHP Security Best Practices For Sys Admins” for more info.

How to see log files for my Apache server

Use the cat command/tail command as follows to Apache/httpd logs on RHEL 8:
ls -l /var/log/httpd/
sudo tail -f /var/log/httpd/access_log
sudo grep 'foo' /var/log/httpd/error_log

How do I configure HTTPD further?

All Apache configuration files are as follows:

  • /etc/httpd/ – Main Apache config directory
  • /etc/httpd/conf/httpd.conf – Main Aapache config file. You can edit this file running sudo vi /etc/httpd/conf/httpd.conf command
  • /var/log/httpd/ – Apache error and access log files
  • /etc/httpd/conf.modules.d – Apache modules (such as proxy, php and more) config directory


That is all for now. You successfully set up a LAMP server on RHEL 8. Next time you will learn about securing Apache server using TLS/SSL. For Apache config see this page.

This entry is 1 of 3 in the RHEL 8 LAMP Tutorial series. Keep reading the rest of the series:
  1. How To Install Linux, Apache, MySQL, PHP (LAMP) stack On RHEL 8
  2. How to secure Apache with Let's Encrypt Certificates on RHEL 8
  3. How To Install MariaDB on RHEL 8

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum