Skip to main content

Command Palette

Search for a command to run...

Docker compose for PHP 8 and Nginx

useful docker compose file for php 8 and Nginx

Published
2 min read
Docker compose for PHP 8 and Nginx
R

Full-stack developer with mastery across mobile (React Native, Ionic), front-end (React, Svelte, Vue.js), back-end (Express.js, FastAPI, Django), e-commerce (Shopify, OpenCart), and CMS (Wordpress, Laravel, Drupal, headless). Building web and mobile applications that deliver!

Here's a Docker Compose configuration for PHP 8, using Nginx as the web server:

YAML

version: '3.8'

services:
  nginx:
    image: nginx:1.25-alpine
    ports:
      - "80:80"
    volumes:
      - ./app:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php

  php:
    image: php:8.1-fpm-alpine
    volumes:
      - ./app:/var/www/html
    working_dir: /var/www/html

Explanation:

  • version: Specifies the Docker Compose file format version.

  • services: Defines the services to run in the environment.

    • nginx:

      • image: Uses the Nginx image for web serving.

      • ports: Maps port 80 on the host machine to port 80 in the container.

      • volumes: Mounts two volumes:

        • The app directory (containing your PHP code) to the Nginx document root.

        • A custom Nginx configuration file for PHP support.

      • depends_on: Ensures the php service starts before Nginx.

    • php:

      • image: Uses the PHP 8.1 FPM image for processing PHP code.

      • volumes: Mounts the app directory to the PHP container's working directory.

      • working_dir: Sets the working directory within the container.

To use this configuration:

  1. Create a directory for your project: Bash

     mkdir my-php-app
     cd my-php-app
    
  2. Create a docker-compose.yml file and paste the configuration above into it.

  3. Create an app directory to hold your PHP code.

  4. Optionally, create a nginx/default.conf file for custom Nginx configuration.

  5. Run the containers: Bash

     docker-compose up -d
    

Now you can access your PHP application at http://localhost!

Additional notes:

  • Custom Nginx configuration: If you need to adjust Nginx settings for your application, create a nginx/default.conf file and mount it as shown in the configuration.

  • Other services: You can add other services to your environment, such as databases or caches, by defining them in the services section of the Docker Compose file.

  • Versioning: Be sure to specify the specific version of Nginx and PHP that you need in the image definitions.