LAMP development environment

Disclaimer: I'm not a PHP developer. Any of the practices here described do not claim to be "good practices". Improvements and/or other solutions, tricks and tips are much appreciated.

A while back I was asked for some support/analysis on a PHP web application (CakePHP to be a little more specific) . Well, my knowledge of PHP is (to be kind!) limited. Apart for some PHP coding back in college I never had any professional experience with it nor with any of it’s frameworks.

Moving forward, I setup a LAMP environment using XAMPP. Due to my own lack of knowledge and lack of documentation it wasn’t long before I started running into some trouble (database connection was “shaky”, cake command was not working properly, etc). I need another approach.

I could have setup a LAMP server on my own machine but I had a couple of reasons not to:

  • I don’t like installing software system wide unless I used it frequently;
  • I like keep development environments as self contained as possible and make dependencies manageable.

I usually work with Python and PostgreSQL, but the LAMP stack comes in handy sometimes. Because it saves me time (and headaches, for the matter) I like keep things as automated/replicable as possible. With this in mind I turned once again to Vagrant and VirtualBox for help (you need to install them both before you proceed). So our development server requirements, for this example, are:

  • LAMP server – Linux, Apache, MySQL and PHP;
  • WordPress development;
  • We want to write code on our host machine and see the results immediately on the browser (like when we use Django or Rails own development servers).

For that purpose, I setup a repository on Github with an initial configuration. Let’s begin!

Get into the folder you would like the code to live and run:

cd /folder/where/the/code/will/live

git clone https://github.com/mccricardo/lamp_server

This will create a folder called lamp_server with the contents of the repository. You can rename it to whatever you want. Let’s look at our bootstrap file:

#!/usr/bin/env bash

export DEBIAN_FRONTEND=noninteractive

echo "Update repos"
apt-get update

echo "Install apache"
apt-get install -y apache2 apache2-mpm-worker

echo "Install MySQL"
apt-get install -y mysql-server libapache2-mod-auth-mysql php5-mysql

echo "Activate MySQL"
mysql_install_db

echo "Install PHP"
apt-get install -y php5 libapache2-mod-php5 php5-mcrypt

This will install our LAMP server dependencies. Please note that MySQL will be installed with a root user without password. For this example will will leave it like that, but you should change it in the future.

Let’s take a look at the Vagrantfile:

# -*- 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 = "precise64"
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"

  config.vm.provision :shell, :path => "bootstrap.sh"
  config.vm.network "forwarded_port", guest: 80, host:8080

  config.vm.provider :virtualbox do |vb|
      vb.customize ["modifyvm", :id, "--memory", "1024"]
  end
end

The VM will have a Ubuntu 12.04LTS OS, will listen on port 8080 and will have 1GB of memory. You can change all of these as you require. You can also give the VM a name by adding the following line, below vb.customize:

vb.name = “Name You Desire”

Next step, run:

vagrant up

The initial setup will go through. Wait for a little while and at the end you’ll have your LAMP server up and running. For a sanity check, go to http://localhost:8080/ and see if it works. It does? We’re moving fast. Let’s proceed.

Next we get WordPress. Download for the link provided and extract to the same folder as before (the folder will live next to the Vagrantfile and reaming files). We want that folder, because good old Vagrant keeps that folder synced with one inside the VM. That way we can write code from our host and the server will be able to server it from the VM  without any fuss. Don’t believe me? The do:

vagrant ssh

ls /vagrant/

 Trust me now? Good 🙂 We’re approaching  the end.  We now need to tell Apache where to find our code.

Edit the file /etc/apache2/sites-available/default so that it points to our WordPress code:

DocumentRoot /vagrant/worpress
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory  /vagrant/worpress/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

Restart apache: sudo service apache2 restart

You can now follow the Famous 5-Minute Install to setpup WordPress.  Et voila! You now have a LAMP server with a fully functional WordPress installation.

Advertisements

One thought on “LAMP development environment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s