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 rexify.org/get/. 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.

echo 'deb http://rex.linux-files.org/debian/ squeeze rex' >> /etc/apt/sources.list

After that add the GPG key to the apt keyring.

wget -O - http://rex.linux-files.org/DPKG-GPG-KEY-REXIFY-REPO | 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.

git clone https://github.com/krimdomu/rex-example-modules.git 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.
pass_auth;
# 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::setup();
   apache::module::setup({name => "jk"});
   tomcat::setup();
   tomcat::role::add_manager();
   tomcat::user::add_manager({
      username => "manager",
      password => "passw0rd",
   });
   apache::restart();
   tomcat::restart();
   # 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/worker.properties
## this file is managed by rex
##
worker.list=tc,jkstatus
worker.tomcat.port=8009
worker.tomcat.host=<%+ $::Network->{"networkconfiguration"}->{"eth0"}->{"ip"} %>
worker.tomcat.type=ajp13
worker.tomcat.lbfactor=1
worker.tomcat.reference=worker.template
worker.tc.type=lb
worker.tc.balance_workers=tomcat
worker.tc.sticky_session=false
worker.jkstatus.type=status
worker.template.type=ajp13

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/worker.properties
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
</Location>

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/worker.properties",
      content   => template("files/worker.properties"),
      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 code.google.com. Create a folder called packages, extract the zip archive and copy the file probe.war into that directory.

mkdir packages
cd packages
wget http://psi-probe.googlecode.com/files/probe-2.2.3.zip
unzip probe-2.2.3.zip

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.

Share this page:

0 Comment(s)