Giter Club home page Giter Club logo

docker-composer-basic-for-php-devs's Introduction

How to Deploy a PHP Application with Nginx and MySQL Using Docker and Docker Compose for Developments

Contents

Prerequisites

  • Docker runner in Linux or Docker-Desktop with WSL in Windows

Directory Structure

├── docker-compose.yml
├── nginx
│   ├── default.conf
│   └── Dockerfile
├── php
│   └── Dockerfile
└── www
    └── html
        └── index.php

Docker compose file

The file is shown below.

You can change the value of environment variables as per your need.

Remembering that if you change the root password environment variable MYSQL_ROOT_PASSWORD you will also have to change it in line 16 that tests if the database is up

version: '2.1'
services:
  db:
    image: mysql:5.7
    container_name: mysql-container
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_DATABASE=mydb
      - MYSQL_USER=myuser
      - MYSQL_PASSWORD=password
    ports:
      - '3306:3306'
    volumes:
      - './data:/var/lib/mysql'
    healthcheck:
      test: 'mysqladmin ping -h localhost -psecret'
      interval: 30s
      timeout: 30s
      retries: 3
  fpm:
    build: ./php/
    container_name: php-container
    expose:
      - 9000
    links:
      - db
    volumes:
      - "./www/html/:/var/www/html/"
    depends_on:
      db:
        condition: service_healthy
  web:
    build: ./nginx/
    container_name: nginx-container
    ports:
      - 8080:80
    links:
      - fpm
    volumes:
      - "./www/html/:/var/www/html/"
    depends_on:
      fpm:
        condition: service_started

T

Nginx default configuration file to run your PHP application:

The nginx configuration file is in the path nginx/default.conf and is also shown below.

server {  

     listen 80 default_server;  
     root /var/www/html;  
     index index.html index.php;  

     charset utf-8;  

     location / {  
      try_files $uri $uri/ /index.php?$query_string;  
     }  

     location = /favicon.ico { access_log off; log_not_found off; }  
     location = /robots.txt { access_log off; log_not_found off; }  

     access_log off;  
     error_log /var/log/nginx/error.log error;  

     sendfile off;  

     client_max_body_size 100m;  

     location ~ .php$ {  
      fastcgi_split_path_info ^(.+.php)(/.+)$;  
      fastcgi_pass fpm:9000;  
      fastcgi_index index.php;  
      include fastcgi_params;  
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  
      fastcgi_intercept_errors off;  
      fastcgi_buffer_size 16k;  
      fastcgi_buffers 4 16k;  
    }  

     location ~ /.ht {  
      deny all;  
     }  
    } 

Dockerfile for PHP-FPM

The Dockerfile file is in the path php/Dockerfile and is also shown below.

FROM php:7.0-fpm
RUN docker-php-ext-install pdo_mysql 

If you need to add other php dependencies please change the dockerfile for your need.

docker-composer-basic-for-php-devs's People

Contributors

phaelfp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.