Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/data/certbot
/data/nginx/app.conf
2 changes: 1 addition & 1 deletion data/nginx/app.conf → data/nginx/app.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ server {
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

location / {
proxy_pass http://example.org;
proxy_pass http://your-web-server:3000;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Expand Down
42 changes: 34 additions & 8 deletions init-letsencrypt.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
#!/bin/bash

if ! [ -x "$(command -v docker-compose)" ]; then
echo 'Error: docker-compose is not installed.' >&2
if ! [ -x "$(command -v docker)" ]; then
echo 'Error: docker is not installed.' >&2
exit 1
fi

domains=(example.org www.example.org)
# Ask for domain
read -p "Enter your domain (e.g., example.org): " domain
if [ -z "$domain" ]; then
echo "Error: Domain is required." >&2
exit 1
fi

# Ask for webserver URL
read -p "Enter your webserver URL (e.g., http://localhost:3000): " webserver_url
if [ -z "$webserver_url" ]; then
echo "Error: Webserver URL is required." >&2
exit 1
fi

domains=($domain www.$domain)
rsa_key_size=4096
data_path="./data/certbot"
email="" # Adding a valid address is strongly recommended
staging=0 # Set to 1 if you're testing your setup to avoid hitting request limits

# Create nginx configuration file
echo "### Creating nginx configuration file ..."
if [ ! -f "data/nginx/app.conf.example" ]; then
echo "Error: data/nginx/app.conf.example not found." >&2
exit 1
fi

# Create the app.conf file with replacements
sed "s/example.org/$domain/g; s|http://your-web-server:3000|$webserver_url|g" data/nginx/app.conf.example > data/nginx/app.conf
echo "Created data/nginx/app.conf with domain: $domain and webserver URL: $webserver_url"
echo

if [ -d "$data_path" ]; then
read -p "Existing data found for $domains. Continue and replace existing certificate? (y/N) " decision
if [ "$decision" != "Y" ] && [ "$decision" != "y" ]; then
Expand All @@ -30,7 +56,7 @@ fi
echo "### Creating dummy certificate for $domains ..."
path="/etc/letsencrypt/live/$domains"
mkdir -p "$data_path/conf/live/$domains"
docker-compose run --rm --entrypoint "\
docker compose run --rm --entrypoint "\
openssl req -x509 -nodes -newkey rsa:$rsa_key_size -days 1\
-keyout '$path/privkey.pem' \
-out '$path/fullchain.pem' \
Expand All @@ -39,11 +65,11 @@ echo


echo "### Starting nginx ..."
docker-compose up --force-recreate -d nginx
docker compose up --force-recreate -d nginx
echo

echo "### Deleting dummy certificate for $domains ..."
docker-compose run --rm --entrypoint "\
docker compose run --rm --entrypoint "\
rm -Rf /etc/letsencrypt/live/$domains && \
rm -Rf /etc/letsencrypt/archive/$domains && \
rm -Rf /etc/letsencrypt/renewal/$domains.conf" certbot
Expand All @@ -66,7 +92,7 @@ esac
# Enable staging mode if needed
if [ $staging != "0" ]; then staging_arg="--staging"; fi

docker-compose run --rm --entrypoint "\
docker compose run --rm --entrypoint "\
certbot certonly --webroot -w /var/www/certbot \
$staging_arg \
$email_arg \
Expand All @@ -77,4 +103,4 @@ docker-compose run --rm --entrypoint "\
echo

echo "### Reloading nginx ..."
docker-compose exec nginx nginx -s reload
docker compose exec nginx nginx -s reload