How To Use Vagrant on Your Own VPS Running Ubuntu
This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:
- Upgrade to Ubuntu 14.04.
- Upgrade from Ubuntu 14.04 to Ubuntu 16.04
- Migrate the server data to a supported version
Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.
Vagrant is a powerful open source software for configuring and deploying multiple development environments. It is designed to work on Linux, Mac OS X, and Windows and although it comes with VirtualBox for the virtualization needs, it can be used also with other providers such as VMware or AWS.
This tutorial continues a previous one in which we looked at installing Vagrant and setting up your first simple guest machine running Ubuntu on top of your existing DigitalOcean VPS. It therefore assumes you have followed the steps covered in it and are familiar with its lessons.
Boxes for VirtualBox
As I mentioned in the previous tutorial, Vagrant comes with VirtualBox installed by default as its provider. This means that the box we added (precise32) is specifically made for VirtualBox and won't work with other providers. The precise32 box is an image for a guest machine running Ubuntu Precise 12.04 32 bit. However, there are other boxes you can add that come with different operating systems.
The Vagrant github project gives you the official links to the other boxes you can add to your VirtualBox powered Vagrant. There are 3 more available: precise64 (the 64 bit version of Ubuntu Precise 12.04), lucid32 and lucid64 (for the Lucid distribution of Ubuntu 12.04). These are the official ones.
There is, however, a community website with lots of other boxes available for VirtualBox and various other providers. This list, however, has not been verified by the Vagrant project and are to be used at your own risk. But if you want to add any of these to your Vagrant, follow the same procedure as you did in the previous tutorial.
Now that you know what boxes are and how to add them to your Vagrant, you are probably wondering what's next? You can configure a Vagrant file to use a box to quickly power up a guest machine running Ubuntu, but then?
Lets say you want to install Apache on the Ubuntu guest machine you played with in the previous article. You can SSH into it and do it manually. However, this means that whenever you or anyone else wants to work with this guest machine after destroying it, all these operations need to be performed over and over.
One of the cool things about Vagrant is automatic provisioning. It means that you can specify a shell script to be run every time you use the vagrant up command. So let's say that we want our guest machine to always be deployed with Apache installed on it. To do this, we can put all the necessary commands to be performed into a file in the project root folder (next to the Vagrantfile):
Inside this newly created file, paste the following script:
#!/usr/bin/env bash apt-get update apt-get install -y apache2 rm -rf /var/www ln -fs /vagrant /var/www
This script installs Apache and links the /var/www folder to the /vagrant folder (the one that gets synced with the host machine project root folder, if you remember). This way you can edit the files for the Apache server from your host machine and you don't need to worry about synchronization.
Next, we need to edit the Vagrantfile and specify that when the guest machine is being deployed, it should use this script:
In this file, right below where we specified the default box to use, add the following line:
config.vm.provision :shell, :path => "bootstrap.sh"
This basically tells Vagrant to use the shell provisioner and run the script located in that file (the path to which being relative to the project root folder of the host machine).
Save the file and run the vagrant up command to deploy a new guest machine which will now come with Apache installed. If you are already running the guest machine, you can reload it with the following command:
This will restart the machine and run the provisioning. You will now have Apache installed on the guest machine every time you run vagrant up or vagrant reload.
The purpose of installing Apache on your guest machine is obvious: you want a VPS to serve pages to a browser. But there is no way yet to access the guest machine from the browser. Enter the networking and port forwarding features of Vagrant.
Port forwarding lets you specify a port on your host machine to be shared with the guest machine. This means that if you point your browser to the address of your host using a particular port, you'll end up on the guest machine. To do this, navigate on your host machine to the Vagrant project root folder and edit your Vagrantfile:
Under the lines you added above when you did the provisioning, paste in the following line:
config.vm.network :forwarded_port, host: 4567, guest: 80
This specifies that if the port 4567 is requested on the host machine, it should route the request to port 80 on the guest machine (which is the default Apache port). Save the file and run vagrant up if the guest machine is not already running. If it is, you can use the vagrant reload command.
If you used the bootstrap.sh script I showed you above, you made it that the Apache server installed on the guest machine will find its files to serve in the /vagrant folder of the guest machine which in turn is synced with the project folder on your host machine. So go to whichever of these folders is closest to you and create a test index.html file with some random text in it.
Now if point your browser to your host machine and specify the port 4567, the index.html page should be served by the Apache server of the guest machine. If you have installed Vagrant locally, try the following url:
If you are using it on a remote server, try the following:
Make sure you replace the IP with the one corresponding to your remote host machine.
In this tutorial, you learned a bit about what boxes you can use with your Vagrant, how to set up provisioning to run various scripts automatically with every vagrant up command and you saw how to use port forwarding to access the guest machine from the browser. These are powerful features indeed, but Vagrant gives you even more if you know what you are doing.
In the next tutorial, we will look at how to add a new provider to Vagrant, namely to use DigitalOcean droplets as the guest machines you deploy. For this, we will need to install a Vagrant plugin.