We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Using the Caddy web server with Elixir Phoenix is really easy and straightforward.
Let's show how easy it can be to get up and running with Caddy.
Just for the reference, we're running Ubuntu here. If you are using another distribution, you might want to check the Caddy installation guide for the proper install procedure.
Let's first install Caddy itself as described in the documentation:
$ sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
$ 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
Next up is to edit the /etc/caddy/Caddyfile
, adding the mysite.com
website:
mysite.com, www.mysite.com {
root * /var/www/mysite.com
encode zstd gzip
reverse_proxy localhost:4000
}
As you can see, the config is really easy. Let's go over this line by line:
This first line indicates which hostnames we want to serve. We're serving both the domain with and without the www.
prefix.
The root
directive tells Caddy where the files for that site can be found. It's important to point to the public
folder here.
The encode
directive is used to compress the responses. In this case, we're enabling zstd
and gzip
compression.
The reverse_proxy
directive proxies requests to one or more backends. In our case, we are proxying to the Elixir Phoenix app running on localhost
port 4000
.
Once that is done, all that is left is to restart Caddy:
$ sudo systemctl restart caddy
You can check if it's up and running like using the status
command:
$ sudo systemctl status caddy
That's it, it's as easy as that. All the rest is done automatically. It will automatically generate the Let's Encrypt SSL certificate and serve the site.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.