How to Run Multiple PHP Versions in Docker for Different WordPress Sites

One WordPress site needs PHP 8.1 because an old plugin breaks on anything newer. Another site on the same VPS wants PHP 8.4 for security fixes and speed. On a plain server install, that conflict forces an ugly choice: hold every site back, or risk breaking the old one. Running multiple PHP versions in Docker removes the conflict. Each site gets its own PHP-FPM container with the version it needs, while one Nginx reverse proxy sends traffic to the right container. This guide covers the full setup on one VPS: folder layout, Nginx config, and the checks that prove each site runs its own version.

Why Docker Beats Installing Several PHP Versions on the Host

You can install two PHP versions on one server without Docker. Ubuntu users add the Ondřej Surý PPA, install php8.1-fpm and php8.4-fpm side by side, and point each server block at a different socket. That works, and running two PHP versions with Nginx and PHP-FPM is a solid approach for a server that only hosts a couple of sites.

Docker pulls ahead when the setup grows. The php-fpm docker containers each carry their own PHP version, extensions, config, and dependencies, so updating one site’s PHP never touches another. You can test PHP 8.4 on a staging copy while production stays on 8.1, then switch by changing one image tag. The same Compose file also moves cleanly to a new VPS.

The trade-off is real. Every extra container uses memory, so a 1 GB VPS gets tight once you run three or four sites with separate databases. Debugging also gains a layer: a 502 can sit in Nginx, the container network, or PHP-FPM itself. For two or three sites on a 2 GB VPS, the isolation is usually worth it.

How Multiple PHP Versions in Docker Fit Together

In a docker php-fpm wordpress stack, Nginx acts as the front door and never runs PHP itself. Behind it, each WordPress site gets a PHP-FPM container built from the official image for the version that site needs, for example php:8.1-fpm for the old site and php:8.4-fpm for the new one. A MariaDB container holds the data, and a named Docker network lets Nginx reach each PHP container by name.

When a request for a PHP page arrives, Nginx passes it to the matching container with fastcgi_pass. The container runs the code, talks to the database, and hands the result back. Because the containers share nothing except the network, a crash or a bad extension in one site cannot take the others down. This is the core idea: Nginx routes, PHP-FPM executes, and each site stays sealed off from the rest.

The guide to running multiple WordPress sites on one VPS with Docker covers the shared reverse-proxy layout this setup builds on.

What You Need Before You Start

  • A VPS with at least 2 GB of RAM and Docker Engine plus Docker Compose installed.
  • Two or more domains (or subdomains) pointed at the server with A records.
  • Comfort editing YAML and Nginx config files and running docker compose commands.
  • Backups of every site you plan to move: snapshot the VPS or export the databases before you touch anything live.

Step 1: Create the Project Layout

Keep every site in its own folder under one project directory, each with its own Compose file and site files. Separate files per site make it easier to restart or rebuild one site without touching the rest.

/srv/wordpress/
├── site-a/          # old site, PHP 8.1
│   ├── docker-compose.yml
│   └── html/        # WordPress files
└── site-b/          # new site, PHP 8.4
    ├── docker-compose.yml
    └── html/

Mount the WordPress files as a bind mount so you can edit themes and plugins from the host and back them up normally. The choice between volumes and bind mounts matters for permissions and backups, and the comparison of Docker volumes vs bind mounts for WordPress walks through when each one wins.

Step 2: Write the Compose File for Two PHP Versions

Here is a minimal docker compose wordpress php setup for one site. The PHP service uses the official php:8.1-fpm image, mounts the site files, and joins a shared network so Nginx can reach it. Copy it for the second site and change only the image tag, folder, and container name.

services:
  php81:
    image: php:8.1-fpm
    container_name: site-a-php
    restart: unless-stopped
    volumes:
      - ./html:/var/www/html
    networks:
      - wp-net

  db:
    image: mariadb:11
    container_name: site-a-db
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: change-me
      MYSQL_DATABASE: site_a
      MYSQL_USER: site_a
      MYSQL_PASSWORD: change-me-too
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - wp-net

volumes:
  db-data:

networks:
  wp-net:
    name: wp-net

For the second site, use image php:8.4-fpm with a different container name, database, and folder. Note that the official PHP image ships without the extensions WordPress needs, such as mysqli, gd, and zip. Install them in a small custom Dockerfile built FROM php:8.4-fpm, or use a WordPress image that bundles them. Skipping this step is the most common reason a fresh container shows a blank page or a “missing MySQL extension” error.

Step 3: Point Nginx at the Right PHP-FPM Container

Nginx needs one server block per site. The only part that differs is the fastcgi_pass line, which names the PHP container for that site. Run Nginx in a container on the same network, or on the host.

server {
    listen 80;
    server_name site-a.example.com;
    root /var/www/html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .php$ {
        fastcgi_pass site-a-php:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

For site B, duplicate the block, change server_name and root, and set fastcgi_pass site-b-php:9000. That single line is what gives each site its own PHP version. The walkthrough of running multiple PHP websites on one VPS with Docker and Nginx shows the same routing with full HTTPS and virtual-host examples.

Step 4: Verify Each Site Runs Its Own PHP Version

Confirm both sites report their version. Run this for each container:

docker exec site-a-php php -v
docker exec site-b-php php -v

Then confirm inside WordPress: in each site’s admin area, go to Tools → Site Health → Info → Server and check the PHP version listed. As a second check, drop a phpinfo.php file into each site’s folder and load it in a browser to confirm the versions differ. Delete both files immediately afterwards. A phpinfo page left on a live site hands attackers a full map of your server.

Common Problems and Fixes

  • 502 Bad Gateway: Nginx cannot reach the PHP container. Check the container is running, the fastcgi_pass name matches container_name exactly, and both share the same network.
  • Blank page after moving a site: a missing PHP extension. Check docker logs and install the extension the error names.
  • Uploads fail or files cannot be written: the bind-mounted files belong to a different user than www-data inside the container. Match the UID, or chown the folder to the container’s user.
  • Changes do not show up: OPcache in the PHP container still holds the old files. Restart the PHP container or enable OPcache revalidation while developing.
  • One site works, the other shows the wrong site: two server blocks share a server_name, or the default block catches the request. Give every site a unique server_name.

Keeping PHP Versions Updated Without Breaking Sites

Running multiple PHP versions in Docker means each site upgrades on its own schedule. Pin the minor version in your Compose file: use php:8.1-fpm, not php:8-fpm or php:fpm, so a routine docker compose pull cannot jump a site to a new major version overnight. When a new PHP release arrives, copy the site to a staging container with the new tag, click through the admin area and key pages, and watch the error log. If it passes, change the tag in production and run docker compose up -d. Keep the old image for a day so you can roll back with one command if a plugin misbehaves.

Old versions eventually stop receiving security fixes. PHP 7.4 reached end of life years ago, so keeping a public site on it trades a working plugin for an unpatched server. When one plugin is the only thing holding a site back, fix or replace that plugin instead of freezing the PHP version.

FAQ

Can I run PHP 7.4 and PHP 8.4 side by side? Yes. Docker does not care how far apart the versions are. But PHP 7.4 no longer gets security updates, so treat it as a short-term bridge while you replace whatever needs it, not a permanent setup.

Does each site need its own database container? No. One MariaDB container can hold a separate database per site. Separate database containers give stronger isolation and simpler per-site backups, at the cost of more memory.

How many PHP versions can one VPS run? As many as your RAM allows. Each PHP-FPM container adds memory use, so count the database and Nginx containers too before adding more sites to a small VPS.

Start with two sites, confirm both report the right version, then move the rest over one at a time.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.