Accessing Apache server on an Android device locally - android

I'm having trouble finding a way to accessing the page of a virtual host (and the default Apache index.html page) on my Android phone. My hosts file on both my Android and PC use 127.0.0.1 as localhost and domain1.com. I've restarted my Apache server on my PC, and I can access it fine on my PC's browser at domain1.com, but I can't seem to access it on my Android browser. I've tried a public IP address, domain1.com, domain1.com:8000, 127.0.0.1:8000, localhost:8000... but none seem to work. They work on my local PC, though. Here's my httpd.conf file:
<Directory /home/*/public_html/>
AllowOverride FileInfo AuthConfig Limit
Order allow,deny
Allow from all
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
<Limit GET POST OPTIONS>
Order allow,deny
Allow from all
</Limit>
<LimitExcept GET POST OPTIONS>
Order deny,allow
Deny from all
</LimitExcept>
</Directory>
ScriptAlias /cgi-bin/ "/home/*/public_html/domain1.com/public/"
Listen 8000
NameVirtualHost *:8000
<VirtualHost *:8000>
ServerName domain1.com
DocumentRoot /home/*/public_html/domain1.com/public
</VirtualHost>

localhost (a.k.a., 127.0.0.1) on your phone points to your phone. localhost on your PC points to your PC. Your PC is not your phone. Your phone is not your PC.
If you want your phone's Web browser to access your PC, use the IP address of your PC in the URL, along with your desired port number (8000, apparently). Your PC's IP address is something other than localhost.

Related

How to access local virtualhost with android device

i setup few virtualhosts on my laptop, and all of them are name based for example :
www.example1.dev
www.example2.dev
...
and i want to access them with my real android device using USB cable.
and im already conncted to same wifi network as well.
but everytime i try to access one of my virtualhost from my android device i get address not found
i also checked this question
How can I access my localhost from my Android device?
but its related to ip address while im using name based virtualhost
edit 1 :
this is one of my virtual host setup that im trying to accessing it from my android device:
/etc/apache2/sites-avaliable/examplesite1.dev.conf :
<VirtualHost *:80>
<Directory /var/www/examplesite1>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ServerName examplesite1.dev.com
ServerAlias www.examplesite1.dev.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/examplesite1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
and /etc/hosts :
127.0.0.1 examplesite1.dev.com
192.168.250.1 examplesite1.dev.com
192.168.1.51 examplesite1.dev.com
You can get access to the local host via:
http://192.168.1.1/{{service end points}}
it will help you to access the localhost from your mobile

WAMP not connecting to my Android phone [duplicate]

So I set up a few virtual hosts with unique urls and they work just fine on the desktop. However, when I connect a mobile device on the network, it can't seem to access anything properly but the default localhost virtualhost and that's only when it's the only virtualhost I have up.
My setup and coding is pretty much this except with a different site title
wamp server 3.0 virtual host on another device
and while that solution redirects me to my unique url, it has a lack of images on a default wordpress website.
Has anyone managed to get mobile devices fully accessing links other than on localhost?
Since I posted the answer you referenced, I have decided upon a simpler solution.
What the actual problem is
Because we cannot fiddle with the configuration of a phone like we can with a PC, the phone can never find the domain name we create in our Virtual Host definition on the Server machine, because it does not exist in any DNS Server for it to locate the IP Address in, and a DNS Server is the only place a phone can look, unless it is jail broke.
If you wanted to access one of your Virtual Hosts domains from another PC you could just add a line like this into the HOSTS file on the other PC like this.
192.168.0.10 example.local
But you cannot do that on a phone/tablet.
What Apache expects to be able to asssociate a request to a Vhost
When we create an Apache Virtual Host, we are actually telling Apache to look at the domain name on the incoming connection and match that domain name to a ServerName that exists in one of our multiple Virtual Hosts definitions.
But if we use for example example.local as our virtually hosted domain when we attempt to connect to that from our phone, the phone does a DNS Lookup and does not find that domain and therefore cannot get its ip address.
The simplest way to get round this is:
Assuming we do not have access to adding record to a DNS Server we have to come up with a different solution.
The simplest of these is to use the IP Address of the PC running the WAMPServer(Apache) server and a specific port number. So thats a different port number for each of our sites we want to use from a phone.
So how do we do this
Add the new listening port to httpd.conf like so after the 2 existing Listen statements
WAMPServer 3: Do this using the menus, not by doing a manual edit on httpd.conf
right click wampmanager-> Tools -> Add listen port for Apache
#Listen 12.34.56.78:80
Listen 0.0.0.0:80
Listen [::0]:80
Listen 0.0.0.0:8000
Listen [::0]:8000
Suggested httpd-vhosts.conf file
#
# Virtual Hosts
#
# Always keep localhost, and always first in the list
# this way a ramdom look at your IP address from an external IP
# maybe a hack, will get told access denied
<VirtualHost *:80>
ServerName localhost
DocumentRoot c:/wamp/www
<Directory "c:/wamp/www/">
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
# The normal Vhost definition for one of our sites
<VirtualHost *:80>
ServerName example.local
DocumentRoot "c:/websrc/example/www"
<Directory "d:/websrc/example/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
# Access example.dev from phone for testing
<VirtualHost *:8000>
ServerName example.local
DocumentRoot "c:/websrc/example/www"
<Directory "d:/websrc/example/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
# assuming yoursubnet is 192.168.0.?
# allow any ip on your WIFI access
Require ip 192.168.0
</Directory>
</VirtualHost>
Restart Apache from wampmanager after completing these edits.
Now you test this from the WAMPServer PC by using the ServerName i.e example.dev and from the phone using the ip of the PC running WAMPServer with the port number i.e. 192.168.0.10:8000
Apache will find the correct code to serve from both requests.
If you want more than one Virtual Host to be accessible from your phone you just duplicate this idea and change the port number for each new site, lets say you would use 8001,8002,8003 etc. For as many sites as you want to access.
You may also have to amend your firewall to allow access on http on port 8000, or whatever port you pick to use

Accessing node.js server on my local system using android app

I have a node.js server running on my local system which interacts with postgresql in the same system to fetch and save data. I want to access node.js server from my android app to save and fetch data from postresql. The problem is my app is not able to connect to localhost. As localhost for my phone is different from my local system and if I am providing my system's IP address then also its refusing the connection.
I made server listen to hostname '0.0.0.0'. Initially I was not providing any hostname as argument in server.listen i.e it was like
server.listen('portnumber') which I changed to
server.listen('portnumber', '0.0.0.0');
if you are using android emulator and trying to access the the host machine's
local host use IP address : 10.0.2.2
for genymotion use IP address: 10.0.2.3
this should allow you to access the host machine's local host, but you will have to change this IP address to local IP address when running app on a device and connected to same network, to access from other network you need to open port(port forwarding I guess) in your router and talk ask your ISP to do the same.
here's a link to similar question : here

Android: HTTP connection to localhost web site

I am developing a tablet application which needs to connect to a web site to collect online content.
I have seen that we can indeed connect to a web server on a local system by addressing it via it's IP address.
However, I am using virtual hosts on my system, so as to use virtual domains.
This is setup on my system in the apache httpd-vhosts.conf file like this -
#
# Project wordpress dev site
#
<VirtualHost *:80>
DocumentRoot "C:/web/www/boutique"
ServerName boutique.project.dev
</VirtualHost>
with my hosts file having the following entry
127.0.0.1 boutique.project.dev # project woocommerce site
I am using the HttpPost and HttpClient classes and I cannot see how I can provide the real IP address whilst still transmitting the host name in the URL request.
So, what I need to know is how can I make queries from my application using the virtual address " boutique.project.dev " ?
Is this possible ?
Edit :
Following one comment, I need to make things more clearer.
I am wanting to do this in code. Each time we make a connection to a site, the URL does a DNS lookup to determine the IP address to use. I need to intercept this and provide the IP address of my local system. I have seen some examples for proxy's using HttpHost, but I am unclear as to how to use this or even if it is relevant.
I think you don't do that unless you modify host file in android device (with root permissions)
This is a similar question: Apache Virtual Host (Subdomain) access with different computer on LAN
You also could setup a DNS server in your computer (i.e bind9) and set entries for your apache virtualhost and configure android in order to use that DNS server.
Here is the solution which I hope will avoid some useless "banging the head against a wall" for those of you who might have the same need.
Two or three steps are necessary.
1st step : Open the firewall to allow connections.
This is obviously necessary or else, whatever you do the connection will not be made. I use Comodo and so I added a rule in the firewall permitting all connections coming from the local LAN.
2nd step : Tell your server, apache in my case, to listen on its IP address.
I added the following entry in my httpd.conf file :
Listen [my ip address]:80
3rd step : Code the connection
The key to getting this to work is to tell which virtual server is needed. So, without further boring details, here is the code :
// setup the needed objects
HttpClient client = new DefaultHttpClient();
// create the request using the IP address of the server machine
HttpGet request = new HttpGet("http://[target ip address]:80/testpage.php");
// here is the code magic - manually set the host header
request.setHeader(HttpHeaders.HOST, "boutique.project.dev");
// now execute the request
HttpResponse response = client.execute(request);
// read back the response as normal
....
The result is that the apache server will map the request to the virtual host.
I hope this will be useful for others here !
Oh, I almost forgot, don't forget to enable the WiFi on your tablet - can save hours of wondering why it doesn't work !

how to access webpage hosted on tomcat server from the android mobile

I have designed a webpage and run that on my browser using tomcat server http://localhost:8084/neclogin/main.jsp
I also accessed it from my android emulator it works well.
http://10.0.2.2:8084/neclogin/main.jsp it works well.
But when I tried on my real device (connected to PC via hotspot)
http://192.xxx.xxx.xxx:8084/neclogin/web/main.jsp
It shows on browser that HTTTP 404 ERROR - DESCRIPTION :requested resource is unavailable
I am confused whether my android mobile is unable to connect to localhost or can't trace the path of my main.jsp
FOR MORE DETAIL
1)windows 7
2)NETBEANS IDE WITH APACHE TOMCAT SERVER 8.03
3)**Both connected via wifi hotspot on my mobile**
4)path to my main.jsp is `C:\Users\dell\Documents\NetBeansProjects\neclogin\web\main.jsp`
5)path to my tomcat server installed is `C:\Program Files\Apache Software Foundation\Apache Tomcat 8.0.3`
i have disabled my windows and antivirus firewall
First try to access http://192.xxx.xxx.xxx:8084/neclogin/web/main.jsp from your PC to see if the IP is correct.
a) If that works and you can access it, then indeed there is a problem with the external access and the problem is somewhere in your firewall settings or alike. Potentially it doesn't let traffic through 'non-standard' ports (80,433 etc)
b) If you can't access it on your PC either using then you got the IP wrong. Try running ipconfig from the command line and see if with the IP listed there it works. ipconfig will return a bunch of different IPs you need to look for the one that starts with 192 most likely labeled as IPv4 or something like that.
It's also worth checking what the 404 message looks like. If it's tomcat's 404 page then you're almost there as you can access tomcat but not using the correct path.

Categories

Resources