VirtualBox virtual machines with Vagrant and Ansible for website development
This blog post will begin with a short discussion of why one would invest the time to use these tools, then cover the required software, an overview of how Vagrant works with VirtualBox and the use of Ansible to provision a VM.
This post builds on our previous post about Using VirtualBox to install and test eZ Publish 5, which describes how to create a VM with VirtualBox to test eZ Publish 5. This post describes the tools you can use to automate the creation and provisioning of VMs. There is much to cover, but for now we will focus on providing a general overview.
Why should you use VMs for website development?
The traditional ways that developers work on websites are on remote development machines or locally, directly on their main operating system. There are many advantages to using VMs for development, including the following:
- The entire development team can have the same server and configuration, without purchasing additional hardware
- Their local VMs can be more representative of the production servers
- Bring up the VM when you need to work with it and shut it down when you're done. This is especially helpful if you need different versions of software such as PHP for different projects.
Vagrant and Ansible help to automate the provisioning of the virtual machines. Vagrant handles the starting and stopping of the machines as well as some configuration, and Ansible breaks down the details of the machines into easy-to-read configuration files and installs and configures the software within the VMs.
We usually keep the site code on the host operating system and share these files from the host operating system to the virtual machines. This way, the VMs can be loaded with only the software required to run the site, and each team member can use their favourite local tools for code editing, version control, and more under the OS they use most often.
This scheme is not without risk and complexity. A VM is a great tool for your collection, but like any tool, you will need to take some time to learn how to use it.
- When Vagrant, Ansible, and VirtualBox are running well together, they help development to run more efficiently and can improve the quality of your work. However, when things go wrong they can distract from actual web development. They represent additional tools that you need to maintain and troubleshoot, and you need to train and support your development team to use them properly.
- Host operating system: Be sure your host OS supports the tools you're planning to use. As I mentioned, this blog post focusses on Ansible. Ansible is not officially supported on Windows as the host. That means if you only have a Windows machine to work with, you'll need to consider using a Linux OS as the controller. (There are some tricks to get it to work on Windows.)
- Performance: Remember the intent of these VMs is to support development. They will not run as fast as standalone servers. If that is an issue, you will likely need to invest some time in improving the performance.
- An implicit assumption is made that only one instance of the VM will be running on the host at any given time. If you will be using more than one instance of the VM, you'll need to take that into account as you set it up.
You will likely need some VirtualBox extensions and Vagrant plugins as well. Take the time to read the documentation carefully.
Then, you will need a starting point for your virtual server, typically called a "base box". For your first VM, it is easiest to use an existing box. There are many boxes up at HashiCorp's Atlas and at Vagrantbox.es.
Once you've chosen your box, these commands should bring it to life:
$ vagrant box add name-of-box url-of-box $ vagrant init name-of-box $ vagrant up
Provisioning, customizing, and accessing the VM
Once the box is up and running, you can start adding software to it using Ansible. Plan to spend a lot of time learning Ansible. It is well worth the investment. You'll use Ansible to load the system software, create databases, configure the server, create users, set file ownership and permissions, set up services, and much more -- basically to configure the virtual machine to include everything you need. Once you have the Ansible scripts set up you will be able to re-use them with different base boxes / VMs and also run them against remote servers (which is a topic for another day!).
The easiest way to SSH into the box is:
$ vagrant ssh
You may update your /etc/hosts file to map the VM box IP address to an easy to remember name for SSH and browser access. Once the box is running and serving pages, you can start working on the site.
Using a VM as described here doesn't significantly change normal development workflows when it comes to editing code. The host machine and VM share the application files through the path configured under Vagrant. You can edit the files using your code editor of choice on the host, or make minor adjustments with a text editor on the VM. You can also keep version control tools on the host machine. In other words, all of the code modifications made on the host machine automatically show up on the virtual machine.
All going well, you get the convenience of local development and an environment that mimics the server(s)!
Commentsblog comments powered by Disqus
Hi, we're Mugo Web - Nice to meet you!
We're a group of web experts who solve complex web problems.Learn more about us »
- Business solutions (72)
- Case study (27)
- eZ Publish add-ons (21)
- eZ Publish community (11)
- eZ Publish development tips (66)
- Front-end development (4)
- Productivity tools (16)
- Site performance (12)
- User experience (33)
- Web accessibility (14)
- Web solutions (69)
- Work at Mugo (11)
Yes - we can do that.
Many years of experience with complex websites allows us to offer total solutions.Learn more about what we can do »
We love our clients (and they love us too)
We've solved problems across North America and around the world.Learn more about what we've done »
We tweet too
Follow us on Twitter for the latest Mugo happenings