Deploy a Gunicorn application using a Cloudflare tunnel

Intro

I have a small mini PC at home with Linux installed.

I have always wondered how to host my websites on this PC...

I once tried to point the DNS A record directly to the public IP address of my Mini PC. It did not work.

The problem had to do with my internet provider not allowing port forwarding. Or something like that.

Luckily Clouflare is there for us.

Set up a Cloudflare account and domain

First of all, you need an accound. So, visit Cloudflare and sign up.

Then add your domain in Cloudflare.

Cloudflare header

Enter your domain name down there:

Input of the domain name

Choose a plan. The free plan is enough for what we are trying to do.

Cloudflare plans

And complete the domain registration just by adding the Cloudflare Nameservers to your domain Registar.

Nameservers for the domain

When done, the domain registration in Cloudflare can take some minutes or hours. So, meanwhile you can create the tunnel.

Cloudflare Tunnel

Go to the Zero Trust Overview. And click on Acces > Tunnels.

Tunnel list

Once there, create your first tunnel. Click on + Create a tunnel. Then enter the name. It does not matter which one, it should be a helpful name for yourself.

Tunnel name

Then proceed with installing the Tunnel connector in your machine by selecting first you operating system.

Tunnel connector

This will install a service in your PC a token.

cloudflared service

Check if your tunnel is active. Once it is active you can now connect your domains

Gunicorn

Finally, you can add a public hostname in the tunnel and point it out to your gunicorn UNIX sock file (/run/gunicorn.sock).

tunnel public hostname

In case your gunicorn is bind with a localhost and a port. For instance gunicorn <--args> --bind 127.0.0.1:8000, you can select the http Service type in the hostname.

Gunicorn bind to localhost

🌐 Change language