Brian Sewell

Creating a Codeigniter App on AppFog

December 18, 2012

AppFog is a PaaS (platform as a service) provider. They created PHP Fog which will be discontinued on December 21, 2012. AppFog is a great solution for those who are uncomfortable with managing their own VPS and all that it entails. Many developers start off using low-cost shared hosting solutions that require no dev ops knowledge or server admin chops.

There does come a time when most developers grow out of these services and need to explore a VPS or dedicated server. These are usually more expensive depending on the RAM you get. Wouldn’t you love it if you had the simplicity of shared hosting, but also the benifits of a VPS and still retain most of the customizations you’d have with a VPS? That’s where these PaaS providers step in. I have yet to look into some of AppFog’s competitors like Engine Yard and pagoda box, but I’m sure they too offer similar pricing and solid up-time.

AppFog is my choice for now simply because I used their PHP Fog service for a bit and their roadmap for upcoming features looks pretty enticing.

PHP Fog offered a quick start for Codeigniter applications, however AppFog is currently not offering a clean install of Codeigniter, so you’ll have to start with an empty PHP app and upload Codeigniter yourself.

1. Create a PHP App

Create an AppFog account.

Once logged in you will be prompted to create your first app. Select the PHP application.

The next step is to select your prefferred infrastructure. I will be using AWS US East for purposes of this tutorial.

Give your app a name and click Create App.

Once your app is built, you will be redirected to your app’s dashboard:

2. Install AppFog’s Command Line Tool

AppFog offers a handy command line tool to create, update and manage your hosted apps, as well as tunneling into services they offer like MySQL, Redis, MongoDB, PostgreSQL and RabbitMQ. PHP Fog, the first implementation of AppFog had used git and git deployments originally to manage apps, however they recognized that not every developer uses git for source control.

The installation instructions for the AppFog command line tool is pretty straight-forward. All you have to do is install the af gem. This gem does require that you have Ruby 1.8.7 or newer installed on your machine.

$ gem install af

3. Create a local version of your app

$ mkdir ci-test-app
$ cd ci-test-app

Download a fresh Codeigniter install into the folder containing your app.

4. Upload your Codeiginiter install to AppFog

Now that you have a clean Codeigniter install on your machine, it’s time to upload it to your AppFog app using the af tool.

Run the following command inside your app folder on your local machine:

$ af update ci-test-app

Where ci-test-app is the name of your app.

You should receive feedback that looks like this:

$ af update ci-test-app
$ Uploading Application:
$   Checking for available resources: OK
$   Processing resources: OK
$   Packing application: OK
$   Uploading (6K): OK
$ Push Status: OK
$ Stopping Application 'ci-test-app': OK
$ Staging Application 'ci-test-app': OK
$ Starting Application 'ci-test-app': OK

If everything goes well, you should have successfully pushed a Codeigniter install to your AppFog app.

You can check to see if everyone uploaded correctly by clicking on Visit Live Site on your app’s dashboard.

Hopefully you’ll see this…

5. Provision your MySQL database

I’m going to use MySQL for this tutorial… if you prefer Mongo, PostreSQL or Redis, the following steps will be relatively similar.

On your app’s dashboard, click on the Services tab on the side. From here, you can create your MySQL database.

Select MySQL from the services list and give it a name. I named mine “ci-test-app-mysql”.

Click Create and wait for your db to bind and provision.

6. Connect Codeigniter to your MySQL database

Now that your MySQL database has been created, you need to add the proper credentials to your database config file in your CI install.

Open your application/config/database.php file and locate this section at the bottom:

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

You will need to add the following code above this block

$services_json = json_decode(getenv("VCAP_SERVICES"),true);
$mysql_config = $services_json["mysql-5.1"][0]["credentials"];

Then replaces the first four lines with these:

// $db['default']['hostname'] = 'localhost';
// $db['default']['username'] = '';
// $db['default']['password'] = '';
// $db['default']['database'] = '';
$db['default']['hostname'] = $mysql_config['hostname'];
$db['default']['username'] = $mysql_config['user'];
$db['default']['password'] = $mysql_config['password'];
$db['default']['database'] = $mysql_config['name'];
$db['default']['port']     = $mysql_config['port'];

The reason you need to do this is because AppFog stores all credentials for your bound services inside an environment variable called VCAP_SERVICES. This data is formatted as a JSON document. You can read more about it here.

Ok, so now all you have to do is autoload the database library in Codeigniter so that your CI install will attempt to connect to your database.

Edit the line in application/config/autoload.php as follows:

$autoload['libraries'] = array('database');

Go back to your app dashboard and visit your live site. If you still see the same default CI page, then you’ve successfully connected your MySQL database!

7. Setup phpMyAdmin

AppFog treats phpMyAdmin as it’s own app, similar to how it treats your Codeigniter Install. Aside from tunnelling into your MySQL database, phpMyAdmin will be your best way to interface with your database for now.

Goto your dashboard and click on Create App in the top navigation

Select phpMyAdmin from the application list

Same as you did for your PHP app, select your preferred infrastructure… once again I’m using AWS US East. Then name your phpMyAdmin install… I just named mine “ci-test-app-mysql”.

Once your app is created, from your dashboard click on Services on the side. You will see your MySQL database you created earlier. Click the Bind button to bind this service to your phpMyAdmin installation.

You’ll also need to add a password to your phpMyAdmin app. Click on the Env Variables tab on the left. Create an environment variable called PMA_PASSWORD and set it to whatever password you want.

You can set the value to whatever you want as your phpMyAdmin password. Your default phpMyAdmin username is the email address your AppFog account is under, but you can optionally override that to something of your choice by creating another environment variable called PMA_USERNAME.

As an example I’ve set both my username and password to “root”. I do not recommend using these values at all by the way.

So now you will be able to login to your phpMyAdmin installation to manage your database by clicking on the Visit Live Site link on your phpMyAdmin app dashboard.

You’re all set!

Enjoy using AppFog to host your Codeigniter app.

comments powered by Disqus