In this module, we will have a look at how we can host a web server on Android and port forward it using ngrok so that we can have a flexible webserver on the go.
Let’s get right into the steps to host a web server on our Android devices now.
First, we will need two Applications before we continue on : Termux (which will help us issue commands) and Hacker’s Keyboard (which will help us to use keys like Ctrl, Esc, Alt which are usually not available on standard Android Keyboards)
You can get Termux from their official website or from Google Playstore
You can install Hacker’s Keyboard as well for better navigation within the Termux window.
Now we need to install some of the packages we require on Termux. Open the app and type in the following commands :
This should upgrade our current system. Next up we will need to install some necessary packages with :
Note that this section is completely optional. Here we will install some additional packages just to aid us during the whole process:
First let’s enable ssh so that we can connect to our phone via our PC by :
Next, check your username and IP with ifconfig:
Now we need to check which port the SSH service is running. You can check it with:
Once you have the Port Number, switch over to your PC and connect to your device with :
For example, for me, the complete command would be :
Next up change the default shell with :
At this point, you can issue commands to your device from the terminal of your PC
Now, we can start our server with:
By default, the webpage is hosted on port 8080. Visiting the said port on our browser, we find the following message :
To edit the message we need to change the index.html file with the vi editor:
Let’s change the contents of index.html to :
Refreshing the page, we should now see our changes being reflected :
At this point, you can customize your page to look/do whatever you want it to do. However, to extend our functionality even further, we can enable port forwarding with ngrok !
First, we need to get the compressed file from the offical website with :
Unzip the file with :
Next up, you need to sign up on their website to get an authorization token.
Now, you should get an authorization toke. To add the token to the default config file, type in :
Now, we should have access more features and longer sessions. Finally with our webserver still running, type :
We get a ngrok link, where we would find our webserver’s homepage :
Now, we need can access our server from outside our local network using the ngrok link !
Thus in this way, we can use our Android phones as web servers. On non-rooted phones, you can host a webserver on any of the higher ports while on rooted phones you can use the default port 80.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Nice tutorial!
- Jabir Ali