Web development environments with Vagrant

by Michael Birch

In the past I have used MacPorts on my MacBook Pro for development. I decided to set up a virtual machine using Vagrant and found it easy to set up.

I already use VirtualBox for testing websites on various versions of Internet Explorer on Windows.

I downloaded the Mac dmg file from http://downloads.vagrantup.com/ and installed Vagrant.

I found a list of base boxes.

Make a directory for Vagrant (e.g. vagrant/test) and cd to it in terminal.

vagrant init debian-squeeze64 http://www.emken.biz/vagrant-boxes/debsqueeze64.box
vagrant up

Edit the Vagrantfile, which is in the test directory.

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

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "debian-squeeze64"
  config.vm.network :private_network, ip: "192.168.111.2"
 
end

Permissions

I found that Apache could not write to the www directory and so I created a web page with:

<?php echo shell_exec('whoami') ?>

This provides the name of the user, which is www-data.

The Vagrant documentation explains how to change the owner/group and I added this to the Vagrantfile:

config.vm.synced_folder "www/", "/vagrant/www", owner: "www-data", group: "www-data"

Provisioning

Simon Jodet posted a guide for setting up a development environment with Vagrant and I adapted his bash script for my needs:


#!/bin/sh
echo mysql-server mysql-server/root_password select "vagrant" | debconf-set-selections
echo mysql-server mysql-server/root_password_again select "vagrant" | debconf-set-selections
apt-get install -y mysql-server apache2 php5 libapache2-mod-php5 php5-mysql
VHOST=$(cat <<EOF
<VirtualHost *:80>
  DocumentRoot "/vagrant/www"
  ServerName localhost
  <Directory "/vagrant/www">
    AllowOverride All
  </Directory>
</VirtualHost>
EOF
)
echo "${VHOST}" > /etc/apache2/sites-enabled/000-default
sudo a2enmod rewrite
service apache2 restart
sed -i 's/127.0.0.1/0.0.0.0/g' /etc/mysql/my.cnf
service mysql restart
apt-get clean

Add this line to the Vagrantfile:

config.vm.provision :shell, :path => "deploy/deploy.sh"

Final version of the Vagrantfile:

After making changes to Vagrantfile, use the reload command. Use the --provision flag to get the shell script to execute.

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

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "debian-squeeze64"
  config.vm.network :private_network, ip: "192.168.111.2"
  config.vm.provision :shell, :path => "deploy/deploy.sh"
  config.vm.synced_folder "www/", "/vagrant/www", owner: "www-data", group: "www-data"
end

Connecting to MySQL

Use a ssh tunnel to connect to MySQL with a client on the host machine (I use Navicat Lite):

ssh -l vagrant 192.168.111.2 -L localhost:8888:localhost:3306

Extra configuration

ssh into the virtual machine with ssh root@192.168.111.2 (password vagrant) set the timezone with:

dpkg-reconfigure tzdata

I needed GD and cURL:

sudo apt-get install php5-gd
sudo apt-get install curl libcurl3 libcurl3-dev php5-curl

Next time, I’ll add this to the shell script.

Further information

blog comments powered by Disqus