Web Server on android

ideas

I never knew that I will need a complete web-server on android. I was also in the league of people thinking why would someone need a server on their phone. What purpose can it serve apart from just the joy that “Holla!! I have a full-fledged web server in my pocket!!”.
For my current product it seemed such a useful thing that now I can think of multiple ways where running server on android can be so helpful and it can be used for so many ways. Just think it as a cheap Web Server with its own small network when WiFi hotspot mode is on. You can create your own server client environment, specific to your needs. Like for sharing things may be in a conference room setup.

Getting the server going

Now coming to original point of the blog how to do it.
First the major part is getting the web-server. There are so many open source products I like the PAW server
http://paw-android.fun2code.de/ Other options are i-jetty
https://code.google.com/p/i-jetty/
nanohttpd

https://github.com/NanoHttpd/nanohttpd
JWS

http://tjws.sourceforge.net/
etc. After you get the webserver running you will need to put your own webpages in proper places as for PAW server I just deleted everything inside the folder /sdcard/paw/html and put my own pages as I like them. You just need to copy your web space assuming this is the starting directory. I used PAW because it needed least configuration. You can use whatever suits you. Just overwrite everything they have in that directory.
After getting the server running you can see your webpage getting displayed at http://192.168.43.1:port_no. Of-course you will need to turn on hotspot and connect your device(from where you want to browse) to the hotspot.
For PAW server it is http://192.168.43.1:8080 . Where 192.168.43.1 is default ip for android hotspot if not changed by manufacturer. If your devices are on same network you have to know the ip of the android device hosting web.

This surely is not good if you want the web-server serving many people. You want to remove the ip address and the port make it something like http://www.myserver.com huh? or http://myandroid.com with or without www replacing anything in place of com. Just for fun or say aha!! Now I can host anything without paying.
For removing IP from url you just need small changes but your device should be rooted.

Making it more real

Port forwarded is a great app to forward traffic to specified port. Since http requests come to port 80 you will need to create a rule in port forwarder to forward all incoming traffic on port 80 to port 8080 or the port number your server is running on. This will remove the port specification from url.
Finally we have to remove the ip address typing in URL. For this you just need to edit one file and add your own domain name and address. Mount your system in write mode:
mount -o rw,remount /system
location of system directory is device specific.
You can get the ip address of the device by connecting to hotspot and checking the default gateway. In general it is 192.168.43.1

Change /system/etc/hosts file and add below line

192.168.43.1 com myphone.com

com is my domain name and myphone is my hostname you can put whatever you like. This just indicate that given device address

Now change or create /etc/dnsmasq.conf file and add domain name like this:

local=/com/
domain=com

now you can even connect via adb like this:
adb connect android.com
cool huh!!.
Just restart hotspot and you are done :).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s