Skip to content

Instantly share code, notes, and snippets.

@peteristhegreat
Last active September 13, 2024 15:02
Show Gist options
  • Save peteristhegreat/30c6f31272dbbc96669db8d95d3e8940 to your computer and use it in GitHub Desktop.
Save peteristhegreat/30c6f31272dbbc96669db8d95d3e8940 to your computer and use it in GitHub Desktop.
Amazon Linux 2023, Centos based certbot + nginx setup
sudo yum update
sudo yum install -y docker nginx certbot python3-certbot-nginx git

sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -a -G docker ec2-user

sudo systemctl start nginx
sudo systemctl enable nginx

# setup auto updates
# see https://serverfault.com/questions/1145773/how-to-setup-automatic-security-updates-on-aws-linux-al2023
echo -e '!/bin/bash\n
readonly V=$(/usr/bin/dnf check-release-update --latest-only --version-only 2>&1)\n
[ -n "$V" ] && /usr/bin/dnf upgrade --security --assumeyes --releasever=$V\n' | sudo tee /etc/cron.daily/dnf-updates

# symlink in your nginx config, and tweak the default in /etc/nginx/nginx.conf
sudo certbot --nginx -d example.com
sudo systemctl restart nginx

# examine the state of the auto renewal
ls /lib/systemd/system/certbot-renew.timer
sudo systemctl status certbot-renew.timer
sudo systemctl enable certbot-renew.timer
sudo systemctl start certbot-renew.timer
systemctl list-timers --all | grep certbot

Could not automatically find a matching server block

sudo certbot --nginx -d example.com

fails with "Could not automatically find a matching server block."

certbot/certbot#5817

Trim the default nginx config to not have a server block or change its name.

Dump something useful in the /etc/nginx/conf.d/ folder, if that is included in the default one.

See what script nginx is configured for with

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Consider setting up a proxy server to a modern app similar to what is on here:

https://medium.com/@yusufkaratoprak/setting-up-a-flask-application-with-docker-nginx-and-lets-encrypt-1a2f33f86867

or here

https://github.com/peteristhegreat/flask-docker-nginx-certbot/blob/main/ReadMe.md

Put that file in /etc/nginx/conf.d/www.example.com.conf

Also you may need /etc/nginx/proxy_params (included on debian, not on centos based nginx)

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;
proxy_set_header X-Forwarded-Proto $scheme;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment