Shammah Rashad

Software Engineer & Physicist


Back
18 Oct 2024

Caddy server setup for local development

Often when developing web projects locally I often have multiple services running on the localhost on different ports and after some time it gets annoying to run localhost:PORT every time to get to these services. A better approach would be to have a local DNS server running that points say for example api.project.local or web.project_name.local to localhost:PORT.

Editing the local /etc/hosts file does not allow adding ports so the best solution is to install a lightweight server and set custom domain mappings in /etc/hosts file. These days I am experimenting with Caddy server, which is written in Go and is quite easy to configure (with SSL) and get started. Easier than traefik but not as reliable as nginx.

Installing Caddy on Linux

For detailed installation instructions follow Caddy Installation Instruction

sudo apt install -y debian-keyring debian-archive-keyring\
apt-transport-https curl curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key'\
| sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' \
| sudo tee /etc/apt/sources.list.d/caddy-stable.list \
sudo apt update sudo apt install caddy

Local domain names with hosts file

Simply edit the /etc/hosts file with sudo privileges, add map a custom domain to the loopback address as follows. 127.0.0.1 PROJECT_NAME.local etc. Now add a reverse_proxy for the local domain by editing the Caddyfile

Updating Caddyfile

Caddy configs live in a Caddyfile. Note uppercase C. This can be in any directory or the system default Caddyfile is in /etc/caddy/Caddyfile. A simple reverse proxy setup would look like follows


web.PROJECT_NAME.local {
	reverse_proxy localhost:PORT
}

Now simply reload the caddy server by running caddy reload or caddy run in the same directory as the Caddyfile. Optionally you may want to add it to .gitignore.