> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Install Multiple PHP Versions on Ubuntu and Debian

> Install PHP 8.3, 8.4 or 8.5 side by side on Ubuntu or Debian with the ondrej/php repository, run each as its own PHP-FPM service and pick a version per site.

To install multiple PHP versions on Ubuntu, add the `ppa:ondrej/php` repository (on Debian, `packages.sury.org`), then install each version's FPM package, for example `sudo apt install php8.3-fpm php8.4-fpm`. Each version runs as its own service with its own socket, such as `/run/php/php8.4-fpm.sock`. Point each site's `fastcgi_pass` (Nginx) or `SetHandler` (Apache) at the socket for the version it needs.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Intermediate | 15 minutes | Ubuntu 22.04 / 24.04, Debian 12 / 13 (AlmaLinux / Rocky: see below) | sudo, Nginx or Apache |

## Before you start

* Use a user with `sudo`. If you are `root`, leave out `sudo`.
* Take a [backup or snapshot](https://help.freakhosting.com/portals/vps-panel/how-to-schedule-and-restore-backups.md) first. Adding a repository can upgrade the PHP version you already run.
* Supported PHP branches today: 8.2 and 8.3 get security fixes only (until 31 December 2026 and 31 December 2027), 8.4 and 8.5 get full support. Avoid 8.1 and older; they are end-of-life upstream.

Each Ubuntu and Debian release ships one PHP version: 8.3 on Ubuntu 24.04, 8.4 on Debian 13. For other versions, the usual source is the repository run by Ondřej Surý, who maintains PHP packages for Debian. It is a third-party repository, not part of Ubuntu or Debian.

## How to add the PHP repository

> [tab] Ubuntu
>
> ```bash theme={null}
> sudo apt update
> sudo apt install software-properties-common
> sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
> sudo apt update
> ```
>
> The PPA supports Ubuntu LTS releases in standard support (22.04 and 24.04).

> [tab] Debian
>
> ```bash theme={null}
> curl -sSL https://packages.sury.org/php/README.txt | sudo bash -x
> sudo apt update
> ```
>
> The script installs the signing key and adds `/etc/apt/sources.list.d/php.list`. Debian stable and oldstable (13 and 12) are supported.

## How to install a second PHP version

Install the FPM package and the extensions your app needs, with the version in each name:

```bash theme={null}
sudo apt install php8.4-fpm php8.4-mysql php8.4-curl php8.4-gd php8.4-intl php8.4-mbstring php8.4-xml php8.4-zip
```

Repeat with `php8.3-…`, `php8.5-…` and so on. Each version gets its own files:

| Version | Service | Socket | php.ini (FPM) |
| - | - | - | - |
| 8.3 | `php8.3-fpm` | `/run/php/php8.3-fpm.sock` | `/etc/php/8.3/fpm/php.ini` |
| 8.4 | `php8.4-fpm` | `/run/php/php8.4-fpm.sock` | `/etc/php/8.4/fpm/php.ini` |
| 8.5 | `php8.5-fpm` | `/run/php/php8.5-fpm.sock` | `/etc/php/8.5/fpm/php.ini` |

Check that both services run:

```bash theme={null}
systemctl status php8.3-fpm php8.4-fpm
```

## How to choose the PHP version per site

> [tab] Nginx
>
> In each site's server block, set the socket for the version that site needs:
>
> ```nginx theme={null}
> location ~ \.php$ {
>     include snippets/fastcgi-php.conf;
>     fastcgi_pass unix:/run/php/php8.4-fpm.sock;
> }
> ```
>
> ```bash theme={null}
> sudo nginx -t && sudo systemctl reload nginx
> ```

> [tab] Apache
>
> Use PHP-FPM instead of `mod_php`, then set a handler per virtual host:
>
> ```bash theme={null}
> sudo a2enmod proxy_fcgi setenvif
> ```
>
> ```apache theme={null}
> <VirtualHost *:80>
>     ServerName example.com
>     DocumentRoot /var/www/example.com/public_html
>
>     <FilesMatch \.php$>
>         SetHandler "proxy:unix:/run/php/php8.4-fpm.sock|fcgi://localhost"
>     </FilesMatch>
> </VirtualHost>
> ```
>
> ```bash theme={null}
> sudo apache2ctl configtest && sudo systemctl reload apache2
> ```
>
> If `libapache2-mod-php` is installed, disable it (`sudo a2dismod php8.3`) so it does not override the handler.

## How to change the default PHP for the command line

The `php` command (used by Composer, cron jobs and WP-CLI) follows the Debian alternatives system:

```bash theme={null}
sudo update-alternatives --config php
```

Pick a number from the list, or set it directly:

```bash theme={null}
sudo update-alternatives --set php /usr/bin/php8.4
php -v
```

You can also call a version by name, for example `php8.3 artisan migrate`.

## How to check it worked

Put a test file in the site's web root:

```bash theme={null}
echo '<?php echo PHP_VERSION, " ", php_sapi_name(), "\n";' > /var/www/example.com/info.php
curl -H "Host: example.com" http://127.0.0.1/info.php
rm /var/www/example.com/info.php
```

The output shows the version and `fpm-fcgi`, for example `8.4.x fpm-fcgi`.

## How to run several PHP versions on AlmaLinux and Rocky

Use Remi's repository, which installs extra versions as prefixed packages next to the system PHP:

```bash theme={null}
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-10.noarch.rpm
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-10.rpm
sudo crb install
sudo dnf install php85 php85-php-fpm php85-php-mysqlnd
sudo systemctl enable --now php85-php-fpm
```

On release 9, use `epel-release-latest-9` and `remi-release-9`. The PHP 8.5 FPM socket is `/var/opt/remi/php85/run/php-fpm/www.sock` and its configuration lives under `/etc/opt/remi/php85`. Use that socket in `fastcgi_pass`.

## Frequently asked questions

### Will adding the repository upgrade my current PHP?

It can upgrade the patch level and extensions of the version you already have. It does not switch your sites to a new major version; you choose that per site.

### Which extensions do I need for each version?

Extensions are per version. If a site needs `mbstring` on PHP 8.4, install `php8.4-mbstring` even if `php8.3-mbstring` is already there. List loaded modules with `php8.4 -m`.

### How do I remove an old PHP version?

Switch every site away from it first, then run `sudo apt purge 'php8.2*'` and `sudo apt autoremove`.

### Do php.ini changes apply to all versions?

No. Each version has its own `php.ini` and FPM pool. See [increase PHP upload size](https://help.freakhosting.com/portals/other/how-to-increase-php-upload-size-and-memory-limit.md).

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every server guide in one place.

> [card] [Install a LEMP stack](https://help.freakhosting.com/portals/other/how-to-install-a-lemp-stack.md)
>
> Nginx with PHP-FPM.

> [card] [Install a LAMP stack](https://help.freakhosting.com/portals/other/how-to-install-a-lamp-stack.md)
>
> Apache with PHP.

> [card] [Fix 502 Bad Gateway](https://help.freakhosting.com/portals/other/how-to-fix-502-bad-gateway-nginx.md)
>
> Wrong socket name? Start here.

> [card] [Update Linux packages](https://help.freakhosting.com/portals/other/how-to-update-linux-packages.md)
>
> Keep every version patched.

***

> [!NOTE]
>
> **Checked against the official [PHP supported versions page](https://www.php.net/supported-versions.php), the [deb.sury.org FAQ](https://github.com/oerdnj/deb.sury.org/wiki/Frequently-Asked-Questions) and [Remi's PHP 8.5 guides](https://blog.remirepo.net/post/2025/07/04/PHP-8.5-as-Software-Collection)** on 4 November 2026; Nginx and Apache per-site PHP-FPM configuration and `update-alternatives` tested on Ubuntu 24.04 LTS with PHP 8.3. Adding the third-party repositories was not tested. Commands can differ on other distributions or releases.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.