breaking into the unknown…

tomcat on ubuntu : install configure start stop


Tomcat is application server .  more detail on it is available here. In simple words, tomcat is container for memory resident java program called servlets. Since, servlets are memory-resident, they can quickly respond to requests, as they do not incur the overhead of process creation and subsequent cleanup, unlike CGI-based scripting, e.g. perl, etc.

Although, there is no context of JSP in this post, I just want to mention it here as servlets and jsp is at the core of java and if you have to do any thing with java you must know servlets and JSP. So,  JSP is comparable to other technologies such as PHP and ASP, which combine programming/scripting with a markup language like HTML. The key difference being the programming language of choice. For example, PHP uses a C/C++/Java hybrid, ASP uses VBScript, and JSP utilizes the full power of the Java programming language.

With, the above introduction we will now install and configure tomcat. I have referenced myself from ubuntu help center here.

STEP 1 -> Prerequisite :

NOTE : I’ am using ubuntu 12.04 and current tomcat version available with it is tomcat7 if you get any higher version say 8 then replace all 7 with the current version i,e say 8

$ which java # check that java is installed if not install it. I will write a post on it some other day.

/usr/bin/java # so our java is present at the given location, if you do not find any you need to install java before proceeding further.

NOTE : the /usr/bin/java is symlinked file i,e it just contain information about the location of jvm. You must know the location of your jvm. Depending on how you have installed java, it may be present at different location. In my case it is present at /usr/lib/jvm. Further, you may have multiple java installed within your jvm folder, you just need to specify one of them in your tomcat configuration. If you find it difficult to locate your jvm folder, you can try below in terminal.

Locating installed java on ubuntu

$ which java
/usr/bin/java # so you java is installed and present in /usr/bin/java
$ cd /usr/bin/ # let us move to /usr/bin
$ ls -l | grep java # list the file with name java
lrwxrwxrwx 1 root   root          22 Oct 16 14:20 java -> /etc/alternatives/java
lrwxrwxrwx 1 root   root          23 Oct 16 14:21 javac -> /etc/alternatives/javac
lrwxrwxrwx 1 root   root          25 Oct 16 14:21 javadoc -> /etc/alternatives/javadoc
lrwxrwxrwx 1 root   root          32 Oct 16 14:21 javafxpackager -> /etc/alternatives/javafxpackager
lrwxrwxrwx 1 root   root          23 Oct 16 14:21 javah -> /etc/alternatives/javah
lrwxrwxrwx 1 root   root          23 Oct 16 14:21 javap -> /etc/alternatives/javap
lrwxrwxrwx 1 root   root          25 Oct 16 14:20 java_vm -> /etc/alternatives/java_vm
lrwxrwxrwx 1 root   root          24 Oct 16 14:20 javaws -> /etc/alternatives/javaws

NOTE : the files are again symlinked file pointing to /etc/alternatives i,e we need to find 
our jvm there
$ cd /etc/alternatives/ #moving to /etc/alternatives

$ ls -l | grep java # listing java file within it
.......many more lines 
lrwxrwxrwx 1 root root  34 Oct 16 14:21 jar -> /usr/lib/jvm/java-7-oracle/bin/jar
lrwxrwxrwx 1 root root  39 Oct 16 14:20 java -> /usr/lib/jvm/java-7-oracle/jre/bin/java

NOTE : the files are again symlinked file pointing to /usr/lib/jvm/java-7-oracle. Now if you go 
to /usr/lib/jvm, you can find different installed java, in my case, I am having only java-7-oracle. 
So my JVM_HOME will be /usr/lib/jvm/java-7-oracle

$ sudo apt-get install libtomcat7-java # it install java related dependency of tomcat7

$ sudo apt-get install tomcat7-common  # it will install some other common dependency of tomcat

STEP 2 ->Installing Tomcat :

$ sudo apt-get install tomcat7 # the last few lines are, check them as it will tell you about any 
faults or other things you need to do

Unpacking tomcat7 (from .../tomcat7_7.0.26-1ubuntu1.1_all.deb) ...
Selecting previously unselected package authbind.
Unpacking authbind (from .../authbind_1.2.0build3_i386.deb) ...
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot
Processing triggers for man-db ...
Setting up libcommons-collections3-java (3.2.1-5) ...
Setting up libcommons-pool-java (1.5.6-1) ...
Setting up libcommons-dbcp-java (1.4-1ubuntu1) ...
Setting up libecj-java (3.5.1-3) ...
Setting up libservlet3.0-java (7.0.26-1ubuntu1.1) ...
Setting up libtomcat7-java (7.0.26-1ubuntu1.1) ...
Setting up tomcat7-common (7.0.26-1ubuntu1.1) ...
Setting up tomcat7 (7.0.26-1ubuntu1.1) ...

Creating config file /etc/default/tomcat7 with new version
Adding system user `tomcat7' (UID 120) ...
Adding new user `tomcat7' (UID 120) with group `tomcat7' ...
Not creating home directory `/usr/share/tomcat7'.
* no JDK found - please set JAVA_HOME
invoke-rc.d: initscript tomcat7, action "start" failed.
Setting up authbind (1.2.0build3) ...

Since the log tell us to set java home, we will set it

NOTE : In ubuntu anything you install through sudo apt-get command install it to /etc folder , so you can find a tomcat7 folder here. If you install anything through download package it will get installed to /usr/local folder

$ sudo su – # you need to be root user to edit /etc/default/tomcat7 file

$ nano /etc/default/tomcat7 # to use nano editor see here. You can use any other editor also to edit this file, but at end you should be able to add java_home to it


~# exit # exist as the root user

STEP 3 -> Starting tomcat

If you install any thing with sudo apt-get install the start file resides in /etc/init.d folder

$ sudo /etc/init.d/tomcat7 start #the other options are  stop restart try-restart force-reload and status

STEP 4 -> See it running in the browser

http://localhost:8080 # on browser will load the page with message its working.

If the port 8080 is allready in use by some other server, you can change it by changing below line in /etc/tomcat6/server.xml

<Connector port="8080" protocol="HTTP/1.1"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

Step 5 -> Installing standard webapps

Tomcat is shipped with webapps that you can install for documentation, administration or demo purposes.

$ sudo apt-get install tomcat7-docs # it will install the documentation for your reference

http://localhost:8080/docs/ # you can see the complete reference in your local browser onward

Step 6 -> Installing tomcat admin # will provide you a interface to administer tomact i,e start stop this

$ sudo apt-get install tomcat7-admin

Try to login to the admin page
http://localhost:8080/manager/html # it will ask you for username password  Access to the host-manager application is also protected by default:  you need to define a user with the role “admin” in  /etc/tomcat6/tomcat-users.xml  before you can access it.

add below line to this file

<role rolename="manager-gui" />
<user username="arun" password="happyarun" roles="manager-gui" />

restart the server

# sudo /etc/init.d/tomcat7 restart

reload the page and enter username and  password can see the admin page

For security reasons, the tomcat7 user cannot write to the /etc/tomcat7 directory by default. Some features in these admin webapps (application deployment, virtual host creation) need write access to that directory. If you want to use these features execute the following, to give users in the tomcat7 group the necessary rights:

$ sudo chgrp -R tomcat7 /etc/tomcat7
$ sudo chmod -R g+w /etc/tomcat7

STEP 7 -> Installing tomcat example package

The tomcat7-examples package contains two webapps that can be used to test or demonstrate Servlets and JSP features, which you can access them by default at http://yourserver:8080/examples. You can install them by entering the following command in the terminal prompt:

$ sudo apt-get install tomcat7-examples # after completion see the example app in browser as below

http://localhost:8080/examples/ # will show you the example web app

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

6 thoughts on “tomcat on ubuntu : install configure start stop

  1. Thank you! This saved me hours of headache. It works perfectly. I tried other methods and this worked great for Ubuntu 12.04 LTS. I was looking for an easy way to NOT run as privileged user (non root). I hope you do a tutorial on TC7 and APR setups.

  2. have done all settings on tomcat7 on ubuntu but servlet does not run.
    i have a folder named myserv in webapps
    in it i have WEB-INF AND BELOW it classes folder
    in classes i have my HelloWorld.class file
    my web.xml is ok.
    JAVA_HOME is set to /usr/lib/jvm/java-1.7.0-openjdk-i386
    still i get servlet not found error
    i changed JAVA_HOME in /etc /default/tomcat
    still the same

  3. mcanode00037@mcanode00037:~$ sudo /etc/init.d/tomcat7 status
    * Tomcat servlet engine is running with pid 3372,using java – /usr/lib/jvm/java-1.7.0-openjdk-i386
    the version of java used is also ok

  4. a hidden web.xml is created similar to the one i have created in WEB-INF. is it normal?

  5. hi there! thanks for your post! just wanted to point out that there are a couple of pointers to config files as /etc/tomcat6 that should be /etc/tomcat7… not big mistakes but just in case a nooby wasn’t able to figure it out…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s