Vagrant, Chef, PHP and XDebug

How to install XDebug with Vagrant and Chef

  1. Install Chef Development Kit
  2. Create new cookbook
    ➜ dev berks cookbook xdebug-box
    create xdebug-box/files/default
    create xdebug-box/templates/default
    create xdebug-box/attributes
    create xdebug-box/libraries
    create xdebug-box/providers
    create xdebug-box/recipes
    create xdebug-box/resources
    create xdebug-box/recipes/default.rb
    create xdebug-box/metadata.rb
    create xdebug-box/LICENSE
    create xdebug-box/README.md
    create xdebug-box/CHANGELOG.md
    create xdebug-box/Berksfile
    create xdebug-box/Thorfile
    create xdebug-box/chefignore
    create xdebug-box/.gitignore
    create xdebug-box/Gemfile
    create .kitchen.yml
    conflict chefignore
    Overwrite /Users/bela/dev/xdebug-box/chefignore? (enter "h" for help) [Ynaqdh]
    force chefignore
    append Thorfile
    create test/integration/default
    append .gitignore
    append .gitignore
    append Gemfile
    append Gemfile
    You must run `bundle install' to fetch any new gems.
    create xdebug-box/Vagrantfile
  3. Go to directory
    cd xdebug-box
  4. Install bundles
    ➜  xdebug-box git:(master) ✗ bundle install
    Fetching gem metadata from https://rubygems.org/........
    Fetching additional metadata from https://rubygems.org/..
    Resolving dependencies...
    Using addressable 2.3.8
    Using multipart-post 2.0.0
    Using faraday 0.9.1
    Using httpclient 2.6.0.1
    Using berkshelf-api-client 1.3.0
    Using buff-extensions 1.0.0
    Using hashie 2.1.2
    Using varia_model 0.4.0
    Using buff-config 1.0.1
    Using buff-ruby_engine 0.1.0
    Using buff-shell_out 0.2.0
    Using hitimes 1.2.2
    Using timers 4.0.1
    Using celluloid 0.16.0
    Using nio4r 1.1.1
    Using celluloid-io 0.16.2
    Using cleanroom 1.0.0
    Using minitar 0.5.4
    Using sawyer 0.6.0
    Using octokit 3.8.0
    Using retryable 2.0.2
    Using buff-ignore 1.1.1
    Using erubis 2.7.0
    Using json 1.8.3
    Using mixlib-log 1.6.0
    Using mixlib-authentication 1.3.0
    Using net-http-persistent 2.9.4
    Using semverse 1.2.1
    Using ridley 4.2.0
    Using dep-selector-libgecode 1.0.2
    Using ffi 1.9.10
    Using dep_selector 1.0.3
    Using solve 1.2.1
    Using thor 0.19.1
    Using berkshelf 3.3.0
    Using mixlib-shellout 2.1.0
    Using net-ssh 2.9.2
    Using net-scp 1.2.1
    Using safe_yaml 1.0.4
    Using test-kitchen 1.4.2
    Using kitchen-vagrant 0.18.0
    Using bundler 1.7.5
    Your bundle is complete!
    Use `bundle show [gemname]` to see where a bundled gem is installed.
  5. Add dependencies to your metadata.rb
    depends 'apache2', '~>; 3.1.0'
    depends 'php', '~> 1.7.2'
    depends 'xdebug', '~> 1.0.0'
  6. Comment out 3 lines in Vagrantfile
    #if Vagrant.has_plugin?
        config.omnibus.chef_version = 'latest'
    #end

    and

    config.vm.synced_folder "../data", "/vagrant_data"
  7. Create ../data with index.php
  8. Add attributes/default.rb
    default['xdebug-box']['document_root'] = '/vagrant_data'

    default['xdebug']['directives'] = {
        "remote_autostart" => 1,
        "remote_connect_back" => 1,
        "remote_enable" => 1,
        "remote_log" => '/tmp/remote.log'
    }

    default['xdebug']['config_file'] = '/etc/php5/apache2/conf.d/xdebug.ini'
  9. Add templates/default/apache2.conf.erb
    <VirtualHost *:80>
        ServerAdmin <%= node['apache']['contact'] %>
        ServerName xdebug.local

        DocumentRoot <%= node['xdebug-box']['document_root'] %>

        <Directory <%= node['xdebug-box']['document_root'] %>>
            AllowOverride All
            Order allow,deny
            Allow from All
            Require all granted
        </Directory>
        RewriteEngine On

        ErrorLog <%= node['apache']['log_dir'] %>/error.log

        LogLevel warn

        CustomLog <%= node['apache']['log_dir'] %>/access.log combined
        ServerSignature Off
    </VirtualHost>
  10. Add templates/default/xdebug.ini.erb
    ; configuration for php xdebug module
    zend_extension = "xdebug.so"
    xdebug.remote_enable = 1
    xdebug.remote_connect_back = 1
    xdebug.remote_port = 9000
    xdebug.remote_handler = "dbgp"
    xdebug.profiler_enable = 0
    xdebug.profiler_enable_trigger = 1
  11. Add recipes/default.rb
    #
    # Cookbook Name:: xdebug-box
    # Recipe:: default
    #

    include_recipe 'apt'

    include_recipe 'apache2'
    include_recipe 'apache2::mpm_prefork'

    include_recipe 'apache2::mod_php5'

    package "php5-xdebug" do
      action :install
    end

    template "#{node['php']['ext_conf_dir']}/xdebug.ini" do
      # Overwrite xdebug.ini
      # (Temporary workaround for https://github.com/opscode-cookbooks/php/issues/108)
      source "xdebug.ini.erb"
      owner "root"
      group "root"
      mode "0644"
      action :create
      notifies :restart, resources("service[apache2]"), :delayed
    end

    apache_site '000-default' do
      enable false
    end

    web_app "xdebug-box" do
       template 'apache2.conf.erb'
       server_name node['xdebug-box']['hostname']
    end
  12. Install vagrant omnibus plugin
    vagrant plugin install vagrant-omnibus
  13. Bring machine up. It can take some time
    ➜  xdebug-box git:(master) ✗ vagrant up
  14. Set a breakpoint in the code and run the page in the browser


You can find the code on my GitHub

Categories: Chef, PHP