Archive for September, 2010


How to Get Last Day of Last Month in Perl

Getting the last day of last month in perl can be a little tricky but I have just the solution for it.
First you find todays date using my favorite subroutine.

&get_date(0); #0 for number of days in past = today.

Second you take todays date and pass it to &get_date subroutine:

$current_day = $da; # $da is given a value by above subroutine.
&get_date($current_day); # Pass along todays date back to the get_date subroutine.

Now that you’ve passed the current day of month back to get_date, all the date values will be that of last day of last month.
Suppose today is September 7 2010

$yyyy = 2010
$mo = 08
$da = 31

Now lets put it all together.

#!/usr/bin/perl -w
use strict;
use Time::localtime; # &get_date needs this

# Date declarations
my $yyyy;
my $mo;
my $da;

my $current_days;    # Will be used to hold number of days in current month.
my $lastDayofLastMonth; # Variable for last day of the last month

# -----------------------------------------------------------------------
# Main
# -----------------------------------------------------------------------

&get_date_info;

print "Last day of last month was $lastDayofLastMonth \n";  # Will print out the last day of last month.

# -----------------------------------------------------------------------
# Figure last day of last month
# -----------------------------------------------------------------------
sub get_date_info {
    &get_date(0);
    $current_days = $da;
    &get_date($current_days);
    $lastDayofLastMonth = $yyyy . $mo . $da;

}

# -----------------------------------------------------------------------
# get date (my favorate subroutine!)
# -----------------------------------------------------------------------

sub get_date {
   my ($pastno) = @_; #Used to pass value for specific date in past.
   my $pastdate = localtime(time -(86400 * $pastno)); #pastno is # of days in past - set above
   $yyyy = $pastdate->year() + 1900;  # localtime->year() return years since 1900
   $mo = $pastdate->mon() + 1;        # localtime->mon() return 0-11,  Jan. - Dec.
   $da = $pastdate->mday();

   $mo = "0"."$mo"  if ($mo <= 9);
   $da = "0"."$da"  if ($da <= 9);

}
I don’t think this can be any simpler. If you have a much better way of doing this – PLEASE SHARE!

How to Setup Tomcat to Run on Port 80 Part 2

This is the second part to the “How to Setup Tomcat to Run on Port 80”.
Part 2
  1. Configure httpd.conf
  2. Add init.d Script
  3. Restart HTTPD

Configure The Domain’s httpd.conf

You will need to add some entries to the domain.conf file in /etc/httpd/conf.d/ or in httpd.conf file. I prefer to utilize the conf files in /etc/httpd/conf.d/ and use httpd.conf inclusion because it is so easy to mess up the httpd.conf file.

vi /etc/httpd/conf.d/yourdomain.conf

Add the following code before the </VirtualHost> entry.

<Location /WEB-INF/ > # (1) AllowOverride None deny from all </Location> <Location /META-INF/ > # (2) AllowOverride None deny from all </Location> JkAutoAlias /home/userX/tomcat/webapps # (3) JkMount /* tomcat1 # (4) JkMount /*/* tomcat1 # (5) JkUnMount /php/* tomcat1 # (6)
1. Security location entry for WEB-INF
2. Security for META-INF too
3. Kind of an auto invoker for webapps within webapps dir.
4. Invokes tomcat1 worker for root directory in url /
5. Invokes the worker for any directory deeper than /.  I have seen problems with recursive directories in this case and found this to solve the problem.
6. Perhaps you want apache to run a particular directory and not Tomcat. Just add UnMount to tell Apache to render instead.

Setup init.d Script

You’ll want an init.d script to start up your container when you boot up or reboot your server.
Create a file called tomcat.sh and place it in /etc/init.d/ and put in the following contents:

<div id="_mcePaste">#!/bin/sh
# Tomcat Startup Script

start() {

echo -n "Starting up tomcat1"
su username -c /home/username/tomcat/bin/start.sh

}
stop() {

echo -n "Stopping tomcat1"
su username -c /home/username/tomcat/bin/shutdown.sh

}

# See how we are called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                stop
                start
                ;;
        *)
                echo $"Usage: tomcat {start|stop|restart}"
                exit
        esac
You will need to update the path to correctly point to your start and shutdown scripts. The su username -c is to insure that the system starts up your script as the unix user you have tomcat installed. Be sure to chmod the file to 755 so it can be executed when needed.

To complete this automation of the start up script, you will need to add a symlink in rc.3 in /etc/rc3.d/ as run level 3 is the default startup run level.
cd /etc/rc3.d/
ln -s ../init.d/tomcat.sh S99tomcatStartup
S is for startup and 99 is the process priority – you want tomcat to startup last.

Restart Apache

Finally best we restart httpd with service httpd restart to initialize in the updates we made to the domain.conf file.

Please leave comments. I am always interesting in learning better ways to do things.

How to Setup Tomcat to Run on Port 80 Part 1

This one is going to be a long post so I will break it up in two parts. My notes on this are brief but I will be more descriptive of this setup for the newbie benefit. Many people in the web host business have asked me how to setup tomcat or multiple instances of tomcat. I’ve given individuals an insight of what I learned years ago, but this is the first time I’ve posted for all to see. It’s actually very simple.

Steps for part 1

  1. Install Tomcat Connector
  2. worker.properties
  3. httpd_jk.conf
  4. Install Tomcat
  5. Configure server.xml
  6. Set JAVA_HOME
  7. start tomcat

Steps for part 2

  1. Configure domain’s httpd.conf file
  2. Add init script and symlink in rc.d
  3. restart httpd
You can start these steps in any order, but as I’ve always said, “Theres more than one way to skin a tomcat”.

Install Apache Tomcat Connector

Get latest release here: http://tomcat.apache.org/download-connectors.cgi The current version of this writing is tomcat-connectors-1.2.30-src.tar.gz and upload to your /root dir and untar.
tar -xvzf tomcat-connectors-1.2.30-src.tar.gz
cd into the tomcat-connectors-1.2.30-src/native and compile:
./configure --with-apxs=/usr/sbin/apxs  (or wherever your apxs is)
make
su -c 'make install';
You will then find mod_jk.so module in /usr/lib64/httpd/modules/
However, we’re not done with this yet. We still need to place a copy of workers.properties in /etc/httpd/conf/ and add mod_jk.so to apache by adding a file called httpd_jk.conf in /etc/httpd/conf.d where apache httpd.conf inclusion.
In the folder tomcat-connectors-1.2.30-src/conf/ you can find a copy of httpd_jk.conf and workers.properties.

workers.properties

cp workers.properties /etc/httpd/conf/
Vi into workers.properties and update the following:
Add a new worker after the manager worker entry as shown below for worker “tomcat1”:
worker.list=tomcat1 # (1)
worker.tomcat1.port=8007 # (2)
worker.tomcat1.host=localhost # (3)
worker.tomcat1.type=ajp13 # (4)
1. You specify the name of the new worker which in this case is tomcat1. This name can be any name, but must be unique among existing workers and with no spaces or special characters.
2. You specify the AJP port for the connector. 8007 is default. You can change this port here and in tomcat server.xml to utilize multiple instances of tomcat.
3. localhost since apache and tomcat are on the same server. Use IP or Hostname if apache and tomcat are on different servers which is common with clustering/load balancing.
4. ajp1.3 is currently the default worker type. ajp12 is deprecated and as of this writing ajp1.4 hasn’t been release.
Save the file.

httpd_jk.conf File

Now we need to copy the httpd_jk.conf to /etc/httpd/conf.d to enable mod_jk module in apache.
vi /etc/httpd/conf.d/httpd_jk.conf and validate the paths. Modify the path to workers with root path: /etc/httpd/conf/workers.properties and change logs/ to /var/log/httpd/. You may change them to whatever suits you best, but the above is my habit.
Now restart httpd and fix any errors that show up. This is end of installation of the apache connector.

Next step is to install Apache Tomcat Container

Download apache tomcat 6 from here: http://tomcat.apache.org/download-60.cgi
Untar it in the directory you wish to host it from. Should not be in /root or owned by root. I usually install it in /home/username/ and have it owned by that user.
tar -xvzf apache-tomcat-6.0.29.tar.gz
After you extract, create a symbolic link ‘tomcat’ to make things easier. Saves you from having to update other config if you ever upgrade tomcat.
ln -s apache-tomcat-6.0.29 tomcat

Configure server.xml

Update HTTP connector port if different from 8080. I usually defer to something else for security reasons.
Update the AJP 1.3 connector port if different from 8009 in workers.properties worker entry. This is where we match our entries in workers.properties so apache and tomcat know which worker to deal with when rendering pages.
Update defaultHost= from localhost to your domain (without www)
Update Host name to your domain name (without www).
Add alias to include www.domain.com
<Alias>www.domainX.com</Alias>

Add context path before closing host tag if you plan to use webapp other than ROOT:

<Context path=""
	docBase="/home/username/tomcat/webapps/webappName" crossContext="false" reloadable="true">
</Context>
Save file. Its good practice to make a backup of server.xml when you know this one is working.

Set JAVA_HOME

Did you install java? no? Best we do this now. Download the latest jdk 1.6 bin file wherever. I prefer the bin over the rpm installation because it allows me to setup multiple JDK versions. You can download and setup as user root in opt. Lets create folder in /opt/ called java and extract the file there.
First make jdk executable
chmod +x jdk-6u21-linux-i586.bin
Then expand. It will prompt you to agree to terms for it completes.
./jdk-6u21-linux-i586.bin
You will end up with a folder jdk-1.6_21
Now we need to set JAVA_HOME for your tomcat installation. There are 2 ways to do this, one is by setting export JAVA_HOME in your .bash_profile or you can set it up in catalina.sh file along with JAVA_OPTS. I prefer to use catalina.sh as sometimes I will have different jdk requirements for different containers on the same user. I find the latter method provides for better flexibility. Newer tomcat installations now have setclasspath.sh where JAVA_HOME can be defined. Also if you’re using the java service wrapper, then you will want to define JAVA_HOME in the wrapper.conf file in the conf directory of tomcat.
Add following after the comments in catalina.sh in the bin/ directory:
JAVA_HOME=/opt/java/jdk-1.6_21
export JAVA_HOME
At this point, you should be able to start your container.
cd tomcat/bin/
./catalina.sh run (if you want to watch the output - same as catalina.out log)
./startup.sh (or start it up and forget about it)
View tomcat/logs/catalina.out for any errors.
Now you can access your tomcat webpage or webapp using the HTTP port 8080 if its open.
Next post I will finish the rest of the httpd configuration so you can run your application on port 80.