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:
http://your-server-ip-here/
OR use FQDN:
http://rhel8.cyberciti.biz/

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

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:

<?php phpinfo();
?>

Save and close the file. Test your PHP. Fire a web browser and type url:
The address you want to visit will be:
http://your-server-ip-here/test.php
OR
http://rhel8.cyberciti.biz/test.php
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

Conclusion

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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Similar Posts