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

ADVERTISEMENTS



Linux OS

Apache web server

MySQL or MariaDB server to store data

PHP for a server-side dynamic web generation programming language

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:

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:

Update your system by running sudo dnf update Install Apache HTTPD in RHEL 8: sudo dnf install httpd Set up MariaDB (MySQL clone) on RHEL 8 sudo dnf install mariadb-server Finally install PHP 7.x to complete a LAMP set up on RHEL 8, run sudo dnf install @php 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 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 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



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/



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



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



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



$ sudo systemctl restart httpd.service

A note about searching and installing additional PHP modules

You must restart httpd service to access PHP:

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 ( ) ; ?> <?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



$ sudo rm /var/www/html/test.php

How do I secure PHP?

If you see the output as above, then your PHP is installed and working correctly. You can delete the file using rm command:

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

– 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

– Main Aapache config file. You can edit this file running command /var/log/httpd/ – Apache error and access log files

– 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.



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