How to Install PHP and PHP-FPM on an Ubuntu VPS Print

  • PHP-FPM Nginx, Ubuntu 26.04, install PHP Ubuntu, VPS hosting, PHP Apache, PHP-FPM, PHP extensions, PHP 8.3, PHP Ubuntu VPS, PHP, PHP security, KVM VPS, PHP 8.5, PHP 8.4, Ubuntu 24.04, PHP configuration, Linux VPS, Owned Networks
  • 0

How to Install PHP and PHP-FPM on an Ubuntu VPS

PHP is a widely used server-side programming language that powers WordPress, Laravel, WHMCS, and many other web applications. PHP-FPM (FastCGI Process Manager) provides efficient PHP execution and process management for web servers such as Nginx and Apache.

This guide explains how to install PHP and PHP-FPM on an Ubuntu VPS, configure common extensions, integrate PHP with Nginx or Apache, and apply basic security and performance optimizations.

The instructions apply to Ubuntu 24.04 LTS, Ubuntu 26.04 LTS, and other supported Ubuntu releases. The installed PHP version depends on your Ubuntu release and configured package repositories.

Prerequisites

  • An Ubuntu VPS with root or sudo access.
  • Ubuntu 24.04 LTS, Ubuntu 26.04 LTS, or another supported Ubuntu release.
  • An installed Nginx or Apache web server.
  • An updated operating system.

Need a Linux server? Explore Owned Networks KVM VPS hosting for full root access and flexible hosting resources.

1. Update Ubuntu and Check Available PHP Versions

Update your package repositories and check which PHP version is available:

sudo apt update && sudo apt upgrade -y

apt-cache policy php
apt-cache policy php-fpm

apt-cache search '^php[0-9]+\.[0-9]+-fpm$'

Ubuntu 24.04 LTS provides PHP 8.3 through its official repositories. Other Ubuntu releases may provide newer PHP versions.

For most installations, using the PHP version maintained by your Ubuntu release is recommended.

2. Install PHP and PHP-FPM

Install PHP, PHP-FPM, and commonly used extensions:

sudo apt install -y \
php php-cli php-fpm php-common \
php-mysql php-curl php-gd php-mbstring \
php-xml php-zip php-intl php-bcmath \
php-opcache

Verify the installed PHP version:

php -v

Check the installed PHP-FPM packages and services:

dpkg -l | grep php
systemctl list-unit-files 'php*-fpm.service'

3. Identify Your PHP-FPM Version

PHP-FPM service names and configuration paths include the installed PHP version.

For example:

php8.3-fpm
php8.4-fpm
php8.5-fpm

Identify the installed version:

php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION.PHP_EOL;'

For the remaining commands, set the PHP version variable to the version you installed:

PHP_VERSION=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')

echo "$PHP_VERSION"

Important: If multiple PHP versions are installed, the default PHP CLI version may differ from the PHP-FPM version used by your website. Verify the relevant FPM service and socket before modifying its configuration.

4. Enable and Start PHP-FPM

Enable PHP-FPM and verify that the service is running:

sudo systemctl enable --now "php${PHP_VERSION}-fpm"

sudo systemctl status "php${PHP_VERSION}-fpm" --no-pager

Check the PHP-FPM socket:

ls -l /run/php/

The socket normally follows this naming convention:

/run/php/php8.3-fpm.sock
/run/php/php8.4-fpm.sock
/run/php/php8.5-fpm.sock

5. Configure PHP-FPM with Nginx

Nginx uses FastCGI to communicate with PHP-FPM.

Open your website configuration:

sudo nano /etc/nginx/sites-available/example.com

Inside the existing server block, configure PHP processing:

index index.php index.html;

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

location ~ \.php$ {
    try_files $uri =404;

    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;

    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}

location ~ /\.(?!well-known).* {
    deny all;
}

Replace php8.3-fpm.sock with the socket corresponding to your installed PHP-FPM version.

Note: This configuration is suitable for a basic PHP website. Applications such as Laravel and WordPress may require different routing rules.

Validate and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

6. Configure PHP-FPM with Apache

Apache can also use PHP-FPM through its proxy FastCGI integration.

Enable the required modules:

sudo a2enmod proxy_fcgi setenvif

Enable the PHP-FPM Apache configuration corresponding to your installed version:

sudo a2enconf "php${PHP_VERSION}-fpm"

sudo apache2ctl configtest
sudo systemctl reload apache2

If Apache already uses mod_php, review the existing PHP handler configuration before switching to PHP-FPM. Avoid configuring multiple handlers for the same PHP files.

7. Configure PHP Security and Performance

PHP settings can be adjusted through the PHP-FPM configuration file:

sudo nano "/etc/php/${PHP_VERSION}/fpm/php.ini"

Review and adjust the following directives according to your application's requirements:

expose_php = Off
display_errors = Off
log_errors = On

memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 60

cgi.fix_pathinfo = 0

These are example settings rather than universal requirements. Applications with large uploads or resource-intensive workloads may need different limits.

Restart PHP-FPM after modifying the configuration:

sudo systemctl restart "php${PHP_VERSION}-fpm"

8. Enable and Verify OPcache

OPcache improves PHP performance by caching compiled PHP bytecode, reducing the overhead of repeatedly parsing scripts.

The php-opcache package was installed earlier in this guide.

Verify that OPcache is available:

php -m | grep -i opcache

Review its configuration:

sudo nano "/etc/php/${PHP_VERSION}/fpm/conf.d/10-opcache.ini"

For a typical production website, consider the following settings:

opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.validate_timestamps=1
opcache.revalidate_freq=2

These values are starting points. Adjust OPcache memory and file limits based on application size and available VPS resources.

Restart PHP-FPM:

sudo systemctl restart "php${PHP_VERSION}-fpm"

9. Test PHP Processing

Create a temporary PHP test file inside your website document root:

echo '<?php echo "PHP version: " . PHP_VERSION; ?>' \
| sudo tee /var/www/example.com/html/php-test.php

Replace the example document root with the actual directory configured for your website.

Visit:

https://example.com/php-test.php

If PHP is configured correctly, the page will display the PHP version used by the web server.

Remove the test file immediately after verification:

sudo rm -f /var/www/example.com/html/php-test.php

10. Useful PHP and PHP-FPM Commands

# Check PHP CLI version
php -v

# List installed PHP modules
php -m

# Display PHP configuration files
php --ini

# Check PHP-FPM configuration
sudo "php${PHP_VERSION}-fpm" -t

# Check PHP-FPM service
sudo systemctl status "php${PHP_VERSION}-fpm" --no-pager

# Restart PHP-FPM
sudo systemctl restart "php${PHP_VERSION}-fpm"

# View PHP-FPM logs
sudo journalctl -u "php${PHP_VERSION}-fpm" -n 50 --no-pager

11. Troubleshooting Common PHP-FPM Problems

502 Bad Gateway: Verify that PHP-FPM is running and that Nginx references the correct socket.

sudo systemctl status "php${PHP_VERSION}-fpm"
ls -l /run/php/
sudo tail -n 50 /var/log/nginx/error.log

PHP files download instead of executing: Check the web server's PHP handler configuration. PHP source files must never be served as downloads or plain text.

Missing PHP extensions: Install the extension required by your application and restart PHP-FPM.

sudo apt install php-mbstring php-xml php-curl
sudo systemctl restart "php${PHP_VERSION}-fpm"

PHP version mismatch: The CLI and PHP-FPM versions may differ when multiple PHP versions are installed. Check the active web server configuration and the PHP-FPM socket.

Permission denied: Verify the website directory permissions and the PHP-FPM pool user. Avoid granting unnecessary write permissions to the web server.

Related Ubuntu VPS Tutorials

Deploy PHP Applications with Owned Networks

Host PHP applications, WordPress websites, Laravel projects, and custom web services on an Owned Networks KVM VPS with full root access and flexible server resources.

With a Linux VPS, you can choose your PHP version, configure PHP-FPM pools, install required extensions, and optimize your hosting environment for your applications.

Explore Owned Networks VPS Hosting and deploy your next PHP application today.


Was this answer helpful?

KVM VPS & Dedicated Servers

Deploy a Linux VPS in under 2 minutes — AlmaLinux, Rocky Linux, Ubuntu, Debian. Full root access, KVM virtualization, US and EU locations.

« Back