Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connecting to local backend from android app

I have my Laravel backend that I am trying to connect to from android app. my hosts file has

   127.0.0.1    my.backend.test

the httpd conf file in my backend is

<VirtualHost *:80>
ServerName      my.backend.test
DocumentRoot    /var/www/mybackend.co.uk/code/public/
DirectoryIndex  index.php
RewriteEngine   On
ErrorLog        /var/www/mybackend.co.uk/code/storage/logs/apache_error.txt

Header set Access-Control-Allow-Origin "http://localhost:4200"

<Directory /var/www/mybackend.co.uk/code/public/>
    AllowOverride All
</Directory>

I can access http://my.backend.test/api/some-route fine from google ARC but not from my phone. I seem to have to use my local ip address but I dont understand how to set that up.

I tried adding

<VirtualHost 192.168.my.ip:8080>
ServerName      my.mobilebackend.test
DocumentRoot    /var/www/mybackend.co.uk/code/public/
DirectoryIndex  index.php
RewriteEngine   On
ErrorLog        
/var/www/mybackend.co.uk/code/storage/logs/apache_error.txt

<Directory /var/www/mybackend.co.uk/code/public/>
     AllowOverride All
</Directory>

to httpd conf and

192.168.my.ip my.mobilebackend.test

to hosts

like image 665
user3074140 Avatar asked Oct 16 '22 12:10

user3074140


2 Answers

If I understand correctly : you can access your server from a different machine on the same network (so this is not a firewall problem) but only if you use the name (added in client's hosts file) and not the IP address.

Is your configuration the first virtualhost ? apache search for a ServerName matching the name provided in the query (e.g. the URL), then falls back to the first one, in your case the ubuntu home page.

As a test, what happens if you use

ServerName      192.168.0.1 (i.e. the IP used in the URL)

I think it will work if you access the server by IP address, but not anymore by name

like image 108
bwt Avatar answered Oct 30 '22 13:10

bwt


Try this method

ip:port

Example

10.0.2.2:80

Note: Firewalls may cause problems with this. Windows has Microsoft Windows Firewall enabled by default. You may have to disable it


IP address

If you are using an emulator:

10.0.2.2

If you are using a real device:

  1. Connect them into the same network. Either using a WiFi connection or using USB tethering option in your phone.
  2. Find the IP address of your computer by typing ipconfig in cmd (if you are using Windows)

Port

Default port is 80. If you have changed it, use the new one.

like image 41
Bertram Gilfoyle Avatar answered Oct 30 '22 12:10

Bertram Gilfoyle