Featured image of post Running Nginx and Multiple PHP-FPM Versions with Docker

Running Nginx and Multiple PHP-FPM Versions with Docker

Learn how to configure Docker Nginx with multiple PHP-FPM versions, manage port mappings, enable local HTTPS access, and troubleshoot common connection errors.

Running Nginx and Multiple PHP-FPM Versions with Docker

When maintaining multiple PHP projects, different applications may require different PHP versions. For example, one project may depend on PHP 7.1, another on PHP 8.1, and a newer application on PHP 8.3.

Running multiple PHP-FPM containers with Docker is a practical solution. However, when all containers use the default PHP-FPM port 9000, configuring Nginx to communicate with the correct container requires some consideration.

This guide explains how to configure Nginx and multiple PHP-FPM containers, how to access them through local domains, and how to troubleshoot common connection issues.

1. Understanding the Port Conflict

Suppose you have three PHP-FPM containers:

  • PHP 7.1
  • PHP 8.1
  • PHP 8.3

All three containers listen on port 9000 internally.

This is completely normal because each container has its own network environment. The ports do not conflict inside Docker.

However, if you want the host machine’s Nginx to communicate with these containers, you must map their internal ports to different host ports.

Configure Docker Port Mapping

For example, using Docker Compose:

services:
  php71:
    image: php:7.1-fpm-alpine
    ports:
      - "127.0.0.1:9071:9000"
    volumes:
      - /var/www/html:/var/www/html

  php81:
    image: php:8.1-fpm-alpine
    ports:
      - "127.0.0.1:9081:9000"
    volumes:
      - /var/www/html:/var/www/html

  php83:
    image: php:8.3-fpm-alpine
    ports:
      - "127.0.0.1:9083:9000"
    volumes:
      - /var/www/html:/var/www/html

The mapping format is:

HOST_IP:HOST_PORT:CONTAINER_PORT

For example:

127.0.0.1:9081:9000

This means that connections to 127.0.0.1:9081 on the host are forwarded to port 9000 inside the PHP 8.1 container.

Security consideration:

Always consider binding PHP-FPM ports to 127.0.0.1 when they only need to be accessed from the host machine.

Avoid exposing PHP-FPM ports directly to public network interfaces unnecessarily.

2. Configure Host Nginx to Connect to PHP-FPM

Once the port mappings are configured, the host Nginx can communicate with the corresponding PHP-FPM container.

For example, to use PHP 8.1:

server {
    listen 80;
    server_name example.com;

    root /var/www/html/your_project/public;
    index index.php index.html;

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

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9081;
        fastcgi_index index.php;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

The important configuration is:

fastcgi_pass 127.0.0.1:9081;

You can switch between PHP versions simply by changing the target port.

For example:

PHP VersionHost PortContainer Port
PHP 7.190719000
PHP 8.190819000
PHP 8.390839000

Important: Keep File Paths Consistent

Nginx and PHP-FPM must be able to access the same project files using compatible paths.

For example, if the host directory is:

/var/www/html

And the container mounts it as:

/var/www/html

You can use:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

However, if the host directory is:

/home/user/project

But the container mounts it as:

/var/www/html

You must provide the path as seen inside the PHP container:

fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;

Otherwise, PHP-FPM may return a File not found error.

3. A Better Architecture: Run Nginx Inside Docker

Although exposing different PHP-FPM ports to the host works, there is a cleaner approach.

Instead of using host Nginx, you can run Nginx inside Docker and connect it to all PHP-FPM containers through a shared Docker network.

This approach has several advantages:

  • No need to expose PHP-FPM ports to the host.
  • No need to manage different host ports for PHP versions.
  • Containers communicate using service names.
  • Easier to maintain and migrate the development environment.

Example Docker Compose Configuration

services:
  nginx:
    image: nginx:alpine
    container_name: dev-nginx
    ports:
      - "80:80"
      - "8443:443"
    volumes:
      - /var/www/html:/var/www/html
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./nginx/certs:/etc/nginx/certs
    depends_on:
      - php71
      - php81
      - php83

  php71:
    image: php:7.1-fpm-alpine
    container_name: php71
    volumes:
      - /var/www/html:/var/www/html

  php81:
    image: php:8.1-fpm-alpine
    container_name: php81
    volumes:
      - /var/www/html:/var/www/html

  php83:
    image: php:8.3-fpm-alpine
    container_name: php83
    volumes:
      - /var/www/html:/var/www/html

Docker Compose automatically creates a shared network for these services.

Therefore, Nginx can communicate with PHP-FPM using service names.

For example:

fastcgi_pass php71:9000;

Or:

fastcgi_pass php81:9000;

You no longer need to publish PHP-FPM ports to the host.

Configure Docker Nginx

Create a configuration file such as:

./nginx/conf.d/default.conf

server {
    listen 80;
    listen 443 ssl;

    server_name api.example.local;

    ssl_certificate /etc/nginx/certs/cert.pem;
    ssl_certificate_key /etc/nginx/certs/key.pem;

    root /var/www/html/your_project/public;
    index index.php index.html;

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

    location ~ \.php$ {
        fastcgi_pass php71:9000;
        fastcgi_index index.php;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

The key configuration is:

fastcgi_pass php71:9000;

Docker’s internal DNS resolves php71 to the corresponding container IP address.

This allows Nginx to communicate with the correct PHP-FPM version without exposing the PHP-FPM port externally.

4. Configure Local Domain Resolution

After moving Nginx into Docker, you can stop the host Nginx service to release ports 80 and 443 if Docker needs to use them.

sudo systemctl stop nginx

If you no longer need the host Nginx service to start automatically:

sudo systemctl disable nginx

Next, configure local domain resolution.

Edit the host machine’s /etc/hosts file:

sudo nano /etc/hosts

Add:

127.0.0.1 api.example.local

Now, requests to api.example.local resolve to the local machine.

The request flow is:

Browser
   |
   v
api.example.local
   |
   v
127.0.0.1
   |
   v
Host Port 8443
   |
   v
Docker Nginx Container (443)
   |
   v
PHP-FPM Container (9000)

This is especially useful for local development environments where multiple projects use different PHP versions.

5. Use a Different External HTTPS Port

Sometimes port 443 is already occupied by another service, or you simply want to avoid conflicts with the host Nginx.

Docker allows you to map the container’s HTTPS port to a different host port:

ports:
  - "80:80"
  - "8443:443"

The mapping means:

  • Host port 80 forwards to container port 80.
  • Host port 8443 forwards to container port 443.

You can then access your local project using:

https://api.example.local:8443

Notice that Nginx inside the container still listens on port 443. Only the externally exposed port changes.

6. Troubleshooting Common Errors

During the configuration process, several connection errors may occur.

Understanding which layer generates the error makes troubleshooting much easier.

Error 1: Connection Refused on Port 443

Example:

connect to 127.0.0.1 port 443 failed: Connection refused

This usually means that no service is listening on port 443 on the host machine.

Check the listening ports:

ss -tulnp | grep :443

If there is no output, verify whether:

  • Host Nginx is running.
  • Docker Nginx has published port 443.
  • The container is running.
  • The port mapping is configured correctly.

You can also check Docker:

docker ps

Important: 127.0.0.1 always refers to the current network namespace.

If you execute a request inside a PHP container, 127.0.0.1 refers to that PHP container itself, not the host machine.

Error 2: Nginx Cannot Connect to PHP-FPM

Example:

connect() failed (111: Connection refused)
while connecting to upstream

With an upstream configuration such as:

fastcgi_pass 127.0.0.1:9081;

This means Nginx cannot establish a connection to the configured PHP-FPM endpoint.

If you are using host Nginx, check whether the host port is actually listening:

ss -tulnp | grep :9081

Check container status and port mappings:

docker ps -a

You should see something similar to:

127.0.0.1:9081->9000/tcp

If the mapping is missing, recreate the container with the correct port configuration.

For Docker Compose:

docker-compose up -d --force-recreate

For Docker Run:

docker rm -f php81

docker run -d \
  --name php81 \
  -p 127.0.0.1:9081:9000 \
  -v /var/www/html:/var/www/html \
  php:8.1-fpm-alpine

Error 3: Check PHP-FPM Listening Configuration

If the host port is correctly mapped but connections are still refused, check the PHP-FPM listening configuration inside the container.

For example:

docker exec -it php71 sh

Then execute:

grep -rn "listen =" /usr/local/etc/php-fpm*

PHP-FPM should listen on port 9000 in a way that allows connections from outside the container.

For example:

listen = 9000

If a custom configuration sets:

listen = 127.0.0.1:9000

It may prevent connections forwarded from outside the container.

Update the configuration and restart PHP-FPM or recreate the container as appropriate.

Error 4: HTTP 500 Internal Server Error

Suppose your request successfully reaches Docker Nginx and PHP-FPM, but the response is:

HTTP/1.1 500 Internal Server Error
Server: nginx/1.31.2
X-Powered-By: PHP/7.1.33

This is an important distinction.

The response headers indicate that:

  • The request reached Nginx.
  • Nginx successfully forwarded the PHP request.
  • PHP 7.1.33 processed the request.
  • The application returned an HTTP 500 response.

At this point, the Docker networking configuration is likely working.

You should investigate the PHP application instead.

For Laravel applications, check the application log:

tail -n 50 storage/logs/laravel.log

You can also inspect container logs:

docker logs --tail 50 php71
docker logs --tail 50 dev-nginx

Common causes include:

1. Incorrect Database or Redis Host

If your application uses:

DB_HOST=127.0.0.1

Remember that 127.0.0.1 inside a PHP container refers to the PHP container itself.

If MySQL or Redis runs in another Docker container, use its Docker Compose service name instead.

For example:

DB_HOST=mysql
REDIS_HOST=redis

2. Directory Permission Issues

PHP-FPM commonly runs as the www-data user.

If the mounted Laravel directories do not have appropriate permissions, the application may fail when writing logs, cache files, or session data.

Check the permissions of:

storage/
bootstrap/cache/

Make sure the PHP-FPM user has the required write permissions.

Avoid using chmod -R 777 as a permanent solution. Configure ownership and permissions according to your development environment instead.

For a development environment with multiple PHP versions, I recommend running Nginx and PHP-FPM inside Docker.

The architecture looks like this:

Host Machine
    |
    | /etc/hosts
    |
    v
127.0.0.1:8443
    |
    v
Docker Nginx
    |
    +------ php71:9000
    |
    +------ php81:9000
    |
    +------ php83:9000

Each project can select its required PHP version through its own Nginx configuration.

For example:

ProjectPHP VersionFastCGI Upstream
Project APHP 7.1php71:9000
Project BPHP 8.1php81:9000
Project CPHP 8.3php83:9000

This architecture keeps the environment isolated and avoids unnecessary port mappings.

Conclusion

Running multiple PHP-FPM versions with Docker is straightforward once you understand how Docker networking and port mapping work.

There are two practical approaches:

  1. Host Nginx + Docker PHP-FPM: Map each container’s port 9000 to a different host port, then configure fastcgi_pass accordingly.
  2. Docker Nginx + Docker PHP-FPM: Put all services on the same Docker network and communicate using service names.

For a multi-project local development environment, the second approach is generally easier to maintain.

It avoids exposing PHP-FPM ports to the host, simplifies service discovery, and makes it easier to manage different PHP versions independently.

One final troubleshooting tip: distinguish between network errors and application errors. A Connection refused error usually indicates a connectivity problem, while an HTTP 500 response after successful PHP execution usually requires investigating the application logs.

comments powered by Disqus