Skip to content

Commit 1af4d4d

Browse files
feat: Add SSL support with configuration updates for Apache, FrankenPHP and Nginx, including certificate files. (#108)
1 parent 4d569e0 commit 1af4d4d

File tree

14 files changed

+200
-71
lines changed

14 files changed

+200
-71
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ A modern, Bootstrap 5-powered Yii2 application template designed for rapid web-a
3838
-**Multi-language Support** - Built-in internationalization (i18n) support with message translations.
3939
-**Ready-to-Use Pages** - Pre-built pages including home, about, contact, and error handling.
4040
-**Security Features** - Built-in CSRF protection, input validation, and secure configurations.
41+
-**SSL Support** - Configured for secure HTTPS connections with SSL (mkcert).
4142
-**Testing Ready** - Codeception test suite with examples for functional and unit testing.
4243

4344
## Environment support
@@ -120,10 +121,10 @@ http://localhost:8080/
120121
http://localhost:8080/
121122

122123
# For FrankenPHP
123-
http://localhost:8082/
124+
http://localhost:8081/
124125

125126
# For Nginx
126-
http://localhost:8081/
127+
http://localhost:8082/
127128
```
128129

129130
### Basic usage

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.frankenphp.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ services:
1111
- caddy_data:/data
1212
- caddy_config:/config
1313
ports:
14-
- '8082:80'
15-
- '8443:443'
16-
- '8443:443/udp'
14+
- '8081:80'
15+
- '8444:443'
16+
- '8444:443/udp'
1717
environment:
1818
TZ: "UTC"
1919
tty: true

docker-compose.nginx.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ services:
99
- ./:/app
1010
- ~/.composer-docker/cache:/root/.composer/cache:delegated
1111
ports:
12-
- '8081:80'
12+
- '8082:80'
13+
- '8445:443'
1314
environment:
1415
TZ: "UTC"

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ services:
1111
- ~/.composer-docker/cache:/root/.composer/cache:delegated
1212
ports:
1313
- '8080:80'
14+
- '8443:443'
1415
environment:
1516
TZ: "UTC"

docker/apache/Dockerfile

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
FROM yiisoftware/yii2-php:8.4-apache
22

3-
# change web server config
3+
# Enable SSL modules and configure ports before copying config
4+
RUN a2enmod ssl rewrite headers mime \
5+
&& echo "Listen 80" > /etc/apache2/ports.conf \
6+
&& echo "Listen 443 ssl" >> /etc/apache2/ports.conf
7+
8+
# Change web server config
49
COPY docker/apache/apache.conf /etc/apache2/apache2.conf
510
COPY docker/apache/vhost.conf /etc/apache2/sites-available/000-default.conf
611

7-
# change PHP config
12+
# Change PHP config
813
COPY docker/php/php.ini /usr/local/etc/php/conf.d/base.ini
914

10-
# install supervisord and Node.js (includes npm)
15+
# Install supervisord and Node.js (includes npm)
1116
RUN apt-get update && apt-get install -y \
1217
supervisor \
1318
curl \
@@ -17,14 +22,14 @@ RUN apt-get update && apt-get install -y \
1722
&& apt-get clean \
1823
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
1924

20-
# copy supervisord config
25+
# Copy supervisord config
2126
COPY docker/apache/supervisord/supervisord.conf /etc/supervisor/supervisord.conf
2227

23-
# copy supervisord program configs
28+
# Copy supervisord program configs
2429
COPY docker/apache/supervisord/conf.d/apache2.conf /etc/supervisor/conf.d/apache2.conf
2530

26-
# copy queue worker config uncommented for use with yii2-queue
31+
# Copy queue worker config uncommented for use with yii2-queue
2732
#COPY docker/apache/supervisord/conf.d/queue.conf /etc/supervisor/conf.d/queue.conf
2833

29-
# run supervisord
34+
# Run supervisord
3035
CMD ["supervisord", "-c", "/etc/supervisor/supervisord.conf"]

docker/apache/vhost.conf

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,71 @@
1+
# Global configuration for the public directory
12
<Directory /app/public>
23
Options FollowSymLinks
34
AllowOverride All
45
Require all granted
6+
7+
# Ensure Apache serves static files directly
8+
<FilesMatch "\.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$">
9+
Header unset ETag
10+
FileETag None
11+
12+
# Completely disable PHP processing for these files
13+
RemoveHandler .php
14+
SetHandler default-handler
15+
</FilesMatch>
516
</Directory>
617

18+
# HTTP Virtual Host - Redirect to HTTPS
719
<VirtualHost *:80>
8-
# The ServerName directive sets the request scheme, hostname and port that
9-
# the server uses to identify itself. This is used when creating
10-
# redirection URLs. In the context of virtual hosts, the ServerName
11-
# specifies what hostname must appear in the request's Host: header to
12-
# match this virtual host. For the default virtual host (this file) this
13-
# value is not decisive as it is used as a last resort host regardless.
14-
# However, you must set it for any further virtual host explicitly.
15-
#ServerName example.com
16-
#ServerAlias www.example.com
17-
1820
ServerAdmin webmaster@localhost
1921
DocumentRoot /app/public
2022

21-
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
22-
# error, crit, alert, emerg.
23-
# It is also possible to configure the loglevel for particular
24-
# modules, e.g.
25-
#LogLevel info ssl:warn
23+
# Redirect all HTTP to HTTPS
24+
RewriteEngine On
25+
RewriteCond %{HTTPS} off
26+
RewriteRule ^(.*)$ https://localhost:8443%{REQUEST_URI} [R=301,L]
2627

28+
# Logging configuration
2729
ErrorLog ${APACHE_LOG_DIR}/error.log
2830
CustomLog ${APACHE_LOG_DIR}/access.log combined
31+
</VirtualHost>
32+
33+
# HTTPS Virtual Host
34+
<VirtualHost *:443>
35+
# ServerName directive, which sets the request scheme, hostname and port that
36+
ServerName localhost
37+
ServerAdmin webmaster@localhost
38+
DocumentRoot /app/public
39+
40+
# SSL Configuration
41+
SSLEngine on
42+
SSLCertificateFile /app/docker/ssl/localhost.pem
43+
SSLCertificateKeyFile /app/docker/ssl/localhost-key.pem
44+
45+
# Enable rewrite logging for debugging (remove in production)
46+
LogLevel warn rewrite:trace6
47+
48+
# Yii2 URL Rewriting Configuration
49+
RewriteEngine On
50+
51+
# Step 1: Exclude static files by extension completely
52+
RewriteRule \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|map)$ - [L,QSA]
53+
54+
# Step 2: Exclude specific asset directories by path
55+
RewriteRule ^(assets|image|images|css|js|fonts|media)/ - [L,QSA]
56+
57+
# Step 3: If it's an existing file, serve it directly
58+
RewriteCond %{REQUEST_FILENAME} -f
59+
RewriteRule ^ - [L]
60+
61+
# Step 4: If it's an existing directory, serve it directly
62+
RewriteCond %{REQUEST_FILENAME} -d
63+
RewriteRule ^ - [L]
64+
65+
# Step 5: Only if none of the above apply, send to index.php
66+
RewriteRule ^(.*)$ index.php [L,QSA]
2967

30-
# For most configuration files from conf-available/, which are
31-
# enabled or disabled at a global level, it is possible to
32-
# include a line for only one particular virtual host. For example the
33-
# following line enables the CGI configuration for this host only
34-
# after it has been globally disabled with "a2disconf".
35-
#Include conf-available/serve-cgi-bin.conf
36-
</VirtualHost>
68+
# Logging configuration for SSL
69+
ErrorLog ${APACHE_LOG_DIR}/ssl_error.log
70+
CustomLog ${APACHE_LOG_DIR}/ssl_access.log combined
71+
</VirtualHost>

docker/frankenphp/Caddyfile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
auto_https off
44
}
55

6-
# Main server block
7-
:80 {
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+
811
# Document root
912
root * /app/public
1013

@@ -16,6 +19,7 @@
1619
X-Frame-Options "SAMEORIGIN"
1720
X-XSS-Protection "1; mode=block"
1821
X-Content-Type-Options "nosniff"
22+
Strict-Transport-Security "max-age=31536000; includeSubDomains"
1923
-Server
2024
}
2125

@@ -50,3 +54,9 @@
5054
# Try files for Yii2 URL rewriting
5155
try_files {path} {path}/ /index.php?{query}
5256
}
57+
58+
# HTTP server block - redirect to HTTPS
59+
http://localhost:80 {
60+
# Redirect all HTTP traffic to HTTPS
61+
redir https://localhost:8444{uri} permanent
62+
}

docker/frankenphp/Dockerfile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
FROM dunglas/frankenphp:1.8-php8.4
22

3-
# change web server config
3+
# Change web server config
44
COPY docker/frankenphp/Caddyfile /etc/caddy/Caddyfile
55

6-
# set document root to /app/public (Yii2 structure)
6+
# Set document root to /app/public (Yii2 structure)
77
WORKDIR /app
88

9-
# install required system packages for PHP extensions for Yii 2.0 Framework
9+
# Install required system packages for PHP extensions for Yii 2.0 Framework
1010
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
1111
RUN install-php-extensions \
1212
bcmath \
@@ -22,13 +22,13 @@ RUN install-php-extensions \
2222
xdebug \
2323
zip
2424

25-
# set composer environment
25+
# Set composer environment
2626
ENV COMPOSER_ALLOW_SUPERUSER=1
2727

28-
# change PHP config
28+
# Change PHP config
2929
COPY docker/php/php.ini /usr/local/etc/php/conf.d/base.ini
3030

31-
# install supervisord and Node.js (includes npm)
31+
# Install supervisord and Node.js (includes npm)
3232
RUN apt-get update && apt-get install -y \
3333
supervisor \
3434
curl \
@@ -38,13 +38,13 @@ RUN apt-get update && apt-get install -y \
3838
&& apt-get clean \
3939
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
4040

41-
# copy supervisord config
41+
# Copy supervisord config
4242
COPY docker/frankenphp/supervisord/supervisord.conf /etc/supervisor/supervisord.conf
4343

44-
# copy supervisord program configs
44+
# Copy supervisord program configs
4545
COPY docker/frankenphp/supervisord/conf.d/frankenphp.conf /etc/supervisor/conf.d/frankenphp.conf
4646

47-
# copy queue worker config uncommented for use with yii2-queue
47+
# Copy queue worker config uncommented for use with yii2-queue
4848
#COPY docker/frankenphp/supervisord/conf.d/queue.conf /etc/supervisor/conf.d/queue.conf
4949

5050
RUN mkdir -p /var/run && chown -R www-data:www-data /var/run

docker/nginx/Dockerfile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
FROM yiisoftware/yii2-php:8.4-fpm-nginx
22

3-
# change nginx config
3+
# Change nginx config
44
COPY docker/nginx/nginx.conf /etc/nginx/nginx.conf
55
COPY docker/nginx/default.conf /etc/nginx/conf.d/default.conf
66

7-
# change PHP config
7+
# Change PHP config
88
COPY docker/php/php.ini /usr/local/etc/php/conf.d/base.ini
99

10-
# install supervisord and Node.js (includes npm)
10+
# Install supervisord and Node.js (includes npm)
1111
RUN apt-get update && apt-get install -y \
1212
supervisor \
1313
curl \
@@ -17,15 +17,15 @@ RUN apt-get update && apt-get install -y \
1717
&& apt-get clean \
1818
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
1919

20-
# copy supervisord config
20+
# Copy supervisord config
2121
COPY docker/nginx/supervisord/supervisord.conf /etc/supervisor/supervisord.conf
2222

23-
# copy supervisord program configs
23+
# Copy supervisord program configs
2424
COPY docker/nginx/supervisord/conf.d/nginx.conf /etc/supervisor/conf.d/nginx.conf
2525
COPY docker/nginx/supervisord/conf.d/php-fpm.conf /etc/supervisor/conf.d/php-fpm.conf
2626

27-
# copy queue worker config uncommented for use with yii2-queue
27+
# Copy queue worker config uncommented for use with yii2-queue
2828
#COPY docker/apache/supervisord/conf.d/queue.conf /etc/supervisor/conf.d/queue.conf
2929

30-
# run supervisord
30+
# Run supervisord
3131
CMD ["supervisord", "-c", "/etc/supervisor/supervisord.conf"]

0 commit comments

Comments
 (0)