Skip to content

Commit 35f916a

Browse files
committed
feat: enhance SSL configuration by making Certbot optional in Docker setup
1 parent 474193d commit 35f916a

4 files changed

Lines changed: 33 additions & 26 deletions

File tree

Dockerfile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ FROM php:$PHP_VERSION-fpm-$DEBIAN_VERSION
66
# Add environment variables for domain and port
77
ENV SERVER_NAME="localhost"
88
ENV SSL_ENABLED="false"
9+
ENV CERTBOT_ENABLED="true"
910

1011
ENV WP_DB_HOST="hub.docker.internal"
1112
ENV WP_DB_USER="wordpress"
@@ -129,11 +130,15 @@ RUN chown -R www-data /var/www/html
129130

130131
# Copy default configuration files of nginx
131132
RUN mkdir /usr/src/nginx-defaults
132-
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
133-
COPY ./nginx/wordpress.conf.include /etc/nginx/conf.d/wordpress.conf.include
134-
# COPY ./nginx/default_ssl.conf /usr/src/nginx-defaults/default_ssl.conf
135-
# COPY ./nginx/options-ssl-nginx.conf /usr/src/nginx-defaults/options-ssl-nginx.conf
136-
# RUN mkdir "/var/ssl";
133+
COPY ./nginx/default.conf /usr/src/nginx-defaults/default.conf
134+
COPY ./nginx/default_ssl.conf /usr/src/nginx-defaults/default_ssl.conf
135+
COPY ./nginx/wordpress.conf.include /usr/src/nginx-defaults/wordpress.conf.include
136+
RUN mkdir -p /etc/nginx/ssl
137+
COPY ./nginx/options-ssl-nginx.conf /etc/nginx/ssl/options-ssl-nginx.conf
138+
# Generate the Diffie-Hellman certificate
139+
RUN openssl dhparam -out /etc/nginx/ssl/ssl-dhparams.pem 2048
140+
# Create a directory for SSL certificates when certbot is disabled
141+
RUN mkdir "/var/ssl";
137142

138143
# Expose the default Nginx ports
139144
EXPOSE 80

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ services:
9090
environment:
9191
- SERVER_NAME=example.com
9292
- SSL_ENABLED=true
93+
- CERTBOT_ENABLED=true
9394
- WP_DB_HOST=db
9495
- WP_DB_USER=wordpress
9596
- WP_DB_PASSWORD=password
@@ -113,12 +114,7 @@ services:
113114
114115
Note that we use letsencrypt's certbot to generate SSL certificates for you, you need to prove the domain is controlled by you, in most case your domain name should already resolved to the host you run this container, otherwise the certbot will fail, and the container will not be able to serve.
115116
116-
~~We highly recommend using lets-encrypt as your SSL solution. You need to use tools such as certbot to generate SSL in the host, then copy the cert files from /etc/letsencrypt/live/{yourdomain.com} to the volume. Please note the options-ssl-nginx.conf and ssl-dhparams.pem files from /etc/letsencrypt of the host are also needed to be placed in the same volume. The typical content of this ssl volume should contain such files:~~
117-
118-
~~cert.pem chain.pem fullchain.pem options-ssl-nginx.conf privkey.pem README ssl ssl-dhparams.pem~~
119-
120-
~~Or you can directly mount /etc/letsencrypt/live/{yourdomain.com} to the /var/ssl volume.~~
121-
117+
However, if you want to use your own certificate or if your environment does not support certbot to automatically generate certificate, you can set CERTBOT_ENABLED to false, and mount a volumn to /var/ssl, then put your own fullchain.pem and privkey.pem to this folder.
122118
123119
## Develop
124120
Feel free to visit the repository site on Github: [https://github.com/augwit/wordpress/](https://github.com/augwit/wordpress/)

entrypoint.sh

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
#!/bin/bash
22

3-
# if [ "$SSL_ENABLED" = "true" ]; then
4-
# cp /usr/src/nginx-defaults/options-ssl-nginx.conf /var/ssl;
5-
# cp /usr/src/nginx-defaults/default_ssl.conf /etc/nginx/conf.d/;
6-
# fi
7-
8-
# Update entrypoint to configure Nginx and acquire SSL certificates
9-
sed -i "s/server_name localhost;/server_name $SERVER_NAME;/" /etc/nginx/conf.d/default.conf
10-
11-
if [ "$SSL_ENABLED" = "true" ]; then
12-
certbot --nginx -d $SERVER_NAME --non-interactive --agree-tos --register-unsafely-without-email -m admin@$SERVER_NAME
13-
# cerrtbot started nginx but we need to stop it for now. Later we will start it in the foreground.
14-
service nginx stop
3+
if [ -z "$(ls -A /etc/nginx/conf.d)" ]; then
4+
cp /usr/src/nginx-defaults/default.conf /etc/nginx/conf.d/;
5+
cp /usr/src/nginx-defaults/wordpress.conf.include /etc/nginx/conf.d/wordpress.conf.include
6+
7+
# Update entrypoint to configure Nginx and configure SSL certificates
8+
sed -i "s/server_name localhost;/server_name $SERVER_NAME;/" /etc/nginx/conf.d/default.conf
9+
10+
# If SSL is enabled and Certbot is not enabled, copy the default SSL configuration
11+
if [ "$SSL_ENABLED" = "true" ] && [ "$CERTBOT_ENABLED" = "false" ]; then
12+
cp /usr/src/nginx-defaults/default_ssl.conf /etc/nginx/conf.d/;
13+
fi
14+
15+
# If SSL is enabled and Certbot is enabled, run Certbot to obtain SSL certificates
16+
if [ "$SSL_ENABLED" = "true" ] && [ "$CERTBOT_ENABLED" = "true" ]; then
17+
certbot --nginx -d $SERVER_NAME --non-interactive --agree-tos --register-unsafely-without-email -m admin@$SERVER_NAME
18+
# cerrtbot started nginx but we need to stop it for now. Later we will start it in the foreground.
19+
service nginx stop
20+
fi
1521
fi
1622

1723
# Download the latest wordpress
@@ -24,7 +30,7 @@ if [ ! -f /var/www/html/index.php ]; then
2430
fi
2531

2632
# If wp-config.php does not exist and wp-config-sample.php exists, copy wp-config-sample.php to wp-config.php and update database configuration
27-
if [ ! -f /var/www/html//wp-config.php ] && [ -f /var/www/html/wp-config-sample.php ]; then
33+
if [ ! -f /var/www/html/wp-config.php ] && [ -f /var/www/html/wp-config-sample.php ]; then
2834
cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
2935
sed -i "s/database_name_here/${WP_DB_NAME}/" /var/www/html/wp-config.php
3036
sed -i "s/username_here/${WP_DB_USER}/" /var/www/html/wp-config.php

nginx/default_ssl.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ server {
66
listen 443 ssl;
77
ssl_certificate /var/ssl/fullchain.pem;
88
ssl_certificate_key /var/ssl/privkey.pem;
9-
include /var/ssl/options-ssl-nginx.conf;
10-
ssl_dhparam /var/ssl/ssl-dhparams.pem;
9+
include /etc/nginx/ssl/options-ssl-nginx.conf;
10+
ssl_dhparam /etc/nginx/ssl/ssl-dhparams.pem;
1111
}

0 commit comments

Comments
 (0)