Easily Start a Localtunnel for Your DDEV Sites
Every time I needed a public tunnel for local development, I was repeating the same handful of steps. Localtunnel is a great free alternative to ngrok, and it deserves a proper DDEV command. Follow the steps to make one:
Step 1: Create this file in your project’s directory: .ddev/commands/host/localtunnel
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#!/usr/bin/env bash ## Description: Start a localtunnel for this DDEV project ## Usage: lt [subdomain] ## Example: ddev lt myname-mysite set -e SUBDOMAIN=${1:-${DDEV_SITENAME}} # Get the HTTPS host port from docker PORT=$(docker inspect "ddev-${DDEV_SITENAME}-web" \ --format '{{range $p, $conf := .NetworkSettings.Ports}}{{if eq $p "80/tcp"}}{{(index $conf 0).HostPort}}{{end}}{{end}}') if [ -z "$PORT" ]; then echo "Could not determine HTTPS port for ${DDEV_SITENAME}. Is DDEV running?" exit 1 fi echo "Starting localtunnel for ${DDEV_SITENAME} on port ${PORT}..." echo "Subdomain: https://${SUBDOMAIN}.loca.lt" echo "" lt --port="${PORT}" --local-host "127.0.0.1" --subdomain="${SUBDOMAIN}" |
Step 2: chmod +x .ddev/commands/host/localtunnel
Step 3: Use it like this: ddev lt adam-mysite
Of course, this will only work if you have localtunnel installed ( brew install localtunnel ).
When you run the command it’ll show you the resulting URL (e.g., https://adam-mysite.local.lt).
When finished, hit CTRL-C (on a Mac, at least) to close the session.