NeuroAgent

Configuring Ports for php5.6-fpm in Docker

Complete guide to configuring ports for php5.6-fpm in Docker containers and connecting to nginx. Learn how to run PHP 5.6 alongside PHP 7.4.

Question

Setting up ports for php5.6-fpm in Docker container

Questions about Docker container configuration with PHP 5.6-FPM

Main situation:

On an Ubuntu 20.04 server, there’s a setup with nginx, php7.4-fpm, mariadb, and symfony2. I need to migrate a site from another server (Ubuntu 14.04) where it was running on php5.6-fpm. To avoid rewriting functionality for the new PHP version, I’ve chosen to run php5.6-fpm in a Docker container.

Questions:

  1. Port configuration for Docker container:
    Which ports should I specify when running php5.6-fpm from a container to connect it to nginx?

    Current command:

    sudo docker container run -d --name php5.6-fpm -p ???:??? vicenterusso/php56-fpm
    

    Example from the image documentation:

    sudo docker container run --rm --name phpfpm -v $(pwd):/var/www/html -p 3000:3000 vicenterusso/php56-fpm php -S="0.0.0.0:3000" -t="/var/www/html"
    
  2. Nginx configuration for connecting to the container:
    In the nginx site configuration, I need to specify which socket to bind to for the containerized php-fpm. The current setting is:

    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    

    What needs to be changed to connect to php5.6-fpm in the Docker container?

  3. Parallel operation of PHP versions:
    Will the php5.6-fpm container work in parallel with php7.4-fpm on the host? Can any sites connect to php5.6-fpm, and do URLs need to include port numbers?

NeuroAgent

Configuring php5.6-fpm in Docker Container and Connecting to nginx

To configure php5.6-fpm in a Docker container and connect it to nginx, use the following parameters:

  1. The main port for php5.6-fpm is 9000. By default, PHP-FPM listens on port 9000, so to connect the container to nginx, you need to specify this port. For your command:
bash
sudo docker container run -d --name php5.6-fpm -p 127.0.0.1:9999:9000 vicenterusso/php56-fpm

Here, 9999 is the host port that you can choose as available, while 9000 is the port inside the container.

  1. Nginx configuration should be changed from unix socket to TCP connection:
nginx
fastcgi_pass 127.0.0.1:9999;

Or, if you’re using docker-compose with a network:

nginx
fastcgi_pass php-fpm:9000;
  1. Parallel operation of PHP versions is possible without issues. The container with php5.6-fpm will run independently of the host system with php7.4-fpm. To connect sites to php5.6-fpm, you need to:
  • Specify the correct port in the nginx configuration
  • If needed, you can configure different virtual hosts for different PHP versions
  • No need to specify port numbers in URLs if you’re using standard ports 80/443

Contents


Basic port configuration for php5.6-fpm

PHP-FPM by default listens on port 9000, as specified in the PHP documentation. For a Docker container with php5.6-fpm, you need to properly configure port forwarding.

Standard startup command:

bash
sudo docker container run -d --name php5.6-fpm \
  -p 127.0.0.1:9999:9000 \
  -v /path/to/your/project:/var/www/html \
  vicenterusso/php56-fpm

Explanation:

  • 127.0.0.1:9999:9000 - forwards port 9000 from the container to host port 9999 only on localhost
  • vicenterusso/php56-fpm - image with PHP 5.6-FPM
  • -v /path/to/your/project:/var/www/html - mounts the project into the container

Alternative option with TCP connection:

If you want nginx to connect to php-fpm through Docker network:

bash
sudo docker container run -d --name php5.6-fpm \
  --network my-app-network \
  -p 9000:9000 \
  vicenterusso/php56-fpm

Configuring nginx to connect to Docker container

To connect nginx to php5.6-fpm in a Docker container, you need to modify the fastcgi_pass configuration.

Option 1: Through TCP port (recommended)

nginx
server {
    listen 80;
    server_name your-domain.com;
    root /var/www/html;
    index index.php index.html;

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

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9999;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Option 2: Through Docker network

If using docker-compose with a shared network:

nginx
location ~ \.php$ {
    fastcgi_pass php5.6-fpm:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Option 3: Through Unix socket (more complex)

To use unix sockets, you need to mount a shared volume between containers:

yaml
# docker-compose.yml
version: '3.4'
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./php5.6-fpm.sock:/var/run/php5.6-fpm.sock
    depends_on:
      - php5.6-fpm

  php5.6-fpm:
    image: vicenterusso/php56-fpm
    volumes:
      - ./php5.6-fpm.sock:/var/run/php5.6-fpm.sock
nginx
# nginx.conf
location ~ \.php$ {
    fastcgi_pass unix:/var/run/php5.6-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Parallel PHP version operation

Yes, a container with php5.6-fpm will run in parallel with php7.4-fpm on the host system without conflicts.

How it works:

  1. Process isolation: Docker container is isolated from the host system, so PHP versions don’t conflict
  2. Different ports: Different ports are used for connection (9000 for php7.4-fpm on host, 9999 for php5.6-fpm in container)
  3. Different configs: nginx can have different location blocks for different PHP versions

Example configuration for multiple PHP versions:

nginx
# Site with PHP 7.4 (host system)
server {
    listen 80;
    server_name site1.com;
    root /var/www/site1;
    
    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

# Site with PHP 5.6 (Docker container)
server {
    listen 80;
    server_name site2.com;
    root /var/www/site2;
    
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9999;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

URLs:

In URLs, you don’t need to specify port numbers if you’re using standard ports 80/443. Port numbers are only specified in the nginx configuration for internal connection to php-fpm.


Docker-compose configuration examples

Simple example with TCP port:

yaml
version: '3.4'
services:
  nginx:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./project:/var/www/html
    depends_on:
      - php5.6-fpm

  php5.6-fpm:
    image: vicenterusso/php56-fpm
    ports:
      - "127.0.0.1:9999:9000"
    volumes:
      - ./project:/var/www/html
nginx
# nginx.conf
events {
    worker_connections 1024;
}

http {
    server {
        listen 80;
        server_name localhost;
        root /var/www/html;
        index index.php index.html;

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

        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9999;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}

Example with Docker network:

yaml
version: '3.4'
services:
  nginx:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./project:/var/www/html
    networks:
      - app-network

  php5.6-fpm:
    image: vicenterusso/php56-fpm
    volumes:
      - ./project:/var/www/html
    networks:
      - app-network

networks:
  app-network:
    driver: bridge
nginx
# nginx.conf using Docker network
location ~ \.php$ {
    fastcgi_pass php5.6-fpm:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Troubleshooting common issues

Issue 1: Connection refused

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

Solution:

  • Check that the container is running: docker ps
  • Ensure the port is properly forwarded
  • Check firewall: sudo ufw status

Issue 2: Permission denied for socket

Error: Permission denied when working with unix socket

Solution:

  • Ensure nginx and php-fpm use the same user
  • Or use TCP connection instead of socket

Issue 3: PHP-FPM not starting

Solution:

  • Check logs: docker logs php5.6-fpm
  • Ensure php-fpm configuration is correct
  • Check permissions on mounted volumes

Issue 4: Extension incompatibility

Solution:

  • For PHP 5.6, some extensions may not be available
  • Use Alpine versions of images for smaller size
  • Build a custom Dockerfile with required extensions

Sources

  1. Stack Overflow - How to correctly link php-fpm and Nginx Docker containers
  2. Medium - Setting up nginx and PHP-FPM in Docker with Unix Sockets
  3. pascallandau.com - PHP, PHP-FPM and NGINX on Docker
  4. Stack Overflow - How to connect nginx to php-fpm using unix socket in docker
  5. GitHub - eustatos/docker-nginx-php-fpm-socket-tcp
  6. inanzzz - Using unix socket for php-fpm and nginx docker setup

Conclusion

  1. The main port for php5.6-fpm is 9000, which needs to be forwarded to a host port (e.g., 9999) using the -p 127.0.0.1:9999:9000 parameter

  2. Nginx configuration should use fastcgi_pass 127.0.0.1:9999; instead of unix socket to connect to the container

  3. Parallel operation of PHP versions is fully possible - the container is isolated from the host system, and port numbers in URLs are not required

  4. Recommended approach - use TCP connection through a forwarded port, as it’s easier to configure and debug

  5. For production consider using docker-compose with a shared network between nginx and php-fpm containers