Installing Apache and PHP-FPM on AlmaLinux 9

Linux 5 min read

This guide demonstrates how to install the Apache web server and PHP on an unmanaged VPS running AlmaLinux or Rocky Linux 9 without a control panel. Together with the database server (see: Installing MariaDB on AlmaLinux 9), this provides the so-called LAMP environment (Linux, Apache, MySQL/MariaDB, PHP) required for WordPress and most PHP applications.

If you do not wish to manage the server via the command line, consider using a control panel (see: Which control panel should I choose for my VPS?).

Run the commands as the root user or using sudo.

1. Updating the system

sudo dnf update -y

2. Installing Apache

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

If firewalld is sudo firewall-cmd –state on the server (the output of sudo firewall-cmd --state shows running), open the web ports (see: How do I configure a firewall on my VPS?). On AlmaLinux cloud images, firewalld is not installed by default, so this step can be skipped:

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

At this point, the Apache test page will appear in the browser at the address http://<szerver IP-címe>.

3. Selecting the PHP version

AlmaLinux 9 offers several PHP versions as modules (module stream). The available versions are:

dnf module list php

The default version is 8.0, which is no longer supported by the PHP developers and on which many newer applications will not run. Please select a newer version, such as 8.3 (if it appears in the list):

sudo dnf module reset php -y
sudo dnf module enable php:8.3 -y

If you require a newer version of PHP than those listed, or wish to run multiple PHP versions in parallel, you can use the Remi repository. The options are compared in the article ‘Installing PHP on AlmaLinux 9 and 10’.

4. Installing PHP and common extensions

sudo dnf install -y php php-fpm php-cli php-mysqlnd php-gd php-xml php-mbstring php-intl php-opcache php-pecl-zip

WordPress and most CMSs work with these plugins; the php -m command lists the installed plugins.

On AlmaLinux 9, Apache runs PHP via the PHP-FPM service; this is automatically configured by the php-fpm package (/etc/httpd/conf.d/php.conf). Start it and reload Apache:

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

Verification

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

Open the URL http://<szerver IP-címe>/info.php: on the PHP information page, the ‘Server API’ line should show the value FPM/FastCGI. Once you have checked this, delete the file, as it reveals a lot of information about the server:

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

5. Virtual host for the domain

If you wish to host one or more domain websites on a single server, create a virtual host for each one. Create the website directory:

sudo mkdir -p /var/www/example.hu/public_html
sudo chown -R apache:apache /var/www/example.hu

Then the configuration file:

sudo nano /etc/httpd/conf.d/example.hu.conf
<VirtualHost *:80>
    ServerName example.hu
    ServerAlias www.example.hu
    DocumentRoot /var/www/example.hu/public_html

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

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

AllowOverride All enables the use of .htaccess files, which are required, for example, for WordPress’s clean URLs (see: What is an .htaccess file and what is its purpose?).

Check the configuration, then restart Apache:

sudo apachectl configtest
sudo systemctl reload httpd

Set the domain’s A record (and the www subdomain) to the server’s IP address. Until the DNS has updated, you can also test the site using the curl command (see: Network Troubleshooting).

6. SELinux

SELinux, which is enabled by default in AlmaLinux, handles files under /var/www correctly. If the website needs to write to files (uploads, caching), connect to an external service or send emails, it requires specific permissions, for example:

sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/example.hu/public_html/wp-content(/.*)?"
sudo restorecon -Rv /var/www/example.hu
sudo setsebool -P httpd_can_network_connect on

In detail: SELinux: why am I getting a ‘Permission denied’ error?

7. PHP settings

The general PHP settings are located in the /etc/php.ini file. The most commonly modified values are:

upload_max_filesize = 64M
post_max_size = 64M
memory_limit = 256M
max_execution_time = 120
date.timezone = Europe/Budapest

You can control the number of PHP-FPM processes in the file /etc/php-fpm.d/www.conf using the pm.max_children setting. Adjust this to suit your server’s memory, as setting the value too high may cause a memory shortage (see: Out of memory: OOM killer and swap file).

After making changes, restart PHP-FPM:

sudo systemctl restart php-fpm

8. SSL certificate

If the domain is already pointing to the server, request a free Let’s Encrypt certificate using Certbot: Requesting a Let’s Encrypt certificate with Certbot. Certbot automatically configures the Apache virtual host with HTTPS settings.

Nginx instead of Apache

If you would prefer to use the Nginx web server instead of Apache, the installation process is similar: sudo dnf install nginx, followed by sudo systemctl enable --now nginx php-fpm. The php-fpm package also includes a ready-made PHP configuration for Nginx (/etc/nginx/default.d/php.conf). You can create virtual hosts in the /etc/nginx/conf.d/ directory as server blocks; for PHP, include the line include /etc/nginx/default.d/*.conf; in each one. Nginx does not use ..htaccess files; redirects must be specified within the server block. The latest version of Nginx can be installed from the developers’ own repository; see: Installing Nginx mainline on AlmaLinux.

AlmaLinux 10

There are no modules on AlmaLinux and Rocky Linux 10: step 3 is omitted, and the system installs PHP 8.3. Since AlmaLinux 10.2, PHP 8.4 has been available from the system repository as packages prefixed with php8.4- (e.g. php8.4-fpm); these replace version 8.3. Further details are provided in the article ‘Installing PHP on AlmaLinux 9 and 10’. The remaining steps are the same.

Back: Linux articles (62)