Skip to content

Commit

Permalink
Add codes (#1)
Browse files Browse the repository at this point in the history
* add several folders

* add certbot_renewal.sh

* add nginx confs

* add docker compose files

* add moodle_config.php

* add moodle_docker-fpm.ini

* add .github

* add readme.md

* add Dockerfile

* update readme.md

* update readme.md

* update readme.md

* update readme.md

* update readme.md

* update readme.md

* update readme.md

* update readme.md

* update readme.md

* update readme.md

* update readme.md

* update readme.md
  • Loading branch information
rahmatnazali authored Nov 13, 2024
1 parent ff2d66e commit 8aa4c71
Show file tree
Hide file tree
Showing 15 changed files with 1,923 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: rahmatnazali
73 changes: 73 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
FROM php:8.2-fpm

WORKDIR /var/www/html

RUN apt-get update && \
apt-get install -y \
git \
zip \
curl

# Postgreqsl dependency
# https://github.com/docker-library/php/issues/221#issuecomment-254153971
RUN apt-get install -y libpq-dev \
&& docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
&& docker-php-ext-install pdo pdo_pgsql pgsql

# Install all required extensions
# gd
# https://hub.docker.com/_/php#:~:text=php%2Dsource%20delete-,PHP%20Core%20Extensions,-For%20example%2C%20if
RUN apt-get install -y \
libfreetype-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd

# zip
# https://stackoverflow.com/a/45775922/6558550
RUN apt-get install -y libzip-dev
RUN docker-php-ext-install zip

# intl
RUN apt-get install -y libicu-dev
RUN docker-php-ext-install intl

# opcache
# https://docs.moodle.org/405/en/OPcache
RUN docker-php-ext-configure opcache --enable-opcache \
&& docker-php-ext-install opcache

# soap
# https://stackoverflow.com/a/50121691/6558550
RUN apt-get install -y libxml2-dev
RUN docker-php-ext-install soap

# exif
RUN docker-php-ext-install exif

# set up cron for moodle
# https://forums.docker.com/t/cron-does-not-run-in-a-php-docker-container/103897
# https://stackoverflow.com/q/46235982/6558550
RUN apt-get install -y cron
RUN echo "* * * * * root /usr/local/bin/php /var/www/html/moodle/admin/cli/cron.php >/dev/null" >> /etc/crontab
RUN echo "0 0 * * * root echo \"This cronjob runs daily for monitoring. \" >> /var/log/moodle-cron.log 2>&1" >> /etc/crontab
# for debugging, see the moodle-cron.log for possible errors
#RUN echo "* * * * * root /usr/local/bin/php /var/www/html/moodle/admin/cli/cron.php >> /var/log/moodle-cron.log" >> /etc/crontab

# clean dependencies leftover
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Set up source code
COPY . moodle

RUN chown -R root moodle/
RUN chmod -R 755 moodle/

EXPOSE 9000

# https://stackoverflow.com/a/66280277/6558550
# - Set environment variables at /etc/environment to be used by cron
# - Run the cron service
# - Run the php-fpm
CMD bash -c "printenv > /etc/environment && cron && php-fpm"
3 changes: 3 additions & 0 deletions certbot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!.gitignore

18 changes: 18 additions & 0 deletions certbot_renewal.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This script should ideally be set on cron to run every 15 days or so
# assuming letsencrypt 3 months certificate period, with `certbot renewal` already evaluate time remaining.
# The result should also be logged on the host.
# 0 0 15 * * cd /home/moodle && bash certbot_renewal.sh >> /var/log/moodle-cron-ssl.log 2>&1

# Print current date for monitoring
date

# List currently active certificates
docker compose -f docker-compose.production.yaml run --rm certbot certificates

# Attempting SSL certificate renewal
docker compose -f docker-compose.production.yaml run --rm certbot renew

# Reloading nginx for the new certificates without downtime
docker exec nginx service nginx reload

echo -e '\n\n\n'
49 changes: 49 additions & 0 deletions docker-compose.dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# demonstrating local deployment with http and local database

services:
moodle:
container_name: moodle
build:
context: ./moodle
environment:
- DB_HOST=database
- DB_PORT=5432
- DB_NAME=moodle
- DB_USER=user
- DB_PASSWORD=password
- WWW_ROOT=http://localhost
volumes:
- moodle_source:/var/www/html/moodle
- ./moodle-data:/home/moodle-data:rw
- ./moodle_docker-fpm.ini:/usr/local/etc/php/conf.d/docker-fpm.ini:ro
- ./moodle_config.php:/var/www/html/moodle/config.php:ro
- ./moodle-plugins/mass_enroll:/var/www/html/moodle/local/mass_enroll:ro
depends_on:
- database
nginx:
image: nginx:latest
container_name: nginx
volumes:
- ./nginx/http.conf:/etc/nginx/conf.d/default.conf:ro
- moodle_source:/var/www/html/moodle
- ./moodle-data:/home/moodle-data:rw
ports:
- "80:80"
depends_on:
- moodle
database:
image: postgres:16.4-alpine
container_name: database
volumes:
- database_volume:/var/lib/postgresql/data/
restart: always
shm_size: 128mb # set shared memory limit when using docker-compose
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: moodle
ports:
- "5440:5432"
volumes:
moodle_source:
database_volume:
20 changes: 20 additions & 0 deletions docker-compose.ssl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# demonstrating ssl issue and renewal with nginx and certbot

services:
nginx:
image: nginx:latest
container_name: nginx
volumes:
- ./nginx/https-ssl-issuance.conf:/etc/nginx/conf.d/default.conf:ro
- ./certbot/www/:/var/www/certbot/:ro
- ./certbot/conf/:/etc/nginx/ssl/:ro
restart: always
ports:
- "80:80"
- "443:443"
certbot:
image: certbot/certbot:latest
container_name: certbot
volumes:
- ./certbot/www/:/var/www/certbot/:rw
- ./certbot/conf/:/etc/letsencrypt/:rw
43 changes: 43 additions & 0 deletions docker-compose.staging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# demonstrating https serving with certificate issuance and renewal with dedicated external database
# for production: copy this file and edit the DB params accordingly

services:
moodle:
container_name: moodle
build:
context: ./moodle
environment:
- DB_HOST=database
- DB_PORT=5432
- DB_NAME=moodle
- DB_USER=user
- DB_PASSWORD=password
- WWW_ROOT=https://example.com
volumes:
- moodle_source:/var/www/html/moodle
- ./moodle-data:/home/moodle-data:rw
- ./moodle_docker-fpm.ini:/usr/local/etc/php/conf.d/docker-fpm.ini:ro
- ./moodle_config.php:/var/www/html/moodle/config.php:ro
- ./moodle-plugins/mass_enroll:/var/www/html/moodle/local/mass_enroll:ro
nginx:
image: nginx:latest
container_name: nginx
volumes:
- moodle_source:/var/www/html/moodle
- ./nginx/https.conf:/etc/nginx/conf.d/default.conf:ro
- ./moodle-data:/home/moodle-data:rw
- ./certbot/www/:/var/www/certbot/:ro
- ./certbot/conf/:/etc/nginx/ssl/:ro
ports:
- "80:80"
- "443:443"
depends_on:
- moodle
certbot:
image: certbot/certbot:latest
container_name: certbot
volumes:
- ./certbot/www/:/var/www/certbot/:rw
- ./certbot/conf/:/etc/letsencrypt/:rw
volumes:
moodle_source:
Empty file added moodle-plugins/.gitkeep
Empty file.
Empty file added moodle/.gitkeep
Empty file.
Loading

0 comments on commit 8aa4c71

Please sign in to comment.