Vagrant for PHP Developers

Vagrant for PHP Developers

As presented at NomadPHP, 22-05-2014

719435d98d452de7ac367c828266cf01?s=128

Erika Heidi

May 22, 2014
Tweet

Transcript

  1. None
  2. whoami • Brazilian, living in Amsterdam since 2012 • PHP

    <independent> developer • Author of Vagrant Cookbook on LeanPub @erikaheidi
  3. What to expect from this talk 1)Vagrant overview 2)What's New:

    1.5 and 1.6 3)Provisioner Tasting: Ansible, Puppet and Chef 4)ProTips 5)Useful Resources
  4. None
  5. Why Vagrant?

  6. “It works on my machine” - every developer, ever.

  7. Why Vagrant? • Reproducible and portable development environment • Enables

    easier code collaboration • Backend env tests / benchmark • Automation Tools learning and testing
  8. None
  9. Some Terms • Boxes • Provider / Provisioner • Host

    / Guest • Vagrantfile • Synced Folder
  10. The simplest thing that does something Vagrant.configure("2") do |config| config.vm.box

    = "hashicorp/precise64" config.vm.provision "shell", inline: "echo Hello World!" end
  11. None
  12. Commands • up • reload • provision • suspend •

    resume • destroy [ --provision ] [ --provision ]
  13. DEMO

  14. Another Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise64" config.vm.network :private_network,

    ip: "192.168.33.101" config.vm.provision "ansible" do |ansible| ansible.playbook = "playbook.yml" end config.vm.synced_folder "./", "/vagrant", :nfs => true end
  15. What's New

  16. Recent new features Vagrant 1.5 • Vagrant Cloud / boxes

    2.0 • Vagrant Share • Rsync • SMB Vagrant 1.6 • Global Status and Control • Windows as Guest • Docker Provider • Post-up Message
  17. Vagrant Cloud (1.5)

  18. Vagrant Share (1.5)

  19. Post-up message (1.6)

  20. Global status and control (1.6)

  21. None
  22. 1. Ansible • Tasks, Playbooks, Roles • Tasks are defined

    with YAML • 3rd most used • Modules Directory: Ansible Galaxy • Requires installation of Ansible in the Host
  23. 1.1 A Task - name: Install Nginx apt: pkg=nginx state=latest

  24. 1.1 A Task - name: Install Nginx apt: pkg=nginx state=latest

    - name: Install PHP Packages apt: pkg={{ item }} state=latest with_items: - php5-fpm - php5-cli
  25. 1.2 A Playbook # playbook.yml --- - hosts: all sudo:

    true tasks: - name: Update apt-cache apt: update_cache=yes - name: Install Nginx and php5-fpm apt: pkg={{ item }} state=latest with_items: - nginx - php5-fpm
  26. 1.3 A Role . ├── playbook.yml └── roles ├── init

    │ └── tasks │ └── main.yml └── nginxphp ├── tasks │ └── main.yml └── templates └── vhost.tpl #playbook.yml --- - hosts: all sudo: true vars: doc_root: /vagrant/web roles: - init - nginxphp
  27. 1.4 Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise64" config.vm.provision "ansible"

    do |ansible| ansible.playbook = "playbook.yml" end end
  28. None
  29. 2. Puppet (puppet-apply) • Resources, Manifests, Modules • Non-sequential execution

    order • Custom language based on Ruby • 1st most used • Modules Directory: Puppet Forge
  30. 2.1 A Resource package { 'nginx': ensure => 'installed' }

  31. 2.1 A Resource package { 'nginx': ensure => 'installed' }

    package { ['php5-fpm', 'php5-cli']: ensure => 'installed' }
  32. 2.2 A Manifest # manifests/default.pp exec { 'apt-get update': command

    => 'apt-get update' } package { ['nginx', 'php5-fpm']: ensure => 'installed', require => Exec['apt-get update'] }
  33. 2.3 A Module . ├── manifests │ └── default.pp └──

    modules └── nginxphp ├── manifests │ └── init.pp └── templates └── vhost.erb # manifests/default.pp exec { 'apt-get update': command => 'apt-get update', before => Class['nginxphp'], } class { 'nginxphp': doc_root => '/vagrant/web', }
  34. 2.4 Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise64" config.vm.provision :puppet

    do |puppet| puppet.module_path = "modules" end end
  35. None
  36. 3. Chef (chef_solo) • Resources, Recipes, Cookbooks • Resources defined

    with Ruby • 2nd most used, 1st with Ruby devs • Modules Directory: Cookbooks • Complex but very powerful
  37. 3.1 A Resource apt_package "nginx" do action :install end

  38. 3.1 A Resource apt_package "nginx" do action :install end ["nginx",

    "php5-fpm"].each do |p| apt_package p do action :install end end
  39. 3.2 A Recipe # cookbooks/main/recipes/default.rb execute "apt-get update" do command

    "apt-get update" end ["nginx", "php5-fpm"].each do |p| apt_package p do action :install end end
  40. 3.3 A Cookbook . └── cookbooks ├── main │ └──

    recipes │ └── default.rb └── nginxphp ├── recipes │ └── default.rb └── templates └── default └── vhost.erb # cookbooks/main/recipes/default.rb execute "apt-get update" do command "apt-get update" end include_recipe 'nginxphp'
  41. 3.4 Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise64" config.vm.provision "chef_solo"

    do |chef| chef.add_recipe "main" end end
  42. None
  43. None
  44. 6.1 Debugging • Unknown Vagrant error – Use VirtualBox /

    Vmware GUI • Unknown Provisioner error – Increase provisioner verbosity • Not working as expected – Login, fix, automate
  45. 6.2 Sync Folder Performance • Synchronization has a cost •

    Framework cache/logs – Too much writing operations on disk – We don't need this in our synced folder
  46. None
  47. None
  48. None
  49. Useful Resources

  50. None
  51. None
  52. Examples on GitHub • Basic examples • Comparing Provisioners –

    PHP5 + Nginx example https://github.com/erikaheidi/nomad-vagrant
  53. Vagrant Cookbook Special discount coupon for NomadPHP http://bit.ly/vc-nomad

  54. Questions?

  55. erikaheidi.com/vagrant https://joind.in/11181 Vagrant Resources: Rate this talk: @erikaheidi Get in

    Touch: