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 -ysudo dnf update -yInstall Nginx
Nginx will serve static files directly and forward PHP requests to PHP-FPM.
sudo apt install -y nginxsudo dnf install -y nginxIf 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 updatecurl -sSL https://packages.sury.org/php/README.txt | sudo bash -xsudo 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 -yInstall 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-zipsudo dnf install -y php php-fpm php-cli php-mysqlnd php-curl php-gd php-mbstring php-xml php-zipCreate 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/publicSet 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 {} \;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/defaultserver {
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;
}
}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 -tCreate 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.phpwww-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 nginxsudo systemctl restart php-fpm
sudo systemctl restart nginx
sudo systemctl enable php-fpm nginxFor 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 reloadsudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reloadVerify 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-pagerVisit 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.phpA 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.phpCommon issues
| Symptom | Cause | Fix |
|---|---|---|
| 502 Bad Gateway | PHP-FPM is not running or the socket path in the server block does not match the pool config | Run 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 error | PHP errors are suppressed in production settings | Temporarily set display_errors = On in php.ini and check /var/log/php8.4-fpm.log |
| PHP files download instead of executing | The location ~ \.php$ block is missing or misconfigured | Confirm the PHP location block exists and sudo nginx -t passes |
| Permission denied in Nginx error log | Web root owned by the wrong user or missing execute bit on a parent directory | Run sudo chown -R www-data:www-data /var/www/example.com and set directories to 755 |
| 502 after server reboot | PHP-FPM not enabled to start on boot | sudo systemctl enable php8.4-fpm |
| Nginx fails to reload | Syntax error in the server block | sudo nginx -t to locate the exact line |
Next steps
- Configure PHP-FPM with Nginx reverse proxy and SSL certificates
- Configure NGINX SSL termination with Certbot for Let's Encrypt certificates
- Configure PHP Composer for dependency management and autoloading
- Optimize PHP-FPM performance with memory tuning and connection pooling for high-traffic websites
- Set up NGINX rate limiting and security headers safely
Running this in production?
Automated install script
Run this to automate the entire setup
#!/usr/bin/env bash
set -euo pipefail
# ------------------------------------------------------------------
# Install and configure Nginx with PHP 8.4
# Supports: Ubuntu, Debian, AlmaLinux, Rocky Linux
# ------------------------------------------------------------------
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log() { echo -e "${GREEN}$*${NC}"; }
warn() { echo -e "${YELLOW}$*${NC}"; }
err() { echo -e "${RED}$*${NC}" >&2; }
usage() {
echo "Usage: $0 -d <domain> [-i <server_ip>]"
echo " -d Domain name (e.g. example.com)"
echo " -i Server IP to bind (optional, informational)"
exit 1
}
DOMAIN=""
SERVER_IP=""
while getopts ":d:i:h" opt; do
case "$opt" in
d) DOMAIN="$opt_arg" ;;
i) SERVER_IP="$opt_arg" ;;
h) usage ;;
*) usage ;;
esac
done
# Manual arg parsing fallback (getopts quirk fix)
while getopts ":d:i:h" opt 2>/dev/null; do :; done
OPTIND=1
while getopts ":d:i:h" opt; do
case "$opt" in
d) DOMAIN="$OPTARG" ;;
i) SERVER_IP="$OPTARG" ;;
h) usage ;;
\?) usage ;;
esac
done
[ -z "$DOMAIN" ] && usage
# ------------------------------------------------------------------
# Prerequisite checks
# ------------------------------------------------------------------
if [ "$(id -u)" -ne 0 ]; then
err "This script must be run as root (use sudo)."
exit 1
fi
if [ ! -f /etc/os-release ]; then
err "Cannot detect OS. /etc/os-release not found."
exit 1
fi
. /etc/os-release
case "$ID" in
ubuntu|debian)
PKG_MGR="apt"
PKG_INSTALL="apt install -y"
WEB_USER="www-data"
PHP_FPM_SOCK="/run/php/php8.4-fpm.sock"
PHP_FPM_SVC="php8.4-fpm"
NGINX_AVAILABLE="/etc/nginx/sites-available"
NGINX_ENABLED="/etc/nginx/sites-enabled"
;;
almalinux|rocky|centos|rhel|ol|fedora)
PKG_MGR="dnf"
PKG_INSTALL="dnf install -y"
WEB_USER="nginx"
PHP_FPM_SOCK="/run/php-fpm/www.sock"
PHP_FPM_SVC="php-fpm"
NGINX_AVAILABLE="/etc/nginx/conf.d"
NGINX_ENABLED="/etc/nginx/conf.d"
;;
amzn)
PKG_MGR="yum"
PKG_INSTALL="yum install -y"
WEB_USER="nginx"
PHP_FPM_SOCK="/run/php-fpm/www.sock"
PHP_FPM_SVC="php-fpm"
NGINX_AVAILABLE="/etc/nginx/conf.d"
NGINX_ENABLED="/etc/nginx/conf.d"
;;
*)
err "Unsupported distro: $ID"
exit 1
;;
esac
WEBROOT="/var/www/${DOMAIN}/public"
SITE_CONF="${NGINX_AVAILABLE}/${DOMAIN}.conf"
# ------------------------------------------------------------------
# Rollback on failure
# ------------------------------------------------------------------
cleanup_on_error() {
err "Installation failed. Rolling back changes..."
rm -f "$SITE_CONF"
[ -L "${NGINX_ENABLED}/${DOMAIN}.conf" ] && rm -f "${NGINX_ENABLED}/${DOMAIN}.conf" || true
exit 1
}
trap cleanup_on_error ERR
TOTAL_STEPS=9
log "[1/${TOTAL_STEPS}] Updating system packages..."
if [ "$PKG_MGR" = "apt" ]; then
apt update && apt upgrade -y
else
$PKG_MGR update -y
fi
log "[2/${TOTAL_STEPS}] Installing Nginx..."
$PKG_INSTALL nginx
log "[3/${TOTAL_STEPS}] Adding PHP 8.4 repository..."
if [ "$PKG_MGR" = "apt" ]; then
if [ "$ID" = "ubuntu" ]; then
$PKG_INSTALL software-properties-common ca-certificates lsb-release apt-transport-https gnupg2
add-apt-repository -y ppa:ondrej/php
apt update
else
# Debian uses Sury repo
$PKG_INSTALL ca-certificates apt-transport-https lsb-release gnupg2 curl
curl -sSL https://packages.sury.org/php/README.txt | bash -x
apt update
fi
else
if [ "$ID" != "fedora" ]; then
$PKG_INSTALL dnf-utils "https://rpms.remirepo.net/enterprise/remi-release-9.rpm"
dnf module reset php -y
dnf module enable php:remi-8.4 -y
else
warn "Fedora detected: PHP 8.4 assumed available in default repos, skipping Remi setup."
fi
fi
log "[4/${TOTAL_STEPS}] Installing PHP 8.4 and PHP-FPM..."
if [ "$PKG_MGR" = "apt" ]; then
$PKG_INSTALL php8.4-fpm php8.4-cli php8.4-mysql php8.4-curl php8.4-gd php8.4-mbstring php8.4-xml php8.4-zip
else
$PKG_INSTALL php php-fpm php-cli php-mysqlnd php-curl php-gd php-mbstring php-xml php-zip
fi
log "[5/${TOTAL_STEPS}] Creating web root directory..."
mkdir -p "$WEBROOT"
log "[6/${TOTAL_STEPS}] Setting ownership and permissions..."
chown -R "${WEB_USER}:${WEB_USER}" "/var/www/${DOMAIN}"
find "/var/www/${DOMAIN}" -type d -exec chmod 755 {} \;
find "/var/www/${DOMAIN}" -type f -exec chmod 644 {} \;
log "[7/${TOTAL_STEPS}] Creating test info.php page..."
cat > "${WEBROOT}/info.php" <<'EOF'
<?php
phpinfo();
EOF
chown "${WEB_USER}:${WEB_USER}" "${WEBROOT}/info.php"
chmod 644 "${WEBROOT}/info.php"
log "[8/${TOTAL_STEPS}] Creating Nginx server block..."
# Detect actual fpm socket location dynamically if possible
if [ "$PKG_MGR" = "apt" ]; then
FASTCGI_INCLUDE="include snippets/fastcgi-php.conf;"
else
FASTCGI_INCLUDE='include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;'
fi
cat > "$SITE_CONF" <<EOF
server {
listen 80;
listen [::]:80;
server_name ${DOMAIN} www.${DOMAIN};
root ${WEBROOT};
index index.php index.html;
location / {
try_files \$uri \$uri/ =404;
}
location ~ \.php\$ {
${FASTCGI_INCLUDE}
fastcgi_pass unix:${PHP_FPM_SOCK};
}
location ~ /\.ht {
deny all;
}
}
EOF
# Enable site (Debian/Ubuntu uses symlink model; RHEL loads conf.d directly)
if [ "$PKG_MGR" = "apt" ]; then
ln -sf "$SITE_CONF" "${NGINX_ENABLED}/${DOMAIN}.conf"
# Remove default site to avoid conflicts on port 80
rm -f /etc/nginx/sites-enabled/default
fi
log "[9/${TOTAL_STEPS}] Configuring firewall, starting services, and verifying..."
# Configure firewall without disabling it
if command -v ufw >/dev/null 2>&1; then
ufw allow 'Nginx Full' || true
elif command -v firewall-cmd >/dev/null 2>&1; then
firewall-cmd --permanent --add-service=http || true
firewall-cmd --permanent --add-service=https || true
firewall-cmd --reload || true
fi
# Test nginx config before reloading
nginx -t
systemctl enable --now nginx
systemctl enable --now "$PHP_FPM_SVC"
systemctl restart nginx
systemctl restart "$PHP_FPM_SVC"
# ------------------------------------------------------------------
# Verification
# ------------------------------------------------------------------
FAILED=0
if ! systemctl is-active --quiet nginx; then
err "Nginx is not running."
FAILED=1
fi
if ! systemctl is-active --quiet "$PHP_FPM_SVC"; then
err "PHP-FPM service ($PHP_FPM_SVC) is not running."
FAILED=1
fi
if [ ! -S "$PHP_FPM_SOCK" ]; then
warn "PHP-FPM socket not found at $PHP_FPM_SOCK. Check your PHP-FPM pool config (listen directive)."
fi
if [ ! -f "${WEBROOT}/info.php" ]; then
err "Test page info.php not found."
FAILED=1
fi
if [ "$FAILED" -eq 1 ]; then
cleanup_on_error
fi
trap - ERR
log "Installation complete!"
log "Visit: http://${DOMAIN}/info.php to verify PHP processing (remove this file afterwards)."
[ -n "$SERVER_IP" ] && log "Server IP provided: ${SERVER_IP}"
warn "Remember to delete info.php in production: rm ${WEBROOT}/info.php"
Review the script before running. Execute with: bash install.sh