Beginning cloud development with cloudControl - Part 2 - MySQL

In part one of the series we got a birds eye view of a great cloud development solution for PHP - cloudControl. We looked at the concept of what it is, what you can do with it and ran through a basic deployment with a rather basic application. If you missed the first part, I strongly encourage you to read it before continuing on with part two. When you’re done, come on back and work through it here.

In this, part two of the series, things start to get more serious. In this part, we’re going to start to flesh out the application started in part one, adding in MySQL support - showing just how simple cloudControl makes this for us.

In part one of the series we got a birds eye view of a great cloud development solution for PHP - cloudControl. We looked at the concept of what it is, what you can do with it and ran through a basic deployment with a rather basic application. If you missed the first part, I strongly encourage you to read it before continuing on with part two. When you’re done, come on back and work through it here.

In this, part two of the series, things start to get more serious. In this part, we’re going to start to flesh out the application started in part one, adding in MySQL support - showing just how simple cloudControl makes this for us.

The application will be a simple CRUD application that will allow us to mange a set of users. We’ll be able to do the following operations by the time we’re done:

  • Add
  • Edit
  • Delete
  • List (or view)

Now initially I thought I’d write a pretty plain vanilla application. But as the work progressed, it appeared that that approach wouldn’t work well across multiple environments - which is one of the key benefits of cloudControl. So given that yet in the spirit of keeping things as simple as possible and professional, I decided to use a set of third party libraries to make it easier. These include:

  • [Zend Framework (1.11)][1]
  • [HTML 5 Boilerplate][2]

There’s more code in there than I’d really planned to put in it, but it really helps to show you a good, if basic, application that will work across multiple environments. If you want, grab a copy of the code, available at https://github.com/maltblue/cloudcontrol-project and follow along in your local development environment.

Branches and Deployments

The application will have 4 branches: production, staging, testing and development. Production is a branch of the default, or master. For the 3 main branches there will be an accompanying cloudControl deployment.

Let’s create the application

Change to the project directory you created in part one and then run the following command:

[bash] # create (and checkout) a development branch from the master branch git checkout -b development [/bash]

Following this, you’ll need to do the following:

  1. Grab yourself a copy of the [HTML 5 Boilerplate][2] and [Zend Framework][1] libraries
  2. Extract the HTML 5 Boilerplate in to your project directory
  3. Create a directory, library, under the root of your project directory and extract the Zend Framework library in to it
  4. Under the root of the project create a new directory, config, and in there, create a file application.ini
  5. Also under the root of the project create a new directory, data/db, and in there, create a file called: dbload.sql
  6. In the css directory (created by the HTML 5 Boilerplate), create a new file main.css
  7. Create a file config.php in the root directory
  8. Create a new directory called includes and in there create two new files: template.footer.php and template.header.php

Adding MySQL Support

As we’re going to be using MySQL, then we’re going to be needing the cloudControl [MySQL add-on][3]. The MySQL add-on comes in a variety of flavours, from free, which we’ll be  using as well as in the following space options:

  • 512 MB
  • 20 GB
  • 250 GB
  • 1 TB

To add support for MySQL run the following command to add it to our development deployment:

[bash] cctrlapp maltbluedev/development addon.add mysql.free [/bash]

To work with it, we’re going to need the configuration options for it. To get these, run the following command:

[bash] cctrlapp maltbluedev/default addon mysql.free [/bash]

This will list all of the essential information as shown in the screenshot below:

[bash] [production] ; Database database.adapter = PDO_MYSQL database.params.host = 127.0.0.1 database.params.username = depe2yjxwjz database.params.password = 2gVSZRDv5HReX3E database.params.dbname = depe2yjxwjz [/bash]

The Database Data

Ok, we’re going to need some real data to get us started. In data/db/dbload.sql, add the following which will load a default table, complete with 10 records for us:

[sql] — database table DROP TABLE IF EXISTS staff;

— the staff table CREATE TABLE staff ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, firstName VARCHAR(100) NOT NULL, lastName VARCHAR(100), emailAddress VARCHAR(150) NOT NULL, occupation VARCHAR(50), CONSTRAINT UNIQUE INDEX uniq_email(emailAddress) ) ENGINE = InnoDB, COMMENT = “Store list of staff members”;

INSERT INTO staff (firstName, lastName, emailAddress, occupation) VALUES (“Muhammad”,“Ali”,“mali@gmail.com”,“Boxer”), (“Lou”,“Ambers”,“lambers@gmail.com”,“Boxer”), (“Vito”,“Antuofermo”,“vantuofermo@gmail.com”,“Boxer”), (“Jorge”,“Arce”,“j.arce@gmail.com”,“Boxer”), (“Alexis”,“Arguello”,“a.arguello@gmail.com”,“Boxer”), (“Henry”,“Armstrong”,“h.armstrong@gmail.com”,“Boxer”), (“Abe”,“Attell”,“a.attell@gmail.com”,“Boxer”), (“Monte”,“Attell”,“m.attell@gmail.com”,“Boxer”), (“Yuri”,“Arbachakov”,“y.arbachakov@gmail.com”,“Boxer”), (“Satoshi”,“Aragaki”,“s.aragaki@gmail.com”,“Boxer”); [/sql]

Loading the Data in to the cloudControl Database

This is where it gets a bit interesting. In the interests of security, cloudControl provide a secure connection to your database available via an stunnel server. So to access it you’re going to need a copy of the [stunnel client][4] and create an encrypted connection from your development environment to the cloudControl servers. Grab and install a copy of stunnel from the stunnel website.

In the case of our application, we’re going to create a secure tunnel to the cloudControl mysql servers on the standard port or 3307 via the local port 4711. To do this, we run the command below:

[bash] stunnel -c -d 4711 -r mysql.cloudcontrolled.com:3307 -P /tmp/stunnel4.pid [/bash]

Following this, we can use the MySQL clients as normal, but specifying port 4711 instead of the default 3307. Given that we’ve setup the tunnel, let’s import the data:

[bash] mysql -u depe2yjxwjz -p -h 127.0.0.1 -P 4711 depe2yjxwjz < data/db/dbload.sql [/bash]

Following this, you’ll be prompted for the password as normal, so enter it and the database table will now be created and loaded up with our ten records.

The Templates

Now we need to lay the ground work for the user interface. In includes/template.footer.php, add the following code:

[php] <!- JavaScript at the bottom for fast page loading -> <!- Grab Google CDN’s jQuery, with a protocol relative URL; fall back to local if offline ->

<!- scripts concatenated and minified via ant build script->

<!- end scripts-> <!- Prompt IE 6 users to install Chrome Frame. Remove this if you want to support IE 6. chromium.org/developers/how-tos/chrome-frame-getting-started -> <!-[if lt IE 7 ]>

<![endif]->

In includes/template.header.php, add the following code:

[php] <!doctype html> <!- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -> <!-[if lt IE 7]> <![endif]-> <!-[if IE 7]> <![endif]-> <!-[if IE 8]> <![endif]-> <!- Consider adding an manifest.appcache: h5bp.com/d/Offline -> <!-[if gt IE 8]><!-> <!-<![endif]->

<!- Mobile viewport optimized: j.mp/bplateviewport ->

<!- Place favicon.ico and apple-touch-icon.png in the root directory: mathiasbynens.be/notes/touch-icons ->

<!- CSS: implied media=all -> <!- CSS concatenated and minified via ant build script->

<!- More ideas for your here: h5bp.com/d/head-Tips ->

<!- All JavaScript at the bottom, except for Modernizr / Respond. Modernizr enables HTML5 elements & feature detects; Respond is a polyfill for min/max-width CSS3 Media Queries For optimal performance, use a custom Modernizr build: www.modernizr.com/download/ ->

The core configuration

Now we’re starting to build up nicely. In config.php, add the following code, which I’ll step through and explain.

[php]


You might also be interested in...


comments powered by Disqus

Books

Buy Mezzio Essentials. Learn the fundamentals that you need, to begin building applications with the Mezzio framework today! Buy Now

Latest YouTube Video

Learn how to write SQL queries in PhpStorm