<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

	<title>david winter - you were expecting someone else?</title>
	<link href="http://davidwinter.me/index.xml" rel="self"/>
 	<link href="http://davidwinter.me" rel="alternate"/>
	<updated>2012-04-22T06:51:02-07:00</updated>
	<id>http://davidwinter.me</id>
	<author>
		<name>David Winter</name>
	</author>

	
	<entry>
		<title>Install and manage WordPress with Git</title>
		<link href="http://davidwinter.me/articles/2012/04/09/install-and-manage-wordpress-with-git/"/>
		<updated>2012-04-09T00:00:00-07:00</updated>
		<id>http://davidwinter.me/articles/2012/04/09/install-and-manage-wordpress-with-git</id>
		<content type="html">&lt;p&gt;I've been a massive fan of the '&lt;a href=&quot;http://codex.wordpress.org/Installing/Updating_WordPress_with_Subversion&quot;&gt;Installing/Updating Wordpress with Subversion&lt;/a&gt;' instructions on the WordPress codex for a good few years. If you have ssh access to a server and are managing a couple of WordPress sites, keeping them up-to-date with the latest version is easy with just one svn command.&lt;/p&gt;

&lt;p&gt;However, with a bigger WordPress project in the pipeline, I need to be able to manage my themes and plugins in a git repository, while at the same time keep a reference to the core WordPress files, and allow it to be as easy to update when a new version is released.&lt;/p&gt;

&lt;p&gt;I'm not going to continue with the SVN instructions because that'd add a lot of mess to my git repository - including SVN history. I want to keep track of my changes only - none of the WordPress related ones.&lt;/p&gt;

&lt;p&gt;The good news is, this is all possible. It takes a few minutes longer to setup, but once done, updating will be as easy. You'll also have a really tidy git repo.&lt;/p&gt;

&lt;h2&gt;Setup&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: You can view the finished project on Github; &lt;a href=&quot;https://github.com/davidwinter/wordpress-with-git&quot;&gt;davidwinter/wordpress-with-git&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a new project directory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir mysite &amp;amp;&amp;amp; cd mysite
git init
touch README.md
git add README.md
git commit -m &quot;Initial commit.&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You now have a blank project to start with.&lt;/p&gt;

&lt;p&gt;We'll be using the &lt;a href=&quot;https://github.com/WordPress/WordPress&quot;&gt;Github WordPress&lt;/a&gt; mirror that is synced with the official SVN repository every 30 minutes. It includes all tags and branches.&lt;/p&gt;

&lt;p&gt;We want to add this as a subrepository to our main project.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git submodule add git://github.com/WordPress/WordPress.git wordpress
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;WordPress is quite a big project if you haven't already noticed, so it might take a while to make a clone. Once done, commit the new subrepository:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git commit -m &quot;Add Wordpress subrepository.&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We want to ensure our project is using the current stable version. As of writing, this is 3.3.1.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd wordpress
git checkout 3.3.1
cd ..
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This has now updated the WordPress repository to 3.3.1 - commit the changes to your main project.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git commit -am &quot;Checkout Wordpress 3.3.1&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The subrepository is now isolated, and we don't want to make any changes to it, besides bumping version numbers. This means we can't modify wp-config.php, themes or plugins. We'll have to move things about a bit.&lt;/p&gt;

&lt;p&gt;First up, we'll create our config file. WordPress allows for this to be stored one directory up from the core WordPress files.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cp wordpress/wp-config-sample.php wp-config.php
git add wp-config.php
git commit -m &quot;Adding default wp-config.php file&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now that this is outside of the Wordpress repository, we'll be able to configure Wordpress and commit the changes to our main project repository.&lt;/p&gt;

&lt;p&gt;Now, in order to manage themes and plugins we need to make a copy of the &lt;code&gt;wp-content&lt;/code&gt; directory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cp -R wordpress/wp-content .
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will copy the directory and its contents into our main project repo.&lt;/p&gt;

&lt;p&gt;Now, at this point you may like to tidy up that directory a little. This isn't required, but will save you commiting themes and plugins you'll probably never use.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rm wp-content/plugins/hello.php
rm -rf wp-content/themes/twentyten
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you plan on using the default WordPress theme, Twenty Eleven, don't run the following command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rm -rf wp-content/themes/twentyeleven
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now commit this directory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git add wp-content
git commit -m &quot;Adding default wp-content directory&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We now have a trimed down &lt;code&gt;wp-content&lt;/code&gt; directory ready for only &lt;em&gt;our&lt;/em&gt; themes and plugins.&lt;/p&gt;

&lt;p&gt;To finish off the project structure, we need to copy the WordPress &lt;code&gt;index.php&lt;/code&gt; file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cp wordpress/index.php .
git add index.php
git commit -m &quot;Adding index.php&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we need to configure these files and let WordPress know where to talk to everything.&lt;/p&gt;

&lt;h2&gt;Configuring&lt;/h2&gt;

&lt;p&gt;We'll start with &lt;code&gt;index.php&lt;/code&gt;. This is the file all requests go via, and we just need to update a &lt;code&gt;require&lt;/code&gt; statement. Find the following line:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require('./wp-blog-header.php');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And update it to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require('./wordpress/wp-blog-header.php');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Commit:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git commit -am &quot;Pointing index.php to the correct location&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now into &lt;code&gt;wp-config.php&lt;/code&gt;, open it up. Because we have the core Wordpress files in a different directory to that of the &lt;code&gt;index.php&lt;/code&gt; file, we need to let Wordpress know about it. Add the following two constants to the file. I prefer just below the &lt;code&gt;@package WordPress&lt;/code&gt; statement so my changes are near the top of the file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; * @package WordPress
 */

define('WP_SITEURL', 'http://' . $_SERVER['SERVER_NAME'] . '/wordpress');
define('WP_HOME',    'http://' . $_SERVER['SERVER_NAME']);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This lets WordPress know that the core files are in the &lt;code&gt;wordpress&lt;/code&gt; directory, but that the site is to be served at the root of the project directory.&lt;/p&gt;

&lt;p&gt;Because we moved the &lt;code&gt;wp-content&lt;/code&gt; directory out of the core WordPress directory, we also have to let it know about this change. Add the following after the previous define statements:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;define('WP_CONTENT_DIR', $_SERVER['DOCUMENT_ROOT'] . '/wp-content');
define('WP_CONTENT_URL', 'http://' . $_SERVER['SERVER_NAME'] . '/wp-content');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And finally, if you're not using the default &lt;code&gt;twentyeleven&lt;/code&gt; theme, and want to specify the one WordPress should use instead:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;define('WP_DEFAULT_THEME', 'mytheme');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Simply replace &lt;code&gt;mytheme&lt;/code&gt; with the name of the theme you have. Be sure that you've placed it inside the &lt;code&gt;wp-content/themes&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;Now simply complete the database details as normal further down in the file, and then visit the project on your webhost and you'll be up and running.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git commit -am &quot;Update settings in wp-config.php&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Updating&lt;/h2&gt;

&lt;p&gt;Change into the Wordpress subrepository:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd wordpress
git fetch --tags
git checkout 3.3.2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Replace 3.3.2 with the correct version number.&lt;/p&gt;

&lt;p&gt;Now commit the changes subrepository version to your main project:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd ..
git commit -m &quot;Update Wordpress to version 3.3.2&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Helpful references&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://codex.wordpress.org/Hardening_WordPress#Securing_wp-config.php&quot;&gt;Moving wp-config.php&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://codex.wordpress.org/Changing_The_Site_URL&quot;&gt;Changing the WordPress directory&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://codex.wordpress.org/Editing_wp-config.php#Moving_wp-content&quot;&gt;Moving wp-content&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://codex.wordpress.org/Network_Admin_Themes_Screen#Default_Theme&quot;&gt;Setting default theme constant&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</content>
	</entry>
	
	<entry>
		<title>Introduction to Puppet</title>
		<link href="http://davidwinter.me/articles/2012/03/04/introduction-to-puppet/"/>
		<updated>2012-03-04T00:00:00-08:00</updated>
		<id>http://davidwinter.me/articles/2012/03/04/introduction-to-puppet</id>
		<content type="html">&lt;p&gt;Have you ever had that déjà vu feeling when setting up a new server? That you've done it all before, and that it's really tedious to have to do it all over again? You'd rather be able to just hit a button to deploy your app and start showing it off to the world.&lt;/p&gt;

&lt;p&gt;It's exactly what happened to me, and at first I thought I'd just write a bash script with all the various steps that I had started noting down in a plain text file. Luckily, at the point I was considering this, I saw the release of a new project called &lt;a href=&quot;http://vagrantup.com/&quot;&gt;Vagrant&lt;/a&gt;. It's a tool that allows you to run a virtual machine locally, with a helpful shared directory to your files on your local (host) machine. It allows you to setup a self contained development environment without messing around with your system.&lt;/p&gt;

&lt;p&gt;Reading through the &lt;a href=&quot;http://vagrantup.com/docs/index.html&quot;&gt;documentation&lt;/a&gt;, I saw it mention &lt;a href=&quot;http://puppetlabs.com/&quot;&gt;Puppet&lt;/a&gt; and &lt;a href=&quot;http://www.opscode.com/chef/&quot;&gt;Chef&lt;/a&gt; which both appeared to be some sort of witchcraft that allowed you to automatically configure and setup these virtualised servers. Bingo. Easy server configuration management.&lt;/p&gt;

&lt;p&gt;You don't have to use Vagrant to use Puppet. It's just a really easy way to test it out locally. For your production environments you'll still use the same puppet manifests, but you'll manage the running of those manifests a &lt;a href=&quot;http://docs.puppetlabs.com/#part-two-masteragent-puppet&quot;&gt;little different&lt;/a&gt;. I'll be covering the Vagrant approach for this blog post.&lt;/p&gt;

&lt;p&gt;What do I want to demonstrate? Just a quick demo of how to get Puppet to install &lt;a href=&quot;http://wiki.nginx.org/Main&quot;&gt;Nginx&lt;/a&gt; and &lt;a href=&quot;http://uk.php.net/manual/en/install.fpm.php&quot;&gt;php-fpm&lt;/a&gt; on a server, serving a simple &lt;code&gt;phpinfo()&lt;/code&gt; file. It should be enough to make you thirtsy for more.&lt;/p&gt;

&lt;h2&gt;Some quick steps for setup&lt;/h2&gt;

&lt;p&gt;You'll need to have &lt;a href=&quot;https://www.virtualbox.org/&quot;&gt;Virtual Box&lt;/a&gt; installed with the command line tools option. Then install the Vagrant gem on your computer with a simple&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem install vagrant
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now create a new directory on your machine where you're going to work on this project.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir testing-vagrant
cd testing-vagrant/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All Vagrant projects use a 'box' as a starting point for your project. It's an virtual disk image of an OS installation that has all the dependencies to interact with Vagrant, and to run puppet manifests. Now, the one on the Vagrant website is Ubuntu 10.04 Lucid Lynx, and without some tinkering, it's not as easy to get the latest versions of packages. So instead, I created my own Vagrant box for Ubuntu 11.10 Oneiric Ocelot using the open source tool &lt;a href=&quot;https://github.com/jedi4ever/veewee&quot;&gt;VeeWee&lt;/a&gt;. It's hosted on my public &lt;a href=&quot;http://db.tt/AamRjYgb&quot;&gt;Dropbox&lt;/a&gt; folder, and to add this box to your vagrant setup, run the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;vagrant box add oneiric32 http://dl.dropbox.com/u/11342885/oneiric32.box
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once this has been downloaded, you need to initialise your Vagrant project with this box:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;vagrant init oneiric32
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will create a file named &lt;code&gt;Vagrantfile&lt;/code&gt; in your project directory. This file has a bunch of different configuration options commented out. It's a really helpful file to read through.&lt;/p&gt;

&lt;p&gt;One configuration option you'll need to set is the port forwarding of the webserver. Ensure this line is present:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;config.vm.forward_port 80, 8080
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will forward on the port &lt;code&gt;8080&lt;/code&gt; of your local machine to port &lt;code&gt;80&lt;/code&gt; on the vagrant virtualbox. If you don't have any other webservers running on your local machine, feel free to point this to port &lt;code&gt;80&lt;/code&gt; directly.&lt;/p&gt;

&lt;p&gt;Also, ensure these lines are present too:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;config.vm.provision :puppet, :module_path =&amp;gt; &quot;private/puppet/modules&quot; do |puppet|
  puppet.manifests_path = &quot;private/puppet/manifests&quot;
  puppet.manifest_file  = &quot;base.pp&quot;
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is the Puppet configuration. It tells Vagrant where the modules and manifests directories are, and the base manifest file.&lt;/p&gt;

&lt;p&gt;And finally, create these directories in your project for storing the different Puppet configuration files we need:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir -p private/puppet/{manifests,modules/nginx/files}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Playing with Puppet&lt;/h2&gt;

&lt;p&gt;To understand how to use Puppet, you need to think of a Puppet manifest as a file that defines the different resources your server requires. Resources in Puppet speak are things like users, groups, packages, commands, services and files--to name a few. We define these resources in a manifests file which you'll place in &lt;code&gt;private/puppet/manifests/base.pp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now the first thing we want to do with the base box is ensure that everything is up-to-date. The first resource we'll define is an executable command to run. Basically an &lt;code&gt;apt-get update&lt;/code&gt;. In your &lt;code&gt;base.pp&lt;/code&gt; file add the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;exec { 'apt-get update': 
    command =&amp;gt; '/usr/bin/apt-get update',
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All resources follow this same structure:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;resource_type { 'title':
    options =&amp;gt; value,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;title&lt;/code&gt; is quite an important value, as it is often used as a default option value too. For example, with the &lt;code&gt;user&lt;/code&gt; resource, the title you specify will be the name of the user to create. This can be overriden if you need to define a descriptive title that isn't the name of the user you want to create. For the &lt;code&gt;user&lt;/code&gt; resource, you'd override this with the &lt;code&gt;name&lt;/code&gt; option. For example, the following would both create the same user:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user { 'david':
    ensure =&amp;gt; present,
}

user { 'my descriptive title for my username':
    name   =&amp;gt; 'david',
    ensure =&amp;gt; present,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Note: Due to an issue with VeeWee not creating a required user group on the Oneiric box, you need to also tell Puppet to create the &lt;code&gt;puppet&lt;/code&gt; group. This can be done with the following. Not doing this will result in errors:&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;group { 'puppet':
    ensure =&amp;gt; present,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Back to our example. We now have a command resource that will run &lt;code&gt;apt-get update&lt;/code&gt;. Once this has been run, we want to install the packages &lt;code&gt;nginx&lt;/code&gt; and &lt;code&gt;php-fpm&lt;/code&gt;. We want to ensure though that &lt;code&gt;apt-get update&lt;/code&gt; has run beforehand. Now, with Puppet, it's important to note that the manifest files are not run through sequentially as you might expect. Each time they are run, they can happen in a different order. You need to explicity set dependencies and requirements.&lt;/p&gt;

&lt;p&gt;To install the &lt;code&gt;nginx&lt;/code&gt; package, we'll add the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package { 'nginx': 
    ensure =&amp;gt; present,
    require =&amp;gt; Exec['apt-get update'],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A few things to note here. The package to install is being derived from the resource title. It defaults to this, or you can specify the option with the option key &lt;code&gt;name&lt;/code&gt;. We tell puppet to ensure that it's present on the system. We also say that this resource requires that the executable command &lt;code&gt;apt-get update&lt;/code&gt; has already been applied.&lt;/p&gt;

&lt;p&gt;When you reference other resources in a manifest file, the resource type has the first letter capitalised. Then you specify the title of the resource you're referencing. This is where resource titles are used.&lt;/p&gt;

&lt;p&gt;We'll also do the same for the &lt;code&gt;php-fpm&lt;/code&gt; package:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package { 'php5-fpm':
    ensure =&amp;gt; present,
    require =&amp;gt; Exec['apt-get update'],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Each of these packages come with a service that runs in the background, that we want to ensure is running all the time. We can make Puppet check this is the case with the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;service { 'nginx':
    ensure =&amp;gt; running,
    require =&amp;gt; Package['nginx'],
}

service { 'php5-fpm':
    ensure =&amp;gt; running,
    require =&amp;gt; Package['php5-fpm'],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice the require dependencies? Whenever Puppet runs it will check that these are running, and if they're not, for whatever reason, it will start them.&lt;/p&gt;

&lt;p&gt;Ready to see some magic? Save the file and run &lt;code&gt;vagrant up&lt;/code&gt;. Vagrant will boot up the virtual machine, and run the Puppet manifest on it. Once it's finished visit &lt;a href=&quot;http://localhost:8080&quot;&gt;http://localhost:8080&lt;/a&gt; and you should see:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Welcome to nginx!&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;You've not had to ssh into the machine once or run anything on the command line of that VM.&lt;/p&gt;

&lt;p&gt;Now in order to get PHP setup and running with nginx, we need to modify some of the nginx config files. We need to do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Disable the default nginx virtual host.&lt;/li&gt;
&lt;li&gt;Copy over a new virtual host for our vagrant setup that forwards on PHP requests to &lt;code&gt;php-fpm&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Symbolically link the virtual host config file to the sites-enabled directory.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;In the &lt;code&gt;private/puppet/modules/nginx/files&lt;/code&gt; directory we created, add the following in a file called &lt;code&gt;vagrant&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;server {
    listen 80;
    server_name _;
    root /vagrant;
    index index.php;

    location / {
        try_files $uri /index.php;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is a standard nginx configuration file for a virtual host. It will use &lt;code&gt;/vagrant&lt;/code&gt; on the virtual machine as the document root. This is a shared directory that Vagrant sets up to link to your project directory on your local machine.&lt;/p&gt;

&lt;p&gt;Saving that file, we can now open up the &lt;code&gt;base.pp&lt;/code&gt; file again and add a few more resources. The first is to copy over this virtual host to the correct location:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;file { 'vagrant-nginx':
    path =&amp;gt; '/etc/nginx/sites-available/vagrant',
    ensure =&amp;gt; file,
    require =&amp;gt; Package['nginx'],
    source =&amp;gt; 'puppet:///modules/nginx/vagrant',
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now lets disable the default virtual host the nginx package provides:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;file { 'default-nginx-disable':
    path =&amp;gt; '/etc/nginx/sites-enabled/default',
    ensure =&amp;gt; absent,
    require =&amp;gt; Package['nginx'],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And finally, enable our new vagrant virtual host:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;file { 'vagrant-nginx-enable':
    path =&amp;gt; '/etc/nginx/sites-enabled/vagrant',
    target =&amp;gt; '/etc/nginx/sites-available/vagrant',
    ensure =&amp;gt; link,
    notify =&amp;gt; Service['nginx'],
    require =&amp;gt; [
        File['vagrant-nginx'],
        File['default-nginx-disable'],
    ],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Save the file and run &lt;code&gt;vagrant reload&lt;/code&gt;. This tells vagrant to run the puppet provisioner again and ensure that the virtual machine matches the resources specified in the manifests file. We've just added three new ones, so it will action these.&lt;/p&gt;

&lt;p&gt;Ready to test it has all worked as expected? Create an &lt;code&gt;index.php&lt;/code&gt; file in your project directory, the same location where your &lt;code&gt;Vagrantfile&lt;/code&gt; is. In there just put a simple:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;?php phpinfo();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Save the file. Visit &lt;a href=&quot;http://localhost:8080&quot;&gt;http://localhost:8080&lt;/a&gt; again and you should see the standard &lt;code&gt;phpinfo&lt;/code&gt; page being served over php-fpm via nginx.&lt;/p&gt;

&lt;p&gt;That's it for this introduction. Hopefully it's enough to get you excited. For me, it means being able to keep a manifest file up-to-date, and if I ever change my VPS to a different provider and want to get it up and running within a few minutes rather than hours, I can apply it with Puppet. No more keeping a log of the many steps I would have to manually run over ssh as the alternative.&lt;/p&gt;

&lt;p&gt;If you want to know more, here are some great resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.puppetlabs.com/puppet_core_types_cheatsheet.pdf&quot;&gt;Puppet cheatsheet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.puppetlabs.com/#part-one-serverless-puppet&quot;&gt;Official puppet documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://vagrantup.com/docs/index.html&quot;&gt;Vagrant documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/davidwinter/Puppet-nginx-php-fpm-example&quot;&gt;Github repo of this example&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</content>
	</entry>
	
	<entry>
		<title>Testing Javascript websites with Behat</title>
		<link href="http://davidwinter.me/articles/2012/01/14/testing-javascript-websites-with-behat/"/>
		<updated>2012-01-14T00:00:00-08:00</updated>
		<id>http://davidwinter.me/articles/2012/01/14/testing-javascript-websites-with-behat</id>
		<content type="html">&lt;p&gt;&lt;strong&gt;This is my third blog post on how to use Behat for your web application testing. If you haven't already, be sure to read &lt;a href=&quot;/articles/2011/11/06/getting-started-with-behat/&quot;&gt;Getting started with Behat&lt;/a&gt; and &lt;a href=&quot;/articles/2012/01/13/using-behat-with-mink/&quot;&gt;Using Behat with Mink&lt;/a&gt; as this post continues on where they left off.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In my previous post I left you with being able to create your own custom, reusable definitions in Behat. We've touched on the real basics of testing with Behat on a very simple example website. However, in 2012, you'll find it very hard to find a website that doesn't have some kind of Javascript included providing a dynamic and better user experience for visitors. It's crucial that we're able to test and guarantee that the animations and ajax requests that the users interact with work as we expect.&lt;/p&gt;

&lt;p&gt;In this blog post, we'll be using the &lt;a href=&quot;http://twitter.github.com/bootstrap/javascript.html&quot;&gt;twitter bootstrap example&lt;/a&gt; site. It has a variety of different JS related widgets and interactions which are perfect for explaining some of the basic Javascript testing concepts when it comes to Behat. Here is the &lt;code&gt;behat.yml&lt;/code&gt; configuration file being used:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;default:
  context:
    parameters:
      base_url: http://twitter.github.com/bootstrap/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now take a look at the following example test:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Feature: Modal dialog

    Scenario: Open modal dialog
        Given I am on &quot;/javascript.html&quot;
        And I should see &quot;Launch Modal&quot;
        When I press &quot;Launch Modal&quot;
        Then I should see &quot;One fine body…&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you visit the &lt;a href=&quot;http://twitter.github.com/bootstrap/javascript.html#modal&quot;&gt;modals section&lt;/a&gt; on the example site, there is a red button under the demo heading. Clicking that opens a modal box with the text 'One fine body…'. That's what our above test is for. Run that test and you'll probably see:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Feature: Modal dialog

  Scenario: Open modal dialog            # features/modal.feature:3
    Given I am on &quot;/javascript.html&quot;     # FeatureContext::visit()
    And I should see &quot;Launch Modal&quot;      # FeatureContext::assertPageContainsText()
    When I press &quot;Launch Modal&quot;          # FeatureContext::pressButton()
      The selected node does not have a form ancestor.
    Then I should see &quot;One fine body…&quot; # FeatureContext::assertPageContainsText()

1 scenario (1 failed)
4 steps (2 passed, 1 skipped, 1 failed)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Great. Test failure. But we know it should work because we just did the exact same steps our test has in our browser. Why did it fail?&lt;/p&gt;

&lt;p&gt;Behat by default uses a headless driver called Goutte for all tests. It's a very fast driver, but it doesn't support Javascript. As the &lt;a href=&quot;http://mink.behat.org/&quot;&gt;official documentation for Mink&lt;/a&gt; so perfectly states:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Those browser emulators send a real HTTP requests against an application and parse the response content. They are very simple to run and configure, because this type of emulators can be written in any available programming language and can be run through console on servers without GUI. Headless emulators have both, advantages and disadvantages. Advantages are simplicity, speed and ability to run it without the need in real browser. But this type of browsers have one big disadvantage - they have no JS/AJAX support. So, you can’t test your rich GUI web applications with headless browsers.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;We need to use a different type of driver for our Javascript-dependant tests. These type of drivers are called 'browser controllers'. They launch the browser you designate and then it does exactly what your tests say. This means your tests have the full Javascript support of your browser behind them.&lt;/p&gt;

&lt;p&gt;The easiest of these drivers to get setup and running is &lt;code&gt;Selenium2Driver&lt;/code&gt;. We're going to use this with Google Chrome to get our tests passing. &lt;em&gt;Thanks to &lt;a href=&quot;https://twitter.com/colonelrosa&quot;&gt;@ColonelRosa&lt;/a&gt; for pointing out Selenium2 support in Mink 1.3.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First of all you'll need the &lt;code&gt;chromedriver&lt;/code&gt; on your machine, and have it available in your path. You're able to download the driver from the &lt;a href=&quot;http://code.google.com/p/chromium/downloads/list&quot;&gt;Google code project page&lt;/a&gt;. Or if you're on a Mac and have homebrew installed, simply run &lt;code&gt;brew install chromedriver&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The last thing you'll need is the WebDriver. Go to &lt;a href=&quot;http://seleniumhq.org/download/&quot;&gt;http://seleniumhq.org/download/&lt;/a&gt; and then you want to choose the 'Selenium Server'. As of writing, it's version 2.16.1. This will download a Java &lt;code&gt;.jar&lt;/code&gt; file. Once you have this, simply run the following command in the terminal. It's good idea to have this run in it's own window or tab, because it must be running while the tests are active:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;java -jar /path/to/selenium-server-standalone-2.16.1.jar
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You now have all the required tools.&lt;/p&gt;

&lt;p&gt;Now we just need to update our behat config, and our tests slightly. Update your &lt;code&gt;behat.yml&lt;/code&gt; to contain:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;default:
  context:
    parameters:
      base_url: http://twitter.github.com/bootstrap/
      browser: chrome
      javascript_session: webdriver
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is telling behat that for our javascript tests, we want it to use the &lt;code&gt;WebDriver&lt;/code&gt; driver (&lt;code&gt;Selenium2&lt;/code&gt;). Also, that it is to use Google Chrome as the browser the driver controls.&lt;/p&gt;

&lt;p&gt;Now, we need to simply mark our test as being Javascript dependant, which is as easy as adding a &lt;code&gt;@javascript&lt;/code&gt; tag above the &lt;code&gt;Scenario&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Feature: Modal dialog

    @javascript
    Scenario: Open modal dialog
        Given I am on &quot;/javascript.html&quot;
        And I should see &quot;Launch Modal&quot;
        When I press &quot;Launch Modal&quot;
        Then I should see &quot;One fine body…&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now run behat.&lt;/p&gt;

&lt;p&gt;Damn. The tests are still failing. This is because when the button is pressed, Javascript animates the opening of the modal window, and hasn't completed by the time the next step runs searching for the text. The animation hasn't completed, and therefore the step fails.&lt;/p&gt;

&lt;p&gt;Well, we can get the test passing, and also add an additional definition that will make testing modals easier in the future. Lets update the scenario to the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    @javascript
    Scenario: Open modal dialog
        Given I am on &quot;/javascript.html&quot;
            And I should see &quot;Launch Modal&quot;
        When I press &quot;Launch Modal&quot;
        Then I should see the modal &quot;Modal Heading&quot;
            And I should see &quot;One fine body…&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We've added a new definition &lt;code&gt;I should see the modal &quot;Modal Heading&quot;&lt;/code&gt;. This will allow us to write other tests that depend on modal dialogs opening with various headings. Run behat to get the skeleton definition method and add it to your &lt;code&gt;FeatureContext.php&lt;/code&gt; file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/**
 * @Then /^I should see the modal &quot;([^&quot;]*)&quot;$/
 */
public function iShouldSeeTheModal($title)
{
    $this-&amp;gt;getSession()-&amp;gt;wait(20000, '(0 === jQuery.active &amp;amp;&amp;amp; 0 === jQuery(\':animated\').length)');
    $this-&amp;gt;assertElementContainsText('#modal-from-dom .modal-header h3', $title);
    assertTrue($this-&amp;gt;getSession()-&amp;gt;getPage()-&amp;gt;find('css', '#modal-from-dom')-&amp;gt;isVisible());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I'll run through this method now line-by-line:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We use the &lt;code&gt;wait&lt;/code&gt; method to allow us to wait for the animation to complete. The first parameter sets 20 seconds to wait or until the following Javascript evaluates to true. We test to ensure that all Jquery ajax calls have completed, and that no elements are being animated.&lt;/li&gt;
&lt;li&gt;We want to check that the heading portion of the modal dialog contains the heading text that we pass through in the step. In our test, this is &quot;Modal Heading&quot;. So we use a CSS selector to do this.&lt;/li&gt;
&lt;li&gt;Finally, we just want to be sure that the modal dialog is actually visible on the page.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;That's it. One final change we can make is to move our Jquery wait check code into it's own method so that we can use this in any other custom definitions we may create in future:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;protected function jqueryWait($duration = 1000)
{
        $this-&amp;gt;getSession()-&amp;gt;wait($duration, '(0 === jQuery.active &amp;amp;&amp;amp; 0 === jQuery(\':animated\').length)');
}

/**
 * @Then /^I should see the modal &quot;([^&quot;]*)&quot;$/
 */
public function iShouldSeeTheModal($title)
{
    $this-&amp;gt;jqueryWait(20000);
    $this-&amp;gt;assertElementContainsText('#modal-from-dom .modal-header h3', $title);
    assertTrue($this-&amp;gt;getSession()-&amp;gt;getPage()-&amp;gt;find('css', '#modal-from-dom')-&amp;gt;isVisible());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now run behat, and you should get a perfect test score:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;1 scenario (1 passed)
5 steps (5 passed)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Example code used in this post can be found on &lt;a href=&quot;https://github.com/davidwinter/Testing-Javascript-websites-with-Behat&quot;&gt;Github&lt;/a&gt;.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Using Behat with Mink</title>
		<link href="http://davidwinter.me/articles/2012/01/13/using-behat-with-mink/"/>
		<updated>2012-01-13T00:00:00-08:00</updated>
		<id>http://davidwinter.me/articles/2012/01/13/using-behat-with-mink</id>
		<content type="html">&lt;p&gt;&lt;strong&gt;This is my second blog post on how to use Behat for your web application testing. If you haven't already, be sure to read &lt;a href=&quot;/articles/2011/11/06/getting-started-with-behat/&quot;&gt;Getting started with Behat&lt;/a&gt; as this post continues on where that left off.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In my last post, I left you with hopefully enough knowledge to get started with some basic testing that comes for free, out of the box, by just installing &lt;a href=&quot;http://behat.org/&quot;&gt;Behat&lt;/a&gt; and &lt;a href=&quot;http://mink.behat.org/&quot;&gt;Mink&lt;/a&gt;. You were able to do that because Mink comes with around 34 bundled definitions.&lt;/p&gt;

&lt;p&gt;We created a very basic test to ensure that users were able to log into our example website:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Feature: User sessions
    In order to access their account
    As a user
    I need to be able to log into the website

    Scenario: Login
        Given I am on &quot;/&quot;
            And I should see &quot;Login&quot;
        When I fill in &quot;email&quot; with &quot;myemail@test.com&quot;
            And I fill in &quot;password&quot; with &quot;mysecurepassword&quot;
            And I press &quot;Login&quot;
        Then I should be on &quot;/dashboard&quot;
            And I should see &quot;Welcome back&quot;

    Scenario: Logout
        Given I am on &quot;/dashboard&quot;
        When I follow &quot;Logout&quot;
        Then I should be on &quot;/&quot;
            And I should see &quot;Login&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So that we can work on this further, I've created a basic PHP script that we can use to play with. It's written with the &lt;a href=&quot;http://silex.sensiolabs.org/&quot;&gt;Silex&lt;/a&gt; micro-framework and doesn't require any modification in order for us to write the tests in this blog post.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you do want to play along, clone the &lt;a href=&quot;https://github.com/davidwinter/Using-Behat-with-Mink&quot;&gt;example from github&lt;/a&gt;, setup a virtual host. Then just be sure to update the &lt;code&gt;behat.yml&lt;/code&gt; file so that the &lt;code&gt;base_url&lt;/code&gt; points to your new virtual host.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you run &lt;code&gt;bin/behat&lt;/code&gt; now, it'll run through the test and fail:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;2 scenarios (1 passed, 1 failed)
11 steps (8 passed, 2 skipped, 1 failed)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In our test, we've missed something! For the second scenario, we've not logged in the user! So they're unable to access the &lt;code&gt;/dashboard&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we focus on just the Logout scenario from here on, in order to log in the user, we could do something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Scenario: Logout
    Given I am on &quot;/&quot;
        And I should see &quot;Login&quot;
    When I fill in &quot;email&quot; with &quot;myemail@test.com&quot;
        And I fill in &quot;password&quot; with &quot;mysecurepassword&quot;
        And I press &quot;Login&quot;
        And I should be on &quot;/dashboard&quot;
    When I follow &quot;Logout&quot;
    Then I should be on &quot;/&quot;
        And I should see &quot;Login&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is basically duplicating the steps from the above login scenario. Running this, the tests now pass. Excellent. Are we finished? No. The test above isn't very tidy because we just copy and pasted code. It'd be so much nicer if we could write something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Scenario: Logout
    Given I am logged in as &quot;myemail@test.com&quot; with password &quot;mysecurepassword&quot;
        And I am on &quot;/dashboard&quot;
    When I follow &quot;Logout&quot;
    Then I should be on &quot;/&quot;
        And I should see &quot;Login&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That makes our feature file a lot more readable, cleaner, and also provides us with a new custom definition that we can then use in other future tests. How do we make this work?&lt;/p&gt;

&lt;p&gt;When you run &lt;code&gt;bin/behat&lt;/code&gt; and you've added a custom definition, it's very kind by providing you with an example template for creating the PHP code to have this new definition work:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/**
 * @Given /^I am logged in as &quot;([^&quot;]*)&quot; with password &quot;([^&quot;]*)&quot;$/
 */
public function iAmLoggedInAsWithPassword($argument1, $argument2)
{
    throw new PendingException();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What do we do with this? Paste it directly into &lt;code&gt;features/bootstrap/FeatureContext.php&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we just need to use Mink to program the definition. Looking at the source for the &lt;a href=&quot;https://github.com/Behat/Mink/blob/master/src/Behat/Mink/Behat/Context/BaseMinkContext.php&quot;&gt;base mink context&lt;/a&gt;, we can use the following methods to complete the definition:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/**
 * @Given /^I am logged in as &quot;([^&quot;]*)&quot; with password &quot;([^&quot;]*)&quot;$/
 */
public function iAmLoggedInAsWithPassword($email, $password)
{
    $this-&amp;gt;visit('/');
    $this-&amp;gt;fillField('email', $email);
    $this-&amp;gt;fillField('password', $password);
    $this-&amp;gt;pressButton('Login');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Saving, and then running &lt;code&gt;bin/behat&lt;/code&gt; again, the tests will now pass. And you've just created your first custom definition.&lt;/p&gt;

&lt;p&gt;There is one final change we could make to this. As our definition is using all existing definitions, we could future proof it by changing it to the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/**
 * @Given /^I am logged in as &quot;([^&quot;]*)&quot; with password &quot;([^&quot;]*)&quot;$/
 */
public function iAmLoggedInAsWithPassword($email, $password)
{
    return array(
        new Step\Given('I am on &quot;/&quot;'),
        new Step\When('I fill in &quot;email&quot; with &quot;'.$email.'&quot;'),
        new Step\When('I fill in &quot;password&quot; with &quot;'.$password.'&quot;'),
        new Step\When('I press &quot;Login&quot;'),
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In order to use the above, be sure to update the &lt;code&gt;use&lt;/code&gt; statement at the top of the file so it contains the last line of the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;use Behat\Behat\Context\ClosuredContextInterface,
    Behat\Behat\Context\TranslatedContextInterface,
    Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException,
    Behat\Behat\Context\Step;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That's all there is to it when creating custom definitions. The Mink source is a great way to look at how the out-of-the-box definitions work.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Setup rbenv on Mac OS X</title>
		<link href="http://davidwinter.me/articles/2011/12/10/setup-rbenv-on-mac-os-x/"/>
		<updated>2011-12-10T00:00:00-08:00</updated>
		<id>http://davidwinter.me/articles/2011/12/10/setup-rbenv-on-mac-os-x</id>
		<content type="html">&lt;p&gt;&lt;a href=&quot;https://github.com/sstephenson/rbenv&quot;&gt;rbenv&lt;/a&gt; is a quick way to get different ruby versions installed on your OS, without interfering with any default installs it may provide.&lt;/p&gt;

&lt;p&gt;Run the following commands:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;brew update
brew install rbenv
brew install ruby-build
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In your bash profile file, ensure that the following is included:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;eval &quot;$(rbenv init -)&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, install the latest version of ruby:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rbenv install 1.9.3-p0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Run the following to set &lt;code&gt;rbenv&lt;/code&gt; to use this new version of ruby.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rbenv global 1.9.3-p0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Open a new shell and type &lt;code&gt;ruby --version&lt;/code&gt; and if you see &lt;code&gt;ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]&lt;/code&gt; then all is good and you're all setup!&lt;/p&gt;

&lt;p&gt;One thing to note is that if you install a new gem that installs a binary, you'll need to run &lt;code&gt;rbenv rehash&lt;/code&gt; afterwards in order for the binary to become available to you on the command line.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Getting started with Behat</title>
		<link href="http://davidwinter.me/articles/2011/11/06/getting-started-with-behat/"/>
		<updated>2011-11-06T00:00:00-07:00</updated>
		<id>http://davidwinter.me/articles/2011/11/06/getting-started-with-behat</id>
		<content type="html">&lt;p&gt;Where &lt;a href=&quot;https://github.com/sebastianbergmann/phpunit&quot;&gt;PHPUnit&lt;/a&gt; is for testing the individual pieces of your code (unit testing), &lt;a href=&quot;http://behat.org/&quot;&gt;Behat&lt;/a&gt; is a great tool for testing that an application behaves as expected when following a series of steps (functional testing). And for testing web applications, Behat ties in perfectly with &lt;a href=&quot;https://github.com/behat/mink&quot;&gt;Mink&lt;/a&gt; to give you a bunch of web browser related tests out-of-the-box.&lt;/p&gt;

&lt;h2&gt;Install&lt;/h2&gt;

&lt;p&gt;We'll be using &lt;a href=&quot;http://packagist.org/&quot;&gt;composer&lt;/a&gt; to setup Behat and Mink. Create a &lt;code&gt;composer.json&lt;/code&gt; file in your project directory with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;{
    &quot;require&quot;: {
        &quot;behat/behat&quot;: &quot;&amp;gt;=2.2.4&quot;,
        &quot;behat/mink&quot;: &quot;&amp;gt;=1.3.2&quot;
    },

    &quot;repositories&quot;: {
        &quot;behat/mink-deps&quot;: { &quot;composer&quot;: { &quot;url&quot;: &quot;behat.org&quot; } }
    },

    &quot;config&quot;: {
        &quot;bin-dir&quot;: &quot;bin/&quot;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you don't have the &lt;code&gt;composer.phar&lt;/code&gt; file already on your machine, simply run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;wget http://getcomposer.org/composer.phar
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;php composer.phar install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Behat and Mink will then install into a &lt;code&gt;vendor&lt;/code&gt; and &lt;code&gt;bin&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;In your project directory, run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;bin/behat --init
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will create a &lt;code&gt;features&lt;/code&gt; directory for you that contains the required files to get started.&lt;/p&gt;

&lt;p&gt;In order to setup Behat to use Mink, go into &lt;code&gt;features/bootstrap/FeatureContext.php&lt;/code&gt; and make sure that the following is defined:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require_once __DIR__.'/../../vendor/.composer/autoload.php';
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class FeatureContext extends Behat\Mink\Behat\Context\MinkContext
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here you're including the Mink library, and then ensuring that &lt;code&gt;FeatureContext&lt;/code&gt; extends from &lt;code&gt;MinkContext&lt;/code&gt;. You don't really need to worry about what this file does for this example.&lt;/p&gt;

&lt;p&gt;Now let Behat know where it can test your web app from. In the root of your project directory, create a file called &lt;code&gt;behat.yml&lt;/code&gt; and include the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;default:
  context:
    parameters:
      base_url: http://localhost/mywebapp/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is a &lt;a href=&quot;http://en.wikipedia.org/wiki/Yaml&quot;&gt;YAML&lt;/a&gt; file, and you just need to update the &lt;code&gt;base_url&lt;/code&gt; value to whatever URL behat can access your app.&lt;/p&gt;

&lt;p&gt;You're ready to start.&lt;/p&gt;

&lt;h2&gt;Describe your application with features&lt;/h2&gt;

&lt;p&gt;Run the following command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;bin/behat -dl
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should see a long list of available definitions that Behat currently has available to it. You can use these to write some powerful feature files that test your application.&lt;/p&gt;

&lt;p&gt;First, lets set the scene:&lt;/p&gt;

&lt;p&gt;Say on your homepage you have a login form and you want to make sure when someone logs in, they get directed to a dashboard page with a welcome message. You also want to check that when they logout, they are taken to the homepage with a confirmation message saying they've been logged out.&lt;/p&gt;

&lt;p&gt;Each test file has the &lt;code&gt;.feature&lt;/code&gt; extension that Behat uses to search for tests to run when calling the &lt;code&gt;behat&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;These feature files are written in the &lt;a href=&quot;http://docs.behat.org/guides/1.gherkin.html&quot;&gt;Gherkin syntax&lt;/a&gt;. First of all you specify a particular feature you are testing. This is with the keyword &lt;code&gt;Feature:&lt;/code&gt; and a name. After which, you add a brief description of what the benefit, the role, and the actual feature is. You can have anything here, but it's good practice to follow this format to make things readable.&lt;/p&gt;

&lt;p&gt;For each feature, you have a bunch of different scenarios that you want to test against to ensure it matches the expected behaviour. Scenarios consist of definitions that can be prefixed with either &lt;strong&gt;Given&lt;/strong&gt;, &lt;strong&gt;When&lt;/strong&gt;, &lt;strong&gt;Then&lt;/strong&gt; and &lt;strong&gt;And&lt;/strong&gt;. These keywords don't mean anything, and are there purely to aid readability. &lt;strong&gt;Given&lt;/strong&gt; is where you'd set a bunch of pre-conditions for your test, &lt;strong&gt;When&lt;/strong&gt; for the steps taken to test, and &lt;strong&gt;Then&lt;/strong&gt; should describe what expected result you should see. &lt;strong&gt;And&lt;/strong&gt; can be used to include additional definitions per stage in a scenario.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Feature: User sessions
    In order to access their account
    As a user
    I need to be able to log into the website

    Scenario: Login
        Given I am on &quot;/&quot;
        And I should see &quot;Login&quot;
        When I fill in &quot;email&quot; with &quot;myemail@test.com&quot;
        And I fill in &quot;password&quot; with &quot;mysecurepassword&quot;
        And I press &quot;Login&quot;
        Then I should be on &quot;/dashboard&quot;
        And I should see &quot;Welcome back&quot;

    Scenario: Logout
        Given I am on &quot;/dashboard&quot;
        When I click on &quot;Logout&quot;
        Then I should be on &quot;/&quot;
        And I should see &quot;Logged out&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Because we've written the steps in the available Mink definitions, we don't need to write any PHP code! You can now run &lt;code&gt;bin/behat&lt;/code&gt; in the Terminal and you'll see the result of the tests and whether your application conforms to them--all lit up in green!&lt;/p&gt;

&lt;p&gt;These example tests may seem rather trivial, but having them means you have a safety net for your application, so that if you or someone changes something, however small or large to your code, and it breaks the expected application behaviour, your Behat tests will let you know sooner, when it's easy to fix, rather than later when it'll become a nightmare to track down.&lt;/p&gt;

&lt;p&gt;As an aside, it should be noted that Behat isn't just great for us developers. If you're working on a project for someone else, you can write up these feature files based on a clients spec, and get them to sign off of them for extra security and peace of mind for all parties.&lt;/p&gt;

&lt;p&gt;With the available definitions that Mink provides for Behat, you could quite easily write a ton of tests with just the definitions Mink provides. In a future blog post I'll go on further to show how to write custom defintitions. Hopefully this is enough information to get you started and excited about using Behat in your projects.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Setting up Github Pages</title>
		<link href="http://davidwinter.me/articles/2011/10/29/setting-up-github-pages/"/>
		<updated>2011-10-29T00:00:00-07:00</updated>
		<id>http://davidwinter.me/articles/2011/10/29/setting-up-github-pages</id>
		<content type="html">&lt;p&gt;I've not had time recently to keep my copy of &lt;a href=&quot;http://wordpress.org&quot;&gt;Wordpress&lt;/a&gt; up-to-date (yes, even though it's &lt;a href=&quot;http://codex.wordpress.org/Installing/Updating_WordPress_with_Subversion&quot;&gt;setup with SVN&lt;/a&gt;). I wanted something a little easier to manage. Having read good things about &lt;a href=&quot;http://pages.github.com/&quot;&gt;Github Pages&lt;/a&gt;, I decided to take the plunge. I've now migrated this entire site over. It's simple to setup, and also has &lt;a href=&quot;https://github.com/mojombo/jekyll/&quot;&gt;Jekyll&lt;/a&gt; support enabled by default.&lt;/p&gt;

&lt;p&gt;Jekyll allows you to generate a static website from plain text post files, and templates. When you push your new posts to Github, Jekyll is run on the files and the static site is built and hosted for you.&lt;/p&gt;

&lt;h2&gt;Setup&lt;/h2&gt;

&lt;p&gt;To get started, install &lt;code&gt;jekyll&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo gem install jekyll
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you want to use &lt;a href=&quot;http://daringfireball.net/projects/markdown/&quot;&gt;Markdown&lt;/a&gt; formatting in your posts, also install:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo gem install rdiscount
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you'll need to create a git repository to manage your blog. There is a great &lt;a href=&quot;https://github.com/danielmcgraw/Jekyll-Base&quot;&gt;Jekyll skeleton setup&lt;/a&gt; that I used here, so we'll go ahead and clone that.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git clone https://github.com/danielmcgraw/Jekyll-Base.git yourgithubusername.github.com
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Be sure you replace &lt;code&gt;yourgithubusername&lt;/code&gt; with your Github username. This isn't required on your local repo, but is for the repository you create on Github. It's in this format so that Github can detect it's a Github Pages site, and so it just makes sense to name it the same locally too.&lt;/p&gt;

&lt;p&gt;Now, we don't want the git history for this skeleton setup, so remove the &lt;code&gt;.git&lt;/code&gt; directory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd yourgithubusername.github.com/
rm -rf .git
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To test things are working at this stage, open another terminal window in the same directory and run &lt;code&gt;jekyll --server&lt;/code&gt;. It keeps a process open, along with a webserver running on port 4000, monitoring file changes, and rebuilding the site automatically on the fly. So you'll want to keep this terminal window open and running. You should now be able to see the generated site by visiting &lt;a href=&quot;http://localhost:4000&quot;&gt;http://localhost:4000&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;jekyll&lt;/code&gt; builds the site, it puts the generated files into a &lt;code&gt;_site&lt;/code&gt; directory. You don't want this directory in Git, so in the &lt;code&gt;.gitignore&lt;/code&gt; file add &lt;code&gt;_site&lt;/code&gt; to the bottom so that it'll be ignored.&lt;/p&gt;

&lt;p&gt;Now on Github, create a new repository naming it in the same format &lt;code&gt;yourgithubusername.github.com&lt;/code&gt;. Then on your local machine:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git init
git add .
git commit -m &quot;Initial commit. Jekyll base setup.&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then follow Github's instructions on adding the remote reference, which is along the lines of (substitute &lt;code&gt;username&lt;/code&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git remote add origin git@github.com:username/username.github.com.git
git push -u origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This pushes your site up to Github. After this initial push you can do a plain old &lt;code&gt;git push&lt;/code&gt; when you've made changes that are ready to be published.&lt;/p&gt;

&lt;p&gt;They say it can take up to 10 minutes for new sites to be generated when they are first pushed, however, it'll likely be under a minute. They'll email you either way when it's done.&lt;/p&gt;

&lt;p&gt;You can then visit &lt;a href=&quot;http://yourgithubusername.github.com&quot;&gt;http://yourgithubusername.github.com&lt;/a&gt; to see your new static site all built, and being served. Snappy isn't it?&lt;/p&gt;

&lt;h2&gt;Wordpress migration&lt;/h2&gt;

&lt;p&gt;There were two important things that I wanted to ensure kept the same during the migration to Github pages; the URLs for the posts, and the comments posted.&lt;/p&gt;

&lt;p&gt;My permalink structure on Wordpress was:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/articles/%year%/%monthnum%/%day%/%postname%/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So to set this up on Jekyll, all I had to do was edit &lt;code&gt;_config.yml&lt;/code&gt; and set:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;permalink: /articles/:year/:month/:day/:title/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For comments, I had already been using &lt;a href=&quot;http://disqus.com/&quot;&gt;Disqus&lt;/a&gt; on my Wordpress install, so they already had that content. All I had to do was setup the Javascript to pull in those comments.  In the &lt;code&gt;_layouts/post.html&lt;/code&gt; file, all I did was add:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;disqus_thread&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;cm&quot;&gt;/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;disqus_shortname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;davidwinter&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// required: replace example with your forum shortname&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;disqus_url&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;http://davidwinter.me{{ page.url }}&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/* * * DON&amp;#39;T EDIT BELOW THIS LINE * * */&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;dsq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;createElement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;script&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;dsq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;text/javascript&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;dsq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;dsq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;src&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;http://&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;disqus_shortname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;.disqus.com/embed.js&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementsByTagName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;head&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementsByTagName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;body&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;appendChild&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;dsq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;})();&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;noscript&amp;gt;&lt;/span&gt;Please enable JavaScript to view the &lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;http://disqus.com/?ref_noscript&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;comments powered by Disqus.&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&amp;lt;/noscript&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;http://disqus.com&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;dsq-brlink&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;blog comments powered by &lt;span class=&quot;nt&quot;&gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;logo-disqus&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Disqus&lt;span class=&quot;nt&quot;&gt;&amp;lt;/span&amp;gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The important stuff here is the &lt;code&gt;disqus_shortname&lt;/code&gt; and &lt;code&gt;disqus_url&lt;/code&gt; variables. These are required in order for Disqus to know which site comments to display, and the &lt;code&gt;disqus_url&lt;/code&gt; to know which page comments to render. Because my URLs are staying the same, this was easy by just using the domain and the &lt;code&gt;{{ page.url }}&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;While you're setting this up locally, you may want to include:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;disqus_developer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;As the &lt;a href=&quot;http://docs.disqus.com/help/2/&quot;&gt;help documents&lt;/a&gt; explain:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Tells the Disqus service that you are testing the system on an inaccessible website, e.g. secured staging server or a local environment. If disqus_developer is off or undefined, Disqus' default behavior will be to attempt to read the location of your page and validate the URL. If unsuccessful, Disqus will not load. Use this variable to get around this restriction while you are testing on an inaccessible website.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Be sure that this is only set while testing the integration of Disqus. When you know it's working locally, remove this variable, so that it doesn't get pushed up to Github.&lt;/p&gt;

&lt;p&gt;And also, ensure that your &lt;code&gt;yourgithubusername.github.com&lt;/code&gt; address is added to your trusted domains list in the Disqus admin.&lt;/p&gt;

&lt;p&gt;One last thing that is quite important to bring over to my new Github Pages setup is all of my old posts... duh. Depending on the size of your existing blog, you may just want to do this by hand. However, with 50+ posts, I wanted something automated.&lt;/p&gt;

&lt;p&gt;In Wordpress, you can do an export of your posts under the Tools section of the left hand menu. Just select to export the posts, not all content, as you don't need it.&lt;/p&gt;

&lt;p&gt;I wrote a &lt;a href=&quot;https://github.com/davidwinter/wordpress-to-jekyll&quot;&gt;quick PHP tool&lt;/a&gt; to convert this exported data into Jekyll friendly post files. Once these have been generated, you can copy them into your &lt;code&gt;_posts&lt;/code&gt; directory, and Jekyll will generate the pages (if it's still running).&lt;/p&gt;

&lt;p&gt;Push up to Github, and you're all set. Just tinker around with the layouts and styling and you'll then have a super fast, easy to manage, Jekyll powered Github Pages site.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Simple local web development with Apache and Dnsmasq</title>
		<link href="http://davidwinter.me/articles/2011/06/18/simple-local-web-development-with-apache-and-dnsmasq/"/>
		<updated>2011-06-18T00:00:00-07:00</updated>
		<id>http://davidwinter.me/articles/2011/06/18/simple-local-web-development-with-apache-and-dnsmasq</id>
		<content type="html">&lt;p&gt;When you sit down and start work on a new project, what hoops do you have to jump through before you get your app running in your browser? There are more than likely always going to be two; creating a spoof domain that you create in &lt;code&gt;/etc/hosts&lt;/code&gt; so that you can access your app, and then a virtual host in Apache to make your files accessible. And it sucks having to do this each time. Don't you wish you could just get on with the project as soon as you think of it? Read on.&lt;/p&gt;

&lt;p&gt;This was all inspired by &lt;a href=&quot;http://pow.cx/&quot;&gt;Pow&lt;/a&gt;, a zero-configuration rack server created by &lt;a href=&quot;http://37signals.com/&quot;&gt;37signals&lt;/a&gt;, that allows &lt;a href=&quot;http://rubyonrails.org/&quot;&gt;Rails&lt;/a&gt; developers to do exactly that. By following convention over configuration, this can all be easily achieved. If you're happy with accessing your apps via a spoof domain ending in &lt;code&gt;.dev&lt;/code&gt;, then follow these steps.&lt;/p&gt;

&lt;h2&gt;Requirements&lt;/h2&gt;

&lt;p&gt;I'm assuming that you've got &lt;a href=&quot;http://mxcl.github.com/homebrew/&quot;&gt;Homebrew&lt;/a&gt; installed, and that you're using the built-in version of Apache on Mac OS X 10.6 Snow Leopard. However, these instructions should work fine on Linux too.&lt;/p&gt;

&lt;h2&gt;Dnsmasq&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://www.thekelleys.org.uk/dnsmasq/doc.html&quot;&gt;Dnsmasq&lt;/a&gt; is a simple to install and setup mini DNS server. We'll use it to setup a wildcard record of anything &lt;code&gt;.dev&lt;/code&gt; to point to your local machine.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;brew install dnsmasq
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Follow the instructions after install about copying the configuration file to the expected place in &lt;code&gt;/usr/local/etc/dnsmasq&lt;/code&gt;, but don't copy the &lt;code&gt;launchd&lt;/code&gt; settings yet. First of all edit &lt;code&gt;/usr/local/etc/dnsmasq&lt;/code&gt; and add the following at the very bottom:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;address=/dev/127.0.0.1
listen-address=127.0.0.1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Save. Now copy the &lt;code&gt;launchd&lt;/code&gt; settings across. Dnsmasq should now be running. You now need to tell your system to use it as a DNS server.&lt;/p&gt;

&lt;p&gt;Open up System Preferences and go to your Network settings and the DNS servers. Add &lt;code&gt;127.0.0.1&lt;/code&gt; to the top of the list, and then add whatever your normal internet facing DNS servers are beneath. I use &lt;a href=&quot;http://www.opendns.com/&quot;&gt;OpenDNS&lt;/a&gt;. My DNS server list then looks like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;127.0.0.1&lt;/li&gt;
&lt;li&gt;208.67.222.222&lt;/li&gt;
&lt;li&gt;208.67.220.220&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Apply changes. Now open the Terminal and run a ping on anything ending with a &lt;code&gt;.dev&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ping test.dev
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It should start pinging your machine, &lt;code&gt;127.0.0.1&lt;/code&gt; - if it does, all is good.&lt;/p&gt;

&lt;p&gt;Now we can move onto configuring Apache for the last time.&lt;/p&gt;

&lt;h2&gt;Apache&lt;/h2&gt;

&lt;p&gt;Edit the following file, substituting &lt;code&gt;yourusername&lt;/code&gt; with whatever your system username is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /etc/apache2/users/yourusername.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And then make it look like the following, again replacing &lt;code&gt;yourusername&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;NameVirtualHost *:80

&amp;lt;Directory &quot;/Users/yourusername/Sites/&quot;&amp;gt;
    Options Indexes MultiViews FollowSymLinks Includes
    AllowOverride All
    Order allow,deny
    Allow from all
&amp;lt;/Directory&amp;gt;

&amp;lt;VirtualHost *:80&amp;gt;
    UseCanonicalName off
    VirtualDocumentRoot /Users/yourusername/Sites/%0/public
&amp;lt;/VirtualHost&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Save. And then run &lt;code&gt;sudo apachectl graceful&lt;/code&gt;. This will restart Apache. In the above configuration, we're using Apache's &lt;a href=&quot;http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html&quot;&gt;mod_vhost_alias&lt;/a&gt; module to dynamically configure virtual hosts and document roots on the fly.&lt;/p&gt;

&lt;h2&gt;Testing it all works&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;mkdir -p ~/Sites/test.dev/public
echo &quot;test.dev&quot; &amp;gt; ~/Sites/test.dev/public/index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now visit &lt;a href=&quot;http://test.dev/&quot;&gt;http://test.dev/&lt;/a&gt; in your web browser. Hopefully you should see &lt;code&gt;test.dev&lt;/code&gt; - if so congratulations; you're going to save so much time in the future now.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Setup UFW on Ubuntu 11.04</title>
		<link href="http://davidwinter.me/articles/2011/06/05/setup-ufw-on-ubuntu-11-04/"/>
		<updated>2011-06-05T00:00:00-07:00</updated>
		<id>http://davidwinter.me/articles/2011/06/05/setup-ufw-on-ubuntu-11-04</id>
		<content type="html">&lt;p&gt;Not much more than a few hours have passed since I posted my &lt;a href=&quot;/articles/2011/06/05/install-apf-on-ubuntu-11-04/&quot;&gt;APF setup howto&lt;/a&gt;, but I've found something even simpler.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ufw&lt;/code&gt; - uncomplicated firewall. It does exactly as it says on the tin.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo aptitude install ufw
sudo ufw allow 22
sudo ufw enable
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That's ssh sorted, and the firewall is enabled. It's important to allow port 22 first before enabling, otherwise you'll get locked out. You can see the status of the firewall by running:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ufw status verbose
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And to allow Apache traffic:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ufw allow 80
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That's it. Outgoing traffic is allowed by default. Everything else is blocked. Also, all of these rules are saved transparently, so you don't have to worry about things like that. Reboot away, and everything will just work.&lt;/p&gt;

&lt;p&gt;If you need to disable &lt;code&gt;ufw&lt;/code&gt; just run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ufw disable
&lt;/code&gt;&lt;/pre&gt;
</content>
	</entry>
	
	<entry>
		<title>Install APF on Ubuntu 11.04</title>
		<link href="http://davidwinter.me/articles/2011/06/05/install-apf-on-ubuntu-11-04/"/>
		<updated>2011-06-05T00:00:00-07:00</updated>
		<id>http://davidwinter.me/articles/2011/06/05/install-apf-on-ubuntu-11-04</id>
		<content type="html">&lt;p&gt;I just setup a new Linode to host my new twitter app, &lt;a href=&quot;http://listsdj.com&quot;&gt;lists dj&lt;/a&gt; and wanted to lock it down to basically Apache and SSH.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;iptables&lt;/code&gt; gives me a headache, and I don't want to spend a day learning how to use it. APF uses iptables, but the configuration is so much easier.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo aptitude install apf-firewall
sudo nano /etc/apf-firewall/conf.apf
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you're using Linode, you'll need to ensure the following line and configuration value is set:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;SET_MONOKERN=&quot;1&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is because &lt;code&gt;iptables&lt;/code&gt; is installed into the kernel rather than as a package. Not setting this will prevent APF from running.&lt;/p&gt;

&lt;p&gt;Then, to open the desired ports, just update this line with the port numbers, separated with a space:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;IG_TCP_CPORTS=&quot;22 80 443&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And ensure this line is set:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;DEVEL_MODE=&quot;0&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This means that the firewall isn't in development mode, otherwise your rules will be flushed every 5 minutes. Handy if you're experimenting with new rules and don't want to be locked out of your server. Worse case, you'd only have to wait 5 minutes before being able to get back in. But because I'm only opening ports, I don't need this. Just make sure you leave your SSH port open!&lt;/p&gt;

&lt;p&gt;Lastly, open up this file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /etc/default/apf-firewall
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And set it to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;RUN=&quot;yes&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you can start up APF and you're all protected:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/apf-firewall start
&lt;/code&gt;&lt;/pre&gt;
</content>
	</entry>
	
	<entry>
		<title>PHP development on Mac OS X 10.6 Snow Leopard</title>
		<link href="http://davidwinter.me/articles/2011/05/27/php-development-on-mac-os-x-10-6-snow-leopard/"/>
		<updated>2011-05-27T00:00:00-07:00</updated>
		<id>http://davidwinter.me/articles/2011/05/27/php-development-on-mac-os-x-10-6-snow-leopard</id>
		<content type="html">&lt;p&gt;Having a reliable and easy to use developer environment on my Mac is essential for my work, and the odd project I work on.&lt;/p&gt;

&lt;p&gt;I original used the &lt;a href=&quot;http://www.entropy.ch/software/macosx/php/&quot;&gt;Entropy PHP packages&lt;/a&gt; as they were so easy to install, and &lt;em&gt;just worked&lt;/em&gt;. Unfortunately, Marc Liyanage had stopped updating the packages with new OS X releases, so I then tried alternatives such as &lt;a href=&quot;http://davidwinter.me/articles/2010/01/02/apache-php-and-mongodb-on-mac-os-x-10-6-snow-leopard/&quot;&gt;Macports&lt;/a&gt;, and more recently &lt;a href=&quot;http://www.mamp.info/&quot;&gt;MAMP&lt;/a&gt;. However, it hasn't been working out of late, so I went in search of alternatives.&lt;/p&gt;

&lt;p&gt;For a long shot, I visited Marc's PHP page and noticed that he is now &lt;a href=&quot;http://php-osx.liip.ch/&quot;&gt;endorsing a project&lt;/a&gt; which forked off from his own. Like his packages, it uses the default Apache install that comes with OS X, but uses their own alternative PHP package. It installs into &lt;code&gt;/usr/local&lt;/code&gt; so that it doesn't interfere with anything else. So here are some steps to get this all up and running.&lt;/p&gt;

&lt;h2&gt;Setup&lt;/h2&gt;

&lt;p&gt;Before I started, I uninstalled Macports and MAMP. Not required, but I didn't want any of those packages to interfere or conflict with the new install of PHP. I now use &lt;a href=&quot;http://mxcl.github.com/homebrew/&quot;&gt;homebrew&lt;/a&gt; as a replacement to Macports for tools that I require (such as MongoDB and MySQL).&lt;/p&gt;

&lt;h3&gt;Install PHP for OS X&lt;/h3&gt;

&lt;p&gt;Install PHP via &lt;a href=&quot;http://php-osx.liip.ch&quot;&gt;http://php-osx.liip.ch&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;curl -s http://php-osx.liip.ch/install.sh | bash -
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It'll ask for your password so that it can install to &lt;code&gt;/usr/local&lt;/code&gt;. If you're cautious of running a shell script from a webpage--as you should be!--why not view the source in your browser first to be sure all is as it should.&lt;/p&gt;

&lt;h3&gt;Setup path&lt;/h3&gt;

&lt;p&gt;If you want to use any of the PHP tools via the command line, such as &lt;code&gt;pear&lt;/code&gt; or &lt;code&gt;pecl&lt;/code&gt;, it's best if you add the new PHP &lt;code&gt;bin&lt;/code&gt; to your path. I updated mine to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;export PATH=&quot;/usr/local/php5/bin:/usr/local/bin:$PATH&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The important part here is the inclusion of &lt;code&gt;/usr/local/php5/bin&lt;/code&gt;. You'll then want to open a new terminal window so that the path update takes effect. Check by running:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;echo $PATH
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you're going to use &lt;code&gt;pear&lt;/code&gt; or &lt;code&gt;pecl&lt;/code&gt;, it may be worth checking that it's configured to use the correct &lt;code&gt;php.ini&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Run the following command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pear config-get php_ini
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If it doesn't say:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/usr/local/php5/lib/php.ini
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo pear config-set php_ini /usr/local/php5/lib/php.ini
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And then just check that &lt;code&gt;pecl&lt;/code&gt; has the same config set:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pecl config-get php_ini
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That should now be PHP setup and running.&lt;/p&gt;

&lt;h2&gt;Easy Apache configuration&lt;/h2&gt;

&lt;p&gt;With some great pointers by &lt;a href=&quot;http://www.456bereastreet.com/archive/201104/apache_with_virtual_hosts_php_and_ssi_on_mac_os_x_106/&quot;&gt;Roger Johansson&lt;/a&gt; here is a great way to make configuring Apache easy for projects you work on. You'll need to edit two files. One will make some hostname aliases to your local machine so that you can access a project via a url such as &lt;code&gt;http://myproject.local&lt;/code&gt;. The other file is the Apache configuration file for your user, where you specify the server name and document root location. Here we go:&lt;/p&gt;

&lt;p&gt;Edit &lt;code&gt;/etc/apache2/users/yourusername.conf&lt;/code&gt; substituting &lt;code&gt;yourusername&lt;/code&gt; with the actual username you use on your Mac. Update it so that it looks like this (again updating the path with your actual username):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;NameVirtualHost *:80

&amp;lt;Directory &quot;/Users/yourusername/Sites/&quot;&amp;gt;
    Options Indexes MultiViews FollowSymLinks Includes
    AllowOverride All
    Order allow,deny
    Allow from all
&amp;lt;/Directory&amp;gt;

&amp;lt;VirtualHost *:80&amp;gt;
    ServerName myproject.local
    DocumentRoot /Users/yourusername/Sites/myproject.local
&amp;lt;/VirtualHost&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now edit &lt;code&gt;/etc/hosts&lt;/code&gt; and add to the bottom:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;127.0.0.1    myproject.local
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now reload Apache, and you should be all set:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo apachectl graceful
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You're all set now! Hopefully this will make PHP development easier for you, as it has done for me.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Merging MKV files together</title>
		<link href="http://davidwinter.me/articles/2011/05/21/merging-mkv-files-together/"/>
		<updated>2011-05-21T00:00:00-07:00</updated>
		<id>http://davidwinter.me/articles/2011/05/21/merging-mkv-files-together</id>
		<content type="html">&lt;pre&gt;&lt;code&gt;mkvmerge -o merged_file.mkv file1.mkv + file2.mkv
&lt;/code&gt;&lt;/pre&gt;
</content>
	</entry>
	
	<entry>
		<title>Mod rewrite rules in htaccess files</title>
		<link href="http://davidwinter.me/articles/2011/02/08/mod-rewrite-rules-in-htaccess-files/"/>
		<updated>2011-02-08T00:00:00-08:00</updated>
		<id>http://davidwinter.me/articles/2011/02/08/mod-rewrite-rules-in-htaccess-files</id>
		<content type="html">&lt;p&gt;Rewrite rules in an htaccess file don't need to leading slash, otherwise they won't work.&lt;/p&gt;

&lt;p&gt;Be warned. Don't waste two hours wondering why it isn't working.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Authentication with CodeIgniter 2.0</title>
		<link href="http://davidwinter.me/articles/2011/01/29/authentication-with-codeigniter-2-0/"/>
		<updated>2011-01-29T00:00:00-08:00</updated>
		<id>http://davidwinter.me/articles/2011/01/29/authentication-with-codeigniter-2-0</id>
		<content type="html">&lt;p&gt;I've started using &lt;a href=&quot;http://codeigniter.com/&quot;&gt;CodeIgniter&lt;/a&gt; again recently, and have noticed that my &lt;a href=&quot;/articles/2009/02/21/authentication-with-codeigniter/&quot;&gt;how-to on implementing basic authentication&lt;/a&gt; could do with an upgrade for version 2.0.&lt;/p&gt;

&lt;p&gt;The process is identical. We subclass the base &lt;code&gt;CI_Controller&lt;/code&gt; class. This simply checks whether a logged in session is present. If not, it redirects the user to a sessions controller that can handle the logging in of a user. Once the user is logged in, the controller acts as normal.&lt;/p&gt;

&lt;p&gt;So, first up, lets create our basic subclass:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;?php

class MY_Controller extends CI_Controller 
{
    function __construct()
    {
        parent::__construct();

        $this-&amp;gt;load-&amp;gt;library('session');

        if (!$this-&amp;gt;session-&amp;gt;userdata('loggedin'))
        {
            redirect('sessions/login');
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, this needs to be saved to &lt;code&gt;application/core&lt;/code&gt;. This is important. You can't add it to &lt;code&gt;application/library&lt;/code&gt; like with the previous how-to. There have been some changes to CI that require the change. This new class is also auto-loaded automatically.&lt;/p&gt;

&lt;p&gt;Now, the sessions controller needs to extend &lt;code&gt;CI_Controller&lt;/code&gt;. Users don't need to be authenticated to access this controller.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;?php

class Sessions extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        $this-&amp;gt;load-&amp;gt;library('session');
    }

    function login()
    {
        $this-&amp;gt;load-&amp;gt;view('header');
        $this-&amp;gt;load-&amp;gt;view('login');
        $this-&amp;gt;load-&amp;gt;view('footer');
    }

    function authenticate()
    {
        $this-&amp;gt;load-&amp;gt;model('user', '', true);

        $user = $this-&amp;gt;input-&amp;gt;post('user');

        if ($this-&amp;gt;user-&amp;gt;authenticate($user['email'], $user['password']))
        {
            $this-&amp;gt;session-&amp;gt;set_userdata('loggedin', true);
        }

        redirect('/');
    }

    function logout()
    {
        $this-&amp;gt;session-&amp;gt;unset_userdata('loggedin');

        redirect('/');
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Your &lt;code&gt;login&lt;/code&gt; view will look something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;h2&amp;gt;Login&amp;lt;/h2&amp;gt;
&amp;lt;?php echo form_open('sessions/authenticate'); ?&amp;gt;
    &amp;lt;dl&amp;gt;
        &amp;lt;dt&amp;gt;&amp;lt;?php echo form_label('Email', 'user_email'); ?&amp;gt;&amp;lt;/dt&amp;gt;
        &amp;lt;dd&amp;gt;&amp;lt;?php echo form_input(array(
            'name' =&amp;gt; 'user[email]', 
            'id' =&amp;gt; 'user_email'
        )); ?&amp;gt;&amp;lt;/dd&amp;gt;

        &amp;lt;dt&amp;gt;&amp;lt;?php echo form_label('Password', 'user_password'); ?&amp;gt;&amp;lt;/dt&amp;gt;
        &amp;lt;dd&amp;gt;&amp;lt;?php echo form_password(array(
            'name' =&amp;gt; 'user[password]', 
            'id' =&amp;gt; 'user_password'
        )); ?&amp;gt;&amp;lt;/dd&amp;gt;
    &amp;lt;/dl&amp;gt;
    &amp;lt;ul&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;?php echo form_submit('submit', 'Login'); ?&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href=&quot;&amp;lt;?php echo site_url('/'); ?&amp;gt;&quot;&amp;gt;Cancel&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
&amp;lt;?php echo form_close(); ?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here's a basic &lt;code&gt;user&lt;/code&gt; model you could use. It includes hashing with extra salt security:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;?php

class User extends CI_Model
{
    public function authenticate($email, $password)
    {
        // get salt

        $salt = $this-&amp;gt;db-&amp;gt;select('salt')-&amp;gt;get_where('users', array('email' =&amp;gt; $email))-&amp;gt;row()-&amp;gt;salt;

        if ($salt)
        {
            // hash password with salt and find user

            $hash = sha1($salt.sha1($salt.$password));

            $user = $this-&amp;gt;db-&amp;gt;select('id')-&amp;gt;get_where('users', array(
                'email' =&amp;gt; $email,
                'hash' =&amp;gt; $hash
            ))-&amp;gt;row();

            return $user;
        }

        return false;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now all that's left is to change the parent classes of the controllers that we want protected from &lt;code&gt;CI_Controller&lt;/code&gt; to &lt;code&gt;MY_Controller&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;?php

class Admin extends MY_Controller
{
    ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Authentication is now in place.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>A simple chat room with WebSockets</title>
		<link href="http://davidwinter.me/articles/2010/11/21/a-simple-chat-room-with-websockets/"/>
		<updated>2010-11-21T00:00:00-08:00</updated>
		<id>http://davidwinter.me/articles/2010/11/21/a-simple-chat-room-with-websockets</id>
		<content type="html">&lt;p&gt;Sorry, I promised a blog post, but instead you get a link to bitbucket with the source of a basic chat app written using WebSockets.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://bitbucket.org/davidwinter/websockets-chat/src&quot;&gt;https://bitbucket.org/davidwinter/websockets-chat/src&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Using Capistrano to deploy Mercurial changes</title>
		<link href="http://davidwinter.me/articles/2010/08/22/using-capistrano-to-deploy-mercurial-changes/"/>
		<updated>2010-08-22T00:00:00-07:00</updated>
		<id>http://davidwinter.me/articles/2010/08/22/using-capistrano-to-deploy-mercurial-changes</id>
		<content type="html">&lt;p&gt;We have a few production servers at work, and we have a central &lt;a href=&quot;http://bitbucket.org&quot;&gt;bitbucket&lt;/a&gt; repository to store our core code. Once we make a change on our testing server (!), we used to have to commit, push changes to bitbucket, and then ssh into each server, then pull changes, and update each repository. A pain in the backside! Enter &lt;a href=&quot;http://www.capify.org&quot;&gt;Capistrano&lt;/a&gt;. A ruby ssh automation tool. In a few simple steps you can create a recipe file that will let you do this all with one command.&lt;/p&gt;

&lt;p&gt;On the machine where you'd like to kick this all off---where you'll be pushing the changes from---install Capistrano (requires &lt;a href=&quot;http://www.ruby-lang.org/en/&quot;&gt;Ruby&lt;/a&gt; and &lt;a href=&quot;http://rubygems.org/&quot;&gt;RubyGems&lt;/a&gt; to be installed):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo gem install capistrano
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Capistrano is opinionated software; meaning it makes a &lt;a href=&quot;http://www.capify.org/index.php/Getting_Started&quot;&gt;few assumptions&lt;/a&gt; about your server setup which you'll need to comply with. Mainly that either you have &lt;a href=&quot;/articles/2010/08/22/ssh-and-public-key-authentication/&quot;&gt;public key authentication&lt;/a&gt; setup to connect to these remote servers, or, you use the same password across all of them.&lt;/p&gt;

&lt;p&gt;Now, create a &lt;code&gt;capfile&lt;/code&gt;, perhaps in your home directory, and here is an example that you can modify for your own setup:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;role :production_servers, &quot;srv1.internet.com&quot;, &quot;srv2.internet.com&quot;

task :push_deploy, :roles =&amp;gt; :production_servers do
    system &quot;cd /local/path/to/repos &amp;amp;&amp;amp; hg push&quot;
    run &quot;cd /remote/path/to/repos &amp;amp;&amp;amp; hg pull &amp;amp;&amp;amp; hg update&quot;
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With that now saved, you can issue this task with the following command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cap push_deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will first of all run a local command to push the changes to bitbucket. After that is complete, it'll connect to each of the production servers listed, pull changes and then update the repository. And that's it. You don't need to do anything else. Piece of &lt;a href=&quot;http://fatcakehole.tumblr.com/&quot;&gt;cake&lt;/a&gt;.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>SSH and public key authentication</title>
		<link href="http://davidwinter.me/articles/2010/08/22/ssh-and-public-key-authentication/"/>
		<updated>2010-08-22T00:00:00-07:00</updated>
		<id>http://davidwinter.me/articles/2010/08/22/ssh-and-public-key-authentication</id>
		<content type="html">&lt;p&gt;Fed up with having to type your password in each time you log into a server over SSH? Me too. Down with passwords, and in with public key authentication!&lt;/p&gt;

&lt;p&gt;First thing to do, is to check that you have a SSH key setup already on your local computer:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd ~/.ssh
ls
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you see some files in there starting with &lt;code&gt;id_&lt;/code&gt;, like &lt;code&gt;id_rsa.pub&lt;/code&gt; or &lt;code&gt;id_tsa.pub&lt;/code&gt;, you're all set. If not, you'll need to generate these files:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ssh-keygen -t rsa
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;At the prompts you see, you can just hit enter to accept the defaults.&lt;/p&gt;

&lt;p&gt;With your public key now in place, we need to transfer that to the server we want to log into.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;scp ~/.ssh/id_rsa.pub username@remote.server.com:
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Don't forget the colon at the end. This has now transfered the public key over. Now you need to log into the remote server (with your password - last time, I promise):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ssh username@remote.server.com
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, check if there is a &lt;code&gt;.ssh&lt;/code&gt; directory on the remote server in your home directory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd ~/.ssh/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If there isn't:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir ~/.ssh
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now move the public key to a file named &lt;code&gt;authorized_keys&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mv ~/id_rsa.pub ~/.ssh/authorized_keys
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we need to set the correct permissions to ensure no one can tamper with these files:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And you're done. Now you can logout, and log back in again - and all going well (if your SSH server has been setup correctly - by default it usually is), you won't be prompted for a password.&lt;/p&gt;

&lt;p&gt;If it doesn't work, one problem I encountered was where the actual home directory permissions weren't set to &lt;code&gt;700&lt;/code&gt;. So try:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;chmod 700 /home/username
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All done.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Setting up SSH host shortnames</title>
		<link href="http://davidwinter.me/articles/2010/08/22/setting-up-ssh-host-shortnames/"/>
		<updated>2010-08-22T00:00:00-07:00</updated>
		<id>http://davidwinter.me/articles/2010/08/22/setting-up-ssh-host-shortnames</id>
		<content type="html">&lt;p&gt;Here's an example setup to create SSH host shortnames. On you local computer, add the following to &lt;code&gt;~/.ssh/config&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Host server1
HostName server1.internet.com
User david

Host server2
HostName server2.internet.com
User david

Host *
User davidwinter
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now with this file saved, you can ssh into &lt;code&gt;server2.internet.com&lt;/code&gt; with just the following command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ssh server2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will save you having to type out:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ssh david@server2.internet.com
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And if you have &lt;a href=&quot;/articles/2010/08/22/ssh-and-public-key-authentication/&quot;&gt;public key authentication&lt;/a&gt; setup, it makes the process even smoother. Adding the wildcard host record at the bottom allows you to specify a default username to use for other servers to the ones you've not specified above.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Using o2 PAYG mobile broadband</title>
		<link href="http://davidwinter.me/articles/2010/06/19/using-o2-payg-mobile-broadband/"/>
		<updated>2010-06-19T00:00:00-07:00</updated>
		<id>http://davidwinter.me/articles/2010/06/19/using-o2-payg-mobile-broadband</id>
		<content type="html">&lt;p&gt;Our internet connection via our landline has been dead since Tuesday afternoon, so I've needed an alternative connection in the meantime to give me my twitter fix!&lt;/p&gt;

&lt;p&gt;I'd kept an eye on the o2 pay-as-you-go mobile broadband for a while, because it offered the cheapest, noncommittal setup. A one off £20 for the USB dongle, and then as little as £2 for 500MB for a 24 hour period.&lt;/p&gt;

&lt;p&gt;So I plug in the dongle and we get a nice little installer to set everything up; installing drivers and the o2 Mobile Connect application. The annoying thing is that if you install going this route, you can only connect if you have this app open. You can't just use system preferences. Also, turns out this 'handy' installer program then blocks you from getting to the installation files again, and the dongle will refuse to do anything unless Mobile Connect is open (you'll get a red light on the dongle when it's being evil).&lt;/p&gt;

&lt;p&gt;So, what can you do?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Drag &lt;code&gt;Mobile Connect.app&lt;/code&gt; to the trash from your Applications directory.&lt;/li&gt;
&lt;li&gt;Then head into System &gt; Library &gt; Extensions and delete &lt;code&gt;ZTEUSBCDCACMData.kext&lt;/code&gt; and &lt;code&gt;ZTEUSBMassStorageFilter.kext&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Go into Network in System Preferences and remove the devices starting with ZTEUSB.&lt;/li&gt;
&lt;li&gt;Then restart your Mac.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;Now, let's install things a different route giving everyone more flexibility.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Plug in the dongle and when the CD Icon appears on the desktop, open it up, but rather than double clicking on the installer, right click on it and select 'Show Package Contents'.&lt;/li&gt;
&lt;li&gt;Now head to Contents then Resources. Now double click on the &lt;code&gt;drv.pkg&lt;/code&gt; file and install this only.&lt;/li&gt;
&lt;li&gt;Once complete, restart your Mac again.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;And now to configure the connection:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open System Preferences, and go to the Network pane.&lt;/li&gt;
&lt;li&gt;You should have three new items in there. Click on ZTEUSBModem.&lt;/li&gt;
&lt;li&gt;Set the telephone number to &lt;code&gt;*99#&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Username to &lt;code&gt;o2bb&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Password to &lt;code&gt;password&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Click on Advanced. Select Vendor; Generic, Model; GPRS (GSM/3G) and set the APN to &lt;code&gt;m-bb.o2.co.uk&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Click on OK and then Apply.&lt;/li&gt;
&lt;li&gt;Now you can click 'Connect' and all should work fine.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;That is of course, besides the crappy image compression and caching o2 force onto you! Need to find a solution for that next.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: Here's a solution for getting around the image compression, and Javascript injection; use a SSH SOCKS proxy. Works a treat.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open up &lt;code&gt;Terminal.app&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;ssh -ND 9999 username@yourserver.com&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Enter your password if/when prompted (you may have key authentication setup)&lt;/li&gt;
&lt;li&gt;That's the SOCKS proxy setup. Now go to System Preferences and open the network connection.&lt;/li&gt;
&lt;li&gt;Click on ZTEUSBModem.&lt;/li&gt;
&lt;li&gt;Then Advanced, Proxies&lt;/li&gt;
&lt;li&gt;Check the SOCKS Proxy and enter in &lt;code&gt;localhost&lt;/code&gt; and &lt;code&gt;9999&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Click OK and then Apply.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;All done. You should now get perfect images and no longer have Javascript inserted into all webpages. Both of which weren't clearly mentioned when purchasing my dongle.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Redirecting Apache traffic to a maintenance page</title>
		<link href="http://davidwinter.me/articles/2010/05/03/redirecting-apache-traffic-to-a-maintenance-page/"/>
		<updated>2010-05-03T00:00:00-07:00</updated>
		<id>http://davidwinter.me/articles/2010/05/03/redirecting-apache-traffic-to-a-maintenance-page</id>
		<content type="html">&lt;p&gt;Here's a simple solution to redirect users to a maintenance page in Apache. This rewrite rule can stay in your config (&lt;code&gt;&amp;lt;VirtualHost&amp;gt;&lt;/code&gt; or &lt;code&gt;.htaccess&lt;/code&gt;) all the time; all that you need to enable it is to create the file &lt;code&gt;maintenance.html&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The first rewrite condition checks to see if the file exists, and only if it does, will it redirect all traffic to it. The second rewrite condition is there to prevent an infinite loop, by  only redirecting traffic to files other than &lt;code&gt;maintenance.html&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{REQUEST_FILENAME} !/maintenance.html
RewriteRule ^.*$    /maintenance.html [L] 
&lt;/code&gt;&lt;/pre&gt;
</content>
	</entry>
	
</feed>
