DEV Community

Cover image for Containerization of PHP and Nginx
Hamza Sehouli
Hamza Sehouli

Posted on

Containerization of PHP and Nginx

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.0.2-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:1.19-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 (2)

Collapse
 
dimdev profile image
Dzmitry Kazbiarovich

Why PHP 8.0.2 ?

It's not supported

Collapse
 
trungho profile image
Hồ Phước Trung

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