How I Setup a Web Server
After doing this setup, I will be able to do a git push
to Gitlab to update by static websites. For go sites, there is still a manual step or two.
- Start a new droplet on Digital Ocean using Ubuntu 18.04
- Do initial setup, using script they provide: https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-18-04
ufw allow http
ufw allow https
- Install git
sudo apt-get install git
- Install caddy
curl https://getcaddy.com | bash -s personal http.git,http.realip,tls.dns.cloudflare
- Install go
sudo apt-get install golang-go
- This is so I can compile on the server and only have to push code, not binaries.
- Install hugo
sudo apt-get install hugo
- This is for building static sites on the server, so I only have to push the templates and content.
vhosts directory
All of caddy's sites will have their own files in the vhosts directory.
mkdir ~/logs
mkdir ~/vhosts
echo "import vhosts/*" > ~/Caddyfile
Create helper shell scripts
caddy.sh
Start the caddy server. Fill in the environment variables with the correct values.
#! /bin/sh
CLOUDFLARE_EMAIL= CLOUDFLARE_API_KEY= caddy -conf ~/Caddyfile -log ~/logs/caddy.log &
hard-reload-caddy.sh
Kill the caddy server and restart it.
#! /bin/sh
kill `pgrep caddy`
./caddy.sh
reload-caddy.sh
Send USR2 signal to the caddy process so that it will reload itself gracefully.
#! /bin/sh
kill -s USR2 `pgrep caddy`
runhugo.sh
Runs hugo in the given directory.
#! /bin/sh
cd $1
hugo
update_service
Build from go source and create a systemd service file. Currently really doing a manual step here.
#! /bin/sh
cd ~/go/src/gitlab.com/flowonyx/$1/$2
go get ./...
go build -o ~/$1
echo "[Unit]" > /etc/systemd/system/$1.service
echo "Description=Web server" >> /etc/systemd/system/$1.service
echo "" >> /etc/systemd/system/$1.service
echo "[Service]" >> /etc/systemd/system/$1.service
echo "WorkingDirectory=~/go/src/gitlab.com/flowonyx/$1/src" >> /etc/systemd/system/$1.service
echo "ExecStart=~/$1" >> /etc/systemd/system/$1.service
echo "User=joel" >> /etc/systemd/system/$1.service
echo "Group=joel" >> /etc/systemd/system/$1.service
echo "UMask=007" >> /etc/systemd/system/$1.service
echo "" >> /etc/systemd/system/$1.service
echo "[Install]" >> /etc/systemd/system/$1.service
echo "WantedBy=multi-user.target" >> /etc/systemd/system/$1.service
vhosts files
For static sites, use the git plugin for caddy.
example.com, www.example.com {
root ~/example/public
git {
repo git@gitlab.com:flowonyx/example.git
key ~/.ssh/hook_key
path ~/example
clone_args "--depth=1"
hook /updatehook SECRET
then ~/runhugo.sh ~/example
}
log ~/logs/example.log
}
Remember to setup the hook in Gitlab
Go to Settings -> Integrations for the project and create a "Push event" hook that will use https://example.com/updatehook with the specified SECRET.
Now every time I push to Gitlab, it will post to that hook and the site will get updated on the server.