Skip to content

Commit 949cd54

Browse files
feat: Add Caddy service configuration and Docker setup for improved container management and testing. (#119)
1 parent 3656ada commit 949cd54

File tree

8 files changed

+259
-11
lines changed

8 files changed

+259
-11
lines changed

.github/workflows/docker.yml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,34 @@ jobs:
4141
- name: Run codeception tests.
4242
run: docker exec yii2-apache vendor/bin/codecept run
4343

44+
test-caddy:
45+
runs-on: ubuntu-latest
46+
47+
steps:
48+
- name: Checkout.
49+
uses: actions/checkout@v4
50+
51+
- name: Install docker compose.
52+
run: |
53+
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
54+
sudo chmod +x /usr/local/bin/docker-compose
55+
docker-compose --version
56+
57+
- name: Build and start containers.
58+
run: docker-compose -f docker-compose.caddy.yml up -d --build
59+
60+
- name: Wait for container to be ready.
61+
run: |
62+
echo "Waiting 30 seconds for container initialization..."
63+
sleep 30
64+
docker logs yii2-caddy
65+
66+
- name: Codeception build.
67+
run: docker exec yii2-caddy vendor/bin/codecept build
68+
69+
- name: Run codeception tests.
70+
run: docker exec yii2-caddy vendor/bin/codecept run
71+
4472
test-frankenphp:
4573
runs-on: ubuntu-latest
4674

@@ -66,7 +94,7 @@ jobs:
6694
- name: Codeception build.
6795
run: docker exec yii2-frankenphp vendor/bin/codecept build
6896

69-
- name: Run codeception build and tests.
97+
- name: Run codeception tests.
7098
run: docker exec yii2-frankenphp vendor/bin/codecept run
7199

72100
test-nginx:
@@ -94,5 +122,5 @@ jobs:
94122
- name: Codeception build.
95123
run: docker exec yii2-nginx vendor/bin/codecept build
96124

97-
- name: Run codeception build and tests.
125+
- name: Run codeception tests.
98126
run: docker exec yii2-nginx vendor/bin/codecept run

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ A modern, Bootstrap 5-powered Yii2 application template designed for rapid web-a
4444
## Environment support
4545

4646
[![Apache](https://img.shields.io/badge/apache-%23D42029.svg?style=for-the-badge&label=docker&logo=apache&logoColor=white)](docker-compose.yml)
47+
[![Caddy](https://img.shields.io/badge/caddy-%231F88C0.svg?style=for-the-badge&label=docker&logo=caddy&logoColor=white)](docker-compose.caddy.yml)
4748
[![FrankenPHP](https://img.shields.io/badge/frankenphp-%23FF6B35.svg?style=for-the-badge&label=docker&logo=php&logoColor=white)](docker-compose.frankenphp.yml)
4849
[![Nginx](https://img.shields.io/badge/nginx-%23009639.svg?style=for-the-badge&label=docker&logo=nginx&logoColor=white)](docker-compose.nginx.yml)
4950

@@ -102,6 +103,9 @@ php -S localhost:8080 -t public
102103
# For Apache
103104
docker-compose up -d
104105

106+
# For Caddy
107+
docker-compose -f docker-compose.caddy.yml up -d
108+
105109
# For FrankenPHP
106110
docker-compose -f docker-compose.frankenphp.yml up -d
107111

@@ -118,13 +122,16 @@ After starting the server, you can access your application in your web browser.
118122
http://localhost:8080/
119123

120124
# For Apache
121-
http://localhost:8080/
125+
https://localhost:8443/
126+
127+
# For Caddy
128+
https://localhost:8444/
122129

123130
# For FrankenPHP
124-
http://localhost:8081/
131+
https://localhost:8445/
125132

126133
# For Nginx
127-
http://localhost:8082/
134+
https://localhost:8446/
128135
```
129136

130137
### Basic usage

docker-compose.caddy.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
services:
2+
yii2-caddy:
3+
build:
4+
args:
5+
USER_ID: ${USER_ID:-1000}
6+
GROUP_ID: ${GROUP_ID:-1000}
7+
USER_NAME: ${USER_NAME:-www-data}
8+
GROUP_NAME: ${GROUP_NAME:-www-data}
9+
context: .
10+
dockerfile: docker/caddy/Dockerfile
11+
container_name: yii2-caddy
12+
env_file:
13+
- .env
14+
environment:
15+
TZ: "UTC"
16+
YII_DEBUG: "${YII_DEBUG:-false}"
17+
YII_ENV: "${YII_ENV:-prod}"
18+
ports:
19+
- '8081:80'
20+
- '8444:443'
21+
- '8444:443/udp'
22+
restart: always
23+
tty: true
24+
volumes:
25+
- ./:/app
26+
- caddy_config:/config
27+
- caddy_data:/data
28+
- composer_cache:/var/www/.composer/cache
29+
working_dir: /app
30+
31+
volumes:
32+
caddy_data:
33+
caddy_config:
34+
composer_cache:

docker-compose.frankenphp.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ services:
1717
YII_DEBUG: "${YII_DEBUG:-false}"
1818
YII_ENV: "${YII_ENV:-prod}"
1919
ports:
20-
- '8081:80'
21-
- '8444:443'
22-
- '8444:443/udp'
20+
- '8082:80'
21+
- '8445:443'
22+
- '8445:443/udp'
2323
restart: always
24+
tty: true
2425
volumes:
2526
- ./:/app
2627
- caddy_config:/config
2728
- caddy_data:/data
2829
- composer_cache:/var/www/.composer/cache
2930
working_dir: /app
30-
tty: true
3131

3232
volumes:
3333
caddy_data:

docker-compose.nginx.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ services:
1717
YII_DEBUG: "${YII_DEBUG:-false}"
1818
YII_ENV: "${YII_ENV:-prod}"
1919
ports:
20-
- '8082:80'
21-
- '8445:443'
20+
- '8083:80'
21+
- '8446:443'
2222
restart: always
2323
tty: true
2424
volumes:

docker/caddy/Caddyfile

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
# Global options
3+
auto_https off
4+
}
5+
6+
# HTTPS server block using mkcert certificates
7+
https://localhost:443 {
8+
# Specify mkcert certificates
9+
tls /app/docker/ssl/localhost.pem /app/docker/ssl/localhost-key.pem
10+
11+
# Document root
12+
root * /app/public
13+
14+
# Enable PHP processing with FPM
15+
php_fastcgi unix//var/run/php/php-fpm.sock {
16+
index index.php
17+
try_files {path} {path}/index.php =404
18+
}
19+
20+
# Security headers
21+
header {
22+
X-Frame-Options "SAMEORIGIN"
23+
X-XSS-Protection "1; mode=block"
24+
X-Content-Type-Options "nosniff"
25+
Strict-Transport-Security "max-age=31536000; includeSubDomains"
26+
-Server
27+
}
28+
29+
# Logging
30+
log {
31+
output stdout
32+
format console
33+
}
34+
35+
# Handle static files
36+
@static {
37+
file
38+
path *.css *.js *.png *.jpg *.jpeg *.gif *.ico *.svg *.woff *.woff2 *.ttf *.eot
39+
}
40+
handle @static {
41+
header Cache-Control "public, max-age=31536000"
42+
file_server
43+
}
44+
45+
# Block access to sensitive directories
46+
@forbidden {
47+
path /.git/* /vendor/* /runtime/* /.env*
48+
}
49+
respond @forbidden 404
50+
51+
# Deny PHP execution in assets directory (Yii2 security)
52+
@assets_php {
53+
path /assets/*.php
54+
}
55+
respond @assets_php 403
56+
57+
# Try files for Yii2 URL rewriting
58+
try_files {path} {path}/ /index.php?{query}
59+
}
60+
61+
# HTTP server block - redirect to HTTPS
62+
http://localhost:80 {
63+
# Redirect all HTTP traffic to HTTPS
64+
redir https://localhost:8444{uri} permanent
65+
}

docker/caddy/Dockerfile

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
FROM php:8.4-fpm
2+
3+
# Build arguments for user/group
4+
ARG USER_ID=1000
5+
ARG GROUP_ID=1000
6+
ARG USER_NAME=www-data
7+
ARG GROUP_NAME=www-data
8+
9+
# Set document root to /app/public (Yii2 structure)
10+
WORKDIR /app
11+
12+
# Install required system packages for PHP extensions for Yii 2.0 Framework
13+
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
14+
RUN install-php-extensions \
15+
bcmath \
16+
@composer \
17+
exif \
18+
gd \
19+
imagick \
20+
intl \
21+
opcache \
22+
pdo_mysql \
23+
pdo_pgsql \
24+
soap \
25+
xdebug \
26+
zip
27+
28+
# Set composer environment
29+
ENV COMPOSER_ALLOW_SUPERUSER=1
30+
31+
# Change PHP config
32+
COPY docker/php/php.ini /usr/local/etc/php/conf.d/base.ini
33+
34+
# Install supervisord, gosu, and Node.js (version simple)
35+
RUN apt-get update && apt-get install -y --no-install-recommends \
36+
supervisor \
37+
curl \
38+
gosu \
39+
debian-keyring \
40+
debian-archive-keyring \
41+
apt-transport-https \
42+
&& curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - \
43+
&& apt-get install -y nodejs \
44+
&& apt-get clean \
45+
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
46+
47+
# Install Caddy (standalone)
48+
RUN curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg \
49+
&& curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list \
50+
&& apt-get update \
51+
&& apt-get install -y caddy \
52+
&& apt-get clean \
53+
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
54+
55+
# Change web server config
56+
COPY docker/caddy/Caddyfile /etc/caddy/Caddyfile
57+
58+
# Apply the user/group IDs to www-data
59+
RUN usermod -u ${USER_ID} www-data && groupmod -g ${GROUP_ID} www-data
60+
61+
# Create composer and npm cache directories with proper ownership
62+
RUN mkdir -p /var/www/.composer/cache /var/www/.npm && \
63+
chown -R www-data:www-data /var/www/.composer /var/www/.npm
64+
65+
# Configure PHP-FPM to use Unix socket
66+
RUN sed -i 's|^listen = 127.0.0.1:9000|listen = /var/run/php/php-fpm.sock|' /usr/local/etc/php-fpm.d/www.conf \
67+
&& sed -i 's|^;listen.owner = www-data|listen.owner = www-data|' /usr/local/etc/php-fpm.d/www.conf \
68+
&& sed -i 's|^;listen.group = www-data|listen.group = www-data|' /usr/local/etc/php-fpm.d/www.conf \
69+
&& sed -i 's|^;listen.mode = 0660|listen.mode = 0660|' /usr/local/etc/php-fpm.d/www.conf \
70+
&& mkdir -p /var/run/php \
71+
&& chown www-data:www-data /var/run/php
72+
73+
# Copy supervisord program configs
74+
COPY docker/supervisord/conf.d/caddy-php-fpm.conf /etc/supervisord/conf.d/caddy-php-fpm.conf
75+
76+
# Copy queue worker config uncommented for use with yii2-queue
77+
#COPY docker/supervisor/available/queue.conf /etc/supervisor/available/queue.conf
78+
79+
# Copy scripts
80+
COPY docker/init.sh /usr/local/bin/init.sh
81+
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
82+
83+
# Make scripts executable and validate
84+
RUN chmod +x /usr/local/bin/init.sh /usr/local/bin/entrypoint.sh && \
85+
# Convert any Windows line endings
86+
sed -i 's/\r$//' /usr/local/bin/init.sh /usr/local/bin/entrypoint.sh && \
87+
# Test that scripts have valid syntax
88+
bash -n /usr/local/bin/init.sh && \
89+
bash -n /usr/local/bin/entrypoint.sh && \
90+
echo "✓ Scripts validated successfully..."
91+
92+
# Use ENTRYPOINT to guarantee execution
93+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[program:php-fpm]
2+
command=/usr/local/sbin/php-fpm --nodaemonize
3+
autorestart=true
4+
autostart=true
5+
priority=5
6+
stderr_logfile=/dev/stderr
7+
stderr_logfile_maxbytes=0
8+
stdout_logfile=/dev/stdout
9+
stdout_logfile_maxbytes=0
10+
user=root
11+
12+
[program:caddy]
13+
command=/usr/bin/caddy run --config /etc/caddy/Caddyfile
14+
autorestart=true
15+
autostart=true
16+
priority=10
17+
stderr_logfile=/dev/stderr
18+
stderr_logfile_maxbytes=0
19+
stdout_logfile=/dev/stdout
20+
stdout_logfile_maxbytes=0
21+
user=www-data

0 commit comments

Comments
 (0)