Hi,

Nikhil Mone

Nikhil Mone

In this article we will discuss how to run Tomcat server on Openshift using Do It Yourself cartridges.

A lot of developers like to test their code by deploying it to web-containers, such as Tomcat, due to less complexities, less overhead and less files to tweak 🙂

We can use our own customized Tomcat server to achieve a simple web-hosting over OpenShift.

Lets check how ??

Step-1 : We will create a simple application “mytomcatapp” using diy (do it yourself) cartridge, which allows us a lot of tweak options. Run below command from your project directory:

$ rhc app create -a mytomcatapp -t diy0.1 -l youropenshiftaccount@blah.com

Step-2 : Above creates a directory as “mytomcatapp”, navigate inside up to “action_hooks”:

$Project_DIR/.openshift/action_hooks

for eg : /home/userone/mytomcatapp/.openshift/action_hooks

Step-3 : By default diy cartridge has a ruby server installed, which can be stop/start using start and stop scripts, lets do a “ls -ltra” here :

..
[userone@userone action_hooks]$ ls -ltra
total 32
-rwxrwxr-x. 1 userone userone  178 Jul  2 15:03 stop
-rwxrwxr-x. 1 userone userone  295 Jul  2 15:03 start
-rwxrwxr-x. 1 userone userone  257 Jul  2 15:03 pre_build
-rwxrwxr-x. 1 userone userone  186 Jul  2 15:03 post_deploy
-rwxrwxr-x. 1 userone userone  249 Jul  2 15:03 deploy
-rwxrwxr-x. 1 userone userone  264 Jul  2 15:03 build
drwxrwxr-x. 2 userone userone 4096 Jul  2 15:03 .
drwxrwxr-x. 4 userone userone 4096 Jul  2 15:03 ..

Step-4 : Now lets remove stop and start scripts from here :

$ rm stop start

for eg:

..
[userone@userone action_hooks]$ rm stop start
[userone@userone action_hooks]$ ls -ltra
total 24
-rwxrwxr-x. 1 userone userone  257 Jul  2 15:03 pre_build
-rwxrwxr-x. 1 userone userone  186 Jul  2 15:03 post_deploy
-rwxrwxr-x. 1 userone userone  249 Jul  2 15:03 deploy
-rwxrwxr-x. 1 userone userone  264 Jul  2 15:03 build
drwxrwxr-x. 4 userone userone 4096 Jul  2 15:03 ..
drwxrwxr-x. 2 userone userone 4096 Jul  2 15:11 .
[userone@userone action_hooks]$ pwd
/home/userone/tomcat/.openshift/action_hooks

Step-5 : Now recreate stop and start files :

$ touch stop start

for eg :

..
[userone@userone action_hooks]$ touch stop start
[userone@userone action_hooks]$ ls -ltra
total 24
-rwxrwxr-x. 1 userone userone  257 Jul  2 15:03 pre_build
-rwxrwxr-x. 1 userone userone  186 Jul  2 15:03 post_deploy
-rwxrwxr-x. 1 userone userone  249 Jul  2 15:03 deploy
-rwxrwxr-x. 1 userone userone  264 Jul  2 15:03 build
drwxrwxr-x. 4 userone userone 4096 Jul  2 15:03 ..
-rw-rw-r--. 1 userone userone    0 Jul  2 15:12 stop
-rw-rw-r--. 1 userone userone    0 Jul  2 15:12 start
drwxrwxr-x. 2 userone userone 4096 Jul  2 15:12 .

but here we can see the permissions are different from previous ls output, so lets give 775 permission as the earlier scripts had:

..
[userone@userone action_hooks]$ chmod 775 stop start
[userone@userone action_hooks]$ ls -ltra
total 24
-rwxrwxr-x. 1 userone userone  257 Jul  2 15:03 pre_build
-rwxrwxr-x. 1 userone userone  186 Jul  2 15:03 post_deploy
-rwxrwxr-x. 1 userone userone  249 Jul  2 15:03 deploy
-rwxrwxr-x. 1 userone userone  264 Jul  2 15:03 build
drwxrwxr-x. 4 userone userone 4096 Jul  2 15:03 ..
-rwxrwxr-x. 1 userone userone    0 Jul  2 15:12 stop
-rwxrwxr-x. 1 userone userone    0 Jul  2 15:12 start
drwxrwxr-x. 2 userone userone 4096 Jul  2 15:12 .

Step-6 : Once done lets commit the changes to git repository :

..
[userone@userone action_hooks]$ git commit -a -m "removing scripts"
[master e150ea0] removing scripts
2 files changed, 0 insertions(+), 9 deletions(-)

Step-7 : Lets push the changes to Openshift server now :

..
[userone@userone action_hooks]$ git push
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 471 bytes, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Stopping application...
remote: Done
remote: ~/git/tomcat.git ~/git/tomcat.git
remote: ~/git/tomcat.git
remote: Running .openshift/action_hooks/pre_build
remote: Running .openshift/action_hooks/build
remote: Running .openshift/action_hooks/deploy
remote: Starting application...
remote: Done
remote: Running .openshift/action_hooks/post_deploy
To ssh://420ne8xz7pq64949861a99ea941c8ab3@mytomcatapp-mydomain.rhcloud.com/~/git/tomcat.git/
67f5592..e150ea0  master -> master

Step-8 : Let us login to OpenShift server via ssh :

$ ssh 420ne8xz7pq64949861a99ea941c8ab3@mytomcatapp-mydomain.rhcloud.com

From you home directory navigate to : mytomcatapp/data/

$ cd mytomcatapp/data/

Make sure you do it, because on other directories you won’t have write permission.

Step-9 : Now lets download Apache-Tomcat server, directly on OpenShift :

..
[mytomcatapp-mydomain.rhcloud.com data]> wget http://apache.techartifact.com/mirror/tomcat/tomcat-7/v7.0.28/bin/apache-tomcat-7.0.28.tar.gz
--2012-07-02 05:51:42--  http://apache.techartifact.com/mirror/tomcat/tomcat-7/v7.0.28/bin/apache-tomcat-7.0.28.tar.gz
Resolving apache.techartifact.com... 101.153.143.60
Connecting to apache.techartifact.com|101.153.143.60|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7674156 (7.3M) [application/x-gzip]
Saving to: “apache-tomcat-7.0.28.tar.gz”

100%[==================================================================================================================================================================================================>] 7,674,156   60.5K/s   in 3m 5s

2012-07-02 05:54:48 (40.6 KB/s) - “apache-tomcat-7.0.28.tar.gz” saved [7674156/7674156]

Step-10 : unzip the downloaded file :

..
tar zxvf apache-tomcat-7.0.28.tar.gz
..

Step 11 : Once done remove “apache-tomcat-7.0.28.tar.gz” as it will unneccesarily consume disk space.

..
[mytomcatapp-mydomain.rhcloud.com data]> rm -f apache-tomcat-7.0.28.tar.gz
[mytomcatapp-mydomain.rhcloud.com data]> cd apache-tomcat-7.0.28/
[mytomcatapp-mydomain.rhcloud.com apache-tomcat-7.0.28]> ls -ltra
total 124
drwxr-xr-x. 2 420ne8xz7pq64949861a99ea941c8ab3 420ne8xz7pq64949861a99ea941c8ab3  4096 Jun 15 03:52 work
drwxr-xr-x. 2 420ne8xz7pq64949861a99ea941c8ab3 420ne8xz7pq64949861a99ea941c8ab3  4096 Jun 15 03:52 logs
drwxr-xr-x. 7 420ne8xz7pq64949861a99ea941c8ab3 420ne8xz7pq64949861a99ea941c8ab3  4096 Jun 15 03:53 webapps
-rw-r--r--. 1 420ne8xz7pq64949861a99ea941c8ab3 420ne8xz7pq64949861a99ea941c8ab3 15557 Jun 15 03:54 RUNNING.txt
-rw-r--r--. 1 420ne8xz7pq64949861a99ea941c8ab3 420ne8xz7pq64949861a99ea941c8ab3  8826 Jun 15 03:54 RELEASE-NOTES
-rw-r--r--. 1 420ne8xz7pq64949861a99ea941c8ab3 420ne8xz7pq64949861a99ea941c8ab3  1192 Jun 15 03:54 NOTICE
-rw-r--r--. 1 420ne8xz7pq64949861a99ea941c8ab3 420ne8xz7pq64949861a99ea941c8ab3 56812 Jun 15 03:54 LICENSE
drwxr-xr-x. 2 420ne8xz7pq64949861a99ea941c8ab3 420ne8xz7pq64949861a99ea941c8ab3  4096 Jun 15 03:54 conf
drwxr-xr-x. 9 420ne8xz7pq64949861a99ea941c8ab3 420ne8xz7pq64949861a99ea941c8ab3  4096 Jul  2 06:30 .
drwxr-xr-x. 2 420ne8xz7pq64949861a99ea941c8ab3 420ne8xz7pq64949861a99ea941c8ab3  4096 Jul  2 06:30 bin
drwxr-xr-x. 2 420ne8xz7pq64949861a99ea941c8ab3 420ne8xz7pq64949861a99ea941c8ab3  4096 Jul  2 06:30 lib
drwxr-xr-x. 2 420ne8xz7pq64949861a99ea941c8ab3 420ne8xz7pq64949861a99ea941c8ab3  4096 Jul  2 06:30 temp
drwxr-x---. 3 420ne8xz7pq64949861a99ea941c8ab3 420ne8xz7pq64949861a99ea941c8ab3  4096 Jul  2 06:31 ..

Step-12 : Now navigate to tomcat’s “conf” directory and edit server.xml file as below :

<!--?xml version='1.0' encoding='utf-8'?-->
<!--   Licensed to the Apache Software Foundation (ASF) under one or more   contributor license agreements.  See the NOTICE file distributed with   this work for additional information regarding copyright ownership.   The ASF licenses this file to You under the Apache License, Version 2.0   (the "License"); you may not use this file except in compliance with   the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License. -->
<!-- Note:  A "Server" is not itself a "Container", so you may not      define subcomponents such as "Valves" at this level.      Documentation at /docs/config/server.html  -->

  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->

  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->

  <!-- Prevent memory leaks due to use of particular java/javax APIs-->

  <!-- Global JNDI resources        Documentation at /docs/jndi-resources-howto.html   -->

    <!-- Editable user database that can also be used by          UserDatabaseRealm to authenticate users     -->

  <!-- A "Service" is a collection of one or more "Connectors" that share        a single "Container" Note:  A "Service" is not itself a "Container",        so you may not define subcomponents such as "Valves" at this level.        Documentation at /docs/config/service.html    -->

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"         maxThreads="150" minSpareThreads="4" />
    -->

    <!-- A "Connector" represents an endpoint by which requests are received          and responses are returned. Documentation at :          Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)          Java AJP  Connector: /docs/config/ajp.html          APR (HTTP/AJP) Connector: /docs/apr.html          Define a non-SSL HTTP/1.1 Connector on port 8080     -->

    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"                port="8080" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443          This connector uses the JSSE configuration, when using APR, the          connector should be using the OpenSSL style configuration          described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"                maxThreads="150" scheme="https" secure="true"                clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->

    <!-- An Engine represents the entry point (within Catalina) that processes          every request.  The Engine implementation for Tomcat stand alone          analyzes the HTTP headers included with the request, and passes them          on to the appropriate Host (virtual host).          Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->

      <!--For clustering, please take a look at documentation at:           /docs/cluster-howto.html  (simple how to)           /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" />
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords            via a brute-force attack -->

        <!-- This Realm uses the UserDatabase configured in the global JNDI              resources under the key "UserDatabase".  Any edits              that are performed against this UserDatabase are immediately              available for use by the Realm.  -->

        <!-- SingleSignOn valve, share authentication between web applications              Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.              Documentation at: /docs/config/valve.html              Note: The pattern used is equivalent to using pattern="common" -->

Here the changes made are :

1. Server port is changed to 15005 :

<Server port="15005" shutdown="SHUTDOWN">

2. “address=”127.3.45.2” has been added to :

..
[mytomcatapp-mydomain.rhcloud.com conf]> env | grep -i internal
OPENSHIFT_INTERNAL_PORT=8080
OPENSHIFT_INTERNAL_IP=127.3.45.2

3. Changed the AJP connector port as well :

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="15009" protocol="AJP/1.3" redirectPort="8443" />

4. In “Engine name” default host has been changed :

<Engine name="Catalina" defaultHost="127.3.45.2">

5. In host name we will put:

<Host name="mytomcatapp-mydomain.rhcloud.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

which is our “application-namespace.rhcloud.com”

Note : OpenShift doesn’t allow allocating ports below 15000, except 8080 and that is by default.

Step-13 : Start tomcat server from bin directory using “startup.sh”

Step-14 : Navigate to the URL :

http://mytomcatapp-mydomain.rhcloud.com/

Step-15 : I know all would be interested to click on “Manager App” tab, but it will ask for credentials 🙂

Edit “tomcat-users.xml” file inside tomcat’s conf directory as below :

..
<!--?xml version='1.0' encoding='utf-8'?-->
<!--   Licensed to the Apache Software Foundation (ASF) under one or more   contributor license agreements.  See the NOTICE file distributed with   this work for additional information regarding copyright ownership.   The ASF licenses this file to You under the Apache License, Version 2.0   (the "License"); you may not use this file except in compliance with   the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License. -->

<!--   NOTE:  By default, no user is included in the "manager-gui" role required   to operate the "/manager/html" web application.  If you wish to use this app,   you must define such a user - the username and password are arbitrary. -->
<!--
  NOTE:  The sample user and role entries below are wrapped in a comment
  and thus are ignored when reading this file. Do not forget to remove
  <!.. ..> that surrounds them.
-->
<!--
  <role rolename="tomcat" />
  <role rolename="role1" />
  <user username="tomcat" password="tomcat" roles="tomcat" />
  <user username="both" password="tomcat" roles="tomcat,role1" />
  <user username="role1" password="tomcat" roles="role1" />
-->

Here :

<role rolename="manager-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui"/>

provides us access to “Manager App“, from here you can manage your applications/jvm.

Regards,
Middleware Magic Team 🙂

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.