Monday 1 November 2010

Ubuntu Server as Inexpensive Platform for J2EE

Due to the very small budget of the project I had to find a good but inexpensive platform. The development server is an old single core single CPU Pentium 4 pizza-box with 200 GB hard disk. The productive environment will be much better, but OS and middle-ware should work out-of-the-box and require a very little maintenance.

I decided to try with Ubuntu 10.10 Server because I was impressed by the essential and very good on-line documentation. The productive environment will be (later) installed with Ubuntu 10.04 LTS.

The installation is quite simple: download the ISO image, burn the CD-ROM and boot the server from the CD. There is no special task to do, but be sure the server has a working internet connection and, if you work in a LAN, the DHCP server have to be reachable. I did choose to install OpenSSH and PostgreSQL as additional services.

At the end of the installation I performed an update of the packages (sudo apt-get update/upgrade). After reboot (due to new kernel) Tomcat (6.0.28) and PostgreSQL are up and running. Low memory usage and no unwanted services.

Second Hard-disk

Since the installer takes care of the first disk only I have to prepare and mount the second disk:
  1. Using fdisk (fdisk /dev/sdb) delete all existing partitions (if any).
  2. Save new (empty) partition table (using w).
  3. Add new partition(s) (using n).
  4. Using mkfs (e.g. mkfs -t ext4 /dev/sdb1) format the partition(s).
  5. Using tune2fs (e.g. tune2fs -L /data /dev/sdb1) label the partition(s).
  6. Create the mount point(s) (e.g. mkdir /data).
  7. Modify the file /etc/fstab adding one line per partition (e.g.)
    /dev/sdb1 /data ext4 defaults 1 2
  8. Mount the partition(s) (e.g. mount /data).

Tomcat

First add the admin and the manager roles to the file /etc/tomcat6/tomcat-users.xml and add the role to the tomcat user.

<?xml version="1.0"?>
<tomcat-users>
...
  <role rolename="admin"/>
  <role rolename="manager"/>
  <user username="tomcat" password="tomcat" roles="admin,manager"/>
</tomcat-users>

Restart Tomcat (/etc/init.d/tomcat6 restart).

Using this configuration I may access the manager (http://myserver:8080/manager/html) to quickly (re)deploy my application(s).

PostgreSQL

Perform the following setup (copied from the Ubuntu official documentation):
"Now that we can connect to our PostgreSQL server, the next step is to set a password for the postgres user. Run the following command at a terminal prompt to connect to the default PostgreSQL template database:"
sudo -u postgres psql template1 
"The above command connects to PostgreSQL database template1 as user postgres. Once you connect to the PostgreSQL server, you will be at a SQL prompt. You can run the following SQL command at the psql prompt to configure the password for the user postgres."
ALTER USER postgres with encrypted password 'your_password';
"After configuring the password, edit the file /etc/postgresql/8.4/main/pg_hba.conf to use MD5 authentication with the postgres user":
local all postgres md5
"Finally, you should restart the PostgreSQL service to initialize the new configuration. From a terminal prompt enter the following to restart PostgreSQL":
sudo /etc/init.d/postgresql-8.4 restart

Apache2

The Apache WEB server wasn't installed by default (maybe I omitted to include it during the installation). Anyway with
sudo apt-get install apache2
the service was installed and started.

In order to use it as reverse-proxy there is some configuration work to do. The first step is to enable the proxy module:

user@myserver: cd /etc/apache2/mods-enabled
user@myserver: sudo ln -s ../mods-available/proxy.conf proxy.conf
user@myserver: sudo ln -s ../mods-available/proxy_http.load proxy_http.load
user@myserver: sudo ln -s ../mods-available/proxy.load proxy.load

The second step is to edit the proxy configuration (proxy.conf) as shown above:

<IfModule mod_proxy.c>

# If you want to use apache2 as a forward proxy, uncomment the
# 'ProxyRequests On' line and the <Proxy *> block below.
# WARNING: Be careful to restrict access inside the <Proxy *> block.
# Open proxy servers are dangerous both to your network and to the
# Internet at large.
#
# If you only want to use apache2 as a reverse proxy/gateway in
# front of some web application server, you DON'T need
# 'ProxyRequests On'.

ProxyRequests Off
<Proxy *>
  AddDefaultCharset off
  Order deny,allow
  Allow from all
</Proxy>

# Enable/disable the handling of HTTP/1.1 "Via:" headers.
# ("Full" adds the server version; "Block" removes all outgoing Via: headers)
# Set to one of: Off | On | Full | Block
#ProxyVia Off

</IfModule>

The third step is to edit the configuration of the default site (/etc/apache2/sites-available/default) as follows:

<VirtualHost *:80>
  ServerAdmin me@work

  ProxyPreserveHost On
  ProxyPass /manager/html !
  ProxyPass host-manager/html !
  ProxyPass / http://localhost:8080/
  ProxyPassReverse / http://localhost:8080/
  ServerName myserver

  ErrorLog ${APACHE_LOG_DIR}/error.log

  # Possible values include: debug, info, notice, warn, error, crit,
  # alert, emerg.
  LogLevel warn

  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

At the end check the configuration and restart the apache2:
user@myserver: apachectl configtest
user@myserver: sudo /etc/init.d/apache2 restart

Static IP

The last task I had to do was to move the server from DHCP to a static address. Be aware: if you follows this instructions you have to be logged-in at the console of the server.

I did stop the network interface (sudo ifdown eth0) and start to edit the file /etc/resolv.conf as follow:

nameserver 192.168.1.11
domain mydomain
search mydomain

The next file to edit is /etc/network/interfaces:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
# auto eth0
# iface eth0 inet dhcp
auto eth0
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.1.1
#
# The secondary network interface
#
# auto eth1
# iface eth1 inet dhcp

I did restart the network interface (sudo ifup eth0) and the server was reachable through the static address.

Conclusion

After an hour I was able to start deploying my application on this recycled server. It is essential but enough for many small and mid sized applications. The same work may be done on an Amazon EC2 instance or inside VMWare.

2 comments:

  1. Really nice. Thank you for the step by step instructions.

    You may want to mention that the Apache2 reverse-proxy is required to map requests to the privileged port on your server (which is 80) to the unprivileged port on which you tomcat server runs (8080 usually).

    ReplyDelete