Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save snassr/94894ac029e845b2f6d4ae2db4dcf8b4 to your computer and use it in GitHub Desktop.
Save snassr/94894ac029e845b2f6d4ae2db4dcf8b4 to your computer and use it in GitHub Desktop.
Medium Blog - Build and Deploy a Hugo Blog With Caddy - Setup Deployment Environment
# create a digital ocean ubuntu droplet (smallest size is OK for this example)
# in a new terminal (tab)
# export server username
export SERVER_USERNAME=root
# export ip of remote server (provided by digitalocean for example)
export REMOTESERVER_IP=45.55.250.178
# login to remote server
ssh $SERVER_USERNAME@$REMOTESERVER_IP
# export variables again (unnecessary if in the same terminal session as before)
export GITHUB_REPONAME=myblog
export GITHUB_USERNAME=snassr
export OS_REPOLOCATION=~/$GITHUB_REPONAME
export REMOTESERVER_IP=45.55.250.178
# install hugo & caddy
mkdir -p ~/Downloads
wget -O ~/Downloads/hugo_0.31.1_Linux-64bit.deb https://github.com/gohugoio/hugo/releases/download/v0.31.1/hugo_0.31.1_Linux-64bit.deb
sudo dpkg -i ~/Downloads/hugo_0.31.1_Linux-64bit.deb
curl https://getcaddy.com | bash -s personal http.git,http.minify
hugo version
caddy -version
# install repository myblog
git clone https://github.com/$GITHUB_USERNAME/myblog $OS_REPOLOCATION
# change directory to repo
cd $OS_REPOLOCATION/$GITHUB_REPONAME
# change base url for caddy to serve HTML/CSS/JS correctly
## this should be your domain name, used the IP for simplicity
sed -i 's#baseURL = "http://localhost/"#baseURL = "http://'${REMOTESERVER_IP}'/"#g' config.toml
# create new CaddyFile
# add a caddyfile for the cloud (setup for display on port 80)
cat << EOF > ./CaddyFile_cloud
$REMOTESERVER_IP:80 {
root $OS_REPOLOCATION/$GITHUB_REPONAME/public
}
EOF
# ## this should be a domain name, used the IP for simplicity
# sed -i 's#:80 {#'${REMOTESERVER_IP}' {#g' CaddyFile
# compile and produce (creates a /public folder with the sites HTML/CSS/JS files)
# run this after every update
hugo
# test server
caddy -conf=CaddyFile_cloud
# site should be available at http://$REMOTESERVER_IP
# add caddy service file for systemd
cat << EOF > ./caddy.service
[Unit]
Description=Caddy webserver
Documentation=https://caddyserver.com/
After=network.target
[Service]
User=$USER
LimitNOFILE=10000
PIDFile=/var/run/caddy/caddy.pid
ExecStart=/usr/local/bin/caddy -conf=$OS_REPOLOCATION/$GITHUB_REPONAME/CaddyFile_cloud -pidfile=/var/run/caddy/caddy.pid
Restart=on-failure
StartLimitInterval=600
[Install]
WantedBy=multi-user.target
EOF
# place systemd (service manager) file
sudo cp caddy.service /lib/systemd/system/caddy.service
# snable systemd to run on startup (auto-run, restarts...)
sudo systemctl enable caddy.service
# restart
sudo reboot
# once server is back up, visit http://$REMOTESERVER_IP
# commit
git add --all
git commit -m "Deployment - completed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment