Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion nginx.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ RUN apt-get update && \
sed \
git \
gnupg && \
apt-get clean
apt-get clean && \
snap install core && \
snap refresh core && \
snap install --classic certbot && \
ln -s /snap/bin/certbot /usr/bin/certbot

# Installing Nginx
ENV NGINX_AWS_URL https://${AWS_BUCKET}.s3.amazonaws.com/nginx
Expand Down
12 changes: 9 additions & 3 deletions nginx/nginx-conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ http {

keepalive_timeout 65;

# Listen on all URLs and redirect them to
# Listen on all URLs and redirect them to
server {
listen 80;
server_name _;

location '/.well-known/acme-challenge' {
default_type "text/plain";
root /var/www/letsencrypt;
}

#access_log logs/host.access.log main;
location / {
return 301 https://$host$request_uri;
Expand All @@ -53,8 +58,9 @@ http {
listen 443 ssl;
server_name SERVER_NAME_HERE;

ssl_certificate /web/internal-cert/cert.pem;
ssl_certificate_key /web/internal-cert/key.pem;
ssl_certificate /etc/letsencrypt/live/uclapi-rsa/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/uclapi-rsa/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/uclapi-rsa/chain.pem;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
Expand Down
29 changes: 28 additions & 1 deletion nginx/run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
#!/bin/bash

# Certbot env setup
if [ ${ENVIRONMENT} = "prod" ]; then
certbot_hostname="uclapi.com"
else
certbot_hostname="staging.ninja"
fi

if [ ${DRYRUN} = "1" ]; then
letsencrypt_url="https://acme-staging-v02.api.letsencrypt.org/directory"
else
letsencrypt_url="https://acme-v02.api.letsencrypt.org/directory"
fi

while /bin/true; do
# Certbot setup modified from docker-nginx-certbot
# https://github.com/JonasAlfredsson/docker-nginx-certbot/blob/b119b10640c1a19bbba7a40fd13ab4e14d801ee5/src/scripts/run_certbot.sh#L52-L66
# Certbot will write certificates to/etc/letsencrypt/live/uclapi-rsa/
# See nginx.conf for the actual use of the certificates
certbot certonly --agree-tos --keep --noninteractive \
--authenticator webroot --webroot-path=/var/www/letsencrypt \
--preferred-challenges http-01 \
--email "isd.apiteam@ucl.ac.uk" \
--server "${letsencrypt_url}" \
--key-type "rsa" \
--cert-name "uclapi-rsa" \
--domains ${certbot_hostname}

# Ensure Supervisor is alive first
ps aux | grep supervisor | grep -q -v grep
SUPERVISOR_STATUS=$?
Expand Down Expand Up @@ -36,4 +63,4 @@ while /bin/true; do
fi

sleep 60
done
done