A LAMP stack — Linux, Apache, MariaDB, and PHP — is one of the most widely deployed web server configurations in enterprise environments. RHEL 8 ships with the tools needed to install and configure each component using dnf and its module system. This tutorial walks you through installing and securing a fully functional LAMP stack on RHEL 8, from package installation through a working PHP test page. By the end you will have a production-ready foundation for hosting dynamic web applications.
Prerequisites
- A RHEL 8 server with a non-root sudo user
- Active Red Hat subscription or access to a configured package repository
- Root or sudo privileges
firewalldrunning andSELinuxenforcing
Step 1 — Install Apache
Install the Apache HTTP Server package and enable it to start on boot.
sudo dnf install -y httpd
sudo systemctl enable --now httpd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Verify Apache is running by opening http://your-server-ip in a browser. You should see the default RHEL 8 Apache test page.
Step 2 — Install MariaDB
Install the MariaDB server, enable it, and run the secure installation script to remove anonymous users and set a root password.
sudo dnf install -y mariadb-server
sudo systemctl enable --now mariadb
sudo mysql_secure_installation
Follow the prompts to set a root password, remove anonymous users, disallow remote root login, and remove the test database. Answer Y to all security prompts.
Step 3 — Install PHP 8.0
RHEL 8 uses dnf module streams to manage PHP versions. Enable the PHP 8.0 stream and install the common profile, which includes PHP-FPM and commonly needed extensions.
sudo dnf module reset php
sudo dnf module enable php:8.0 -y
sudo dnf module install php:8.0/common -y
sudo systemctl enable --now php-fpm
Confirm the installed version with php -v.
Step 4 — Configure Apache to Use PHP-FPM
The default PHP module configuration on RHEL 8 already sets up the proxy to PHP-FPM. Verify the configuration file exists and reload Apache.
cat /etc/httpd/conf.d/php.conf
sudo systemctl reload httpd
The php.conf file should contain SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost" for .php files, directing Apache to forward PHP requests to PHP-FPM over a Unix socket.
Step 5 — Configure SELinux for the Web Root
SELinux enforces that files under /var/www/html carry the httpd_sys_content_t context. If you copy files from another location, restore the correct context.
ls -Z /var/www/html
sudo restorecon -Rv /var/www/html
sudo setsebool -P httpd_can_network_connect 1
The boolean httpd_can_network_connect is required if your PHP application makes outbound network connections, such as API calls or database connections to a remote host.
Step 6 — Create and Test a PHP Info Page
Create a simple PHP test page to confirm Apache is passing requests to PHP-FPM correctly.
sudo bash -c 'echo "" > /var/www/html/info.php'
curl http://localhost/info.php | head -30
You should see HTML output beginning with the PHP version banner and configuration table. Once verified, remove the info page to avoid exposing server details: sudo rm /var/www/html/info.php.
Conclusion
You now have a fully operational LAMP stack on RHEL 8 with Apache serving requests, MariaDB handling data, and PHP 8.0 running through PHP-FPM. SELinux remains enforcing and firewalld is configured to allow only HTTP and HTTPS traffic, giving you a secure starting point for deploying PHP applications.
Next steps: How to Configure SSL/TLS with OpenSSL and Self-Signed Certificates on RHEL 8, How to Harden Nginx: Security Headers, TLS 1.3 and OCSP Stapling on RHEL 8, and How to Monitor Nginx with Prometheus nginx-exporter on RHEL 8.