-
Notifications
You must be signed in to change notification settings - Fork 22
Add SSL setup script for Dockerized Nginx with Let's Encrypt #745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: version-2
Are you sure you want to change the base?
Changes from all commits
e583ad9
a547541
da2616b
6ddfcd4
2cd79ff
27fcce5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #!/bin/bash | ||
|
|
||
| # ------------------------------------------------------------------------------ | ||
| # Developer Details | ||
| # ------------------------------------------------------------------------------ | ||
| # Name: Uday Kumar | ||
| # Contact: uday.kumar@bridgeconn.com | ||
| # Date: 21 Nov 2023 | ||
| # Description: This script sets up and manages Let's Encrypt SSL, including automatic certificate renewal. | ||
| # ------------------------------------------------------------------------------ | ||
|
|
||
|
|
||
| # Load environment variables | ||
| if [ -f prod.env ]; then | ||
| source prod.env | ||
| else | ||
| echo "prod.env file not found" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Configuration variables | ||
| NGINX_CONTAINER_NAME="docker-web-server-with-cert-1" | ||
| SSL_CERTS_DIR="/certbot/conf" | ||
| WEBROOT_DIR="/certbot/www" | ||
|
|
||
| # Generate SSL certificates | ||
| generate_certificates() { | ||
| echo "Generating SSL certificates for $DOMAIN..." | ||
|
|
||
| docker run --rm -it \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So does this run docker within docker?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I also checked that the current approach isn't ideal. If it's not suitable, I'm working on creating a single Docker solution, though it's taking some time. After your comment, I tried but I am getting the same issue as certificate not creating. |
||
| -v "$(pwd)${SSL_CERTS_DIR}:/etc/letsencrypt" \ | ||
| -p 80:80 \ | ||
| certbot/certbot certonly --standalone \ | ||
| --email "${CERTBOT_EMAIL}" --agree-tos --no-eff-email \ | ||
| -d "${VACHAN_DOMAIN}" --non-interactive --verbose | ||
|
|
||
| if [ $? -ne 0 ]; then | ||
| echo "Error: Failed to generate SSL certificates." | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| # Renew SSL certificates | ||
| renew_certificates() { | ||
| echo "Renewing SSL certificates for $DOMAIN..." | ||
|
|
||
| docker run --rm \ | ||
| -v "$(pwd)${SSL_CERTS_DIR}:/etc/letsencrypt" \ | ||
| certbot/certbot renew --non-interactive --verbose | ||
|
|
||
| if [ $? -ne 0 ]; then | ||
| echo "Error: Failed to renew SSL certificates." | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| # Restart Nginx container | ||
| start_nginx_container() { | ||
| echo "Starting Nginx container: ${NGINX_CONTAINER_NAME}..." | ||
| docker restart "${NGINX_CONTAINER_NAME}" || { echo "Failed to start Nginx container"; exit 1; } | ||
| } | ||
|
|
||
|
|
||
| # Execute the functions | ||
| if [ ! -e "${SSL_CERTS_DIR}/live/${DOMAIN}/fullchain.pem" ]; then | ||
| generate_certificates | ||
| start_nginx_container | ||
| else | ||
| renew_certificates | ||
| fi | ||
|
|
||
|
|
||
| echo "Script completed successfully." | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -267,25 +267,29 @@ services: | |
| volumes: | ||
| - ./nginx/prod/app.conf.template:/etc/nginx/templates/default.conf.template:ro | ||
| - ./certbot/www:/var/www/certbot/:ro | ||
| - ./certbot/conf/:/etc/nginx/ssl/:ro | ||
| - ./certbot/conf:/etc/nginx/certs/:ro | ||
| - logs-vol:/var/log/nginx/ | ||
| environment: | ||
| - VACHAN_DOMAIN=${VACHAN_DOMAIN} | ||
| - VACHAN_DOMAIN=${VACHAN_DOMAIN} | ||
| profiles: | ||
| - deployment | ||
| networks: | ||
| - VE-network | ||
|
|
||
| certbot: | ||
| image: certbot/certbot:latest | ||
| image: certbot/certbot | ||
| volumes: | ||
| - ./certbot/www/:/var/www/certbot/:rw | ||
| - ./certbot/conf/:/etc/letsencrypt/:rw | ||
| - ./certbot/conf:/etc/letsencrypt | ||
| - ./auto_ssl_script.sh:/auto_ssl_script.sh | ||
| profiles: | ||
| - deployment | ||
| entrypoint: /bin/sh -c ' | ||
| while :; do | ||
| /auto_ssl_script.sh; | ||
| sleep 80d; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to use the ofelia-scheduler which is already used for scheduled backups for this task? |
||
| done;' | ||
| networks: | ||
| - VE-network | ||
|
|
||
| - VE-network | ||
|
|
||
| ofelia-scheduler: | ||
| image: mcuadros/ofelia:v0.3.7 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to not hardcode this and get this from the env?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This we can take from env for sure