How to Install PHP on Linux (8.x, php-fpm, Extensions and Nginx)

install php linux - custom-phpin-featured.png

Install PHP on Linux is one of the most common hosting searches — usually right before WordPress, Laravel, or a legacy admin panel. Modern stacks use
PHP-FPM behind Nginx or Apache, not embedded mod_php inside every Apache worker.

After PHP is running, pair with Nginx reverse proxy,
MariaDB, and
502 troubleshooting when FPM is down.

Choose PHP-FPM for New Servers

PHP-FPM runs as a separate service with its own worker pool — easier to tune and isolate than mod_php.

php-fpm model
not mod_php

Install on Ubuntu / Debian

Use distribution packages unless you need a bleeding-edge minor version from a third-party repo.

bashUbuntu 22.04+
sudo apt update
sudo apt install -y php-fpm php-cli php-mysql php-curl php-mbstring php-xml
php -v
sudo systemctl enable --now php8.3-fpm  # version may vary
apt install
php-fpm

Install on AlmaLinux / Rocky

Enable AppStream PHP or Remi when you need newer minors; always start php-fpm.

bashAlmaLinux 9
sudo dnf install -y php php-fpm php-mysqlnd php-cli
sudo systemctl enable --now php-fpm
php -v
dnf install
php-fpm unit

php-fpm Pool Basics

Default pool listens on a Unix socket; Nginx must use the same path and socket permissions.

inipool snippet (concept)
; listen = /run/php/php8.3-fpm.sock
; listen.owner = www-data
; listen.group = www-data
pool listen
user/group

Extensions You Actually Need

WordPress wants mysql/mysqli, curl, mbstring, xml, zip. Install only what apps require.

bashlist modules
php -m | grep -E 'mysqli|curl|mbstring|xml'
php -m
extensions

Nginx fastcgi_pass

Typical pattern: try_files then pass .php to the FPM socket.

nginxlocation ~ \.php$
location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
fastcgi
try_files

Verify and Harden

Hit a test page, then remove phpinfo() from production.

WarningNever leave phpinfo() on a public URL — it leaks paths, modules, and environment.
production
no phpinfo

Quick Reference

  • Package: php-fpm + app extensions
  • Nginx: fastcgi_pass unix:...
  • 502 often means php-fpm stopped or wrong socket path

Related tutorials

Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.