Skip to content

Instantly share code, notes, and snippets.

@Eyal-Shalev
Last active February 23, 2024 11:59
Show Gist options
  • Save Eyal-Shalev/1b747a0ce47ae3123fe30e0635567803 to your computer and use it in GitHub Desktop.
Save Eyal-Shalev/1b747a0ce47ae3123fe30e0635567803 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;
}
}
}
@Eyal-Shalev
Copy link
Author

Actually it's not Apache but nginx. It's a bit weird to use php-apache docker image overloaded with the nginx web server.

It's weird, but it is an Apache server overloaded with nginx.
Though it was 3 years ago, so I'm unsure why I did it that way.

@tangalor
Copy link

on the docker-compose.yml file I added these lines... (it doesn't work without it)

volumes:
db-data: {}

after that, I can access to db panel on http://my-app.local:8080 but the web server doesn't seem to work fine (http://my-app.local it's not available). What is the path where the index document is searched? I put the index in the MY_APP folder and also in /var/www/html on my laptop but it doesn't work (I set the hosts file and stopped and started docker up after that, but it doesn't work)

cat /etc/hosts
127.0.0.1 localhost
127.0.0.1 my-app.local

can you help me? thank you in advance

@missiria
Copy link

volumes:
db-data: {}

I've tested it and its doesn't work without volumes declaration :
ERROR: Named volume "db-data:/var/lib/mysql:rw" is used in service "db" but no declaration was found in the volumes section.
We should add volumes as you mentioned and thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment