Host your website on an Android phone
Tags: programming, webdev, tutorial, node
Published: 2023-05-01 15:56:27

This blog - https://androidblog.a.pinggy.io is being served from my $180 Android phone running Android 11. This post describes how I turned my Android device into a web server which can be accessed from a public URL, and how you can do it too.

I used [Termux](https://termux.dev/en/), which is an Android terminal emulator on which we can run a web server such as a Node.js http-server, and **[Pinggy](https://pinggy.io/)** for obtaining public URLs for accessing that server.

To give you a glimpse of the setup, here are some photos. The first image shows the Node.js http-server running, while the second image displays Pinggy’s terminal user interface showing the live requests to the website as they arrive.


![The first image shows the Node.js http-server running, while the second image displays Pinggy’s terminal user interface showing the live requests to the website as they arrive.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m44i6h6mjbmfzgpfevw6.jpg)


Follow these simple steps to serve your own webpage from your Android phone.

## Step 1. Install Termux
<a href="https://termux.dev/en/" target="_blank">`Termux`</a> is an application for Android that emulates a terminal and Linux environment. Unlike other similar apps, it does not require rooting or any additional setup. It comes with a basic system, and you can install more packages using the APT package manager.

You can get the Termux app on F-Droid through this <a href="https://f-droid.org/en/packages/com.termux/" target="_blank">F-Droid link</a>.

You can *directly download the Termux APK from the website*.
Simply click on the "Download APK" link located at the bottom of each version section. Read more in this <a href="https://github.com/termux/termux-app#f-droid" target="_blank">GitHub link</a>.

*Note that it is NOT necessary to download the F-Droid app* (from the "Download F-Droid" link) to install Termux. 

## Step 2. Install Packages

In order to serve a webpage, we need to install a web server on our device. We also install openssh client which will help us in sharing the website later.

Update packages:

```bash
pkg update
pkg upgrade
```

Install openssh client and Node.js:
```bash
pkg install openssh
pkg install nodejs-lts
```

Once these are installed correctly, you will be able to check the versions of `node` and `npm` using `node --version` and `npm --version`


![Android phone showing node and npm version on termux](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vmx7tacj8jle8fpwvw9t.png)


## Step 3. Start server and Pinggy tunnel

You can first create a sample HTML page by creating an `index.html` file as follows:

```bash
echo "<h1>Hello World!</h1>" > index.html
```

You can use the `nano` editor to edit the webpage from the terminal: `nano index.html`. If you prefer vim then you can also install that.

**Now start the http-server:**
```bash
npx http-server &
```

*Note:* we add a `&` at the end of the command. This will allow the http server to run in background. Use the `fg` command to bring it to foreground and press *Ctrl + C* to stop it.

This will start the server on port `8080` by default. Check the output to confirm.


**Start Pinggy to get a public URL:**

```bash
ssh -p 443 -R0:localhost:8080 a.pinggy.io
```

You will get a public URL such as https://ranxyzxxxx.a.pinggy.io, which you can use to access your server running on your phone! Next we will discuss how to use a domain or subdomain of your choice.

In case you get a different port than `8080` on the above command, change it in the Pinggy command also. You can use *Ctrl + C* to stop the tunnel.

Now share your URL with your friends and watch live stats of visitors on the Pinggy terminal user interface. You can also hit the return key to see details about the the request and response headers including user agent, etc.


![1. Android phone with node http server on termux, 2. Android phone with pinggy tunnel, 3. Android phone with pinggy tunnel showing request details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7r1fwjs50tikkdjygulb.png)


The first image above shows the Node.js http-server running and listening on port 8080. The second screenshot shows the Pinggy tunnel starting and providing a public URL. It is also showing the live visitor requests to the server. The third screenshot shows more details about a particular request.

## Connect it to your own domain

You can also use your own domain or choose a subdomain for accessing this server. Or you can also choose a suitable subdomain such as https://myandroidblog.a.pinggy.io .

First you need to sign-up for <a href="https://pinggy.io" target="_blank">**`Pinggy`**</a> and subscribe to Pinggy Pro. Then go to the dashboard and click on *Custom Domains* on the nav bar.

Put your own custom domain that you want to use and Pinggy will provide a CNAME record that you need to configure in your DNS settings of the domain.

After the CNAME record is set, just click Validate followed by Issue Certificate button to finish configuring the custom domain.


[![Pinggy custom domain configuration page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lzrssbiojohzx194xh73.jpg)](https://pinggy.io)


Once the custom domain is configured, just use the token from Pinggy dashboard in the pinggy tunnel command:

```bash
ssh -p 443 -R0:localhost:8080  <put your pinggy token here>@a.pinggy.io
```


In conclusion, hosting a website or blog from an Android device may seem like an impossible feat, but with the right tools, it is quite achievable. In this blog post, we have explored the steps required to turn your Android device into a web server that can be accessed from a public URL or your own domain. By using an Android terminal emulator like Termux and obtaining a public URL through Pinggy, you can easily create and share your content with the world. So, why not give it a try and see what creative ideas you can bring to life on your own personal web server?