At this point we have a virtual environment running with Apache serving the basic web project. But so far we can only access it from within the VM, using the command line. Vagrant’s goal is to provide the benefit of a virtualized environment without getting in your way. In order to access your project, Vagrant has a feature known as port forwarding.
Port forwarding allows you to specify ports on the guest machine to forward to the host machine. This enables you to access your web services using your own browser on your machine while the server actually sits and runs within a virtual machine.
In our case, we just want to forward Apache. Port forwarding is specified in the Vagrantfile, like so:
Vagrant::Config.run do |config| # Forward guest port 80 to host port 4567 config.vm.forward_port 80, 4567 end
forward_port is a method which takes two arguments:
Forwarded ports are applied during
vagrant up like any other configuration. But if you already have a running system, calling
vagrant reload will apply them without re-importing and re-building everything.
Note that forwarding ports requires a virtual machine restart since VirtualBox won’t pick up on the forwarded ports until it is completely restarted.
vagrant up, you should be able to take your regular old browser to
localhost:4567 and see the index page we created earlier. At this point, we have a fully functional VM ready for development for a basic HTML website. It should be clear to see that if PHP, Rails, etc. were setup, you could be developing those technologies as well.
For fun, you can also edit the
index.html file, save it, refresh your browser, and immediately see your changes served directly from your VM.