Install Tomcat And Deploy Web Applications With Rex

In this tutorial I will show you how to manage your Tomcat installations and how to deploy Webapps in a repeatable way with Rex. In this tutorial I will use Debian Squeeze, but Rex is also available for other distributions.


Introduction to Rex

Rex is a tool, written in Perl, to ease the administration and deployment of many servers.

From the Website: With (R)?ex you can manage all your boxes from a central point through the complete process of configuration management and software deployment.

The starting point of a Rex project is its Rexfile. Rex will parse this file and executes the defined tasks on your servers. Just think of it like a Makefile but it is written in Perl.

Every Rexfile is divided into tasks. You can define a task for every logical step to install and configure your servers.


Get the Software

You only need Rex on your Workstation. No additional software is needed on your servers. You just need ssh root access to your servers.

You can download Rex from Packages are available for CentOS, Debian, Ubuntu, Fedora, Mageia and OpenSuSE. Sources are available from CPAN.

For Debian just add the Rex repository to your sources.list.d directory.if(typeof __ez_fad_position != ‘undefined’){__ez_fad_position(‘div-gpt-ad-howtoforge_com-medrectangle-4-0’)};

echo 'deb squeeze rex' >> /etc/apt/sources.list

After that add the GPG key to the apt keyring.

wget -O - | apt-key add -

And install the software. We will install rex for the core tool and rex-apache-deploy for the deployment module.

apt-get update && apt-get install rex rex-apache-deploy


Prepare your Rexfile and execute the first task

At first create a directory tomcat and change into it.

mkdir tomcat; cd tomcat

After that checkout the Rex Example Modules from github into the directory lib. There is a tomcat and apache module that will help us.if(typeof __ez_fad_position != ‘undefined’){__ez_fad_position(‘div-gpt-ad-howtoforge_com-box-4-0’)};

git clone lib

Now create the file Rexile and open it with your favorite editor. If you are using vim you can use the following lines to enable syntax highlighting.

:set ft=perl
:syn on

# File: Rexfile
# at first enable strict and warnings
use strict;
use warnings;
# than include all needed example modules
use ALLMODS;    # this sets the include path to the rex example modules
use apache;     # include apache module
use apache::module;
use tomcat;     # include tomcat module
use tomcat::user;
use tomcat::role;
# set user and password to login to your servers
user "root";
password "test";
# enable password authentication.
# if you want to use key authentication use the following
# and comment out the line "pass_auth"
# public_key "/path/to/your/public.key";
# private_key "/path/to/your/private.key";
# define a server group named "tomcat". 
# put all your servers you want to deploy in that group.
group tomcats => "tc01", "tc02";
desc "Install and Configure Apache and Tomcat";
task "prepare", group => "tomcats", sub {
   apache::module::setup({name => "jk"});
      username => "manager",
      password => "passw0rd",
   # take care that the services will start on system boot
   service apache2 => "ensure", "started";
   service tomcat6 => "ensure", "started";

Now save the file, open a terminal and change into the tomcat directory.

To execute the created task on your servers just call it as shown in the next line.

rex prepare

If you want to view your defined tasks you can do this with the -T option.

rex -T


Configure Apache/modjk

After the installation of apache and tomcat you need to configure apache/modjk to redirect the requests to tomcat.

To make this happen create a directory called files and save your configuration files there.

mkdir files
## File: files/
## this file is managed by rex
worker.tomcat.port=8009<%+ $::Network->{"networkconfiguration"}->{"eth0"}->{"ip"} %>

As you see here, there is a special variable in the file. Rex has a buildin templating system. And with this expression you’ll get the ip of the network device eth0.

## File: files/modjk.conf
## this file is managed by rex
JkWorkersFile /etc/apache2/
JkLogFile /var/log/apache2/mod_jk.log
JkShmFile /var/log/apache2/jk.shm
JkMount /* tc
# Add the jkstatus mount point
JkMount /jkmanager/* jkstatus
<Location /jkmanager/>
	JkMount jkstatus

In realworld scenarios there would be a server.xml, too. But for this tutorial you just gonna patch the file to enable the AJP connector.

Now open your Rexfile and add a second task configure.

desc "Configure Apache and Tomcat";
task "configure", group => "tomcats", sub {
   # remove default vhost
   unlink "/etc/apache2/sites-enabled/000-default";
   # upload the file, but parse the file as a template
   file "/etc/apache2/",
      content   => template("files/"),
      owner     => "root",
      group     => "root",
      mode      => 640,
      on_change => sub { apache::restart(); };
   # upload the configuration files
   file "/etc/apache2/conf.d/modjk.conf",
      source    => "files/modjk.conf",
      owner     => "root",
      group     => "root",
      mode      => 640,
      on_change => sub { apache::restart(); };
   # patch server.xml to allow ajp access
   # in realworld use a template for this file
   my $content = cat "/etc/tomcat6/server.xml";
   $content =~ s/<\/Service>/<Connector port="8009" protocol="AJP\/1.3" redirectPort="8443" \/>\n<\/Service>/;
   file "/etc/tomcat6/server.xml",
      content   => $content,
      on_change => sub { tomcat::restart(); };

Now you can run the configure task to configure your server.

rex configure


Deploying an Application

Well, after you have deployed and configured your server you need to upload the application the server should serve.

There is a Rex module for simple deployment tasks called Rex::Apache::Deploy. You’ve installed it already.

In this howto you will deploy psi-probe. You can download it from Create a folder called packages, extract the zip archive and copy the file probe.war into that directory.

mkdir packages
cd packages

Now open your Rexfile and put the following lines to the other use commands.

# include deployment support for tomcat
use Rex::Apache::Deploy "Tomcat";

And create a third task called deploy.

desc "Deploy the application";
task "deploy", group => "tomcats", sub {
   # set the context path for our tomcat application
   context_path "/psiprobe";
   # deploy the app. 
   deploy "packages/probe.war",
      username => "manager",
      password => "passw0rd",
      port     => 8080;


Putting it all together

Now you can execute all 3 tasks one after the other and the result is a server ready to serve our content. At last you can define a batch that executes all your tasks, so that you don’t need to write that much.

Open your Rexfile and append the next line to it.

desc "Execute all tasks prepare, configure and deploy";
batch all => "prepare", "configure", "deploy";

You can execute this batch with the -b option.

rex -b all

Now, after all tasks ran, you can access the just deployed application under /psiprobe on your server.

About the Author

Leave a Reply