Sponsored By: Password Angel - Share passwords, API keys, credentials and more with secure, single-use links.

A LEMP stack using Docker / Nginx / PHP / MySQL stack

Project Structure

|- code
  |- config
    |- docker
      |- nginx
        |- default.conf
      |- php
        |- xdebug.ini
  |- src
  |- tests
  |- ... etc ...
|- database

The compose.yaml file

services:
  database:
    image: mysql/mysql-server:${MYSQL_VERSION:-8.0}
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD:-password}
      - MYSQL_ROOT_HOST=%
    ports:
      - 3306:3306
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      timeout: 5s
      retries: 5
      start_period: 60s
    volumes:
      - ../database:/var/lib/mysql
    networks:
      - default

  nginx:
    image: nginx:latest
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./:/app:cached
      - ./config/docker/nginx:/etc/nginx/conf.d
    networks:
      - default

  php:
    build: .
    working_dir: /app
    volumes:
      - ./:/app:cached
      - ./config/docker/php/xdebug.ini:/usr/local/etc/php/conf.d/xdebug-custom.ini
    depends_on:
      - database
    networks:
      - default

networks:
  default:

The config/docker/nginx/default.conf file

server {
    listen       80;
    server_name  localhost;

    root /app/public;

    # Basic web server configuration.
    index        index.php index.htm;

    # Set up rewrite rules.
    location / {
        try_files $uri /index.php$is_args$args;
    }

    # Pass PHP scripts to php-fpm
    location ~ .php$ {
        fastcgi_pass php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_read_timeout 60;

        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;

        fastcgi_intercept_errors on;
    }
}

[!TIP]

A more detailed example of an nginx configuration file for Symfony can be found at https://chrisshennan.com/examples/nginx-configuration-for-a-symfony-project

The config/docker/php/xdebug.ini file

[xdebug]
xdebug.mode=develop,debug
xdebug.client_host=host.docker.internal
xdebug.start_with_request=yes
xdebug.idekey=PHPSTORM

Additional Reading