Setting up Appfog with Drupal

What is AppFog?

Formerly PHPFog, Appfog is a platform as a service (PaaS). This essentially means that it's a hosting platform that frees you up from actually managing most of the hosting. All you have to worry about is deployment. It's all cloud based which means it's easy to scale your application. Even better news is that Drupal fits in well (with some adjustments).

How much does it cost?

Well, it's free! Kinda. Up until a few weeks ago AppFog provided users with 2GB of RAM across unlimited sites, with unlimited domains. Since then however they've restricted this free package to just using their domain names. So essentially the free package isn't suitable for production sites. However their first package is only $20 which allows you to have unlimited custom domains.

Sounds good, how do I get started?

First and foremost head over to http://appfog.com and register an account. 

Next, you can either create an app with their panel or use the command line. It's worth getting familiar with both as the panel won't allow you to do most of the important stuff but it is easy to get to (for example if you need to restart or resize your instance on the go).

For the purposes of this article we're going to be using the command line.

Installation using OSX.

Installation with OSX is simple. Just fire up a terminal and enter:

gem install af

This should install the appfog ruby gem for you straight away. If you have any issues try doing it as sudo.

Installation using Ubuntu

Installation for ubuntu can be a little bit trickier. First you need to make sure that you have ruby gems installed. Again, in the terminal:

sudo apt-get install ruby-full build-essential

sudo gem install af

Installation for Windows.

For those of you using Windows you can point and click over to the ruby gems installer at http://rubyinstaller.org/ then fire up a ruby enabled command prompt (sorry no escaping the command prompt with this one!) and enter

gem install af

Getting your Drupal site over to AppFog.

Most of you may be accustomed to using tools such as SSH or even FTP to put your files over to your production server. With AppFog that's not possible, which at first may seem frustrating but it helps you get structured with your deployments. The key to AppFog is similar to other PaaS where the empasis is on developing locally. I tend to use a version control system in conjunction to my development but for this case I'm going to ignore it in my workflow (You can do your normal git add, commit, push routine if you like!).

Go to the root of your Drupal installation and type in the following:

af push

Answer the questions on the screen. I would allow at least 512MB of RAM to start off with to ensure smooth running. It can be scaled back or increased later on. When it asks, create an additional service for mysql and give it a name that you can remember.

Would you like to deploy from the current directory? [Yn]: Y
Application Name: kcdemo
Detected a PHP Application, is this correct? [Yn]: Y
1: AWS US East - Virginia
2: AWS EU West - Ireland
3: AWS Asia SE - Singapore
4: Rackspace AZ 1 - Dallas
5: HP AZ 2 - Las Vegas
Select Infrastructure: 2
Application Deployed URL [kcdemo.eu01.aws.af.cm]:
Memory reservation (128M, 256M, 512M, 1G, 2G) [128M]: 512
How many instances? [1]: 1
Bind existing services to 'kcdemo'? [yN]: n
Create services to bind to 'kcdemo'? [yN]: y
1: mongodb
2: mysql
3: postgresql
4: rabbitmq
5: redis
What kind of service?: 2
Specify the name of the service [mysql-d50ac]: kcdemo
Create another? [yN]: n
Would you like to save this configuration? [yN]: y
Manifest written to manifest.yml.
Creating Application: OK
Creating Service [kcdemo]: OK
Binding Service [kcdemo]: OK

Navigate to your new site and....you'll see that we're not quite there yet!

We still need to:

  • Configure the database settings to be dynamic in settings.php
  • Configure the temporary directory settings

Database settings:

Appfog's documentation has a recommended configuration for the database settings in Drupal. One problem with it is that it won't allow you to develop locally with the same settings.php file. Big issue? Well, not really!

In your settings.php file locate where it defines the $databases array. It should look something like this:

$databases = array (
  'default' =>
  array (
    'default' =>
    array (
      'database' => 'db_name',
      'username' => 'username',
      'password' => 'password',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);

Don't delete those settings yet but replace that code with this:

$services = getenv('VCAP_SERVICES');
if ($services) {
  $services_json = json_decode($services,true);
  $mysql_config = $services_json["mysql-5.1"][0]["credentials"];
  $databases['default']['default'] = array(
      'driver' => 'mysql',
      'database' => $mysql_config["name"],
      'username' => $mysql_config["user"],
      'password' => $mysql_config["password"],
      'host' => $mysql_config["hostname"],
      'port' => $mysql_config["port"],
  );
}
else {
  $databases = array (
  'default' =>
  array (
    'default' =>
    array (
      'database' => 'db_name',
      'username' => 'username',
      'password' => 'password',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);
}

What this will do is first check if the environmental variable is present from AppFogs servers. If it can find it then it will use the database settings from that. Otherwise it will use the hardcoded version that is there. This will enable you to still use the same settings.php file for both local and production environments.

Next we need to import your database. Going into full details of how to do a mysql dump is a little out of scope for this article but for quick reference the syntax is:

mysql -u USERNAME -pPASSWORD db_name > dump.sql

Doing that on your local environment should give you a nice fresh backup of your database. Now we need to import it into appfog. This is pretty straightforward although at first it can seem a little awkward.

Remember when I said have a nice simple name for the mysql service you set up for the AppFog application? Well now you're going to use it. Enter this command:

af tunnel service_name

Where service_name is the name of your mysql service. It is not your database name! This should create an error telling you to install something called caldecott. That's easy to resolve:

gem install caldecott 

or 

sudo gem install caldecott

Run af tunnell again and select the service you'd like to tunnell into. It'll take a while to get caldecott working and then it'll ask you what you want to do with the service. Select 1 and press enter. (I've noticed that there seems to be a bug in the cli with this and it doesn't echo to the screen what you type. Just press the number once and then press enter and it should work.)

It will then give you credentials which look like this:

Service connection info:
  username : uf3po6KB2ndnz
  password : ps5Ir56aJOPC4
  name     : d927cbc4c6b254e3a8b85ab861b2f93c8
  infra    : eu-aws

Keep that window open and open a new command line window or tab. Navigate to where you put the mysql dump and enter the following command (substituting with the credentials that appfog gave you):

mysql -h 127.0.0.1 -p 10000 -u USERNAME -pPASSWORD db_name < dump.sql

That should then take a little while and it will import our Drupal database. Nearly done!

Configuring the temporary files location.

Next we need to make sure that the temporary files location isn't set in the database. Because we will be using cloud based hosting the actual location of the writable temporary. Just add the following line to your settings.php

$conf['file_temporary_path'] = realpath($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmp');

Now just run:

af update

And your Drupal site should be up and running! 

Persistant storage

It's worth noting that at the moment Appfog does not offer persistant storage. This can seem as a huge limitation but really it's just a minor annoyance in reality. Just use the amazon s3 module and take advantage of Amazons free 1 year trial.

Add new comment