DEV Community

Cover image for Containerization of PHP and Nginx [Updated 2025]
Hamza Sehouli
Hamza Sehouli

Posted on • Edited on

Containerization of PHP and Nginx [Updated 2025]

Containerizing PHP and Nginx involves creating Docker containers for each service and configuring them to work together. Below are the steps involved in creating a basic Docker configuration for PHP and Nginx. This example is based on a simple PHP application.

1- Create the project structure :
Start by creating the project with the following structure:

//projet
├── index.php
├── nginx
│   └── nginx.conf
├── Dockerfile
├── Dockerfile-nginx
└── docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

2- Create a PHP application :
Create a simple PHP application. For example, in the root "./index.php" :

<?php
echo "Hello!";
Enter fullscreen mode Exit fullscreen mode

3- Configure Nginx :
Create an Nginx configuration file in nginx/nginx.conf :

server {
    listen 80;
    index index.php;
    server_name localhost;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    error_page 404 /index.php;
    root /var/www;
    client_max_body_size 20m;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}
Enter fullscreen mode Exit fullscreen mode

4- Write the Dockerfile :

FROM php:8.2.27-fpm

WORKDIR /var/www
Enter fullscreen mode Exit fullscreen mode

5-Create a Dockerfile-nginx file for Nginx in the root of the project:

FROM nginx:stable-alpine

WORKDIR /var/www
Enter fullscreen mode Exit fullscreen mode

6- Create a .dockerignore file in the root of the :

docker-compose
Dockerfile*
Enter fullscreen mode Exit fullscreen mode

7- Write the Docker Compose file :

Create a docker-compose.yml file in the project root:

version: "3"

services:
  app:
    container_name: app-container
    build:
      context: ./
      dockerfile: ./Dockerfile
    working_dir: /var/www/
    restart: always
    volumes:
      - ./:/var/www
    networks:
      - app-network


  nginx:
    container_name: nginx-container
    build:
      context: ./
      dockerfile: ./Dockerfile-nginx
    restart: always
    ports:
      - "8000:80"
    volumes:
      - ./nginx:/etc/nginx/conf.d
      - ./:/var/www
    depends_on:
      - app
    networks:
      - app-network

networks:
  app-network:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

We've created an app-network, because we're containerizing PHP and Nginx separately with the same network, since the containers have a network isolated from our local system.

8- Build and run containers :

docker-compose up --build
Enter fullscreen mode Exit fullscreen mode

Access the application:
Open your browser and go to http://localhost:8000. You should see the message "Hello!".

Top comments (6)

Collapse
 
dimdev profile image
Dzmitry Kazbiarovich

Why PHP 8.0.2 ?

It's not supported

Collapse
 
hamzasehouli profile image
Hamza Sehouli • Edited

I tested out this the code multiple times and it works seamlessly, just follow the instructions and steps meticulously.

Docker image PHP 8.0.2 standalone doesn't work with Nginx web server, because it does not include the FastCGI Process Manager (FPM), which is essential for handling HTTP requests in a web server setup in Nginx or Apache configured with FastCGI.

Collapse
 
dimdev profile image
Dzmitry Kazbiarovich

Nobody said the code doesn't work.

The question was about used PHP version.
PHP 8.0 is not supported any more php.net/supported-versions.php

Thread Thread
 
hamzasehouli profile image
Hamza Sehouli

Yes you are absolutely right, I overlooked the php version, thanks for pointing that out, I will update this content soon.

Collapse
 
trungho profile image
Hồ Phước Trung

I usually call it DockerPHP link : github.com/hophuoctrung/DockerPHP

Collapse
 
hamzasehouli profile image
Hamza Sehouli

Great 🦾