Developing with Vagrant - Part 2

In the first post I wrote a little summary of the vagrant Getting Started Documentation. But the only thing that has been working was a apache. And for my project I need also mysql and php. I did take a look at a repository of Danilo Braband who made a very cool Magento Skeleton with vagrant, n98 and composer. If you are a magento developer, take a look at it.

Structure

Here is the current tree of my application. (Waman is just my projectname)

waman/
|-- cookbooks/
|   |-- apache2/
|   |   `-- recipes/
|   |       `-- default.rb
|   |-- apt/
|   |   `-- recipes/
|   |       `-- default.rb
|   |-- mysql/
|   |   `-- recipes/
|   |       `-- default.rb
|   `-- php/
|       `-- recipes/
|           `-- default.rb
|-- public/
|   `-- index.php
`-- Vagrantfile
You see, there isn't anymore a bootstrap.sh in the root. It's not really common to install things with a shellskript in vagrant. It's better to use chef or puppet. I did use chef, but I can't say why. The main reason for me, I have no skills of chef or puppet and in the beginning I just steal my recipes from Danilo.

Vagrantfile

At first the Vagrantfile.

Vagrant.configure("2") do |config|
  config.vm.box = "precise32"
  config.vm.network :forwarded_port, host:8080, guest:80

  config.vm.provision :chef_solo do |chef|
    chef.add_recipe "apt"
    chef.add_recipe "apache2"
    chef.add_recipe "mysql"
    chef.add_recipe "php"
  end
end

Recipes

There isn't so much magic in it. We just add our chef recipes.

Aptitude

The first one is cookbooks/apt/recipes/default.rb.

execute "apt-get-update-periodic" do
  command "apt-get update"
  ignore_failure true
end
We just update our sources.

Apache

The next one is cookbooks/apache2/recipes/default.rb

apache_default_config = "/etc/apache2/sites-available/default"

package "apache2" do
  action :install
  package_name "apache2-mpm-prefork"
end

service "apache2" do
  service_name "apache2"

  restart_command "/usr/sbin/invoke-rc.d apache2 restart && sleep 1"
  reload_command "/usr/sbin/invoke-rc.d apache2 reload && sleep 1"

  supports [:restart, :reload, :status]
  action :enable
end

# enable mod rewrite
execute "/usr/sbin/a2enmod rewrite" do
  notifies :restart, "service[apache2]"
  not_if do (::File.symlink?("/etc/apache2/mods-enabled/rewrite.load") and
    ((::File.exists?("/etc/apache2/mods-available/rewrite.conf"))?
      (::File.symlink?("/etc/apache2/mods-enabled/rewrite.conf")):(true)))
  end
end

# allow override for default site
execute "sed -i '11 s/None/All/' #{apache_default_config}" do
  not_if "sed -n '11 p' #{apache_default_config} | grep 'AllowOverride All'"
end

# remove /var/www
directory "/var/www" do
  action :delete
  ignore_failure true
  recursive true
end

# link to /var/www
link "/var/www" do
  to "/vagrant/public"
end

service "apache2" do
  action :start
end
With this recipe we install apache, enable rewrite module, allow override, move the documentroot and start the apache.

Mysql

Our next recipe is cookbooks/mysql/recipes/default.rb.

mysql_root_pwd = "ADMINPASSWORD"
mysql_default_db = "waman"
mysql_config_file = "/etc/mysql/my.cnf"

package "mysql-server"

service "mysql" do
  service_name "mysql"

  supports [:restart, :reload, :status]
  action :enable
end

# set the root password
execute "/usr/bin/mysqladmin -uroot password #{mysql_root_pwd}" do
  not_if "/usr/bin/mysqladmin -uroot -p#{mysql_root_pwd} status"
end

# create the default database
execute "/usr/bin/mysql -uroot -p#{mysql_root_pwd} -e 'CREATE DATABASE IF NOT EXISTS #{mysql_default_db}'"

# grant privileges to the user so that he can get access from the host machine
execute "/usr/bin/mysql -uroot -p#{mysql_root_pwd} -e \"GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '#{mysql_root_pwd}' WITH GRANT OPTION;\""

execute "sed -i 's/127.0.0.1/0.0.0.0/g' #{mysql_config_file}" do
  not_if "cat #{mysql_config_file} | grep 0.0.0.0"
end

service "mysql" do
  action :restart
end
In this recipe we install mysql, set admin password and add a database.

PHP

The last recipe is cookbooks/php/recipes/default.rb.

php_config_file = "/etc/php5/apache2/php.ini"
xdebug_config_file = "/etc/php5/conf.d/xdebug.ini"

%w{php5-mysql php5-xdebug libapache2-mod-php5}.each do |php|
  package php
end

# enable display startup errors
execute "display-startup-errors" do
  not_if "cat #{php_config_file} | grep 'display_startup_errors = On'"
  command "sed -i 's/display_startup_errors = Off/display_startup_errors = On/g' #{php_config_file}"
end

# enable display errors
execute "display-errors" do
  not_if "cat #{php_config_file} | grep 'display_errors = On'"
  command "sed -i 's/display_errors = Off/display_errors = On/g' #{php_config_file}"
end

# enable xdebug remote
execute "xdebug-remote" do
  not_if "cat #{xdebug_config_file} | grep 'xdebug.remote_enable=On'"
  command "echo 'xdebug.remote_enable=On' >> #{xdebug_config_file}"
end

# enable xdebug remote connect back
execute "xdebug-remote-connect-back" do
  not_if "cat #{xdebug_config_file} | grep 'xdebug.remote_connect_back=On'"
  command "echo 'xdebug.remote_connect_back=On' >> #{xdebug_config_file}"
end

service "apache2" do
  action :reload
end
In the last recipe we install php and basic modules. Danilo installed in his template a few more for magento. And if you are a magento developer please use it. But I want to build my own application with zend framework and at this moment I can't say what I will need for it.

index.php

At the end public/index.php to test our box.

<?php
echo 'waman works';
We only have to reload or up our box and should see waman works in our brwoser under http://localhost:8080/. Many thanks to Danilo Braband, mostly everything of this post is stolen from his Magento Template.

Next Previous