Docs cipi.yml Web panel API MCP Guides Compare GitHub Telegram channel
  1. Home
  2. Guides
  3. Put a Laravel server behind Cloudflare Zero Trust, step by step
Security

Put a Laravel server behind Cloudflare Zero Trust, step by step

From zero to a server with no open ports: what a Cloudflare Tunnel and Access are, the token and account you need, routing apps through the tunnel, a login wall for staging and the panel, SSH through Cloudflare, certificates that still renew, closing ports safely, and how to undo every step.

By Updated 4 min read

Normally a server waits for visitors on open ports (80, 443 for websites, 22 for SSH), and anyone on the internet can knock. With a Cloudflare Tunnel, your server dials out to Cloudflare instead, and visitors reach it through Cloudflare. Once every app goes through the tunnel you can close the ports completely: nothing left to attack directly. Cipi does it in small, reversible steps, and nothing closes until you ask.

Key ideas, in plain words#

TermMeaning
TunnelA connection your server opens to Cloudflare; visitor traffic comes back through it
cloudflaredThe small program on the server that keeps the tunnel open
AccessCloudflare's login wall: only people you allow get through
PolicyWho is allowed: e-mail addresses, a domain (@acme.com), a group
LockingClosing ports 80/443 and 22 once the tunnel carries everything
  • Visitors and youbrowser, ssh
  • CloudflareCDN, WAF, Access login
  • Tunneloutbound from your server
  • Your serverports 80, 443, 22 closed

Before you start#

  • Your domain's DNS on Cloudflare (free plan is fine). See Cloudflare in front of Laravel if it isn't yet.
  • A Cloudflare Zero Trust organisation: open Zero Trust in the Cloudflare dashboard once and choose a team name (free plan). Under Settings → Authentication keep One-time PIN (login codes by e-mail) or add Google/GitHub.
  • Your Account ID: Cloudflare dashboard → your domain → right column, Account ID.
  • Access to your VPS provider's web console, in case you lock yourself out.
  • cloudflared on your laptop for SSH later (brew install cloudflared, or the package from Cloudflare's site).

1. Create the token and enable the tunnel#

In Cloudflare: My Profile → API Tokens → Create Token → Custom token, with these permissions:

ScopePermissionUsed for
Account · Cloudflare TunnelEditCreating the tunnel
Account · Access: Apps and PoliciesEditLogin walls
Zone · DNSEditPointing hostnames at the tunnel
Zone · SSL and CertificatesEdit (optional)Origin CA certificates
bash
sudo cipi zt token set --token=CF_API_TOKEN --account=ACCOUNT_ID
sudo cipi zt enable
sudo cipi zt status

enable installs cloudflared, creates the tunnel, and teaches Nginx, Fail2ban and CrowdSec Cloudflare's IP ranges so apps see real visitor IPs. No port is closed: if anything goes wrong, your sites keep working as before.

2. Route the apps through the tunnel#

bash
sudo cipi zt hostname add shop          # public site, through CDN and WAF
sudo cipi zt hostname add shop-stg      # staging
sudo cipi zt hostname add --gui         # the web panel, if installed

Each command points the app's hostname at the tunnel (a DNS record in Cloudflare). Now add a login wall where only your team should enter:

bash
sudo cipi zt access enable shop-stg
sudo cipi zt access enable --gui
Step 1 of 4Visitor opens staging

The request reaches Cloudflare, not your server.

3. SSH through the tunnel#

bash
sudo cipi zt ssh enable --hostname=ssh.example.com

On your laptop, add to ~/.ssh/config (Cipi prints this block for you):

text
Host ssh.example.com
  ProxyCommand cloudflared access ssh --hostname %h
  User cipi
  IdentityFile ~/.ssh/id_ed25519

Then ssh ssh.example.com. The first time, a browser window asks you to log in to Access; after that, SSH works as usual, with your key.

4. Certificates that keep renewing#

Once port 80 is closed, Let's Encrypt can't check your server over HTTP. Switch each app to DNS validation, or use a long-lived Cloudflare Origin CA certificate:

OptionCommandNotes
Let's Encrypt over DNSsudo cipi ssl dns set --token=… then sudo cipi ssl install shop --dns=cloudflareRenews automatically; also works without the tunnel
Cloudflare Origin CAsudo cipi zt origin-cert shop15 years; trusted only by Cloudflare, fine behind the proxy

cipi zt lock http refuses to run while any app still renews over HTTP, so you can't forget one.

5. Test everything from outside#

From your laptop (not from the server):

CheckExpected
https://shop.example.comThe site, through Cloudflare
https://shop-stg.example.comCloudflare's login page first
ssh ssh.example.comA shell, after the Access login
A git push to the deploy branchA deploy (check sudo cipi deploy shop --status)
sudo cipi zt statusTunnel healthy, every hostname listed

Don't go on until all five pass.

6. Close the ports#

bash
sudo cipi zt lock http --yes     # 80/443 closed (or Cloudflare-only if something isn't on the tunnel)
sudo cipi zt lock ssh --yes      # 22 closed: SSH only through Access
sudo cipi zt status

Lock SSH last, only after ssh ssh.example.com works from your machine, and keep your provider's web console open in another tab. Cipi refuses to close 22 if the tunnel doesn't carry SSH yet.

Undo any step#

StepUndo
lock sshsudo cipi zt ssh unlock
lock httpsudo cipi zt unlock http
access enablesudo cipi zt access disable <app>
hostname addsudo cipi zt hostname remove <app>
ssh enablesudo cipi zt ssh disable
Everythingsudo cipi zt disable (reopens 22/80/443, removes what Cipi created in Cloudflare)

Locked out? Log in through your provider's web console as cipi and run the unlock commands.

Troubleshooting#

ProblemFix
502 / 1033 from CloudflareTunnel down: sudo cipi zt status; check cloudflared is running
Access login loopsClear cookies for the team domain; check the policy includes your e-mail
SSH: websocket: bad handshakecloudflared missing or old on your laptop; update it
lock http refusedAn app still renews over HTTP: move it to --dns=cloudflare or origin-cert
Webhook deploys stoppedBot Fight Mode or a WAF rule challenges /cipi/webhook: add a skip rule
Visitors' IPs all the sameRun sudo cipi zt enable again to refresh the real-IP setup; Laravel must trust proxies (guide)
Locked out completelyProvider web console → sudo cipi zt ssh unlock

Questions

Do visitors notice?

No. Public apps stay public and get Cloudflare's CDN and WAF. Only the hostnames you protect with Access ask for a login (an e-mailed code, Google, GitHub or your SSO).

Do Git webhooks still deploy?

Yes. When you protect an app with Access, Cipi bypasses /cipi/webhook automatically, so pushes still deploy.

What if Cloudflare is down?

Traffic through the tunnel stops. Keep a break-glass path: your provider's web console can always reach the server, and cipi zt unlock http / cipi zt ssh unlock reopen the ports.

Do deploys still work with port 22 closed?

Yes. Deploys run on the server itself. Your CI can deploy through the tunnel (cloudflared access ssh) or with cipi-cli over the API.

Is it free?

Cloudflare Tunnel is free, and Cloudflare Zero Trust is free for small teams (check Cloudflare's current limits). Cipi is free.

Can I undo it?

Every step has its undo, and cipi zt disable reopens 22/80/443 and removes everything Cipi created in your Cloudflare account. Let's Encrypt certificates are left alone.

How is this different from just the orange cloud?

The orange cloud hides your IP but the server's ports stay open, so attackers who find the IP can bypass Cloudflare. With a tunnel and locked ports, there's nothing to reach.

Try it on your own server

Free and open source. One command on a fresh Ubuntu 24.04 or 26.04 VPS.

wget -O - https://cipi.sh/setup.sh | bash
Read the quickstart