Once they have been installed on your host (local) machine, its now time to tweak a couple of settings on your machine. These instructions will be for a Mac as thats what I am currently using, the settings are the same for both, but the process might be a little different on windows. Chances are that if you are using Linux then you already know this stuff and you’re the master of the universe.
The Host file
Your Host file is a way of routing requests from your browser so that instead of going out to the internet, they check your local machine. This is part of the magic that helps Vagrant do its thing and make the world a better place.
Open your terminal and type in the following:
sudo nano /etc/hosts
This will then bring up a screen which looks something like this:
# Host Database 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost
Leave these lines in place as they are required by the OS. Underneath, place the following:
#our test subdomain 192.168.100.100 test.localhost.com
The comment is pretty self explanitory, it starts with a hash (#) and lasts until the next break return. The IP address, (which because it starts ‘192.168.xxx.xxx’ that means it will be assumed to be an internal network address) will be mapped to the address http://test.localhost.com.
This is all the OS setup we need to do as Vagrant will do the rest for us automatically.
The VagrantFile is Vagrants main configuration file and is where all the setup comes from. The point of Vagrant is that you can have a single file, which will then setup an empty (VirtualBox) “Box” and you’re ready to go.
Go to a directory on your computer, for instance one inside your User folder, mine is inside the ‘paul’ folder,
Then create a new directory called ‘test’ and move to within it with the following command:
mkdir test cd test
Now lets create our shared folder. This is the folder we will be sharing with the OS on the Box. Any files we place in this folder, will be accessable from our Guest OS and visa versa.
Now we can create a Vagrantfile by:
The Vagrantfile is made up of Ruby, which I am not that knowledgable about, however its reasonably simple to read once you get the hang of it. Type the following into the terminal:
The following is a very basic Vagrantfile configuration that sets up a few of the fundamentals, port forwarding form your machine to the box, a host IP so that you can connect to the box with the Host file addition we already made, and the name and location of the ‘Box’ file. A ‘Box’ file is a VirtualBox file which contains a copy of the Guest OS that is installed. Vagrant takes advantage of this functionality and can then add other software, configuring it to run when the box is started.
#define our config function Vagrant::Config.run do |config| #call the config 'test' config.vm.define :test do |config| #define the 'box' that we want to use config.vm.box = "precise64" #if the box isn't installed, tell Vagrant where to get it from config.vm.box_url = "http://files.vagrantup.com/precise64.box" #define our forwarded ports, so port 8080 on our local machine will forward to 80 on the Vagrant Guest OS config.vm.forward_port 80, 8080 config.vm.forward_port 3306, 3306 #define the name of the host config.vm.host_name = "test" #tell vagrant that we want to use the following IP, this should match the one entered in our Host file config.vm.network :hostonly, "192.168.100.100" #define the location of the shared folder our files will be stored in, matching the shared folder above config.vm.share_folder("www", "/var/www", "./htdocs") end end
When we tell Vagrant to start and configure the box, it will check for a copy of the Box and download it if it cannot be found. The box files are pretty large, upward of 700MB so this can take a while if you’re not on Fiber. Luckily when it’s downloaded and installed you never have to download it again (unless you delete the box that is).
Starting our Vagrant box
Now we have a Vagrantfile inplace, we can run the install command:
The Box will now be downloaded, compiled and initiated. By opening VirtualBox you can see it boot up. In the next article I will start to go into Provisioning.