This document provides instructions on how to set up a Vagrant development environment. Vagrant allows developers to run development environments that match production by creating virtual machines. Key points:
- Vagrant uses VirtualBox to run virtual machines and configure project isolation and version control.
- The basic workflow involves initializing a Vagrantfile configuration, running "vagrant up" to launch the virtual machine, and "vagrant destroy" to wipe it out.
- Additional commands like "vagrant suspend" pause the virtual machine. The document demonstrates configuring ports, shared folders, and provisioning scripts.
- Benefits of Vagrant include no need to install web servers locally, matching production
2. Paul Bearne @pbearne
Sr. Web Developer @ metronews.ca
Plugin author of Author Avatars List ( http://wordpress.org/plugins/author-avatars/ )
WP Site Verification tool ( http://wordpress.org/plugins/wp-site-verification-tool/ )
4. Why use Vagrant?
No need to have a web server installed.
You can match the configuration of production server.
Project isolation - one vagrant setup per project.
Version isolation - more than one version of WordPress.
Works the same on PC/Mac or Linux.
5. Vagrant
“Vagrant is a tool for building complete
development environments. With an easy-to-
use workflow and focus on automation, Vagrant
lowers development environment setup
time, increases development/production
parity, and makes the 'works on my machine'
excuse a relic of the past.”
http://www.vagrantup.com/about.html
14. # -*- mode: ruby -*-
# vi: set ft=ruby :
dir = Dir.pwd
vagrant_dir = File.expand_path(File.dirname(__FILE__))
Vagrant.configure("2") do |config|
# Configurations from 1.0.x can be placed in Vagrant 1.1.x specs like the following.
config.vm.provider :virtualbox do |v|
v.customize ["modifyvm", :id, "--memory", 512]
end
# Forward Agent
#
# Enable agent forwarding on vagrant ssh commands. This allows you to use identities
# established on the host machine inside the guest. See the manual for ssh-add
config.ssh.forward_agent = true
15. # Default Ubuntu Box
#
# This box is provided by Vagrant at vagrantup.com and is a nicely sized (290MB)
# box containing the Ubuntu 12.0.4 Precise 32 bit release. Once this box is downloaded
# to your host computer, it is cached for future use under the specified box name.
config.vm.box = "precise32"
config.vm.box_url = "http://files.vagrantup.com/precise32.box"
config.vm.hostname = "vvv"
16. # Local Machine Hosts
#
# If the Vagrant plugin hostsupdater (https://github.com/cogitatio/vagrant-hostsupdater) is
# installed, the following will automatically configure your local machine's hosts file to
# be aware of the domains specified below. Watch the provisioning script as you may be
# required to enter a password for Vagrant to access your hosts file.
#
# By default, we'll include the domains setup by VVV. A short term goal is to read these in
# from a local config file so that they can be more dynamic to your setup.
if defined? VagrantPlugins::HostsUpdater
config.hostsupdater.aliases = [
"local.wordpress.dev",
"local.wordpress-trunk.dev",
"src.wordpress-develop.dev",
"build.wordpress-develop.dev"
]
end
17. # Default Box IP Address
#
# This is the IP address that your host will communicate to the guest through. In the
# case of the default `192.168.50.4` that we've provided, Virtualbox will setup another
# network adapter on your host machine with the IP `192.168.50.1` as a gateway.
#
# If you are already on a network using the 192.168.50.x subnet, this should be changed.
# If you are running more than one VM through Virtualbox, different subnets should be used
# for those as well. This includes other Vagrant boxes.
config.vm.network :private_network, ip: "192.168.50.4"
18. # /srv/database/
#
# If a database directory exists in the same directory as your Vagrantfile,
# a mapped directory inside the VM will be created that contains these files.
# This directory is used to maintain default database scripts as well as backed
# up mysql dumps (SQL files) that are to be imported automatically on vagrant up
config.vm.synced_folder "database/", "/srv/database"
config.vm.synced_folder "database/data/", "/var/lib/mysql", :mount_options => [ "dmode=777", "fmode=777" ]
# /srv/config/
#
# If a server-conf directory exists in the same directory as your Vagrantfile,
# a mapped directory inside the VM will be created that contains these files.
# This directory is currently used to maintain various config files for php and
# nginx as well as any pre-existing database files.
config.vm.synced_folder "config/", "/srv/config"
# /srv/config/nginx-config/sites/
#
# If a sites directory exists inside the above server-conf directory, it will be
# added as a mapped directory inside the VM as well. This is used to maintain specific
# site configuration files for nginx
config.vm.synced_folder "config/nginx-config/sites/", "/etc/nginx/custom-sites"
# /srv/www/
#
# If a www directory exists in the same directory as your Vagrantfile, a mapped directory
# inside the VM will be created that acts as the default location for nginx sites. Put all
# of your project files here that you want to access through the web server
config.vm.synced_folder "www/", "/srv/www/", :owner => "www-data", :mount_options => [ "dmode=775", "fmode=774" ]
19. # Provisioning
#
# Process one or more provisioning scripts depending on the existence of custom files.
#
# provison-pre.sh acts as a pre-hook to our default provisioning script. Anything that
# should run before the shell commands laid out in provision.sh (or your provision-custom.sh
# file) should go in this script. If it does not exist, no extra provisioning will run.
if File.exists?(File.join(vagrant_dir,'provision','provision-pre.sh')) then
config.vm.provision :shell, :path => File.join( "provision", "provision-pre.sh" )
end
# provision.sh or provision-custom.sh
#
# By default, Vagrantfile is set to use the provision.sh bash script located in the
# provision directory. If it is detected that a provision-custom.sh script has been
# created, that is run as a replacement. This is an opportunity to replace the entirety
# of the provisioning provided by default.
if File.exists?(File.join(vagrant_dir,'provision','provision-custom.sh')) then
config.vm.provision :shell, :path => File.join( "provision", "provision-custom.sh" )
else
config.vm.provision :shell, :path => File.join( "provision", "provision.sh" )
end
# provision-post.sh acts as a post-hook to the default provisioning. Anything that should
# run after the shell commands laid out in provision.sh or provision-custom.sh should be
# put into this file. This provides a good opportunity to install additional packages
# without having to replace the entire default provisioning script.
if File.exists?(File.join(vagrant_dir,'provision','provision-post.sh')) then
config.vm.provision :shell, :path => File.join( "provision", "provision-post.sh" )
end
end
20. # -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant::Config.run do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.network :hostonly, "33.33.33.10"
config.vm.share_folder("vagrant-root", "/vagrant", ".", :nfs => true)
config.vm.provision :puppet do |puppet|
puppet.manifests_path = "puppet/manifests"
puppet.module_path = "puppet/modules"
puppet.options = ['--verbose']
end
end
“:nfs => true” needed for Mac
Source: https://github.com/MikeRogers0/vagrant-nginx-wordpress-puppet/blob/master/Vagrantfile
21. Vagrant Commands
Vagrant up
◦ Start
Vagrant Suspend / resume
◦ pause/play
Vagrant halt
◦ turn off
Vagrant destroy
◦ wipeout
Vagrant status
◦ is it up
Vagrant int
◦ create empty config file
Vagrant box
◦ manage
23. Notes and links
Use: shell or bat to run vagrant
◦ cd varying-vagrant-vagrants
◦ start cmd.exe /k "vagrant up --no-provision“
Use different config files per site
http://www.vagrantup.com/
https://www.virtualbox.org/wiki/Downloads
24. Other tools
http://www.packer.io/ a tool for creating identical machine images for multiple platforms from
a single source configuration including vagrant images.
http://livereload.com/ to auto refresh web page on save.
http://puppetlabs.com/ home of puppet - http://forge.puppetlabs.com/apowers/wordpress