This guide explains how to set up a PHP development environment with Docker by defining services, configuring PHP and Nginx, adding a simple PHP file to verify the setup, and running the containers to serve the application.
Steps1. Create the project directory mkdir docker-php
docker-compose.yml
file with the following configuration to define the services services:
php:
build:
context: .
dockerfile: Dockerfile
container_name: docker-php
volumes:
- .:/usr/share/nginx/html/docker-php
networks:
- docker-php
nginx:
image: nginx:latest
container_name: docker-nginx
ports:
- 80:80
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf
- .:/usr/share/nginx/html/docker-php
networks:
- docker-php
networks:
docker-php:
driver: bridge
Dockerfile
to define the PHP environment FROM php:8.3-fpm
WORKDIR /usr/share/nginx/html/docker-php
nginx.conf
file to configure Nginx to work with PHP-FPM server {
listen 80;
listen [::]:80;
root /usr/share/nginx/html/docker-php/public;
index index.php index.html index.htm;
location ~ .php$ {
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
<?php phpinfo(); ?>
docker compose up -d
[+] Running 3/3
✔ Network docker-php_docker-php Created 0.9s
✔ Container docker-nginx Started 1.7s
✔ Container docker-php Started 1.7s
http://localhost
and you should see the detailed information about the PHP configuration