Instantly share code, notes, and snippets.

Embed
What would you like to do?
bash script to install and set up Caddy web server with a static website hosted on a GitHub repository
# Install script for Ubuntu server 16.0+.
# Installs and sets up Caddy web server with a static website hosted on a GitHub repository.
# Used in https://blog.dipasquale.fr/en/2018/12/27/build-and-deploy-huge-static-websites-with-caddy/
# This script is a condensed version of https://blog.digitalocean.com/deploying-a-fully-automated-git-based-static-website-in-under-5-minutes/
# Download and extract Caddy :
wget -O caddy.tar.gz "https://caddyserver.com/download/linux/amd64?plugins=http.git&license=personal"
mkdir caddy
tar vxf caddy.tar.gz -C caddy
cd caddy
# install the binary :
sudo cp caddy /usr/local/bin
sudo chown root:root /usr/local/bin/caddy
sudo chmod 755 /usr/local/bin/caddy
# allow it to listen on protected ports :
sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/caddy
# create the configuration directories and set proper permissions :
sudo mkdir /etc/caddy
sudo chown -R root:www-data /etc/caddy
sudo mkdir /etc/ssl/caddy
sudo chown -R www-data:root /etc/ssl/caddy
sudo chmod 0770 /etc/ssl/caddy
# install the Systemd service file :
sudo cp init/linux-systemd/caddy.service /etc/systemd/system/
sudo chown root:root /etc/systemd/system/caddy.service
sudo chmod 644 /etc/systemd/system/caddy.service
sudo systemctl daemon-reload
# get external IP (cf. https://unix.stackexchange.com/a/194136)
IP=`dig +short myip.opendns.com @resolver1.opendns.com`
# Use nip.io to have a valid domain name for HTTPS
URL=https://$IP.nip.io
# write a default Caddyfile
cat <<EOM | sudo tee /etc/caddy/Caddyfile
https://$IP.nip.io {
internal /.git
gzip
redir 301 {
if {scheme} is http
/ https://{host}{uri}
}
git https://github.com/kamaln7/basic-static-website.git {
interval 300
}
}
EOM
# start Caddy service
sudo systemctl start caddy
sudo systemctl enable caddy
echo "wait 10 seconds ..."
sleep 10
sudo systemctl restart caddy
echo "last wait of 10 seconds ..."
sleep 10
echo "All done ! open $URL in your browser."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment