0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
JPPINTO
  • Home
  • Blog
  • Certifications
  • About
  • Contact
  • Shop
  • Gallery
  • Current Setup
Contact

Search

July 3, 2026 / Linux, Servers, Ubuntu

Install PHP-FPM and Common PHP Extensions on Ubuntu

Tags: linux, php, php-fpm, server setup, ubuntu, wordpress
Featured image for Install PHP-FPM and Common PHP Extensions on Ubuntu

PHP-FPM is the usual way to run PHP behind Nginx on Ubuntu. It gives you a long-running PHP process manager instead of starting PHP from scratch for every request.

This guide walks through installing PHP-FPM, adding common PHP extensions, detecting the installed PHP version, tuning php.ini, and restarting the right PHP-FPM service.

What the Script Does

The deployment script this article is based on:

  • Installs PHP, PHP-FPM, PHP CLI, and common extensions.
  • Detects the installed PHP major/minor version.
  • Finds the matching PHP-FPM service name.
  • Backs up the PHP-FPM and CLI php.ini files.
  • Updates upload, post, memory, and execution-time limits.
  • Enables and restarts PHP-FPM.

Install PHP and Extensions

Run:

sudo apt update

sudo apt install -y \
    php \
    php-fpm \
    php-cli \
    php-common \
    php-mysql \
    php-curl \
    php-gd \
    php-imagick \
    php-mbstring \
    php-xml \
    php-zip \
    php-intl \
    php-bcmath \
    php-redis \
    php-soap \
    php-readline \
    php-bz2 \
    php-gmp \
    php-ldap \
    php-sqlite3 \
    php-tidy \
    php-dev

Those extensions cover many common WordPress and PHP application needs, including MySQL, image handling, XML, ZIP files, Redis, SOAP, and multibyte strings.

Detect the PHP Version

Ubuntu package names include the PHP version in the FPM service name.

Detect it with:

PHP_VERSION="$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')"
PHP_FPM_SERVICE="php${PHP_VERSION}-fpm"
PHP_FPM_INI="/etc/php/${PHP_VERSION}/fpm/php.ini"
PHP_CLI_INI="/etc/php/${PHP_VERSION}/cli/php.ini"

echo "PHP version: ${PHP_VERSION}"
echo "PHP-FPM service: ${PHP_FPM_SERVICE}"
echo "PHP-FPM ini: ${PHP_FPM_INI}"
echo "PHP CLI ini: ${PHP_CLI_INI}"

Example output:

PHP version: 8.3
PHP-FPM service: php8.3-fpm
PHP-FPM ini: /etc/php/8.3/fpm/php.ini
PHP CLI ini: /etc/php/8.3/cli/php.ini

Back Up PHP Config Files

Create a backup folder:

sudo mkdir -p /opt/server-backups/php
timestamp="$(date +%Y%m%d%H%M%S)"

Back up both PHP-FPM and PHP CLI settings:

sudo cp "${PHP_FPM_INI}" "/opt/server-backups/php/php.ini.fpm.before.${timestamp}.bak"
sudo cp "${PHP_CLI_INI}" "/opt/server-backups/php/php.ini.cli.before.${timestamp}.bak"

Tune PHP-FPM

This example sets:

  • upload_max_filesize = 64M
  • post_max_size = 64M
  • memory_limit = 256M
  • max_execution_time = 300

Apply them to the PHP-FPM php.ini:

sudo sed -i "s/^upload_max_filesize = .*/upload_max_filesize = 64M/" "${PHP_FPM_INI}"
sudo sed -i "s/^post_max_size = .*/post_max_size = 64M/" "${PHP_FPM_INI}"
sudo sed -i "s/^memory_limit = .*/memory_limit = 256M/" "${PHP_FPM_INI}"
sudo sed -i "s/^max_execution_time = .*/max_execution_time = 300/" "${PHP_FPM_INI}"

For CLI PHP, a common setup is to keep memory aligned but allow long-running command-line jobs:

sudo sed -i "s/^memory_limit = .*/memory_limit = 256M/" "${PHP_CLI_INI}"
sudo sed -i "s/^max_execution_time = .*/max_execution_time = 0/" "${PHP_CLI_INI}"

Restart PHP-FPM

Enable and restart the detected service:

sudo systemctl enable "${PHP_FPM_SERVICE}"
sudo systemctl restart "${PHP_FPM_SERVICE}"
sudo systemctl is-active --quiet "${PHP_FPM_SERVICE}"

Save after-change backups:

sudo cp "${PHP_FPM_INI}" "/opt/server-backups/php/php.ini.fpm.after.${timestamp}.bak"
sudo cp "${PHP_CLI_INI}" "/opt/server-backups/php/php.ini.cli.after.${timestamp}.bak"

Full Script

Here is the full Bash script used for this PHP-FPM and extension install workflow:

#!/usr/bin/env bash

__show_script_usage() {
  cat <<'__SCRIPT_USAGE__'
# Install-PHP.sh

Installs PHP-FPM and required PHP extensions, then applies common PHP limits.

## Example Usage

```bash
cd /opt/DevOps/Scripts
sudo bash ./Install-PHP.sh
```

## Notes

This modifies PHP CLI/FPM `php.ini` files, writes backups under `/opt/DevOps/Backups/PHP`, and restarts PHP-FPM.
__SCRIPT_USAGE__
}

if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
  case "${1:-}" in
    -h|--help|--usage)
      __show_script_usage
      exit 0
      ;;
  esac
fi

# ============================================================
# Variables
# ============================================================

DEVOPS_BACKUP_DIR="/opt/DevOps/Backups/PHP"

PHP_UPLOAD_MAX_FILESIZE="64M"
PHP_POST_MAX_SIZE="64M"
PHP_MEMORY_LIMIT="256M"
PHP_MAX_EXECUTION_TIME="300"

# ============================================================
# Safety
# ============================================================

set -euo pipefail

echo "Installing PHP and useful extensions..."

if [ "$EUID" -ne 0 ]; then
    echo "ERROR: Run with sudo."
    exit 1
fi

# ============================================================
# Install PHP
# ============================================================

echo "Updating package lists..."
apt update

echo "Installing PHP-FPM, PHP CLI, and useful PHP extensions..."

apt install -y \
    php \
    php-fpm \
    php-cli \
    php-common \
    php-mysql \
    php-curl \
    php-gd \
    php-imagick \
    php-mbstring \
    php-xml \
    php-zip \
    php-intl \
    php-bcmath \
    php-redis \
    php-soap \
    php-readline \
    php-bz2 \
    php-gmp \
    php-ldap \
    php-sqlite3 \
    php-tidy \
    php-dev

# ============================================================
# Detect PHP Version
# ============================================================

PHP_VERSION="$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')"
PHP_FPM_SERVICE="php${PHP_VERSION}-fpm"
PHP_FPM_INI="/etc/php/${PHP_VERSION}/fpm/php.ini"
PHP_CLI_INI="/etc/php/${PHP_VERSION}/cli/php.ini"

echo "Detected PHP version: ${PHP_VERSION}"
echo "Detected PHP-FPM service: ${PHP_FPM_SERVICE}"

# ============================================================
# Backup Config
# ============================================================

echo "Creating PHP backup directory..."
mkdir -p "${DEVOPS_BACKUP_DIR}"

TIMESTAMP="$(date +%Y%m%d%H%M%S)"

if [ -f "${PHP_FPM_INI}" ]; then
    cp "${PHP_FPM_INI}" "${DEVOPS_BACKUP_DIR}/php.ini.fpm.before.${TIMESTAMP}.bak"
fi

if [ -f "${PHP_CLI_INI}" ]; then
    cp "${PHP_CLI_INI}" "${DEVOPS_BACKUP_DIR}/php.ini.cli.before.${TIMESTAMP}.bak"
fi

# ============================================================
# Configure PHP-FPM
# ============================================================

echo "Configuring PHP-FPM php.ini..."

sed -i "s/^upload_max_filesize = .*/upload_max_filesize = ${PHP_UPLOAD_MAX_FILESIZE}/" "${PHP_FPM_INI}"
sed -i "s/^post_max_size = .*/post_max_size = ${PHP_POST_MAX_SIZE}/" "${PHP_FPM_INI}"
sed -i "s/^memory_limit = .*/memory_limit = ${PHP_MEMORY_LIMIT}/" "${PHP_FPM_INI}"
sed -i "s/^max_execution_time = .*/max_execution_time = ${PHP_MAX_EXECUTION_TIME}/" "${PHP_FPM_INI}"

# ============================================================
# Configure PHP CLI
# ============================================================

echo "Configuring PHP CLI php.ini..."

sed -i "s/^memory_limit = .*/memory_limit = ${PHP_MEMORY_LIMIT}/" "${PHP_CLI_INI}"
sed -i "s/^max_execution_time = .*/max_execution_time = 0/" "${PHP_CLI_INI}"

# ============================================================
# Backup After Config
# ============================================================

cp "${PHP_FPM_INI}" "${DEVOPS_BACKUP_DIR}/php.ini.fpm.after.${TIMESTAMP}.bak"
cp "${PHP_CLI_INI}" "${DEVOPS_BACKUP_DIR}/php.ini.cli.after.${TIMESTAMP}.bak"

# ============================================================
# Restart PHP-FPM
# ============================================================

echo "Enabling PHP-FPM..."
systemctl enable "${PHP_FPM_SERVICE}"

echo "Restarting PHP-FPM..."
systemctl restart "${PHP_FPM_SERVICE}"

echo "Checking PHP-FPM status..."
systemctl is-active --quiet "${PHP_FPM_SERVICE}"

# ============================================================
# Complete
# ============================================================

echo "PHP install complete."
echo "PHP version: ${PHP_VERSION}"
echo "PHP-FPM service: ${PHP_FPM_SERVICE}"
echo "PHP-FPM ini: ${PHP_FPM_INI}"
echo "PHP CLI ini: ${PHP_CLI_INI}"

Troubleshooting

If PHP-FPM does not restart, check:

sudo systemctl status "${PHP_FPM_SERVICE}"
sudo journalctl -u "${PHP_FPM_SERVICE}" -xe

If Nginx returns 502 Bad Gateway, confirm that the Nginx site config points to the correct PHP-FPM socket for your PHP version.

Quick Reference

PHP_VERSION="$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')"
PHP_FPM_SERVICE="php${PHP_VERSION}-fpm"
sudo systemctl restart "${PHP_FPM_SERVICE}"
sudo systemctl status "${PHP_FPM_SERVICE}"

The important habit is to detect the installed PHP version instead of hard-coding one. That makes the script work across different Ubuntu releases.

Post Views: 163
<- Install and Tune Nginx on Ubuntu for Web Hosting
Install and Configure MySQL on Ubuntu for WordPress ->

Categories

  • Active Directory (5)
  • AI (3)
  • Amazon Cloud Services (1)
  • AWS (2)
  • Blazor (1)
  • C# (C-Sharp) (3)
  • CI/CD Pipelines (1)
  • Cloud (1)
  • Cloudflare (2)
  • Codex (1)
  • Containers (4)
  • Deployment (2)
  • Development (5)
  • DNS (1)
  • Docker (3)
  • Email (1)
  • Family (1)
  • General (5)
  • IIS 6.0 (4)
  • IIS 7.0 (10)
  • IIS 8.0 (1)
  • Infrastructure as Code (IaC) (1)
  • Kubernetes (3)
  • Linux (9)
  • Microsoft 365 (2)
  • MySQL (1)
  • Office 2010 (1)
  • PHP (1)
  • PowerShell (11)
  • Productivity (1)
  • Security (1)
  • Servers (9)
  • SharePoint 2007 (8)
  • SharePoint 2010 (19)
  • SharePoint 2013 (2)
  • SharePoint Online (1)
  • SMTP (4)
  • SQL Server 2008 (1)
  • SQL Server 2008 R2 (1)
  • SQL Server 2012 (2)
  • SQL Server 2019 (1)
  • SSL (1)
  • Travel (1)
  • Troubleshooting (1)
  • Ubuntu (9)
  • Uncategorized (1)
  • URL Rewrite (2)
  • Visual Studio 2019 (1)
  • Visual Studio Code (1)
  • Windows 10 (7)
  • Windows 2003 (9)
  • Windows 2008 (18)
  • Windows 2012 (6)
  • Windows 7 (3)
  • Windows Firewall (1)
  • Windows Vista (1)
  • WordPress (3)
  • WP-CLI (3)

Recent Posts

  • GPT-5.6 Sol Changes How We Prompt—and How We Write AGENTS.md
  • Protect a Domain That Does Not Send Email with SPF, DMARC, and Null MX
  • Turning Our Atlanta Vacation Photos into a Video with PowerShell
  • Bulk Create Cloudflare Origin CA Certificates with PowerShell
  • Test a Cloudflare Global API Key Connection with PowerShell

Advertisement

Tags

agents.md ai coding agents aws bash cloudflare cloud storage codex context engineering developer workflow dev to production dns externalize blob externalize sharepoint data full installation http redirect https IIS IIS installation index server configuration installing cumulative updates linux load balance central administration microsoft 365 nginx powerpoint powershell redirect http to https s3 server setup sharepoint 2010 cumulative updates sharepoint 2010 farm build sharepoint 2010 farm configuration sharepoint 2010 farm installation sharepoint data externalization SMTP ssl storagepoint ubuntu web server windows Windows 7 windows server 2008 wordpress wp-cli x86
© 2026 JPPinto.com. All rights reserved.