How to route and network Vagrant VM instances? - vagrant

I have recently started using Vagrant instead of MAMP for some small projects (mainly WordPress) However, decided to try my hand at React and am wondering wether it is possible to use the two together?
At the moment I have a Vagrant setup created via PuPHPet which works fine and runs a local server on a VM which I can access via a URL - local.gethandle.com. I also downloaded a React starter kit as a starting point from a Facebook github page and it requires you to run a server e.g. node server.js which also works and I can access from a separate URL - http://localhost:3000/. But cant seem to access my React application via my Vagrant URL.
How do I combine the two? If at all possible.

Oki doki,
caveat: I've not looked at what kind of VM is provisioned via PuPHPet..
First you'll need to vagrant ssh into your vagrant vm and download the react tools into your vagrant vm
https://docs.vagrantup.com/v2/getting-started/up.html
Second you'll then want to edit your Vagrant file that should be in the directory on your machine where your vagrant settings are, then have a look and add some networking config
https://docs.vagrantup.com/v2/getting-started/networking.html
config.vm.network :forwarded_port, guest: 80, host: 4567
# guest is your machine, host is your vagrant vm
# e.g config.vm.network :forwarded_port, guest: 3000, host: 3000

You probably need to add the port to your firewall:

Related

Drupal-vm port forward

I have drupalvm setup which has drupal installed. In my custom theme, I have styleguide running on port 6006 (can be configured to different as well as it's node app). I want to expose this to host system. when I do vagrant ssh -- -L 6006:localhost:6006 it's works fine and available on host. Now I have added below to Vagrantfile to make this permanent (as per document here - http://docs.drupalvm.com/en/latest/extending/vagrantfile/):
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 6006, host: 6006
end
Now I can see this port forwarded when I do vagrant port But I can't access localhost:6006 (or use already working drupal hostname - local.drupal.com:6006) on host machine. Do I have to add vhost for it in vagrant, if yes how? OR is there any other way to expose node app to host?

Ubuntu - vagrant access from another computer

I have recently learned how to setup a virtual machine using vagrant (virtualbox), and I know how to access it from a web browser via the local adress such as 192.168.x.x:9292 since 9292 is the default gate. But I would like to access the server from another computer locally.
My question is if I could access it via cmd as I connect to it on my own with the command vagrant ssh but from another computer via a command like vagrant ssh 192.168.x.x?
As far as I understand you have to use external programs or setup something inside the vagrant environment? Is there a simple way to access the server or is it password protected? If I setup a website I can access that for example from another local machine but can I access files on the server (from another computer)?
I found how to connect to another computer locally by entering the vagrant environment, vagrant up --> vagrant ssh. And connected to another computer by typing in ssh vagrant#192.168.x.x where 192.168.x.x is the local address to the computer. The vagrant asks for a password and you type in the default password which I believe is vagrant and then you are connected to the other vagrant instance.
Assuming you have virtualbox provider for Vagrant you can achieve this by doing bridging the VBox network interface with Host.
Use this in Vagratfile (for public/private)
For Public
Vagrant.configure("2") do |config|
config.vm.network "public_network",
use_dhcp_assigned_default_route: true
end
For Private
Vagrant.configure("2") do |config|
config.vm.network "private_network", type: "dhcp"
end
above code will create IP in the range of Host network and using this you can access the file server from another machine.

How to access Redis running in Vagrant Virtual Machine

We are trying to use Vagrant to run a Redis server on Mac (using an Ubuntu Guest OS) with this Vagrantfile
Sadly we are unable to access the Redis database inside the Vagrant Box.
We get this error:
Error: Redis connection to 127.0.0.1:6379 failed - read ECONNRESET
This is the Network configuration in the VirtualBox VM:
What else do we need to add to the Vagrantfile to expose Redis to Mac?
(Note: the reason for using Vagrant is both to let people try redis without having to install it on their main OS, but more importantly to ensure that other elements of the app run as expected)
This may be helpful https://serverfault.com/questions/248248/cannot-connect-to-redis-installed-on-virtualbox-running-ubuntu-from-windows-7. I know question is about connecting from Windows, but the solution is mostly modifications to the Redis config within the VM in order to not bind Redis only to a local port in the redis.conf so that it can be accessed on the host machine(Mac in your case).
Also, depending on how you are trying to access Redis you may be able to configure a SSH tunnel on the host machine(Mac) in order to gain access to the Redis server within the Vagrant VM. I ended up going this route for my case to connect to Redis inside of a Vagrant VM for local development of an Ember JS app using ember-cli and ember-cli deploy with ember-cli-deploy-redis
You can give a Private IP to the Vagrant box and then access redis using the private IP.
For example, lets say you want to configure 192.168.33.10 as you Vagrant box IP. Simply add this line in Vagrant file.
Vagrant.configure(2) do |config|
config.vm.network 'private_network', ip: '192.168.33.10'
end
From now u can access your vagrant box using 192.168.33.10
I happen to run Redis within a Docker container in addition to the Vagrant setup. Running into this question again and again, I want to a add my findings.
Indeed making sure binding to net devices is to 0.0.0.0 instead of 127.0.0.1 is essential. Since I am running Docker I just had to make sure the container is properly run:
docker run -d redis -p 0.0.0.0:6379:6379 redis
Then, in addition the known Vagrant configuration:
config.vm.network :forwarded_port, guest: 6379, host: 6379
And voilĂ : running Redis in a Vagrant VM using Docker, able to access it from the Mac OSX host system.
This is without changing the Redis config, since Docker takes care of this.

Laravel Homestead - How to edit homestead.rb when virtualbox not running?

I attempted the top answer on this thread:
Vagrant port forwarding 80 to 8000 with Laravel Homestead
which was to make this change in the homestead.rb file:
config.vm.network "forwarded_port", guest: 80, host: 8000 to
config.vm.network "forwarded_port", guest: 80, host: 80
I am running a newer version of homestead (not sure where to find the exact version), on a mac.
It did not work, when attempting to start up my homestead virtualbox it now says:
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening on
these ports. The forwarded port to 80 is already in use on the host
machine.
I had edited the homestead.rb file via ssh and vim while the homestead virtualbox was running, but now I can't get it to run and I can't figure out how to undo my changes.
Help with both of these would be greatly appreciated!
How to edit the homestead.rb file when the virtualbox is not running? I believe it is inside either VirtualBox VMs/homestead/homestead.vbox or box-disk1.vmdk, but I do not know how to access inside them.
Help with getting the port to forward correctly to 80 since the solution from the other question did not work.
Homestead 2.+ stores it's files inside your .composer directory inside your User directory. For example, for me it is in:
/Users/noel/.composer/vendor/laravel/homestead
You will find all the configuration and script files that used to reside in the older Homestead 1.0 directories.
Now, since something is holding on to your port 80, it probably means that your local apache installation is running. You can test to see what is holding on to the port by running:
sudo lsof -i :80
To list all processes listening on port 80 (incoming and outgoing).
To stop the local apache you can run:
sudo apachectl stop
That should release the port for you to use with your virtual machine.

Vagrant port forwarding not working. Cups not accesible from host

So I'm working with vagrant and I'm trying to use it as a printing server. I installed cups.
Internally everything works just fine. I can even make a quick curl to my localhost:631 (cups port inside my vagrant) and there's everything.
The thing is I cant access it in any way I try from the host machine.
Obviously I forwarded the port and I've tried with several ports. I've also tried with Debian squeeze and Ubuntu 12.04. Here is my current Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "guruDebian"
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.network :forwarded_port, guest: 631, host: 6363 ## HERE IS CUPS
end
Any ideas?
I think what you will find is that the default cups config file is locked down to only work from localhost for security reasons.
Inside your Vagrant VM open the /etc/cups/cupsd.conf file and change the following line:
Listen localhost:631
to
Listen 0.0.0.0:631
That should allow you to connect from any host.
Have you tried accessing port 8080 of guest to your host? if no, and the services inside guest are running. then its a firewall issue in guest.
Try to turn firewall temporarily
service iptables off
then try to access it again from host.

Resources