Install and configure Nginx with PHP 8.4 for beginners

Beginner 25 min Nov 23, 2026
Ubuntu 24.04 Ubuntu 22.04 Debian 12 Debian 11 AlmaLinux 9 Rocky Linux 9 Fedora 41

Learn how to install Nginx and PHP 8.4 with PHP-FPM, configure a server block to process PHP files, and troubleshoot 502 errors and white screens on a fresh Linux server.

Prerequisites

  • A server running Ubuntu, Debian, AlmaLinux, Rocky Linux or Fedora with root or sudo access
  • A registered domain name or a public IP address for testing
  • Basic familiarity with the Linux command line

What this solves

Nginx does not process PHP files on its own. You need PHP-FPM running as a separate service and Nginx configured to pass PHP requests to it over a socket or TCP port.

This tutorial walks through a complete LEMP style setup (Linux, Nginx, MySQL optional, PHP) using PHP 8.4, from installation to a working info.php test page, with the common 502 Bad Gateway and white screen errors explained along the way.

Step-by-step installation

Update your system

Start with an updated package list so you install the latest available versions.

sudo apt update && sudo apt upgrade -y
sudo dnf update -y

Install Nginx

Nginx will serve static files directly and forward PHP requests to PHP-FPM.

sudo apt install -y nginx
sudo dnf install -y nginx

If you need a deeper introduction to virtual hosts, see install and configure Nginx virtual hosts for beginners.

Add the PHP 8.4 repository

Most distributions ship older PHP versions by default. Add a repository that provides PHP 8.4 packages.

sudo apt install -y software-properties-common ca-certificates lsb-release apt-transport-https gnupg2
sudo add-apt-repository -y ppa:ondrej/php
sudo apt update
Note: On Debian, use the Sury repository instead: curl -sSL https://packages.sury.org/php/README.txt | sudo bash -x
sudo dnf install -y dnf-utils https://rpms.remirepo.net/enterprise/remi-release-9.rpm
sudo dnf module reset php -y
sudo dnf module enable php:remi-8.4 -y
Note: On Fedora 41, PHP 8.4 is typically available directly from the default repositories, so the Remi module step can be skipped.

Install PHP 8.4 with PHP-FPM

PHP-FPM is the FastCGI process manager that handles PHP execution outside of Nginx.

sudo apt install -y php8.4-fpm php8.4-cli php8.4-mysql php8.4-curl php8.4-gd php8.4-mbstring php8.4-xml php8.4-zip
sudo dnf install -y php php-fpm php-cli php-mysqlnd php-curl php-gd php-mbstring php-xml php-zip

Create the web root

Use a dedicated directory per site rather than the default Nginx html folder, so multiple sites stay isolated.

sudo mkdir -p /var/www/example.com/public

Set correct ownership and permissions

Nginx and PHP-FPM run as a dedicated web server user (www-data on Debian/Ubuntu, nginx on RHEL based systems). Files must be owned by this user so the web server can read them, and directories need execute permission to be traversed.

Directories should be 755 (owner can read, write, execute, everyone else can read and execute) and files should be 644 (owner can read and write, everyone else can only read). This keeps the web root readable by Nginx without giving write access to other system users.

sudo chown -R www-data:www-data /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod 755 {} \;
sudo find /var/www/example.com -type f -exec chmod 644 {} \;
sudo chown -R nginx:nginx /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod 755 {} \;
sudo find /var/www/example.com -type f -exec chmod 644 {} \;
Never use chmod 777. It gives every user on the system full read, write and execute access to your files. Fix ownership with chown so the web server user actually owns the files, and keep permissions at 755 for directories and 644 for files.

Create a server block for PHP processing

The server block tells Nginx where your site files live and how to forward .php requests to PHP-FPM through its socket.

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    root /var/www/example.com/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.4-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    root /var/www/example.com/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
Note: Confirm the actual socket path with grep listen /etc/php-fpm.d/www.conf, since some packages use 127.0.0.1:9000 instead of a unix socket.

Test the Nginx configuration

Always validate syntax before reloading, to avoid taking down a running site with a typo.

sudo nginx -t

Create a test PHP file

This confirms PHP-FPM is actually executing scripts rather than Nginx serving them as plain text or returning an error.

echo '<?php phpinfo(); ?>' | sudo tee /var/www/example.com/public/info.php
sudo chown www-data:www-data /var/www/example.com/public/info.php
Note: On AlmaLinux, Rocky or Fedora, replace www-data with nginx in the chown command.

Restart and enable the services

Restart both services so the new configuration and PHP-FPM pool take effect, and enable them to survive a reboot.

sudo systemctl restart php8.4-fpm
sudo systemctl restart nginx
sudo systemctl enable php8.4-fpm nginx
sudo systemctl restart php-fpm
sudo systemctl restart nginx
sudo systemctl enable php-fpm nginx

For a broader refresher on managing services, see check and manage running services on Linux with systemctl.

Open the firewall for HTTP traffic

If you use firewalld or ufw, allow port 80 explicitly rather than disabling the firewall.

sudo ufw allow 80/tcp
sudo ufw reload
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

Verify your setup

Check that both services are active before testing in a browser.

sudo systemctl status nginx --no-pager
sudo systemctl status php8.4-fpm --no-pager

Visit http://203.0.113.10/info.php (replace with your server's actual IP or domain). You should see the PHP info page with version 8.4 listed near the top.

curl -I http://localhost/info.php

A response of HTTP/1.1 200 OK with a Content-Type: text/html header confirms PHP is being processed correctly. Once confirmed, delete the test file since phpinfo() exposes server configuration details.

sudo rm /var/www/example.com/public/info.php

Common issues

SymptomCauseFix
502 Bad GatewayPHP-FPM is not running or the socket path in the server block does not match the pool configRun sudo systemctl status php8.4-fpm and compare the socket in fastcgi_pass against /etc/php/8.4/fpm/pool.d/www.conf
White screen with no errorPHP errors are suppressed in production settingsTemporarily set display_errors = On in php.ini and check /var/log/php8.4-fpm.log
PHP files download instead of executingThe location ~ \.php$ block is missing or misconfiguredConfirm the PHP location block exists and sudo nginx -t passes
Permission denied in Nginx error logWeb root owned by the wrong user or missing execute bit on a parent directoryRun sudo chown -R www-data:www-data /var/www/example.com and set directories to 755
502 after server rebootPHP-FPM not enabled to start on bootsudo systemctl enable php8.4-fpm
Nginx fails to reloadSyntax error in the server blocksudo nginx -t to locate the exact line

Next steps

Running this in production?

Want this handled for you? This works for a single server. When you run multiple environments or need this available 24/7, keeping Nginx and PHP-FPM patched, monitored and backed up is a different job. See how we run infrastructure like this for European teams.

Automated install script

Run this to automate the entire setup

#nginx #php #php-fpm #lemp #php8.4 #web server #linux

Don't want to manage this yourself?

We handle infrastructure for businesses that depend on uptime. Fully managed, with one fixed contact who knows your setup.

You get one fixed contact who knows your setup

At their desk in Rotterdam 11:46 · reachable in a message, no ticket form

Learn more: managed cloud infrastructure