Learn how to build, test, deploy and run your PHP applications on the OpenShift cloud platform.
OpenShift is Red Hat's Cloud Computing Platform as a Service (PaaS) offering. For PHP cloud development, OpenShift offers an application platform in the cloud where you can build, test, deploy, and run your applications. Also, OpenShift enables you to use a great variety of the latest and greatest technology Quickstarts that allow you to boot your favorite platform on OpenShift almost instantaneously.
All you need to do to begin PHP cloud development on OpenShift is take three simple steps: Sign up and create an account on OpenShift, find the QuickStart for your favorite platform and follow the instructions on the Quickstart page (usually in the README.md file), and host that platform on OpenShift. Your favorite platform is available on OpenShift, and you can start on your project!
What you can do on cloud?
- Host and run your own blog on your own server using Wordpress Quickstart.
- Set up your own e-commerce site for your burgeoning business using Magento e-commerce Quickstart.
- Developing your own web application using web.py framework Quickstart.
Note: All the PHP application types are available on the official Openshift get started page.
Note: OpenShift Quickstarts can be found on GitHub under the 'OpenShift' account.
Create an OpenShift Account
Creating an OpenShift account is a very simple task: just follow the new account link and sign up with your email address and a password. After signing into your OpenShift account, you should see something like in the below figure:
Figure 1 - OpenShift account management console
After creating the OpenShift account, you will receive a confirmation email, where you will find the next five steps. I list them here, just to point out how easy you can create a cloud application:
1. Go to the OpenShift Web Management Console
2. Choose an Instant Application like Wordpress or an Application Type like JBoss
3. Name your Application and Namespace
4. Click on your URL
5. Congratulations, you're in the cloud!
In the management Console of your OpenShift account, you need to set up the namespace under which your applications will be grouped under. You also have the option to set the namespace later, when creating applications, but I prefer to do this at this point. The namespace set for my incoming application is OctaviaExamples. As you can read in the next figure, the namespace is unique and it is contained in the public URL as a suffix: OctaviaExamples.rhcloud.com.
Figure 2 - Setting up a namespace for your group of applications
Install OpenShift RHC Client Tool
"The OpenShift Client tool, known as rhc, is built and packaged using the Ruby programming language. OpenShift integrates with the Git version control system to provide powerful, decentralized version control for your application source code."
1) To install OpenShift Client tools you need to download Git for Windows. Download it and then run the installer. After the installation is completed, to verify that Git is correctly configured, go in the corresponding folder, where this application was installed and run the Git Bash executable, and run the $ git -- version command. If everything worked fine you should see something like the below figure:
Figure 3 - Verifying installation for Git
2) To install OpenShift Client tools you also need to download RubyInstaller 2.0.0. Download it and then run the installer.
Note: While running the setup you need to select the "Add Ruby executables to your PATH" check box so you can run Ruby from the command line. After the installation is completed, to verify that Ruby is correctly configured, go in the corresponding folder, where this application was installed and run the ruby -e 'puts "welcome"' command. If everything worked fine you should see something like figure below:
Figure 4 - Verifying installation for Ruby
3) After the Ruby and Git are successfully installed, use the RubyGems package manager (included in Ruby) to install the OpenShift client tools. Run the gem install rhc command and you should get something like the figure below:
Figure 5 - Install RubyGems package
Using your OpenShift login and password, execute the $rhc setup command. This command sets up our SSH key, so that you can securely communicate with Git, and creates a namespace for all your applications to be grouped under.
After running the rhc setup command, you should get something like in the next figure:
Figure 6 - Run rhc setup command for uploading SSH keys and setting the namespace for the application
You may notice, from the above figure, that my namespace for the incoming application is OctaviaExamples and, as I said in the section "Create an OpenShift account", was set after I created my OpenShift account, from OPENSHIFT MANAGEMENT CONSOLE, My account tab.
Figure 7 - My OpenShift Account containing namespace and SSH keys, after running the rhc setup command
Create a PHP 5.3 Cloud Application from Command Line
To create a PHP cloud application from the command line, you only need to use the $ rhc app create $appName php-5.3 command. My application name is firstExample, as you can see from the next figure:
Figure 8 - Creating first cloud application from command line
Going into the cloud account, application firstExample should appear now, in the My Applications tab:
Figure 9 - My Applications tab from the OpenShift Account containing the first application created above
Testing the application on cloud, you should get something like the next figure:
Figure 10 - Testing the firstExample application in the cloud
Next, is a list containing the most important commands you can use to control your application:
1. Create - Create an application and add it to a domain
2. git-clone - Clone and configure an application's repository locally
3. delete - Delete an application from the server
4. start - Start the application
5. stop - Stop the application
For example, if we want to delete the firstExample application we will use the following command:
$ rhc app delete firstExample
Of course, you can delete an application from your management console, in your OpenShift account.
This is the fastest and easiest method to create a PHP 5.3 cloud application. So, to accomplish this task all you need to do is:
1. open your OpenShift account
2. go on Create Application tab
3. select PHP from the list (figure 11):
Figure 11 - Choosing PHP from the application types
4. After you select PHP from the above list you should choose a web programming cartridge you want to use next. You may notice from the below figure that Zend Server 5.6, CakePHP, Drupal 7, WordPress 3.x and PHP 5.3 are the application types available for creating a PHP cloud application. You also have the opportunity to enable additional capabilities like databases, cron, metrics, and continuous build support after you create the application. (In the next section of this article we will add a database to this second application)
Figure 12 - The available PHP cartridges for the cloud applications
We will choose to create a simple PHP 5.3 application; therefore we will choose PHP 5.3 application type. All we have to do next is to set a name for our application; we will name it secondExample, and press the Create Application button. That's it! In My Applications tab you should see now the new created application.
Clone the PHP Cloud Application into a Local Repository
To clone the cloud application, secondApplication, we will use the below command, which creates a local copy of the source code repository for you.
$ rhc git-clone secondExample
Figure 13 - Cloning the secondExample cloud application and seeing its structure on our local repository
First, we need to check the available cartridges and for that, we need to use the below command, from command line:
$ rhc cartridge list
Figure 14 - Listing all the available cartridges
You may notice this is a complete list of available cartridges, not only for PHP.
As I said in the previous section, we can add specific features to our application; in our case we will add a database. For this, we need first to add the mysql-5.1 cartridge. There are two methods to add mysql-5.1 cartridge:
1. from command line, using the below command:
$ rhc cartridge add mysql-5.1
Figure 15 - Adding the mysql-5.1 cartridge to application secondExample in command line
2. directly on cloud, by pressing the Add Cartridge button:
Figure 16 - Adding the mysql-5.1 cartridge to application secondExample on cloud
We will get a list with the available cartridges for our PHP application, secondExample:
Figure 17 - Available PHP cartridges
Select MySQL database 5.1 and then press the Add cartridge button. You will receive a user and a password and you should make a note of them or make a print screen figure containing them, because you will need them later when you populate your database:
Figure 18 - Confirmation message that MySQL cartridge was successfully added to your application
Now, that we have added the mysql-5.1 cartridge, we can add the phpMyAdmin 3.4 cartridge, which is a web based MySQL admin tool and for that requires that MySQL cartridge should be installed first. So, select the phpMyAdmin 3.4 cartridge and press the Add Cartridge button. This is the message you will get after adding phpMyAdmin cartridge:
Figure 19 - Confirmation message that phpMyAdmin cartridge was successfully added to your application
Notice that you will also receive a URL and we will use that URL to connect to our database, through the phpMyAdmin MySQL admin tool. Opening this URL into a new browser page, you will get an Authentication Required window, where you should introduce the user and password received previously (when I said that you should note them, in order to remember):
Figure 20 - Connecting to phpMyAdmin
Figure 21 - Entering into phpMyAdmin account
Notice that a database with the name of the cloud application was created simultaneously, secondExample.
These are the main steps in creating a cloud application, which is based on databases; from now on you can expand the cloud application as you like, by adding any cartridges you need, populating the database and so on.
In the last part of this article, we will create a Wordpress quick start example. The Wordpress quick start repository for OpenShift Express is on github. And, to create the cloud application you should run the command from figure 22, using your OpenShift account:
Figure 22 - Creating an Wordpress example using our OpenShift account
That is it! Checking now your OpenShift account, on the My Application tab you should see, beside our two previous examples, also this new Wordpress example:
Figure 23 - My Application tab from the OpenShift account
Now, testing it the first time in a new browser page, you will be asked to provide some information, in order to install Wordpress, as you can see from figure 24:
Figure 24 - Install Wordpress, in order to host and run your own blog on your own server
After the installation process you will be redirected to your Wordpress Dashboard, where you can manage your site however you like:
Figure 25 - The Management Console of our Wordpress quiqstart example
You have learned how to create an OpenShift account in order to create a PHP cloud application, how to create a simple PHP application from the command line and from the web cloud account, how to add cartridges to your application and how to create a Wordpress quickstart example. Further, you can continue exploring and practicing the amazing capabilities of PHP cloud development on your own.