Elevate Your PHP to the Cloud

This article will walk you through cloud hosting for your PHP application with Pagoda Box. It'll detail how to handle PHP dependencies, get your MySQL databases up and running (and debug locally), and customise various aspects of PHP with ease.


Introduction

As a developer who has been playing around with PHP since the age of 11, it's hard to remember many things that have absolutely changed the way I develop. In 2007, I picked up CodeIgniter, which made me rethink how I structure my code and use OOP to get things done. In 2010, I began working with Git and Git Flow for teamwork and branching, but, recently, the biggest thing to change my development workflow has been one "Platform as a Service," called Pagoda Box.

You might be a rockstar developer who knows how to set up a LAMP stack, but how much of your potential development time do you waste setting it up, optimizing it, securing things, locking it down, scaling resources and monitoring traffic.

Every day, I spot a developer battling to defend his server from some group of hackers or script kiddies, getting port flooded, having trouble getting some random security patch working, struggling to migrate to larger more powerful servers, getting mad trying to set up Capistrano, the list goes on. Services like Pagoda Box can help you skip all of that; so let's see how.


Step 1 - Register With Pagoda Box

The first step is to grab yourself an account. It's free, so go ahead.

Register

Step 2 - Create Your Application

Each application needs to have a unique name, as they are also used for preview URLs. You can call this application whatever you wish, as you will be hiding it behind a domain shortly, but call it something obvious and memorable.

SSH Key form

Step 3 - Set Up Git

Git is an extremely useful version management system. Right now, it is massively popular thanks to sites like GitHub who allow for free social code hosting, but it is also great for deploying your code.

If you are new to Git then try the crash course.

If you haven't already, set up Git. Their documentation will help you get started. Windows users will find it a little more difficult than OSX or Linux users, so if you have a Virtual Machine of Ubuntu lying around, now would be a great time to fire it up.


Step 4 - Create Your SSH Key

To push your code to Pagoda Box you need to authorize your computer to have push access to applications on your account. Create an SSH key and paste it here.

SSH Key form

Step 5 - Deploy Your Code

First, set up a folder for your application and set the Git remotes. Remotes are essentially nicknames for a repository URL. By setting up this remote, we're letting our local Git repo know how to push code to Pagoda Box.

Now with all of this account and environment setup done, we can start deploying code. For now, we'll deploy a simple Hello World app:

This is a little command line trick to put some text into a file. You could alternatively create this file with your favorite text editor or IDE.

So far, we have made a new folder for our website, initialized Git, added a remote which tells Git where the Pagoda Box app lives, and added a new index.php file, which will simply say Hello World!. Next:

This is where the magic happens. You will push to a Git remote like any other repository, but then you will see the output of Pagoda Box taking over and creating a deployment. This will fire up a new instance each time and switch over from the live instance to this new instance instantaneously, meaning immediate updates - unlike slow FTP-based file deployment systems. Also unlike some similar services, all of your temporary files (cache, logs, image uploads, etc) will be there after any resize or re-deployment. Awesome!

Now if you refresh the Pagoda Box page, you will see a Dashboard with all sorts of options.


Step 6 - Creating a Boxfile

Pagoda Box has a special config file: Boxfile. This goes in the root of your application. An advanced example may look something like this:

This example gives you the chance to create writable folders, set your index_list (which is index.php by default) and change all sorts of PHP settings that would normally be in a php.ini file.

The most important line here is:

Pagoda Box allows you to list your applications dependencies. The "mysqli" driver is "MySQL Improved" which you should start using, as "mysql" is going to be deprecated in PHP 5.4. "curl" and "gd" are fairly standard and "mbstring" helps you work with UTF-8 code.

It's good to know they are available - along with plenty of others - but for this tutorial we'll only need the following:

So let's make our new Boxfile and save it:

We'll be using this later on.


Step 7 - Create a Database

At the time of this writing, Pagoda Box only supports MySQL databases, which is fine as that is what the majority of developers use.

We can fire up as many small database instances as we like for free; we only start paying when they get bigger, so you won't get surprised by a massive bill just for building a website. Just like the web instances, they can also be scaled to grow with your website.

New Database

To make a new one, click the + icon and give it a name. You'll see a progress bar up top while it builds; then the page will refresh.


Step 8 - Connecting From Your App

Connecting via PHP is a cinch. You are given multiple $_SERVER variables, which contain credentials for each database you have. This means you can keep hard-coded passwords out of your application - which is very useful if you don't want all of your developers to see live passwords, or if you have your complete website on GitHub, like some people are beginning to do.

This is a basic example of the variables in use - connecting to your MySQL database via the PDO extension. If you're using a framework, such as CodeIgniter, you can shove those variables into your database.php config files.


Step 9 - Connecting Remotely

Some web servers - especially those run-of-the-mill cPanel set-ups - have phpMyAdmin installed and can be configured to allow remote access to the MySQL databases.

Instead of this approach, Pagoda Box uses Ruby Gem for - amongst other things - creating a temporary secure SSH tunnel. This means your MySQL databases are locked down behind a firewall and are only accessible via this secure SSH connection.

So, to install the gem run:

Then to create the tunnel, simply run:

The first time you run this (or any other pagoda gem command) you should be asked for a username and password. This is your Pagoda Box username and password, and is needed so that the gem can act on your behalf and is nothing to do with your database.

Go to your dashboard and click on the database, then click "Show Credentials" to see a info window like this:

Database Credentials

Use these credentials to connect. Don't forget to specify which port; it is listed in the output from the gem above - not the green box. Remember, you are connecting to the local tunnel, not directly to the database.


Step 10 - Making a Blog

What sort of tutorial would this be if it didn't involve making a blog in 20 minutes?

For all development you should really be building things locally using MAMP, XAMPP, WAMP, etc., then deploying code and database changes using some sort of "Migrations" or schema change tracking stuff, but we'll do this the old fashioned way.

First connect to your database remotely via the tunnel, then run this query to build a new table:

With some test posts in place we can create this index.php:

With that file saved, and our new Boxfile (from step 6) in place, we can deploy these new changes:

Go to "http://my-awesome-app.pagodabox.com" and see the output:

Database Credentials

This is clearly not something that you'll want running on your website, but it does enough. You can see that your database content is being output to the browser, and your demo URL is working. You can replace this with your CMS of choice, or build something custom.


Step 11 - Use a Real Domain

There is no point having your wonderful new blog on a pagodabox.com subdomain; so let's park a domain on top of it.

First, in the DNS/SSL tab of your dashboard, add in some entries for your domain:

Database Credentials

Now that your application is ready to accept a domain you should head over to whoever handles the DNS for your domain. Go to the DNS management area and set an A record for "myawesomedomain.com" to be "50.97.141.37" - which is the IP address listed on the interface for your app. These IP addresses are not unique per application, but will not always be the same. Set a CNAME record for "www.myawesomedomain.com" to alias "myawesomedomain.com" and you should be all set.


Summary

This may all seem rather different from how you work at the moment, but start to think about how much work you are saving yourself in the long run. Using PaaS is the same reason we use jQuery over native JavaScript, or PHP frameworks over writing native PHP. Why mess around with minor details when you can instead focus on building an awesome app?

Other alternatives you might consider are PHPFog and Orchestr.io, which both offer similar services. Have fun, and thanks for reading!

Tags:

Comments

Related Articles