Development Workflow With Vagrant
In this tutorial we're going to look into using Vagrant and why we should include this in our development workflow.
What Is Vagrant
Vagrant allows you to build and configure complete development working environments localised inside a virtual machine (VM). Vagrant will allow you to increase the automation and speed up the time it takes to setup server environments through out your development.
This means you can create per defined config files which show exactly how you want your server environment to look like. Having the server setup in a config file allows you to easily share, replicate and publish this setup across multiple server environments.
Why Use Vagrant?
As a developer you will normally develop on your local machine, push this code onto a staging server for the client to accept, then push this code onto a live server when it's ready for production. This is a standard workflow in many developers lives, some might skip the staging server but you're always going to have at least 2 environments when working on a project, local and production.
This means that you need to setup your local environment choosing the right versions of software you need, the correct version of PHP, NGINX/Apache, MySQL. Ideally you want to be in the situation where you development environment is exactly the same as your production environment. This isn't always as easy as it sounds, if you work on multiple projects you could have two versions of PHP installed, an older version of Apache installed from a previous project, multiple PHP modules installed from previous projects. You can't always guarantee that your local environment is going to be the same as the production server.
With Vagrant it allows you to create a server setup config file which will include everything you need and what versions you want. Then you simply run
$ vagrant up
You now have a virtual machine which is configured to the exact server setup you want. Now when you push your code to the staging or production server you have a configuration file to setup these servers the same way. This allows you to make sure if you have the code working in your local environment you know when it's live it will work on the production server, so you won't get caught out by the old case-sensitive problems of the past.
This also allows you to share this config setup with other developers on your project to make sure everyone is running on the same environment. In the past this was always a problem with working on a project when some developers are on Linux, others on Mac and some on Windows, removing the "it works on my machine" conversation.
How To Install Vagrant?
To install Vagrant you simply have to go to the download page and download the installer for your operating system.
Once Vagrant is installed vagrant will be added to your PATH environment variable for you to run on the command line.
The quickest way of getting Vagrant up and running is by running the following command.
$ vagrant init hashicorp/trusty64 $ vagrant up
After these commands have ran you now have a virtual machine running with Ubuntu 14.04. Typing in the command
$ vagrant ssh
you are now on the command line within your new virtual machine running Ubuntu 14.04.
If you navigate to your project root you will now see a file VagrantFile which will include the configuration of your new machine. You can now add VagrantFile to your version control to be used by other developers in your project.
To learn more about the configuration setup have a look at the Vagrant documentation.
Deploying With Vagrant
Vagrant can be used to quickly deploy your code to remote servers via FTP, SFTP, Atlas or Heroku.
Vagrant calls this process push and it's started by typing in the command
$ vagrant push
The push locations can be defined in your VagrantFile.
config.push.define "ftp" do |push| push.host = "ftp.company.com" push.username = "..." # ... end
You are most likely going to have different environment locations you can define these in the same location.
config.push.define "staging", strategy: "ftp" do |push| # ... end config.push.define "qa", strategy: "ftp" do |push| # ... end
To push your code to staging you can now use the command.
$ vagrant push staging
Sharing Your Vagrant Environment
It's possible for you to share your local Vagrant environment via a public URL. By using the command
$ vagrant share
Vagrant will create a public URL for others to access your virtual machine via HTTP.
GUI For Creating Vagrant Configs
There are many options and configuration settings for Vagrant, too many for me to go into on this article. But there is a online GUI called PuPHet allowing you to easily go through multiple options to what you want on your server, click the generate button and download the Vagrant configs for your setup.
To find some of the most useful and popular Vagrant boxes which help get you started quicker by installing:
Atlas have created this Vagrant boxes search page just go to the link below.
Fastest WordPress Hosting With WPEngine
Stunning speed, powerful security, and best-in-class customer service. At WP Engine.
Join the weekly newsletter
Sign up for our newsletter. You'll be updated on new tutorials as they come available.
Subscribe today and get a 50% discount code to Paulund