|

How To Provision NGINX Using Vagrant

How To Provision NGINX Using Vagrant

Now that we have learned the basics of how to work with Vagrant, let’s now go a little further into working with the fundamentals in Vagrant. In this vagrant tutorial, we’re going to create a new development environment using the precise32 box like we have done already. This time however, we are going to add some shell provisioning to the Vagrantfile in order to automatically install the NGINX web server on our new virtual machine. Of course we could go in to the VM and manually install nginx via SSH, but that kind of defeats the purpose of Vagrant. We want to be able to share our Vagrantfile with another developer or administrator, and all they need to do is type vagrant up. They won’t have to fiddle with manual installation of software, it is all scripted out for them. This leads to better control over our environment, and easier collaboration with others.


Start a new Vagrant environment

From our home directory, we’ll make a new directory named nginx and then cd into it. Once there, we will go ahead and run the command vagrant init hashicorp/precise32 –minimal. This will give us a brand new Vagrantfile with a minimal configuration base.  As we can see in this screenshot we make use of mkdir nginx, cd nginx, and vagrant init hashicorp/precise32 --minimal.
nginx_vagrant_init_minimal


Enable Vagrantfile Syntax Highlighting

The Vagrantfile is written in Ruby, it has no extension however – so most times you will not see any syntax highlighting when editing it.  In this episode, we are making use of Visual Studio Code, and it turns out there is an extension you can add into the IDE in order to enable syntax highlighting of the Vagrantfile.  In this screenshot, we located it by clicking on View->Extensions, and then searching for Vagrantfile.  After that, all we have to do is click install.

add Vagrantfile syntax highlighting


Add Material Theme

While we are at it, why don’t we add a super cool looking theme to the editor.  This Material Theme is one of the most popular for Visual Studio Code, so let’s go ahead and install that.  We can do this in the same extension viewer as the previous step.
material theme for visual studio code


Looking Good

Behold!  The Vagrantfile in all of it’s syntax highlighted glory!
vagrantfile syntax


Instruct Vagrant to use a shell provisioner

Since we would like Vagrant to handle the task of installing nginx on the virtual machine for us, we need to find a method to do this.  One option is to make use of a shell script.  In order to instruct Vagrant to make use of a shell script, we must adjust the Vagrantfile to how it looks in this snippet.  Basically with that new line added we tell Vagrant that we are going to use a shell script for provisioning, and where to find said shell script via the path attribute. Note the addition of config.vm.provision “shell”, path: “provision.sh”.


Create the shell script to install nginx

At this point, Vagrant knows there is a shell script involved, but we need to actually put some commands into the script file itself in order for it to do anything.  That is exactly what we do in this step.  If we were to install nginx manually, these are the exact commands we would need to run in order to get the nginx software installed and running.  Note we add three simple commands of apt-get -y update, apt-get -y install nginx, and service nginx start to the file and save it.  Do note, this file lives in the same exact directory as the Vagrantfile itself.
nginx shell script install


Confirm the files are correct

On the host operating system, let’s make sure that both the Vagrantfile and the provision.sh file look correct.  A quick inspection indicates they look good.
vagrantfile and provision shell script contents


Ready for vagrant up

At the command prompt, we are ready to type vagrant up. This will kick off the process of Vagrant building our virtual machine for us. In addition to this, since we added a shell script to the Vagrantfile, Vagrant will go ahead and execute anything in that script automatically. As we can see in this screenshot, as part of the process of the VM coming online, nginx is being fetched from a remote server and installed for us. So cool!

nginx is installing now


Check the status of nginx

We can now vagrant ssh into the guest operating system and have a look at the nginx service.  Note that it is running as we see here via the service nginx status command.
nginx is running on the VM now


Fetch the nginx welcome page

We won’t be able to use a browser just yet, but we can fetch the index.html page right from the console via wget -qO- localhost. Check it out, we get the raw html right in the terminal window. It might be nice to also test this from a browser, so let’s set up port forwarding next in order to do this.
wget the nginx default page


Set up port forwarding

We can make one additional configuration change to the Vagrantfile so that we can browse to our newly installed nginx web server. Note the updated line of config.vm.network “forwarded_port”, guest: 80, host: 8080, id: “nginx” below. With this change, we should be able to launch a browser and see the nginx welcome page right away. Do note, any time you make a change to the Vagrantfile and the virtual machine is already running, you will need to run a vagrant reload or vagrant provision to actually make the changes take effect.


Test from a browser

Finally! We can launch a browser, and confirm that our newly installed nginx web server is working perfectly. 🙂
wecome to nginx in google chrome


How To Provision NGINX Using Vagrant Summary

And there it is, a fully functioning virtual machine with nginx installed for us automatically.  It is a simple example, but it shows how developers and administrators can now fully automate the building of custom components into their virtual machines.  What’s more, the Vagrantfile is easily put into version control so that when a developer on the other side of the world needs to test out some software, they can build the exact same environment by simply having access to the right Vagrantfile and typing vagrant up on a Vagrant enabled machine.  In summary, we created a new directory for a new Vagrant environment, configured the Vagrantfile for shell provisioning, created the shell script to install nginx, and ran Vagrant to set everything up for us.  What a great tool Vagrant is!