Rick Hurst Web Developer in Bristol, UK

Menu

Category: apache

Using Vagrant for local LAMP development

I’ve always used a local version of Apache for php dev, either the version provided with OSX, or using something like XAMPP or MAMP. On a recent freelance contract I was introduced to using Vagrant to spin up a tailored virtual machine with specific versions of php, mysql and any other relevant dependencies.

Vagrant will download a base virtual machine, and then run a provisioning script to install dependencies. Vagrant also sets up file sharing from your host machine, and port forwarding so you edit locally in your normal editor, and view locally via a web browser as if you were running a local apache instance.

The advantage of this is that once you are up and running, the Vagrant configuration can be stored in the GIT repo, so that other developers can quickly start developing using exactly the same dev environment.

A typical Vagrantfile looks like this:-

Vagrant.configure(2) do |config|
config.vm.box = "hashicorp/precise32"
# Mentioning the SSH Username/Password:
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
# Begin Configuring
config.vm.define "lamp" do|lamp|
lamp.vm.hostname = "lamp" # Setting up hostname
lamp.vm.network "private_network", ip: "192.168.205.10" # Setting up machine's IP Address
lamp.vm.synced_folder "siteroot", "/var/www", owner: "root", group: "root"
#lamp.vm.provision :shell, path: "provision.sh" # Provisioning with script.sh
end
# End Configuring
config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
end

Running “vagrant up” in a directory with this file would spin up an ubuntu VM, serving files from the “siteroot” directory. It would also attempt to run provision.sh on the VM, which can be used to install php/mysql etc.

The getting started guide on the Vagrant site is the best way to get up and running if your are interested.

Setting up apache on osx lion

For general website/ PHP development, I like to have multiple local sites running from my Sites folder using virtualhosts. Up until now i’ve usually done this with Xampp, but after hearing that the version of apache shipped with OSX 10.7 is fairly useable out of the box I thought i’d run with it.

First of all I uncommented a couple of lines from /etc/apache2/httpd.conf.

To enable PHP5:-


LoadModule php5_module libexec/apache2/libphp5.so


To enable the use of virtualhosts:-


Include /private/etc/apache2/extra/httpd-vhosts.conf


I can then create virtualhosts in /private/etc/apache2/extra/httpd-vhosts.conf like so:-


<VirtualHost *:80>
DocumentRoot "/Users/rickhurst/Sites/sandbox"
ServerName sandbox.macbook.local
</VirtualHost>


Then to serve a local site from http://sandbox.macbook.local add a line to /etc/hosts:-


127.0.0.1 sandbox.macbook.local


One issue that took me a while to figure out was that I was getting a 403 error when I tried to use AllowOverride All, which lets me use .htaccess

I found the answer here. Basically, the options line needs to be set to “Options All”:-


<Directory "/Users/rickhurst/Sites/">
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>


(The above rules are added to a file /private/etc/apache2/users/yourusername.conf). Remember to restart apache after making these changes!