Operating System CentOS

How to Build a LAMP Stack on AlmaLinux 9 or Rocky Linux 9

Set up Apache, MariaDB and a current PHP on AlmaLinux 9 or Rocky Linux 9 with dnf module streams instead of compiling from source as on CentOS 6.

This guide sets up Apache, MariaDB and PHP on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 using the distribution's own packages. At the end you will have a web server that runs PHP through PHP-FPM, a secured database server, a virtual host for your domain, and all three services starting at boot.

Older tutorials on this topic compiled Apache 2.4 and PHP from source on CentOS 6, because that release only shipped Apache 2.2 and PHP 5.3. CentOS 6, 7 and 8 are all end-of-life and receive no security fixes. Their replacements are the RHEL 9 rebuilds (AlmaLinux and Rocky Linux) and CentOS Stream 9, which ship Apache 2.4 and offer newer PHP and MariaDB releases as selectable module streams. You get a current stack with normal security updates and no compiler.

Requirements

  • A server running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9.
  • A user with sudo rights (the examples use deploy). Every command below is run as that user with sudo.
  • Optional: a domain name (the examples use example.com) whose DNS record points to the server, for example 203.0.113.10.

Apply pending updates before you start:

sudo dnf update -y

Step 1: Install Apache

The Apache package is called httpd. Install it, then start it and enable it at boot in one command:

sudo dnf install -y httpd
sudo systemctl enable --now httpd

If firewalld is running, open HTTP and HTTPS and reload the rules:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Open http://203.0.113.10/ in a browser (use your own address). You should see the distribution's Apache test page.

Step 2: Install MariaDB

The base repositories carry MariaDB 10.5 as an ordinary package and MariaDB 10.11 as a module stream. Pick one of the two commands. For 10.5:

sudo dnf install -y mariadb-server

For 10.11 (available from the 9.4 minor release onwards):

sudo dnf module install -y mariadb:10.11/server

Start the service, enable it at boot and run the hardening script:

sudo systemctl enable --now mariadb
sudo mysql_secure_installation

Press Enter at the prompt for the current root password (none is set yet), then answer Y to removing anonymous users, disallowing remote root logins, removing the test database and reloading the privilege tables. The MariaDB root account is authenticated through the local Unix socket by default, so the system root user can always log in with sudo mysql and setting a separate root password is optional.

Create a database and a dedicated account for your application instead of using root. Replace the password with one of your own:

sudo mysql -e "CREATE DATABASE appdb CHARACTER SET utf8mb4;"
sudo mysql -e "CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'change-this-password';"
sudo mysql -e "GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';"

Step 3: Install PHP

Without a module stream, dnf install php gives you PHP 8.0, which upstream PHP no longer maintains. List the streams your release offers:

sudo dnf module list php

Enable the newest stream shown (8.3 at the time of writing) and install PHP with the extensions most applications need:

sudo dnf module reset -y php
sudo dnf module enable -y php:8.3
sudo dnf install -y php php-fpm php-cli php-mysqlnd php-gd php-mbstring php-xml php-opcache php-intl

On RHEL 9 and its rebuilds PHP is not loaded into Apache as mod_php. The packages install /etc/httpd/conf.d/php.conf, which hands every .php request to PHP-FPM over the socket /run/php-fpm/www.sock. PHP-FPM is therefore a separate service that must be running:

sudo systemctl enable --now php-fpm
sudo systemctl restart httpd

The PHP-FPM pool is configured in /etc/php-fpm.d/www.conf and PHP itself in /etc/php.ini. Restart php-fpm (not httpd) after changing either file.

Step 4: Test PHP

Check the command-line version, then create a test page in the default document root:

php -v
echo '<?php phpinfo();' | sudo tee /var/www/html/info.php

Open http://203.0.113.10/info.php. The page should show the PHP version you enabled and "FPM/FastCGI" as the Server API. Delete the file afterwards, because it exposes details about your server:

sudo rm /var/www/html/info.php

Step 5: Add a virtual host for your domain

Apache reads every .conf file in /etc/httpd/conf.d/. Create a document root and a configuration file for the site:

sudo mkdir -p /var/www/example.com
sudo chown -R deploy:deploy /var/www/example.com
sudo nano /etc/httpd/conf.d/example.com.conf

Add this content:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com

    <Directory /var/www/example.com>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/httpd/example.com-error.log
    CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>

Check the syntax and reload Apache:

sudo apachectl configtest
sudo systemctl reload httpd

SELinux and writable directories

SELinux is enforcing by default. Files under /var/www are labelled so Apache and PHP-FPM can read them but not write to them. If your application needs a writable directory (uploads, cache), label that directory explicitly rather than disabling SELinux:

sudo dnf install -y policycoreutils-python-utils
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/example.com/uploads(/.*)?"
sudo restorecon -Rv /var/www/example.com/uploads

The directory must also be writable by the apache user, which is the account the default PHP-FPM pool runs as.

Managing and updating the stack

The old service and chkconfig commands and hand-written init scripts are replaced by systemctl:

sudo systemctl status httpd php-fpm mariadb
sudo systemctl restart php-fpm
sudo systemctl reload httpd

Security fixes for all three components arrive with normal system updates (sudo dnf update). Logs are in /var/log/httpd/, /var/log/php-fpm/ and /var/log/mariadb/. If a page shows "503 Service Unavailable" for PHP files only, PHP-FPM is not running; check sudo systemctl status php-fpm.

Further reading: the Rocky Linux PHP guide and the Apache mod_proxy_fcgi documentation.

Frequently asked questions

Is there still a reason to compile Apache and PHP from source?

Rarely. Module streams give you a maintained PHP 8.x and MariaDB 10.11 with security updates delivered by dnf. A source build makes you responsible for tracking every upstream security release and rebuilding by hand, and it bypasses the packaged SELinux and systemd integration.

Can I upgrade a CentOS 6 or 7 server instead of reinstalling?

Plan a migration to a new server. Those releases are several major versions behind, and a fresh AlmaLinux 9 or Rocky Linux 9 install followed by restoring your site files and a database dump is cleaner and safer than attempting to carry an unsupported system forward.

Can I use MySQL instead of MariaDB?

Yes. The repositories also provide a mysql-server package, but MariaDB and MySQL cannot be installed side by side from them. PHP's php-mysqlnd driver works with either, so the rest of this guide is unchanged apart from the service name.

Why does PHP run as a separate service here?

Apache on these systems uses the threaded event processing model, and the PHP packages are built to run through PHP-FPM rather than inside Apache. In practice this means PHP settings and restarts are handled through the php-fpm service, and Apache only proxies requests to it.

Share story:

Continue reading other guides on How VPS.