How to Access Sinatra App on Host Machine with Vagrant Forwarded Ports

Unable to access Sinatra app on host machine with Vagrant forwarded ports

By default when running in development mode, Sinatra only listens to localhost, not to 0.0.0.0 (this change was made due to security considerations).

To allow requests from any interface, either add set :bind, '0.0.0.0' to your app file, or start your app with the -o option, e.g. ruby myapp.rb -o 0.0.0.0.

You may be able to set this to the actual address assigned to the guest, but I don’t know if it will be worth it.

Virtual Box port forwarding misery with Ruby Sinatra:4567 / Shotgun:9292

NB: THE CAUSE IS SHOTGUN/RUBY NOT VirtualBox nor Firewalls nor port forwarding

@Matt fixed this... his link in the comments is correct, the fix to this is one of two things: (it's actually shotgun running ruby in development mode that causes it)

CAUSE:

  • RUBY running in default set :bind, 'localhost' development mode. (link)

SOLUTION:

  • RUBY runs with options set :bind, '0.0.0.0' (can also be in the config.ru)

Good news is if you're looking to make localhost:4567 work this is your ticket, add the line set :bind, '0.0.0.0' to your config and you're good to go

ANNOYINGLY IF YOU RUN SHOTGUN THIS ISN'T GOING TO BE PICKED UP

Bad news is if you're looking to make localhost:9292 work shotgun does it's own thing here:

  • in fact shotgun won't LOOK at those ruby configs, it'll adopt it's own boot strategy, so you have to MAKE shotgun run in --host=0.0.0.0 mode: EG:
  • # shotgun --server=thin --port=9292 --host=0.0.0.0 config.ru

Vagrant's port forwarding not working

I'll make this an actual answer instead of just more comments.

First thing: try curl 'http://localhost:80' from within the VM. If that doesn't work, then it's definitely not the port forwarding.

Next: try curl -v 'http://localhost:4567/' from your host machine. Curl might give you a better error message than Safari.

I'd check that there are no firewalls set up restricting access to port 80. The default Vagrant VM (Ubuntu) doesn't come with a firewall set up, but you said you're using something else, so it might be worth it to check.

If that's not it, try making something other than Apache listed on port 80. Python ships with a simple HTTP server you can use -- go to the folder with index.html and run sudo python -m SimpleHTTPServer 80, then try hitting that with curl from both boxes. If that works, then it's probably an Apache configuration issue. I don't have enough experience with Apache to help if that's the case (I use nginx).

Web app URL after npm start in Vagrant

You don't need a forwarded port to connect to your Vagrant box by its IP address. The forwarded port is only so that you could access your VM from your host IP address. At least for my workflow, I almost never do this. It just leads to port conflicts when you have several Vagrant VMs running. I prefer to access the Vagrant box directly, and use the vagrant-hosts plugin to update hosts file entries so I can access those boxes by name.

In any case, since your web server on port 80 is fine, the networking side of things is working. Either your Node.js app isn't actually running, it isn't listening on port 8080, it isn't bound to the right interface (or all interfaces), or you have some firewall running.

connection refused from host machine

I had to disable my firewall on the host machine with iptables -F

Unable to create the cache directory (/vagrant/app/cache/dev)

As per the discussion we had, I can say that the problem is with permission for user.

following is the scenario.

Your host os is running under user pc-16, and /var/www directory is having ownership of pc-16 user itself. But by default under Ubuntu/debian apache is running as www-data user mode. So first change it to run as pc-16 (I'l tell you later why we need that)

In HOST OS:

Changing user of apache under Host os to your user.

$sudo vi /etc/apache2/envvars

and change

export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

To,

export APACHE_RUN_USER=pc-16
export APACHE_RUN_GROUP=pc-16

and restart apache2,

$sudo service apache2 restart

it may give you error regarding /var/lock/apache2 directory, In that case just change ownership of /var/lock/apache2 to pc-16 user.

$sudo down pc-16:pc-16 /var/lock/apache2

and restart apache2,

$sudo service apache2 restart

Also make sure /var/www/virtualhost-directory/app/ is writable. If it's not, then change permission from host-os terminal.

$sudo chmod 777 /var/www/virtualhost-directory/app

running $vagrant up will run virtualbox guest machine as pc-16 user mode, and it will have all privileges of user pc-16

The problem is, your apache in host-os was running under www-data user mode, and your web-application is trying to create directory/files under /var/www/, so we have changed it to work with pc-16 user.

Now, in vagrant box, it will run as user vagrant and the shared directory of host will come as vagrant user ownership only, (with few limitations like you can't change ownership of shared files from guest os). So, you need to change apache user of guest os to vagrant.

Follow same steps as described for host-os apache.

In Guest OS:

Changing user of apache under Guest os to vagrant.

$sudo vi /etc/apache2/envvars

and change

export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

To,

export APACHE_RUN_USER=vagrant
export APACHE_RUN_GROUP=vagrant

and restart apache2,

$sudo service apache2 restart

in case locking directory ownership error.

$sudo chown vagrant:vagrant /var/lock/apache2

and restart apache2,

$sudo service apache2 restart

Now you should be able to access web-application from host-os like,

http://admin.testmyltd.com/    <-- it will run project from local directory

http://admin.testmyltd.com:4567/app_dev.php <-- it will run same project from vagrant box (shared project directory)


Related Topics



Leave a reply



Submit