16 June 2021

Enabling HTTPS (SSL) for Laravel Sail using Caddy

I've been using Laravel Sail since it launched and have loved the simple, Docker-based approach to running Laravel on my local machine. However, recently I ran across the issue that Laravel Sail doesn't support HTTPS (SSL). Under the hood Sail uses the php artisan serve command to serve web pages instead of using a full blown web server such as Nginx or Apache. This is a neat trick to keep things simple and lightweight but it's not great if you're working on a project that requires some features that a full web server might offer (such as HTTPS).

As Laravel Sail is basically just a thin wrapper around Docker, customising it is pretty simple. My solution to this problem was to use Caddy as a reverse proxy to the Laravel Sail container. Caddy is great for this as it's very easy to configure and offers a great automatic HTTPS feature. To get this working I needed to make a couple of small changes to my config:

  1. Add Caddy as a service to my docker-compose-yml
  2. Create a simple Caddyfile and configure it as a reverse proxy
  3. Set up an endpoint for Caddy to authorise which domains it generates certificates for

The full source code changes can be seen in this gist.

The final step is to trust the Caddy root certificate so you don't constantly get warnings about the local certificates being invalid. This can be done by:

  1. Find the caddy container ID using docker ps.
  2. Copy the root certificate to your host machine by running docker cp {container_id}:/config/caddy/pki/authorities/local/root.crt ~/Desktop.
  3. Open up "Keychain Access.app" and drag and drop the certificate into the "login" keychain.
  4. Open the certificate (it should be called something like "Caddy Local Authority") and configure it to "Always Trust".

If you've followed these steps you should now have wildcard, automatic HTTPS running on your Laravel Sail project!

Looking for more?

Subscribe to my newsletter to get infrequent updates in your inbox. Or follow me on Twitter.