As people have been asking me how I got GlassFish 4 running on OpenShift, I decided to write down everything I learned in the process. I started out by following these examples, but ran into a lot of trouble along the way. I hope this post can make the process a lot easier for you.
First of all: don't even bother trying this on Windows. Use OS X or Linux.
Start by creating an OpenShift account.
Using their web interface, create a new application with the DIY cartridge and add a MySQL cartridge to it as well.
I will refer to this application as
yourapp from now on.
Lastly, you will need the OpenShift Client Tools, so install those as well.
As for GlassFish, you can download (or clone) my archive from GitHub. This is the official GlassFish 4 release, modified in the following ways:
domain.xmlconfiguration file has been modified to run on OpenShift.
The archive also contains the
stop hooks you need.
Start by cloning your repository to your local machine:rhc git-clone yourapp
This will create a
Move the files you downloaded (or cloned) into
yourapp as follows:
glassfish4directory (the entire directory, not just its contents) into
yourapp/.openshift/action_hooksand check to make sure they're executable.
Finally, add, commit and push the changes you made back to OpenShift:cd yourapp
The push will cause your application to restart and execute the new start hook. Give it a few minutes just to be sure, then check if the server is running at http://yourapp-youraccount.rhcloud.com.
I added the
git status command in there so you can verify if Git picked up all the changes you made.
If not, use
git add to add the files Git missed.
If this is your first time using Git from the command-line, I recommend you set up your name and email address
and change your default editor to something other than
To deploy an application, simply copy its war into
and run the previous commands again to add, commit and push the changes. GlassFish will automatically deploy your application
after launch. If you think something went wrong, use
ssh to log into your application and look for a
your.war_deployed file in the
If there is no such file, take a look at the server logs to find out what went wrong:cd ../logs/
You cannot use the GlassFish administration console with OpenShift.
glassfish-resources.xml seem to be supported. If you need to set up JDBC resources, connection pools,
security realms, ... on your server, I recommend you use the following approach:
domain.xmlto see what changes were made.
yourapp. Usually, this just means adding one or two extra elements.
Working with your MySQL server on OpenShift is pretty easy once you figure out how to connect to it from MySQL Workbench. Launch Workbench and add a new connection. As you can't connect to MySQL on OpenShift directly, you need to use a TCP/IP over SSH connection. The settings I used for my application can be seen in the following screenshot:
To find the settings you need, use the
rhc app show yourapp command. This is what I get for my
MacBook-Air:OpenShift Steven$ rhc app show glassfish glassfish @ http://glassfish-svanimpe.rhcloud.com/ (uuid: 5232bc5f500446f41000014c) ----------------------------------------------------------------------------------- Domain: svanimpe Created: Sep 13 9:18 AM Gears: 1 (defaults to small) Git URL: ssh://email@example.com/~/git/glassfish.git/ SSH: firstname.lastname@example.org diy-0.1 (Do-It-Yourself 0.1) ---------------------------- Gears: Located with mysql-5.1 mysql-5.1 (MySQL Database 5.1) ------------------------------ Gears: Located with diy-0.1 Connection URL: mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/ Database Name: glassfish Password: supersecret Username: adminHWvBmju
The output will give you all of the values you need except for the MySQL hostname and port, as the command only
shows the environment variables and not the actual values. To find out those values,
ssh into your application and
[glassfish-svanimpe.rhcloud.com 5232bc5f500446f41000014c]\> env | grep MYSQL OPENSHIFT_MYSQL_DIR=/var/lib/openshift/5232bc5f500446f41000014c/mysql/ OPENSHIFT_MYSQL_DB_PORT=3306 OPENSHIFT_MYSQL_DB_HOST=127.9.226.130 OPENSHIFT_MYSQL_DB_PASSWORD=supersecret OPENSHIFT_MYSQL_IDENT=redhat:mysql:5.1:0.2.2 OPENSHIFT_MYSQL_DB_USERNAME=adminHWvBmju OPENSHIFT_MYSQL_DB_SOCKET=/var/lib/openshift/5232bc5f500446f41000014c/mysql//socket/mysql.sock OPENSHIFT_MYSQL_DB_URL=mysql://adminHWvBmju:asIQ9W3wc26y@127.9.226.130:3306/ OPENSHIFT_MYSQL_DB_LOG_DIR=/var/lib/openshift/5232bc5f500446f41000014c/mysql//log/
That's it! You should now be up and running with both GlassFish 4 and MySQL.
If you've enjoyed my work or found it helpful, please consider becoming a patron. Your support helps me free up time to work on my books and projects.