Getting Started with Zend and OpenShift

If you are a PHP developer, you have probably used Zend Server and the Zend Framework. Both are very powerful weapons to have your PHP arsenal to create clean and reusable code that scales well. At OpenShift, we believe strongly in the merits of both the PHP language and the quality of applications that PHP developers create. Partnering with Zend to provide both the Zend Server and Zend Framework on the OpenShift platform is yet another display of our commitment to making OpenShift a great cloud platform for PHP developers.

There are multiple ways that you can use OpenShift with the Zend platform. The recommended scenario is to use Zend Studio to create and manage your applications all from inside of the IDE. Another option is to use the OpenShift command line tools to create and manage your application. During this blog post, I will give instructions on how to get up and running using each of the scenarios listed.

Option 1: Using Zend Studio

If you live and breathe inside of the Zend Studio IDE, this is probably the option that you will want to select.

Pros

  • Do all of your work inside of the IDE without having to use the command line

  • Create OpenShift account, domain, and application using the built in wizard

  • Full GUI for performing all of your deployment tasks

  • Use of SFTP to automatically push your changes to your OpenShift server upon file save. This allows for immediate deployment of your source code as you are developing.

Cons

  • Loss of some control over your application. For instance, you will need to have the command line tools to view logs, ssh to your machine, backup or application, etc.

  • Studio will default to using SFTP which will not allow you to use the private GIT repository that OpenShift creates on your server by default. This may cause troubles for you if you want to revert to a previous version of your code.

Let's get started with using OpenShift and Zend Studio. I assume that you have the latest version of Zend Studio already installed that includes support for the OpenShift PaaS.

Step 1: Create New OpenShift Project

Once you have Zend Studio up and running, select to create a new project and then select PHP Project from OpenShift. Click on the next button to continue.

Step 2: Create your OpenShift Target

On the next dialog that is presented, you should see a add target link. Select this so that we can define our target for our deployed application.

At this point, you will be presented with a dialog that will allow you to login to OpenShift using an existing account. If you don't already have an account, you can select to create a new account from this screen.

Once you have a created an OpenShift account, enter in your username and password in the provided text fields. You should also generate a new SSH key if you don't already have one on your machine.

In order to deploy an application to OpenShift, you will need to create a namespace, often referred to as a domain, on the OpenShift cloud. Click on Create New OpenShift Target to create your namespace.

Understanding Namespaces: A namespace (used interchangeably with Domain in the OpenShift documentation) is the subdomain of rhcloud.com for all of your Application URLs.

For example:

namespace: mycoolnamespace 
Subdomain: mycoolnamespace.rhcloud.com 
App URL: {appName}-mycoolnamespace.rhcloud.com

Once you click the next button, your namespace will be created on the OpenShift cloud.

Now that we have a user account and namespace created, the next task is to define an OpenShift application, or target, that will house our PHP code. Give your application a name and select the gear profile.

Note: On the free tier of OpenShift, the only gear type available is small

If you would like to use a MySQL database as a backend for your application, select the box to add this database to your target.

After you name your application, you will also need to specify a password for authenticating to the Zend Server.

The next step is to accept the EULA that is presented in the following dialog. After reading the agreement, and if you agree to the terms, click on the Finish button.

If you selected to add a MySQL database to your OpenShift target, you will be presented with a dialog box that will display the connection information for your database. Make a note of this information as you will need it once you begin your application development.

Step 3: Test your Target Connection

After you have created your OpenShift target, you should test your connection to ensure that everything was created properly. In order to do this, click on Test Connection

If you the connection test passes, you can then create your Zend Studio project. Give your project a name, location, and select the container that we just created. Select to create a new application and specify if you would like to use the Zend Framework for your application.

Step 4: Test your application

At this point, your application should be deployed and publicly available via the following URL:

http://{appName}-{namespace}.rhcloud.com/{project-name}

For example, if you created an application called myapp with a namespace of mynamespace and a Zend Studio project name of myProject, your URL would be:

http://myapp-mynamespace.rhcloud.com/myProject

Step 5: Make a code change and sync change to OpenShift

Inside of Zend Studio, open up the index.phtml file location under application->views->scripts->index and modify the following line

<h1>Welcome to the <span id="zf-name">Zend Framework!</span></h1>

to

<h1>Welcome to the <span id="zf-name">Zend Framework, running on OpenShift!</span></h1>

You may have noticed that once you clicked the save button, Zend Studio automatically uploaded the changes file to your OpenShift target. This allows the change to be immediately reflected in your live application. If you point your browser back to your homepage, you should see the change already deployed.

Step 6: Manage your application with Zend Server

As part of creating your OpenShift target, you should have created a password in order to authenticate to your Zend Server. Point your browser to:

http://{appName}-{namespace}.rhcloud.com/ZendServer

Once you have authenticated, you can manage your application and server to meet your needs.

Option 2: Using the RHC command line tools

OpenShift provides convenient command line tools to interact with the service. The client tools are written in Ruby and therefore require that you ruby and gem installed on your system. Before continuing with this option, please follow the instruction on the getting started page which details how to install the command line tools.

Step 1: Create a Zend Server Gear

Open up your terminal and type the following command:

$ rhc app create zendapp zend-5.6

Once the application has been created, you should see the following output.

IMPORTANT: You will be given the URL to access your Zend Server Administration Console. It is vital that you login immediately to set the password for your server.

Step 2: Create a new project in Zend Studio

In the previous step, after creating your application, you should have been presented with the Git URL for your application. The URL will look like this:

Git URL: ssh://2759236feb48494cb4d5b767343bb52a@zendapp-onpaas.rhcloud.com/~/git/zendapp.git/

Copy this URL to your clipboard as you will need it shortly.

Open up Zend Studio and select to create a PHP Project from Git and click the Next button.

Give your project a name, paste in the Git URL from the previous step and click on Next.

At this point, you will need to create a target for your PHP deployment. Instead of going over these details again, please follow the instructions in step 2 above.

After creating a target, you will be presented with the option to include some predefined libraries to your application.

Step 3: Make a code change

Open up the index.php file located under {ProjectName}/php and modify line number 125 from

Welcome to OpenShift

to

Welcome to OpenShift running Zend!

Save your changes and then right click on your project. Select Team->Commit to commit your changes to your local git repository.

Once you are satisfied with your changes, you will need to deploy your changes to your OpenShift server.

Summary

In this blog post we went over two ways of using the Zend Framework and Zend Server for a OpenShift PHP application. At the time of this writing, we suggest that you use the built in Zend Studio integration for a seamless experience with the OpenShift platform as a service. If you would like more information on using Zend with OpenShift, check out the following:

Getting Started Guide

OpenShift Zend Developer Resources

Zend and OpenShift Webinar