Local machine as a web server without public IP using Cloudflare tunnel(part-1)
Tags: cloudflare, webserver, publicwebserver, localmachinewebserver
Published: 2024-02-22 10:20:48

we can use our local machine as a public webserver using Cloudflare tunnel. you do not need any public IP.

**Prerequisites**
1. PC or laptop
2. you need a public domain(possible without a domain)
3. Cloudflare account

[<u>**Cloudflare Tunnel**</u>](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/)
Cloudflare Tunnel provides you with a secure way to connect your resources to Cloudflare without a publicly routable IP address. With Tunnel, you do not send traffic to an external IP — instead, a lightweight daemon in your infrastructure (‘cloudflared’) creates outbound-only connections to Cloudflare’s global network. Cloudflare Tunnel can connect HTTP web servers, SSH servers, remote desktops, and other protocols safely to Cloudflare

## <u>**Step-1(Install OS)**</u>

I use Ubuntu OS in my daily work. So I installed an Ubuntu server on my laptop which I will use as a server machine. You can also use another OS (ubuntu-desktop, Mac OS, windows). after installation, you have to ensure that you connect to the internet, after setup internet your machine is ready to serve as web-server.

## <u>**Step-2(Connect your domain with cloudflare)**</u>

Create a Cloudflare account. after sign up then login in Cloudflare account. After login, you will redirect to the dashboard .From dashboard click <u>add a site</u> button. then enter your domain and submit then you will see name-server ,this name-serve add to your domain registered dashboard.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhv8rxegon8hjg7ehtka.png)

`Congratulations your domain and cloudflare are connected now. `

## [**<u>Step-3(Install cloudflare in your linux machine)</u>**](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/get-started/create-local-tunnel/#1-download-and-install-cloudflared)

- <u>Add Cloudflare’s package signing key:</u>

```
sudo mkdir -p --mode=0755 /usr/share/keyrings
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
```

- <u>Add Cloudflare’s apt repo to your apt repositories:</u>

```
echo "deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/cloudflared.list
```

- <u>Update repositories and install cloudflared:</u>

```
sudo apt-get update && sudo apt-get install cloudflared
```

After successfully install Cloudflare check
```
cloudflared -v
```

> Installing process for other OS - [Download And Install](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/get-started/create-local-tunnel/#1-download-and-install-cloudflared)


## **<u>[Step-4(Create Tunnel and use)](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/get-started/create-local-tunnel/#2-authenticate-cloudflared)</u>**

**1. Authenticate cloudflared**

```
sudo cloudflared tunnel login
```
After this command you will be redirected to your Cloudflare account otherwise you will get a link, then you will use this link after you will see list of domain which you attach on your cloudflare account . i selected my domain `iamalamin.com` .
after selected youru domain it will create `cert.pem` file to your machine `/root/.cloudflared/` directorey

**2.  Create cloudflare tunnel**

```
sudo cloudflared tunnel create <NAME>
```
after this command you will get a tunnel.this tunnel has `<Tunnel-UUID>` which is store in `/root/.cloudflared/` directory with `<Tunnel-UUID>.json`

```
sudo cloudflared tunnel list
```
show tunnel list in your machine


**3. Create configure file for my tunnel**

In your `/root/.cloudflared` directory, create a `config.yml ` file using any text editor. This file will configure the tunnel to route traffic from a given origin to the hostname of your choice.

```
tunnel: <Tunnel-UUID>
credentials-file: /root/.cloudflared/<Tunnel-UUID>.json

ingress:
  - hostname: example-app.com
    service: http://localhost:3000
    originRequest:
      noTLSVerify: true
      
  - service: http_status:404
```
Here service: `http://localhost:3000` this is your local application running url.

**4. Start routing traffic**

Now assign a CNAME record that points traffic to your tunnel subdomain:

```
sudo cloudflared tunnel route dns <Tunnel-UUID> <your-domain-name>
```
After this command a `CNAME` record will be created in your Cloudflare account domain dashboard.When I visited Cloudflare dashboard's `Websites > your-domain.com > DNS > Records` I saw that a new `CNAME` record is added in my domain's DNS record. This was added by the command I run.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ypsk6iiwzqgau2cwsbm0.png)


**5. Run the tunnel**

```
sudo cloudflared tunnel run <Tunnel-UUID or NAME>
```
You need to always run this command to access your application using this tunnel.

**6.  Run Cloudflared as a service**

To always run your Cloudflare you can use Cloudflare service on your machine.

```
sudo cloudflared service install
sudo systemctl start cloudflared
sudo systemctl status cloudflared
```

> Now your application will run from your local machine publicly using cloudflare tunnel