Sunday, January 10, 2016

Install Apache Tomcat 8.0.30 on Ubuntu 14.04


First install Java 8; for DHIS2 version 2.21 Java 8 and Tomcat 8 is required.

Create Tomcat User

For security reason, Tomcat should be run as an unprivileged user (i.e. not root). We will create a new user and group that will run the Tomcat service. if not exist,  create a new tomcat group by executing;

sudo groupadd tomcat

Then create a new tomcat user. Make this user a member of the tomcat group, with a home directory of /opt/tomcat (where we will install Tomcat), and with a shell of /bin/false (so nobody can log into the account):

sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Download Tomcat Binary

Find the latest version of Tomcat 8 at the Tomcat 8 Downloads page. We now have latest version 8.0.30. Under the Binary Distributions section, then under the Core list, copy the link to the "tar.gz".

Let's download the latest binary distribution to our home directory.

Then use wget and paste in the link to download the Tomcat 8 archive, like this:

wget http://www.apache.org/dist/tomcat/tomcat-8/v8.0.30/bin/apache-tomcat-8.0.30.tar.gz

We're going to install Tomcat to the /opt/tomcat directory. Create the directory, then extract the the archive to it with these commands:

sudo mkdir /opt/tomcat
sudo tar xvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1

Now we have to setup the proper user permissions.

Update Permissions

The tomcat user that we set up needs to have the proper access to the Tomcat installation. 

Change to the Tomcat installation path:

cd /opt/tomcat

Then give the tomcat user write access to the conf directory, and read access to the files in that directory:

sudo chgrp -R tomcat conf
sudo chmod g+rwx conf
sudo chmod g+r conf/*

Then make the tomcat user the owner of the work, temp, and logs directories:

sudo chown -R tomcat work/ temp/ logs/

Now that the proper permissions are setup, we have to setup init script.

Setup Init Script

We want to run Tomcat as a service so we have to setup an Upstart script. Create and open it by running this command:

sudo nano /etc/init/tomcat.conf

Paste in the following script, and modify the value as per your requirement. JAVA_HOME should be as per your JVM installation. You may also want to modify the memory allocation settings as per your requirement. My one looks like:

description "Tomcat Server"

  start on runlevel [2345]
  stop on runlevel [!2345]
  respawn
  respawn limit 10 5

  setuid tomcat
  setgid tomcat

  env JAVA_HOME=/usr/lib/jvm/java-8-oracle
  env CATALINA_HOME=/opt/tomcat

  # Modify these options as needed

  env JAVA_OPTS='-Xmx7500m -Xms4000m'
  env DHIS2_HOME='/home/dhis/config'
  
  exec $CATALINA_HOME/bin/catalina.sh run

  # cleanup temp directory after stop
  post-stop script
    rm -rf $CATALINA_HOME/temp/*
  end script

Save and exit. This script tells the server to run the Tomcat service as the tomcat user, with the settings specified. It also enables Tomcat to run when the server is started.

Now let's reload the Upstart configuration, so we can use our new Tomcat script:

sudo initctl reload-configuration

Tomcat is ready to be run. Start it with this command:

sudo initctl start tomcat

Tomcat is not completely set up yet, but you can access the default splash page by going to your domain or IP address followed by :8080 in a web browser.You will see the default Tomcat splash page, in addition to other information. 

Configure Tomcat Web Management Interface

In order to use the manager webapp that comes with Tomcat, we must add a login to our Tomcat server. We will do this by editing the tomcat-users.xml file:

sudo nano /opt/tomcat/conf/tomcat-users.xml

This file is filled with comments which describe how to configure the file. We may delete all the comments between the following two lines, or we may leave them. We want to add a user who can access the manager-gui. We can do so by defining a user similar to the example below. Be sure to change the username and password to something secure:

<tomcat-users>
    <user username="admin" password="password" roles="manager-gui"/>
</tomcat-users>

Save and quit the tomcat-users.xml file. To put our changes into effect, restart the Tomcat service:

sudo initctl restart tomcat


Monday, January 4, 2016

Install Java 8 and Tomcat 7 as service for DHIS2

Java installation

Oracle Java 8 JDK is the recommended Java option as it provides the greates operating system support, including Ubuntu LTS 14.04. The webupd8team Java PPA provides the necessary packages.

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

Check that your installation is okay by

java -version

You can also ensure that the appropriate environment variables are set by installing this package

sudo apt-get install oracle-java8-set-default

Install Tomcat

To install the Tomcat servlet container install Tomcat package by invoking:

sudo apt-get install tomcat7

Install tomcat admin package and other common package

sudo apt-get install tomcat7-docs tomcat7-admin tomcat7-examples

Next edit the environment file by 

sudo nano /usr/share/tomcat7/bin/setenv.sh

Add the lines below. The first line will set the location of your Java Runtime Environment, the second will dedicate memory to Tomcat and the third will set the location for where DHIS 2 will search for the dhis.conf configuration file. 

export JAVA_HOME='/usr/lib/jvm/java-8-oracle/'
export JAVA_OPTS='-Xmx7500m -Xms4000m'
export DHIS2_HOME='/home/dhis/config'

If tomcat service not started, uncomment the JAVA_HOME entry in /etc/default/tomcat7 and adjust the path accordingly. For our case update JAVA_HOME=/usr/lib/jvm/java-8-oracle.