Tutorial

How To Host A Web Server On Android

Published on August 4, 2022
author

Meghna Gangwar

How To Host A Web Server On Android

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.

Steps to host a web server on Android

Let’s get right into the steps to host a web server on our Android devices now.

1. Install Termux And Hacker’s Keyboard

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

Termux From Playstore
Termux From Playstore

You can install Hacker’s Keyboard as well for better navigation within the Termux window.

Hacker's Keyboard From Google Playstore
Hacker’s Keyboard From Google Playstore

2. Install Packages On Termux

Now we need to install some of the packages we require on Termux. Open the app and type in the following commands :

$ pkg update && pkg upgrade

This should upgrade our current system. Next up we will need to install some necessary packages with :

$ pkg install apache2 git neovim wget curl

3. Connect To Android Device Via SSH

Note that this section is completely optional. Here we will install some additional packages just to aid us during the whole process:

$ pkg install openssh neofetch fish nmap

First let’s enable ssh so that we can connect to our phone via our PC by :

$ sshd

Next, check your username and IP with ifconfig:

$ whoami
u0_a147
$ ifconfig wlan0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.105  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::ce9f:7aff:fe81:3115  prefixlen 64  scopeid 0x20<link>
        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
        txqueuelen 3000  (UNSPEC)
        RX packets 227165  bytes 311846650 (297.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 82264  bytes 7912862 (7.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Now we need to check which port the SSH service is running. You can check it with:

$ nmap -sV 127.0.0.1
Starting Nmap 7.91 ( https://nmap.org ) at 2021-03-12 14:44 IST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0075s latency).
Not shown: 999 closed ports
PORT     STATE SERVICE VERSION
8022/tcp open  ssh     OpenSSH 8.4 (protocol 2.0)

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 4.87 seconds

Once you have the Port Number, switch over to your PC and connect to your device with :

$ ssh -p <PORT> <USER>@<IP>

For example, for me, the complete command would be :

$ ssh -p 8022 u0_a147@192.168.0.105

Next up change the default shell with :

$ chsh /data/data/com.termux/files/usr/bin/fish

At this point, you can issue commands to your device from the terminal of your PC

Connecting To Android Over SSH
Connecting To Android Over SSH

4. Start the webserver on Android

Now, we can start our server with:

$ apachectl 

By default, the webpage is hosted on port 8080. Visiting the said port on our browser, we find the following message :

The Page Hosted On Port 8080
The Page Hosted On Port 8080

To edit the message we need to change the index.html file with the vi editor:

$ vi $PREFIX/share/apache2/default-site/htdocs/index.html

Let’s change the contents of index.html to :

<html><body><h1>Android !</h1></body></html>

Refreshing the page, we should now see our changes being reflected :

Our Changed HTML Page
Our Changed HTML Page

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 !

5. Port Forwarding With Ngrok

First, we need to get the compressed file from the offical website with :

$ wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm64.tgz

Unzip the file with :

$ tar -xvzf ngrok-stable-linux-arm64.tgz

Next up, you need to sign up on their website to get an authorization token.

Ngrok
Ngrok

Now, you should get an authorization toke. To add the token to the default config file, type in :

$ ./ngrok authtoken <token>

Now, we should have access more features and longer sessions. Finally with our webserver still running, type :

$ ./ngrok http 8080
Port Forwarding With Ngrok
Port Forwarding With Ngrok

We get a ngrok link, where we would find our webserver’s homepage :

Our Page Being Served Via Ngrok
Our Page Being Served Via Ngrok

Now, we need can access our server from outside our local network using the ngrok link !

Conclusion

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.

Learn more about our products

About the authors
Default avatar
Meghna Gangwar

author

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.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
December 20, 2021

Nice tutorial!

- Jabir Ali

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.