Monday, April 25, 2011

Setting up Apache2 and Tomcat6 on Ubuntu 10.04

This is a short post on setting up Apache2 and Tomcat6

Install Java
apt-get -y install python-software-properties
add-apt-repository "deb http://archive.canonical.com/ lucid partner"
apt-get update
apt-get install -y sun-java6-jdk

Install tomcat6 with apache2 support
apt-get -y install tomcat6 libtcnative-1 libapache2-mod-jk

Setup worker.properties with the following values:
workers.tomcat_home=/usr/share/tomcat6
workers.java_home=/usr/lib/jvm/java-6-sun
nano /etc/libapache2-mod-jk/workers.properties

Modify /etc/apache2/mods-available/jk.load
cat >>/etc/apache2/mods-available/jk.load <<EOF
JkWorkersFile /etc/libapache2-mod-jk/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel error
EOF

Setup virtual host
cat >/etc/apache2/sites-available/test.henyo.com <<EOF
NameVirtualHost *
<VirtualHost *>
        ServerName test.henyo.com
        JkMount /* ajp13_worker
</VirtualHost>
EOF

Activate the site
a2ensite test.henyo.com

Setup tomcat for ajp13
nano /etc/tomcat6/server.xml

Restart tomcat and apache2
service tomcat6 restart
service apache2 restart

Install and configure UFW
apt-get install ufw
ufw allow ssh
ufw allow http
ufw enable

Install and configure Tripwire
apt-get install tripwire

I hope you found the post useful. You can subscribe via email or subscribe via a feed reader to get relevant updates from this blog. Have a nice day.

Sunday, April 17, 2011

Setting up a Spring Data JPA Project

This post is about setting up a Maven Project based on Spring Data JPA. I spent a few hours today nailing it down. Hopefully it saves someone some time to bring a project up quickly.

- Spring Framework based application
- makes use of Spring Data JPA for easy Repositories
- makes use of JPA 2.0 persistence with EcliplseLink
- JUnit4 and spring-test
- Logging using SL4J to Log4J

For the impatient, I uploaded a sample project on github: spring-data-jpa-trial

You can also download the tar ball or zip file from these links.

I started with a blank Maven Project created with STS 2.5.2.SR1 created using
the New Maven Project wizard checking the Create a simple project(skip archetype selection).

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>test</description>
</project>

Added some entries to the pom file to setup 1.6
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

Setup spring-data-jpa dependency
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.0.0.M2</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<repository>
<id>spring-milestone</id>
<name>Spring Maven MILESTONE Repository</name>
<url>http://maven.springframework.org/milestone</url>
</repository>

Setup persistence dependecies
<!-- Persistence dependencies -->
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${spring.framework.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.2.0</version>
<scope>runtime</scope>
</dependency>
<repository>
<id>EclipseLink Repo</id>
<url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
</repository>

Setup logging dependencies as discussed in the Spring Framework Reference Docs
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${sl4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${sl4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${sl4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>runtime</scope>
</dependency>

Setup Testing Dependencies
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.framework.version}</version>
<scope>test</scope>
</dependency>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8</version>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>        
<argLine>-javaagent:spring-instrument-3.0.5.RELEASE.jar</argLine>
</configuration>
</plugin>

I hope you found the post useful. You can subscribe via email or subscribe via a feed reader to get relevant updates from this blog. Have a nice day.

Friday, January 21, 2011

LXC(Linux Containers) on Ubuntu 10.04

I was able to get LXC(Linux Containers) to work on an Ubuntu 10.04 host system. Linux containers provide lightweight virtualization that lets you isolate processes and resources without the need to provide instruction interpretation mechanisms and other complexities of full virtualization. So if you need to quickly spin-up a virtual server to test a new version of rails or any other software then this post might help you do that.

For the impatient ones, I wrote a bash script which initializes a file system as well as create and runs a container instance. It can be found here and here. You still have to make changes to the host system yourself though before using the scripts.

To install the required tools do:
sudo apt-get install lxc bridge-utils debootstrap

Next is to setup cgroups with:
sudo mkdir /cgroup
sudo echo "none /cgroup cgroup defaults 0 0" >> /etc/fstab
sudo mount /cgroup

Then proceed to setup the network for bridge networking with:
sudo apt-get remove –purge network-manager network-manager-gnome

and then modify /etc/network/interfaces so it contains:
auto lo
iface lo inet loopback

auto br0
iface br0 inet static
address 192.168.0.10
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1
bridge_ports eth0
bridge_stp off
bridge_maxwait 5
post-up /usr/sbin/brctl setfd br0 0

then we setup the nameserver
sudo echo "nameserver 8.8.8.8" > /etc/resolv.conf

then reboot the host machine with:
sudo reboot

Log-in again and check that the network is still working with:
ping 8.8.8.8

and also
ping archive.ubuntu.com

Next we setup a root file system template that will be used by the containers. The following steps can be done easily using this script.

To start, we build a lucid root file system
sudo mkdir /lxc
sudo cd /lxc
sudo debootstrap --variant=minbase --arch i386 lucid rootfs_template

The next commands below will refer to placeholders so make sure to adjust the commands and replace them with the following:
$ROOTFS --> /lxc/rootfs_template
$MIRROR --> http://archive.ubuntu.com/ubuntu

to continue, we fix the device files
rm -rf $ROOTFS/dev
mkdir $ROOTFS/dev
mknod -m 666 $ROOTFS/dev/null c 1 3
mknod -m 666 $ROOTFS/dev/zero c 1 5
mknod -m 666 $ROOTFS/dev/random c 1 8
mknod -m 666 $ROOTFS/dev/urandom c 1 9
mkdir -m 755 $ROOTFS/dev/pts
mkdir -m 1777 $ROOTFS/dev/shm
mknod -m 666 $ROOTFS/dev/tty c 5 0
mknod -m 666 $ROOTFS/dev/tty0 c 4 0
mknod -m 666 $ROOTFS/dev/tty1 c 4 1
mknod -m 666 $ROOTFS/dev/tty2 c 4 2
mknod -m 666 $ROOTFS/dev/tty3 c 4 3
mknod -m 666 $ROOTFS/dev/tty4 c 4 4
mknod -m 600 $ROOTFS/dev/console c 5 1
mknod -m 666 $ROOTFS/dev/full c 1 7
mknod -m 600 $ROOTFS/dev/initctl p
mknod -m 666 $ROOTFS/dev/ptmx c 5 2

next we modify /etc/apt/sources.list so that it contains:
deb $MIRROR lucid main universe multiverse
deb $MIRROR lucid-security main universe multiverse

next we install and update the packages, run the following as root:
chroot $ROOTFS apt-get install --force-yes -y gpgv
chroot $ROOTFS apt-get update
chroot $ROOTFS apt-get install -y language-pack-en
chroot $ROOTFS locale-gen en_US.UTF-8
chroot $ROOTFS /usr/sbin/update-locale LANG="en_US.UTF-8" LANGUAGE="en_US.UTF-8" LC_ALL="en_US.UTF-8" LC_CTYPE="C"
chroot $ROOTFS apt-get install -y \
    adduser \
    apt-utils \
    console-setup \
    iproute \
    iptables \
    nano \
    netbase \
    openssh-blacklist \
    openssh-blacklist-extra \
    openssh-server \
    iputils-ping \
    rsyslog \
    sudo \
    ufw \
    vim
chroot $ROOTFS apt-get clean

as root user, setup mtab with:
chroot $ROOTFS ln -s /proc/mounts /etc/mtab

as root user, next we append the following to $ROOTFS/etc/environment:
LANG="en_US.UTF-8"
LANGUAGE="en_US.UTF-8"
LC_ALL="en_US.UTF-8"
LC_CTYPE="C"

as root user, next remove unneeded files from $ROOTFS/etc/init of the template system with:
cd $ROOTFS/etc/init
rm -f rc-sysinit.conf console* control* hwclock* module* mount* network-interface* plymouth* procps* tty{4,5,6}.conf udev* upstart*

as root user, create a $ROOTFS/etc/init/rc-sysinit file with the following content:
#!/bin/bash
rm -f $(find /var/run -name '*pid')
rm -f /var/lock/apache/*
exit 0

as root user, make it executable with:
chmod a+x $ROOTFS/etc/init/rc-sysinit

as root user, modify ssh.conf to contain:
# ssh - OpenBSD Secure Shell server
#
# The OpenSSH server provides secure shell access to the system.

description     "OpenSSH server"

start on startup
stop on runlevel S

expect fork
respawn
respawn limit 10 5
umask 022
# replaces SSHD_OOM_ADJUST in /etc/default/ssh
oom never

pre-start script
    test -x /usr/sbin/sshd || { stop; exit 0; }
    test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }
    test -c /dev/null || { stop; exit 0; }

    mkdir -p -m0755 /var/run/sshd
end script

# if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
# 'exec' line here instead
exec /usr/sbin/sshd

as root user, we create an archive of the template so we can reuse it:
cd /lxc
tar zcvf rootfs_template.tgz rootfs_template

To make things a lot easier, you can just use the bash script I wrote which can be downloaded from here. Save it in /lxc and make it executable and run it with:
sudo chmod +x /lxc/create-rootfs.sh
sudo cd /lxc
sudo ./create-rootfs.sh

To configure and run a container, download another bash script I wrote here and save it in /lxc.
Make it executable and run it with:
sudo chmod +x /lxc/create-container.sh
cd /lxc
sudo ./create-container.sh

I hope you found the post useful. You can subscribe via email or subscribe via a feed reader to get relevant updates from this blog. Have a nice day.

Friday, November 26, 2010

Lotto Number Generator for the Philippine Grandlotto 6/55

With the current jackpot price at about PHP 685 million, everyone and their mother is talking about the Philippine Grandlotto 6/55.

To help my friends and a few enemies choose numbers to bet on, I cobbled together a few lines of HTML and Javascript to build a Lotto Number Generator for the Philippine Grandlotto 6/55.

This tool is completely free. I have a few more features to add to it so questions, comments and suggestions are welcome. You can click the link below to go to

Lotto Number Generator for the Philippine Grandlotto 6/55



If you do win using a number that my system generated, please PLEASE feel free to donate and throw some of your spare change my way. If you don't like to, even with your millions, then you could at least thank me by acknowledging in public that you picked your winning number thru my system.

I hope you found the post useful. You can subscribe via email or subscribe via a feed reader to get relevant updates from this blog. Have a nice day.

Friday, November 19, 2010

Setting up CouchDB in Ubuntu Server 10.04 LTS on VirtualBox



In my day job, I was assigned the task of building a system that could handle 2 million rows of sales data a day. We are seriously considering putting couchdb to use. To even begin to evaluate couchdb, I of course needed to play around with it in a convenient test environment. Fortunately, as documented in my previous post on setting up an ubuntu machine, I have VirtualBox installed and ready.

To setup a couchdb environment, I first needed to create a virtual machine that runs Ubuntu Server 10.04 LTS. To begin, I downloaded the installer for Ubuntu Server 10.04 LTS from the Ubuntu website. I started VirtualBox and went thru the New Virtual Machine Wizard. I named my virtual machine as couchdb-server and accepted most of the defaults. Before starting the virtual machine, I adjusted the Storage settings of the virtual machine and configured the CD/DVD device to mount the Ubuntu Server 10.04 LTS installer(ISO). Starting the virtual machine will bring up the menu for the Ubuntu Server 10.04 LTS installer. I went thru the installation using the defaults. When asked what services to install, I just chose the OpenSSH server.

With the installation finished, I logged in and installed couchdb using:

sudo aptitude install couchdb

After a few screen fulls of text, couchdb was successfully installed. I then added a few lines to /etc/networking/interfaces to enable me to access the virtual server from the outside.

auto eth1
iface eth1 inet dhcp

Next, I turned off the virtual server using:

sudo shutdown -h now

I then modified the Network settings of the virtual machine using VirtualBox. I enabled Network Adapter 2 and set the Attached to drop down to Host-only Adapter.

I then powered up the virtual machine and after logging in, I checked the configured network devices using:

ifconfig

This should show eth1 to be configured and in my case, its IP address is 192.168.56.101

I then started couchdb with:

sudo /etc/init.d/couchdb start

To test couchdb, I opened a browser and accessed:

http://192.168.56.101:5984/_utils

I hope you found the post useful. You can subscribe via email or subscribe via a feed reader to get relevant updates from this blog. Have a nice day.