Skip to content

Instantly share code, notes, and snippets.

@ihrifat2
Forked from clandestine8/Caddyfile
Created August 2, 2023 04:13
Show Gist options
  • Save ihrifat2/634403acb698e3635762f191d3a2d3ec to your computer and use it in GitHub Desktop.
Save ihrifat2/634403acb698e3635762f191d3a2d3ec to your computer and use it in GitHub Desktop.
Multi App PHP / Laravel Docker Development Environment using Caddy as Reverse Proxy

Multi-App PHP / Laravel Development Environment using Docker Compose

Featuring Caddy, MySQL, PHP 8.1 FPM, Redis, PhpMyAdmin, & Mailhog

Setup

  1. To Start, Create a directory tree as such:
  • Projects
    • Docker
    • Repos
    • Services
      • caddy
      • mysql
  1. Clone your app into the a subfolder under Repos

  2. Copy docker-compose.yaml & Caddyfile into the Projects folder

  3. Copy php-fpm-runtime.Dockerfile into Projects\Docker folder

  4. From the Projects folder run docker compose up -d

  5. Adjust docker-compose.yaml for your projects. Duplicate the example app for PHP based apps

  6. Adjust Caddyfile for your Apps and Services. See https://caddyserver.com/docs/caddyfile for more help

  7. From the Projects folder run docker compose up -d && docker compose restart caddy to apply new settings.

Services Links

https://app.localhost/ is where you will find you PHP App

https://phpmyadmin.localhost/ is your PhpMyAdmin UI

https://mailhog.localhost/ is your mailhog UI

Thank You!

Feel Free to Fork & Enjoy!

app.localhost {
tls internal
root * /srv/App/public
php_fastcgi App:9000 {
root /var/www/public
}
file_server
encode gzip
}
phpmyadmin.localhost {
tls internal
reverse_proxy phpmyadmin:80
}
mailhog.localhost {
tls internal
reverse_proxy mailhog:8025
}
version: "3.7"
services:
######
#APPS#
######
##
App:
image: php-fpm-runtime
user: 1000:1000
build:
dockerfile: $PWD/Docker/php-fpm-runtime.Dockerfile
restart: always
expose:
- 9000
volumes:
- $PWD/Repos/App:/var/www
##
##########
#SERVICES#
##########
##
caddy:
image: caddy:alpine
restart: unless-stopped
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- $PWD/Caddyfile:/etc/caddy/Caddyfile
- $PWD/Repos:/srv
- $PWD/Services/caddy/data:/data
- $PWD/Services/caddy/config:/config
##
mysql:
image: mysql:8.0
restart: always
environment:
MYSQL_ROOT_PASSWORD: "SuperSecurePassword"
MYSQL_ROOT_HOST: "%"
volumes:
- $PWD/Services/mysql/data/:/var/lib/mysql
ports:
- 3306:3306
##
redis:
restart: always
image: redis:6
command: redis-server --appendonly yes
ports:
- 6379:6379
##
mailhog:
restart: always
image: mailhog/mailhog
ports:
- 1025:1025
- 8025:8025
##
phpmyadmin:
image: phpmyadmin:latest
restart: always
environment:
PMA_ABSOLUTE_URI: "https://phpmyadmin.localhost/"
PMA_HOST: mysql
PMA_USER: root
PMA_PASSWORD: "SuperSecurePassword"
UPLOAD_LIMIT: 10G
MEMORY_LIMIT: 2G
MAX_EXECUTION_TIME: 600
FROM php:8.1-fpm-alpine
# Install Deps
RUN apk add --no-cache git sed nodejs npm openssh-client libzip-dev libsodium-dev icu-dev mysql-client supervisor freetype-dev libjpeg-turbo-dev libpng-dev
RUN apk add --no-cache $PHPIZE_DEPS
# install modules
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd
RUN docker-php-ext-install zip
RUN docker-php-ext-install sodium
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install intl
RUN docker-php-ext-install bcmath
RUN docker-php-ext-install sockets
RUN docker-php-ext-install pcntl
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions
RUN install-php-extensions imagick
RUN install-php-extensions redis
RUN install-php-extensions @composer
RUN install-php-extensions opcache
RUN touch /usr/local/etc/php-fpm.d/zz-docker.conf \
&& echo '[global]' >> /usr/local/etc/php-fpm.d/zz-docker.conf \
&& echo 'daemonize = no' >> /usr/local/etc/php-fpm.d/zz-docker.conf \
&& echo '[www]' >> /usr/local/etc/php-fpm.d/zz-docker.conf \
&& echo 'listen = 9000' >> /usr/local/etc/php-fpm.d/zz-docker.conf \
&& echo 'pm = static' >> /usr/local/etc/php-fpm.d/zz-docker.conf \
&& echo 'pm.max_children = 1' >> /usr/local/etc/php-fpm.d/zz-docker.conf
# Configure PHP.ini
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
RUN sed -i 's/memory_limit = 128M/memory_limit = -1/g' $PHP_INI_DIR/php.ini
RUN sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 100M/g' $PHP_INI_DIR/php.ini
RUN sed -i 's/post_max_size = 8M/post_max_size = 100M/g' $PHP_INI_DIR/php.ini
RUN sed -i 's/max_execution_time = 30/max_execution_time = 300/g' $PHP_INI_DIR/php.ini
#Configure Entry
RUN touch /usr/bin/entry \
&& touch /usr/bin/start \
&& touch /usr/bin/first \
&& echo '#!/bin/sh' >> /usr/bin/entry \
&& echo 'umask 0002' >> /usr/bin/entry \
&& echo '/usr/bin/start' >> /usr/bin/entry \
&& echo '#!/bin/sh' >> /usr/bin/start \
&& echo 'supervisord -c /etc/supervisord.conf' >> /usr/bin/start \
&& echo 'supervisorctl reload' >> /usr/bin/start \
&& echo "php-fpm" >> /usr/bin/start \
&& echo '#!/bin/sh' >> /usr/bin/first \
&& echo 'cd /var/www/' >> /usr/bin/first \
&& echo 'php artisan storage:link' >> /usr/bin/first \
&& echo 'php artisan key:generate' >> /usr/bin/first \
&& echo 'php artisan migrate:fresh --seed' >> /usr/bin/first \
&& chmod +x,o+x,g+x /usr/bin/start \
&& chmod +x,o+x,g+x /usr/bin/entry \
&& chmod +x,o+x,g+x /usr/bin/first
RUN rm -rf /var/www/* && chown www-data:www-data -R /var/www
STOPSIGNAL SIGTERM
EXPOSE 9000
ENTRYPOINT ["/usr/bin/entry"]
CMD ["/usr/bin/entry"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment