Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save beisong7/48fec8471c86717504362a760ed5c3f7 to your computer and use it in GitHub Desktop.
Save beisong7/48fec8471c86717504362a760ed5c3f7 to your computer and use it in GitHub Desktop.
DAMP - Docker Apache MySQL PHP setup

DAMP - Setup an Apache, MySQL & PHP local server using Docker

Prerequisites

Setup

  1. Create a new directory for your local server (MY_APP is used as a placeholder)
  2. Add docker-compose.yml, Dockerfile and nginx.conf files to the root of your server directory.
  3. With your favorite terminal application:
    1. cd /path/to/MY_APP (replace /path/to/MY_APP with the path to your local server directory created above.
    2. docker-compose up -d
  4. Open http://localhost:8000 with your favorite browser and start developing.

Reverse Proxy

  1. (Optional) In the nginx.conf file, replace my-app.local with the desired virtual hostname.

    If you choose to use a different hostname, then replace all mentions of my-app.local with your choosen hostname.

  2. Add 127.0.0.1 my-app.local to your hosts file hosts file (how to modify your hosts file - guide)

  3. Start the container docker-compose up -d.

  4. Open http://my-app.local

DB Admin

Adminer is tool for accessing and managing your mysql database. Access it via http://localhost:8080

version: "3.7"
services:
web:
build: .
working_dir: /var/www/html
depends_on: [db]
volumes:
- type: bind
source: . # Relative path to the root of your php source code.
target: /var/www/html/
# To expose the logs, create the relevant folders/files and uncomment the below section.
# - type: bind
# source: ./logs/httpd
# target: /var/log/httpd
# - type: bind
# source: ./logs/php_errors.log
# target: /var/log/php_errors.log
# To enable debugging, uncomment the below section.
# environment:
# XDEBUG_CONFIG: "remote_host=host.docker.internal"
# PHP_IDE_CONFIG: "serverName=my-app"
ports:
- "8000:80"
db:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- db-data:/var/lib/mysql
# To expose the logs, create the relevant folders/files and uncomment the below section.
# - type: bind
# source: ./logs/mysql.log
# target: /var/log/mysql.log
reverse-proxy:
image: nginx:latest
depends_on: [web]
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
# To expose the logs, create the relevant folders/files and uncomment the below section.
# - type: bind
# source: ./logs/nginx
# target: /etc/nginx/logs
ports:
- 80:80
- 443:443
db-admin:
image: adminer:latest
ports:
- "8080:8080"
FROM php:7-apache
# Installes the mySQL client library pdo_mysql.
RUN docker-php-ext-install pdo_mysql
# Uncomment if you require the gd php extension
# RUN apt-get update && \
# apt-get install -y libfreetype6-dev libjpeg62-turbo-dev && \
# docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \
# docker-php-ext-install gd
# Uncomment if you require opcache
# RUN docker-php-ext-install opcache
# Uncomment if you requrie apache rewrite mod.
# RUN a2enmod rewrite
# Uncomment to enable xdebug
# RUN pecl install xdebug-beta &&\
# docker-php-ext-enable xdebug &&\
# echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
# echo "xdebug.remote_autostart=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
# echo "xdebug.remote_connect_back=off" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
# echo "xdebug.remote_handler=dbgp" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
# echo "xdebug.profiler_enable=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
# echo "xdebug.profiler_output_dir=\"/var/www/html\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
# echo "xdebug.remote_port=9000" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
# echo "xdebug.remote_host = host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN apt-get clean
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name my-app.local;
charset utf-8;
location / {
proxy_pass http://web;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment