While debate rages among developers about the merits of IDEs, the list of integrated tasks they can perform is always growing. For PHP developers, the PHP Development Tools (PDT) can be used to add the necessary tooling to create PHP web applications in the open source Eclipse IDE. If you pair PDT with the OpenShift Tools for Eclipse, you can develop PHP apps and push them to the cloud without having to touch a terminal. In this blog post, we will go through how to do that step by step and look at the features the OpenShift Eclipse tools provide.
To get started, choose a version of the Eclipse IDE then download and install it. For this post, I am using the Kepler version of the Eclipse Standard package.
Install PHP Development Tools
The PHP Development Tools (PDT) provide PHP development capabilities within Eclipse. To install them, open Eclipse and go to Help > Install New Software. If you are using a recent Eclipse version you should be able to find the tools by searching for ‘php’ in the default repositories for your version (eg. Juno, Kepler).
Select the PHP Development Tools (PDT) package and click Next to install it. Review the license agreements, click Finish and restart Eclipse when prompted.
Install OpenShift Tools for Eclipse
JBoss Tools provide OpenShift integration within Eclipse. To install them, go to the Eclipse Marketplace (Help > Eclipse Marketplace) and search for ‘jboss’ in all markets and categories. Click Install next to the JBoss Tools package that matches the version of Eclipse you are using (eg. Kepler). Make sure JBoss OpenShift Tools is selected in the expanded package view and click Confirm to install. You will need to accept the license agreements and restart Eclipse when prompted.
Launch OpenShift Application Wizard
In Eclipse, select File > New > Project and then navigate to OpenShift > OpenShift Application and click Next.
Sign in to OpenShift
The next step is to sign in to OpenShift Online. If you do not yet have an account, click on the sign up here link in Eclipse or click here to create one now.
Once you have an OpenShift Online account, enter your username and password and click Next to continue.
If you have not yet chosen a unique domain for your OpenShift account, a window will pop up prompting you to set one now. The domain or namespace you select becomes part of the public URL of your applications, which takes the form applicationName-domain.rhcloud.com. You can also set your domain on the OpenShift Online Management Console’s Settings page.
Configure PHP Project
Give your project a name and select the cartridge type PHP 5.3 (php-5.3). Choose the small gear profile and add a MySQL database to your application by ticking the box for MySQL 5.1 (mysql-5.1) under Embeddable Cartridges.
Notice that under Advanced there is the option to specify a custom Git repository to clone. However, we will be using the default source code for this post. Click Next to continue.
Leave the Create a new project and Create and set up a server for easy publishing options ticked and click Next. You could also choose to continue to work on an existing project by selecting it here.
Set up SSH Keys
In the next section of the wizard, you can choose where you would like the application code to be cloned. At this point you also need to ensure your OpenShift account has a public SSH key for your machine and that Eclipse is aware of the corresponding private key. You can generate a new SSH key and add it to your OpenShift account with the SSH Keys wizard that is linked to in the current section.
In the SSH Keys wizard, click New. Give the new key a name and add a passphrase if you wish. Click Finish.
The new key should now appear in your SSH Public Keys list.
Alternatively, if you have an existing SSH key, you can use that instead by clicking Add Existing in the SSH Keys wizard and browsing to its location.
Create PHP Application
Click Finish in the New OpenShift Application wizard to create your new PHP app. You will see messages showing that the application is being created. Once it is done, a window will pop up displaying your MySQL database credentials. Make a note of these if you wish, but you do not need to as they will be available as application environment variable values.
You will be prompted to publish the committed application changes to OpenShift; select Yes.
Explore OpenShift Tools
To check out the OpenShift features available inside the Eclipse IDE, open the JBoss perspective. You can do this via Window > Open Perspective > Other > JBoss.
Click on the OpenShift Explorer view. If it is not open, click Window > Show View > Other and select JBoss Tools > OpenShift Explorer. Right click on your OpenShift connection in this window to view a menu with options enabling you to:
- Launch the wizard to create a new OpenShift application
- Create, edit or delete your application domain/namespace
- Launch the Manage SSH Keys tool
- View properties of your OpenShift account
If you expand the list below your OpenShift connection and right click on the name of your new PHP application, you can access menu options to:
- Launch a web browser window and navigate to your web application
- Tail your web app’s log files
- Manage port forwarding for your application
- View the environment variables for your app
- Edit the application’s embedded cartridges
- Restart or delete your web app
- Import existing code to your app
- Create a new server to which to publish your app
- View details of your application such as its public URL, type and SSH connection string
Launch the Eclipse web browser window and view your application. You should see a ‘Welcome to OpenShift’ page.
Add Application Code
Our IDE is set up and ready to go, so it’s time to switch into code mode and add some PHP. You can write your own or use the sample code I have created for this post here. Replace the contents of appName/php/index.php with the contents of this file (alternatively, you could create a new application using this repository as the base).
In the JBoss perspective, click on the Servers tab. Right click on the name of your application and select Publish. Select Yes in the dialog that appears to confirm you wish to push your changes. You should now see messages in the Console showing the app rebuilding and restarting.
Go back to your Eclipse web browser window and refresh the application. The app should display a ‘Hello World’ header and a message asking you to add some content to your database.
The next step is to add some data to the MySQL database. There are several ways we could achieve this, including using the phpMyAdmin embedded cartridge, but for this post, we will use port forwarding and the command line. You must have MySQL installed locally and available on your path to complete this step.
We are going to need those MySQL credentials. You can retrieve them by right clicking on the name of your application in the OpenShift Explorer and selecting Environment Variables. Look for OPENSHIFT_MYSQL_DB_USERNAME, OPENSHIFT_MYSQL_DB_PASSWORD and OPENSHIFT_MYSQL_DB_HOST.
The Eclipse OpenShift tools make it simple to start port forwarding so you can access your database with local client tools. In the OpenShift Explorer view, right click on the name of your app and select Port Forwarding. Deselect Use ‘127.0.0.1’ as the local address for all Services and click Start All.
Open a terminal window. Save the contents of this SQL file to your machine as init.sql. Open the file and change the OPENSHIFT_APP_NAME environment variable in the third line to the name of your application. Execute the following command in the terminal, replacing the environment variables with their values.
mysql -u$OPENSHIFT_MYSQL_DB_USERNAME -p$OPENSHIFT_MYSQL_DB_PASSWORD -h$OPENSHIFT_MYSQL_DB_HOST < init.sql
If you did not receive any errors, your database should now be populated with some data. Go back to Eclipse and click Stop All to cease port forwarding.
Open the Eclipse web browser and refresh your application. The page should now include the names of the places found in the database.
Change Application to Hot Deploy
One final feature of the Eclipse OpenShift integration that may not be immediately obvious is the ability to add and remove marker files, such as the marker that tells OpenShift to hot deploy.
To add this marker, go to the Project or Package Explorer and right click on the name of your project. Select OpenShift > Configure Markers. Tick the box next to Hot Deploy and click OK.
At the time of writing, the Eclipse tool had the capability to create and delete marker files in the correct location, but these markers were not automatically added to the Git repository and pushed to OpenShift Online when publishing through Eclipse. To add new marker files to the Git repository, run the command
git add /path/to/repo/appName/.openshift/markers/*. There is more information about hot deployment in this post.
The PHP Development Tools and OpenShift Tools for Eclipse make it possible to achieve many common tasks quickly and easily without having to leave your IDE. You can push changes to the cloud from Eclipse and see the console output as the application rebuilds and restarts, and the resulting application within the IDE’s web browser. This is a convenient alternative for those who dislike the command line or simply want to avoid having to switch windows too frequently.