27
Vagrant HOW TO SET A VAGRANT DEVELOPMENT SYSTEM

How To Set a Vagrant Development System

Embed Size (px)

DESCRIPTION

The slides I used at WordCamp Toronto 2013

Citation preview

Page 1: How To Set a Vagrant Development System

VagrantHOW TO SET A VAGRANT DEVELOPMENT SYSTEM

Page 2: How To Set a Vagrant Development System

Paul Bearne @pbearneSr. Web Developer @ metronews.caPlugin author of Author Avatars List ( http://wordpress.org/plugins/author-avatars/ )WP Site Verification tool ( http://wordpress.org/plugins/wp-site-verification-tool/ )

Page 3: How To Set a Vagrant Development System

Where do you develop your sites?

Page 4: How To Set a Vagrant Development System

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.

Page 5: How To Set a Vagrant Development System

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

Page 6: How To Set a Vagrant Development System

Host Computer

Virtualbox

Shared Folder/usr/html/site C:/user/document/code

Page 7: How To Set a Vagrant Development System

Install

Vagrant: http://downloads.vagrantup.com/ VirtualBox: https://www.virtualbox.org/ Plus a configuration file

Note: Sometimes problems with latest version VirtualBox on windows

Page 8: How To Set a Vagrant Development System

THE BASIC COMMAND LINE

$ vagrant init precise32 http://files.vagrantup.com/precise32.box $ vagrant up

$ vagrant destroy

Page 9: How To Set a Vagrant Development System

ScriptingAUTOMATE THE CONFIG

Page 10: How To Set a Vagrant Development System
Page 11: How To Set a Vagrant Development System
Page 12: How To Set a Vagrant Development System

DEMOJUST RUN IT

Page 13: How To Set a Vagrant Development System

ConfigTHE VAGRANTFILE FROM VARYING-VAGRANT-VAGRANTS

Page 14: How To Set a Vagrant Development System

# -*- mode: ruby -*-# vi: set ft=ruby :

dir = Dir.pwdvagrant_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

Page 15: How To Set a Vagrant Development System

# 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"

Page 16: How To Set a Vagrant Development System

# 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

Page 17: How To Set a Vagrant Development System

# 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"

Page 18: How To Set a Vagrant Development System

# /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" ]

Page 19: How To Set a Vagrant Development System

# 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" ) endend

Page 20: How To Set a Vagrant Development System

# -*- 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']

endend

“:nfs => true” needed for Mac

Source: https://github.com/MikeRogers0/vagrant-nginx-wordpress-puppet/blob/master/Vagrantfile

Page 21: How To Set a Vagrant Development System

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

Page 22: How To Set a Vagrant Development System

C:\Windows\System32\drivers\etc\hosts

192.168.50.4 local.wordpress.dev local.wordpress-trunk.dev src.wordpress-develop.dev build.wordpress-develop.dev

Windows 8 - need to unprotect host in window defender

sudo sh -c 'echo "192.168.50.4 local.wordpress.dev local.wordpress-trunk.dev src.wordpress-develop.dev build.wordpress-develop.dev" >>/private/etc/hosts'

Page 23: How To Set a Vagrant Development System

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

Page 24: How To Set a Vagrant Development System

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

Page 25: How To Set a Vagrant Development System

Questions?

Page 26: How To Set a Vagrant Development System

We're [email protected]

Page 27: How To Set a Vagrant Development System

Slides@ http://www.slideshare.net/pbearneEmail: [email protected]