Get up and running with OpenShift
OpenShift is a Platform as a Service (PaaS) system to go with the increasing number of such systems. There is something about OpenShift though, that many of the start up based PaaS systems don’t have. OpenShift has been created by well known open source/Linux company Red Hat. Red Hat is synonymous with creating stable, robust systems for the enterprise, so it is no surprise that they have joined the ranks of PaaS providers.
In this article, I am going to show you how to get set up with OpenShift, and deploy an existing simple PHP web application to it, and continue developing the application on the OpenShift cloud.
1. Get an account
It literally takes seconds to sign up for a free account so do that first:
2. Make sure your system meets the requirements
There are some operating specific requirements, but the following list covers the general requirements:
- Git should be installed
- Ruby (at least version 1.8.7) should be installed
Linux requirements
for most Linux distros you will need root access, along with the development package for the version of Ruby that you have installed. You need Git, and rubygems too.
Windows requirements
You will need to be running Cygwin (a Linux-like environment for Windows) with the following optional components:
- openssh
- ruby
- make
- gcc
- git
Mac requirements
For Mac (Snow Leopard and Lion), you need Git or the full XCode suite.
3. Set up Help
It’s worth noting that once you have confirmed your account and logged in, there are a number of “getting started” videos to help you set up your system.
4. Installation of the client tools
Regardless of your operating system, you will need to install OpenShift client tools Ruby gems. Jump into the command line/console and do:
gem install rhc
If you are on a Mac you will also need to install the json_pure gem.
You might need to use “sudo” to install the gems if you are on Linux/Windows with Cygwin.
5. Create a namespace
You will offered a link to go through to the control panel so that you can create a namespace:
You will also need to add your public key. If you are not sure how to generate your own public/private key pair, just drop onto the terminal and do:
ssh-keygen
Accepting the default choices is fine.
6. Create an application
Now we can create an application. OpenShift supports Java, Perl, Python, Ruby, or PHP applications. We are going to create a PHP application so go back to terminal and do:
rhc app create -a shouter -t php-5.3 -l your user name
So here, we are creating a new app called “shouter” with the type set to PHP 5.3. You then also add your user name to login. You will then be prompted to enter your password.
After a short while you should get a confirmation message that your app has been created. You will also be given a url where you can see your new application in your web browser. The other vital piece of information provided will be the Git clone address.
Note: you will be asked to add the web app url to your list of known hosts. You should answer yes.
7. Clone the app
Open a new terminal window (I like to keep one open for issuing commands on the app), cd into a projects directory and then do:
git clone the-clone-url-you-were-provided-with shouter
A note about the app
The app I am referring to here is called “shouter”. It is a CodeIgniter based PHP app that provides a simple browser-based “shout” or message system. You can use your own application obviously, just substitute “shouter” with the name of your app.
Once the clone completes, you will be presented with a directory structure that looks like this:
It is the PHP folder that we are most interested in, since that is where our main application files will reside.
8. Just making sure things work…
Now we can make a simple change, commit and push it, and then check that we can see the change in our browser. Add a small section of text to index.php (anything you like) and then commit it:
git commit -am 'just a test commit'
Then we can push back to the OpenShift cloud:
git push origin master
Refresh your browser, and you should see your change appear:
9. Install some cartridges
OpenShift uses the concept of cartridges. They can be thought of as software packages that can be installed to support your application. So for a PHP app, it is quite common to have MySQL and phpMyAdmin available. We can add them for our app with a simple command from the terminal:
rhc app cartridge add -a your-app -c mysql-5.1 -l your-user-name
And phpMyAdmin:
rhc app cartridge add -a your-app -c phpmyadmin-3.4 -l your-user-name
Now you have a MySQL running for your app, and it can be managed via phpMyAdmin. Do take careful note of the log in credentials you are provided with. You’ll need them!
10. Develop your app
So if you have an existing app that you would like to run on OpenShift, it is simple to move it over. One caveat though, OpenShift is, according to Red Hat, a developer preview. That means it probably isn’t suitable for production code. Having said that, I have been using it for the past week, and experienced no problems at all.
If you have your project under Git source management already you will most likely need to scrap the Git files, and just copy your app into the local clone of your OpenShift project. Transferring history from one repo to another is a tricky business, and usually only works where you have one folder.
I would be inclined to keep my old repo as a point of reference, and copy the code into the OpenShift repo.
Work cycle
So now you can develop away, adding new features for your app. You could commit and push to OpenShift each time you update something, or you could use your local AMP(MAMP/WAMP, local Apache) setup to test changes locally before pushing them to OpenShift. That means that you are working in the same way that you might if you were using Github for example, to remotely manage your source code. The only difference here is, you are actually deploying your code when you push to the remote.
Config Files
I mentioned earlier that my app was built on top of the CodeIgniter framework. One of the advantages of using a framework is that you can easily manipulate the config files to work either on a local set up, or the OpenShift deployment. For example, with CodeIgniter, in application/config/config.php you can do something like:
if($_SERVER['SERVER_NAME'] == "shouter.dev")
{
//local environment
$config['base_url'] = 'http://shouter.dev/';
}
else
{
//OpenShift
$config['base_url'] = 'http://your-url.rhcloud.com/';
}
You can do the same for the database config.
.htaccess
You can create custom .htaccess files for your app, they work as expected.
Namespaces
OpenShift uses the concept of namespaces to act as a domain. One namespace can have multiple apps within it; the name you give to the app will form part of the url that you access your app with. If you wanted more namespaces, you would have to create a new account for each.
Domain names
If you have a domain name that you would like to point to your OpenShift app you can. Back on the terminal do:
rhc-ctl-app -a appname -c add-alias --alias www.mydomain.com
Then in your DNS management for mydomain.com you can add a cname to point www.mydomain.com to app-namespace.rhcloud.com.
Finally…
There are plenty of PaaS options out there of course. Considering OpenShift is a developer preview, it already feels very stable. Red Hat are also working on a browser-based management console to make it even easier to set up new apps, manage your namespace, and access your account details. This article has given you an overview of how to get set up and begin/continue development of your app. Do keep in mind, that Ruby, Python, and Java apps are supported to.