A LAMP stack — Linux, Apache HTTP Server, MySQL (or MariaDB), and PHP — is the original and most widely deployed open-source web hosting stack. It has powered millions of websites since the early 2000s and remains the default for hosting control panels (cPanel, Plesk, DirectAdmin) and many shared hosting environments. Apache’s .htaccess support, mod_rewrite, and per-directory configuration make it the preferred choice for shared hosting and applications that rely on directory-level configuration. This guide covers installing all four LAMP components on RHEL 9, configuring Apache virtual hosts, connecting PHP-FPM via mod_proxy_fcgi, securing MariaDB, and testing the complete stack.
Prerequisites
- RHEL 9 with sudo/root access
- Internet access for package installation
Step 1 — Install Apache
dnf install -y httpd
systemctl enable --now httpd
firewall-cmd --permanent --add-service=http --add-service=https
firewall-cmd --reload
curl -s http://localhost | grep -o "[^<]*"
Step 2 — Install and Secure MariaDB
dnf install -y mariadb-server
systemctl enable --now mariadb
mysql_secure_installation
# Create application database and user
mysql -u root -p <<'SQL'
CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'StrongDBPassword';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
SQL
Step 3 — Install PHP 8.3
dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm
dnf module enable -y php:remi-8.3
dnf install -y php php-fpm php-mysqlnd php-xml php-mbstring php-gd php-opcache php-json php-intl php-zip
php -v
systemctl enable --now php-fpm
Step 4 — Configure PHP-FPM for Apache
# /etc/php-fpm.d/www.conf — set user/group to match Apache
[www]
user = apache
group = apache
listen = /run/php-fpm/www.sock
listen.owner = apache
listen.group = apache
listen.mode = 0660
systemctl restart php-fpm
Step 5 — Enable mod_proxy_fcgi and Configure Virtual Host
# Enable proxy modules
dnf install -y mod_proxy_fcgi
# /etc/httpd/conf.d/example.com.conf
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
# Route PHP requests to PHP-FPM via unix socket
SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost/"
DirectoryIndex index.php index.html
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
mkdir -p /var/www/example.com
chown -R apache:apache /var/www/example.com
apachectl configtest && systemctl reload httpd
Step 6 — Test the LAMP Stack
# PHP info page
echo '' > /var/www/example.com/info.php
curl http://example.com/info.php | grep -o "PHP Version [0-9.]*"
# Database connectivity
cat > /var/www/example.com/dbtest.php <<'PHP'
<?php
$db = new PDO('mysql:host=localhost;dbname=myapp', 'myapp_user', 'StrongDBPassword');
echo "DB connection: OKn";
PHP
curl http://example.com/dbtest.php
rm -f /var/www/example.com/info.php /var/www/example.com/dbtest.php
Conclusion
The LAMP stack on RHEL 9 provides a proven PHP hosting environment with Apache’s extensive module ecosystem, MariaDB as a drop-in MySQL replacement, and PHP 8.3 via PHP-FPM over a Unix socket for efficient process management. Apache’s .htaccess support and mod_rewrite make it ideal for WordPress, Drupal, Joomla, and any application that relies on directory-level rewrite rules.
Next steps: How to Configure SSL/TLS with OpenSSL and Self-Signed Certificates on RHEL 9, How to Secure Apache with Let’s Encrypt and Certbot on RHEL 9, and How to Set Up a LEMP Stack on RHEL 9.