Home Rocky LinuxHow to Install Apache and PHP-FPM on Rocky Linux 9 (LAMP Stack)

How to Install Apache and PHP-FPM on Rocky Linux 9 (LAMP Stack)

by Knowledge Base Linux
How to Install Apache and PHP-FPM — How to Install Apache and PHP-FPM on Rocky Linux 9 (LAMP Stack)

This article covers How to Install Apache and PHP-FPM in detail. The classic LAMP stack is still the workhorse behind millions of PHP sites. This tutorial builds it the modern way: Apache with PHP-FPM on Rocky Linux 9. Using PHP-FPM instead of the old mod_php gives you better performance, cleaner process isolation and an easy upgrade path for PHP versions. We will install Apache (httpd), MariaDB and PHP, configure a virtual host that talks to PHP-FPM over a Unix socket, and sort out the SELinux contexts that trip up most first attempts.

Prerequisites for Apache and PHP-FPM on Rocky Linux 9 – How to Install Apache and PHP-FPM

  • A Rocky Linux 9 server with a sudo user — follow the initial server setup first if it is a fresh box.
  • A fully updated system: sudo dnf upgrade -y.

Step 1: Install Apache (httpd) – How to Install Apache and PHP-FPM

sudo dnf install -y httpd
sudo systemctl enable --now httpd
sudo firewall-cmd --permanent --add-service={http,https}
sudo firewall-cmd --reload

Browsing to the server’s IP shows the Rocky Linux test page served by Apache. If you see a connection error instead, work through our guide to configure firewalld on Rocky Linux 9 before continuing.

Step 2: Install MariaDB

sudo dnf install -y mariadb-server
sudo systemctl enable --now mariadb
sudo mysql_secure_installation

Then create a database and a dedicated user for your application — never let a website connect as root:

sudo mysql -e "CREATE DATABASE appdb CHARACTER SET utf8mb4;"
sudo mysql -e "CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'StrongPassword123!';"
sudo mysql -e "GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost'; FLUSH PRIVILEGES;"
PHP application connected to MariaDB in Apache PHP-FPM Rocky Linux 9 setup

Step 3: Install PHP with PHP-FPM on Rocky Linux 9

sudo dnf install -y php php-fpm php-mysqlnd php-cli php-gd php-xml php-mbstring php-opcache
sudo systemctl enable --now php-fpm

Unlike the Nginx setup, the default PHP-FPM pool already runs as the apache user, which is exactly what we want here. The stock configuration in Rocky also ships an Apache snippet (/etc/httpd/conf.d/php.conf) that proxies .php requests to the FPM socket automatically when both packages are installed. Restart Apache to pick it up:

sudo systemctl restart httpd

Step 4: Create a Virtual Host

Create the document root and a virtual host file at /etc/httpd/conf.d/example.com.conf:

sudo mkdir -p /var/www/example.com/html
cat <<'EOF' | sudo tee /etc/httpd/conf.d/example.com.conf
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/html
    ErrorLog /var/log/httpd/example.com-error.log
    CustomLog /var/log/httpd/example.com-access.log combined
    <Directory /var/www/example.com/html>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
EOF
sudo apachectl configtest

If the configuration test prints Syntax OK, reload Apache. The harmless AH00558 warning about a fully qualified domain name disappears once ServerName is set globally.

Step 5: Fix SELinux Contexts for the Document Root

Apache runs under SELinux confinement. Files in unusual locations need the web content label, or you will get a 403:

sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com/html(/.*)?"
sudo restorecon -Rv /var/www/example.com
sudo chown -R apache:apache /var/www/example.com/html

For directories the application must write to (uploads, cache), use httpd_sys_rw_content_t instead. The SELinux on Rocky Linux 9 guide covers these labels in depth.

Step 6: Test PHP and the Database Connection

Create a test script that connects to MariaDB through PHP:

cat <<'EOF' | sudo tee /var/www/example.com/html/test.php
<?php
$conn = new mysqli('localhost', 'appuser', 'StrongPassword123!', 'appdb');
echo $conn->connect_error ? 'FAILED: ' . $conn->connect_error : 'Connected to MariaDB via PHP-FPM';
echo ' | PHP ' . phpversion();
EOF
sudo systemctl reload httpd

Visit http://example.com/test.php. A “Connected” message plus the printed PHP version confirms the full Apache PHP-FPM Rocky Linux 9 stack works. Delete the test file when you are done — it exposes sensitive details.

Troubleshooting

  • PHP source code is displayed or downloaded: Apache is not proxying to FPM. Confirm php.conf exists in /etc/httpd/conf.d/ and that mod_proxy_fcgi is loaded.
  • 403 Forbidden: wrong ownership or SELinux context. Check ausearch -m avc -ts recent before changing permissions blindly.
  • 503 Service Unavailable: PHP-FPM is down or its socket is missing. Check systemctl status php-fpm and ls -l /run/php-fpm/www.sock.

Step 7: Practical PHP Settings and HTTPS

Two finishing touches make the stack production-friendly. First, raise the PHP limits that web applications commonly hit, in /etc/php.ini:

upload_max_filesize = 64M
post_max_size = 64M
memory_limit = 256M
max_execution_time = 120

Restart PHP-FPM after editing. OPcache is already installed from Step 3 and needs no extra work for most sites. Second, put the site behind HTTPS. Certbot is available from EPEL and works with the Apache plugin:

sudo dnf install -y epel-release certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com

Certbot edits the virtual host, installs the certificate and sets up automatic renewal. The same general flow is described in our guide on securing sites with Let’s Encrypt, so the Rocky-based Apache stack ends up with the same TLS quality as any other platform we document.

Conclusion

Your LAMP stack with Apache and PHP-FPM on Rocky Linux 9 is ready for WordPress, Laravel or any classic PHP application. For panel-based hosting on the same platform, compare it with our tutorial on how to install WHM/cPanel on AlmaLinux, and consult the Rocky Linux documentation for production hardening tips.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.