Tutorial

How To Install Apache Tomcat 7 on Ubuntu 14.04 via Apt-Get

Published on April 19, 2014
How To Install Apache Tomcat 7 on Ubuntu 14.04 via Apt-Get

About Apache Tomcat

Apache Tomcat is a web server and servlet container that is used to serve Java applications. Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, released by the Apache Software Foundation.

This tutorial covers the basic installation and some configuration of Tomcat 7 on your Ubuntu 14.04 server.

Note: Tomcat can be installed automatically on your Droplet by adding this script to its User Data when launching it. Check out this tutorial to learn more about Droplet User Data.

There are two basic ways to install Tomcat on Ubuntu:

  • Install through apt-get. This is the simplest method.
  • Download the binary distribution from the Apache Tomcat site. This guide does not cover this method; refer to Apache Tomcat Documentation for instructions.

For this tutorial, we will use the simplest method: apt-get. Please note that this will install the latest release of Tomcat that is in the official Ubuntu repositories, which may or may not be the latest release of Tomcat. If you want to guarantee that you are installing the latest version of Tomcat, you can always download the latest binary distribution.

Step One — Prerequisites

Before you begin with this guide, you should have a separate, non-root user account set up on your server. You can learn how to do this by completing steps 1-4 in the initial server setup for Ubuntu 14.04. We will be using the demo user created here for the rest of this tutorial.

Step Two - Install Tomcat

The first thing you will want to do is update your apt-get package lists:

  1. sudo apt-get update

Now you are ready to install Tomcat. Run the following command to start the installation:

  1. sudo apt-get install tomcat7

Answer yes at the prompt to install tomcat. This will install Tomcat and its dependencies, such as Java, and it will also create the tomcat7 user. It also starts Tomcat with its default settings.

Let’s make a quick change to the Java options that Tomcat uses when it starts. Open the Tomcat7 parameters file:

  1. sudo nano /etc/default/tomcat7

Find the JAVA_OPTS line and replace it with the following. Feel free to change the Xmx and MaxPermSize values—these settings affect how much memory Tomcat will use:

/etc/default/tomcat7 — JAVA_OPTS
JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom -Djava.awt.headless=true -Xmx512m -XX:MaxPermSize=256m -XX:+UseConcMarkSweepGC"

Save and exit.

Now restart Tomcat with this command:

  1. sudo service tomcat7 restart

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:

Open in web browser:
http://server_IP_address:8080

You will see a splash page that says “It works!”, in addition to other information. Now we will go deeper into the installation of Tomcat.

Step Three - Installing Additional Packages

Note: This section is not necessary if you are already familiar with Tomcat and you do not need to use the web management interface, documentation, or examples. If you are just getting into Tomcat for the first time, please continue.

With the following command, we will install the Tomcat online documentation, the web interface (manager webapp), and a few example webapps:

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

Answer yes at the prompt to install these packages. We will get into the usage and configuration of these tools in a later section. Next, we will install the Java Development Kit.

Step Four - Install Java Development Kit (Optional)

If you are planning on developing apps on this server, you will want to be sure to install the software in this section.

The Java Development Kit (JDK) enables us to develop Java applications to run in our Tomcat server. Running the following command will install openjdk-7-jdk:

  1. sudo apt-get install default-jdk

In addition to JDK, the Tomcat documentation suggests also installing Apache Ant, which is used to build Java applications, and a source control system, such as git. Let’s install both of those with the following command:

  1. sudo apt-get install ant git

For more information about Apache Ant, refer to the official manual. For a tutorial on using git, refer to DigitalCloud’s Git Tutorial.

Step 5 - Configure Tomcat Web Management Interface

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

  1. sudo nano /etc/tomcat7/tomcat-users.xml

This file is filled with comments which describe how to configure the file. You may want to delete all the comments between the following two lines, or you may leave them if you want to reference the examples:

tomcat-users.xml excerpt
<tomcat-users>
...
</tomcat-users>

You will want to add a user who can access the manager-gui and admin-gui (the management interface that we installed in Step Three). You 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.xml — Admin User
<tomcat-users>
    <user username="admin" password="password" roles="manager-gui,admin-gui"/>
</tomcat-users>

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

  1. sudo service tomcat7 restart

Step 6 - Access the Web Interface

Now that we’ve configured an admin user, let’s access the web management interface in a web browser:

Open in web browser:
http://server_IP_address:8080

You will see something like the following image:

Tomcat Splashscreen

As you can see, there are four links to packages you installed in Step Three:

  • tomcat7-docs: Online documentation for Tomcat. Accessible via http://server_IP_address:8080/docs/
  • tomcat7-examples: Tomcat 7 Servlet and JSP examples. You can click through the example webapps to get a basic idea of how they work (and also look at the source code to see how they were implemented). Accessible via http://server_IP_address:8080/examples/
  • tomcat7-admin (manager-webapp): Tomcat Web Application Manager. This will allow you to manage and your Java applications.
  • tomcat7-admin (host-manager): Tomcat Virtual Host Manager.

Let’s take a look at the Web Application Manager, accessible via the link or http://server_IP_address:8080/manager/html:

Tomcat Web Application Manager

The Web Application Manager is used to manage your Java applications. You can Start, Stop, Reload, Deploy, and Undeploy here. You can also run some diagnostics on your apps (i.e. find memory leaks). Lastly, information about your server is available at the very bottom of this page.

Now let’s take a look at the Virtual Host Manager, accessible via the link or http://server_IP_address:8080/host-manager/html/:

Tomcat Virtual Host Manager

From the Virtual Host Manager page, you can add virtual hosts to serve your applications in.

Finished!

Your installation of Tomcat is complete! Your are now free to deploy your own webapps!

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Mitchell Anicas
Mitchell Anicas
See author profile
Category:
Tutorial

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
36 Comments
Leave a comment...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

This tutorial is great… only problem: Tomcat7 takes roughly 5 minutes to start. Any idea why?

In looking at the Tomcat logs, I see:

INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [211,911] milliseconds.

Then, Google Foo helped me find:

http://wiki.apache.org/tomcat/HowTo/FasterStartUp

I’m considering a move to the Oracle JDK unless anyone thinks it’s worth digging around and changing the random stuff as described?

Actually, by setting the following in /etc/default/tomcat7, I was fine: <pre> JAVA_OPTS=“-Djava.security.egd=file:/dev/./urandom -Djava.awt.headless=true -Xmx1024m -XX:MaxPermSize=512m -XX:+UseConcMarkSweepGC” </pre>

Hello I have an issue with my tomcat. After completing the first half of the tutorial everything was fine. I edited the webpage to say “It CHANGED !” instead of “It Works!” and it seemed fine. Then after creating the admin user and restarted the server the page load took about 5 minutes to load the plain “It Works !” page that I only hardly modified.

I have now noticed that this happens every single time i restart the tomcat7 service.

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
June 12, 2014

@carsonwevans: To speed up the restarting of Tomcat, checkout Brock’s comment above.

As for losing your changes when restarting, what file exactly are you editing? <pre> /var/lib/tomcat7/webapps/ROOT/index.html </pre>

under example link WebSocket (JSR356) Examples are not working I am always getting Info: WebSocket connection closed. Can some one help ?

To avoid pulling in a bunch of X11 and Gnome packages…

$ sudo apt-get --no-install-recommends install default-jdk

Hi I successfully installed tomcat7 in the localhost but when i open “http://localhost:8080/manager/html” it asking to enter username and password, i am entered the username and password which are defined in “tomcat-users.xml”, but it didn’t display the manager page still asking username and password.

can any one help.

// this written in /etc/tomcat7/tomcat-users.xml ``<?xml version=‘1.0’ encoding=‘utf-8’?>

<tomcat-users> <role rolename=“manager-gui”/> <tomcat-user> <user username=“username” password=“password” roles=“manager-gui,admin-gui” /> </tomcat-user> </tomcat-users>```

I am also facing same issue as Krishna. Hi Krishna…did you find any resolution to this?

Hello,

Try to clear the browser history (cookies, etc), and then try again to access the manager page.

When tomcat started it hangs at

INFO: Initializing ProtocolHandler [“http-bio-8080”] Sep 03, 2014 12:18:41 PM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 1426 ms Sep 03, 2014 12:18:41 PM org.apache.catalina.core.StandardService startInternal INFO: Starting service Catalina Sep 03, 2014 12:18:41 PM org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet Engine: Apache Tomcat/7.0.52 (Ubuntu) Sep 03, 2014 12:18:41 PM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deploying web application directory /var/lib/tomcat7/webapps/ROOT

Any clue what is going on?

i have the same issue, searched everywhere but nothing…

As answered by DigitalOcean Support Joseph: "The issue stems from the use of /dev/random as its RNG, so app startup in Tomcat can be slow due to a lack of entropy (upwards of 5 minutes). You can correct this by modifying /etc/default/tomcat7. On the the JAVA_OPTS line, add the following:

-Djava.security.egd=file:/dev/./urandom"

This comment has been deleted

    What’s the best server requirement to install and run tomcat? Is 512MB/1 CPU sufficient for non demanding web app?

    Mitchell Anicas
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 17, 2014

    It depends on the app, and how much traffic you plan on getting. You can always just upgrade to a larger droplet if you find that you need more memory.

    Awesome tutorial!!! I’m up and running. Thanks :)…

    Hello. May I have a question, please?

    on the step 2. when I go to my ip:8080 it’s shown the message “It works!”

    But after I have done everything to step 6 and I tried to go to ip:8080 again

    but nothing is appear. It seem like a browser is loading the page all the time.

    Do you know how to fix this?

    Thank you in advance.

    Same with me. Did you resolve it?

    Try As answered by DigitalOcean Support Joseph: “The issue stems from the use of /dev/random as its RNG, so app startup in Tomcat can be slow due to a lack of entropy (upwards of 5 minutes). You can correct this by modifying /etc/default/tomcat7. On the the JAVA_OPTS line, add the following: -Djava.security.egd=file:/dev/./urandom”

    @vkolodrevskiy I just wait and restart my host. then it’s working fine. btw thank you for your solution :D

    I just installed tomcat7 using tutorial above, tomcat7 has been started but nothing is display in my browser http://my_ip_address:8080, any solution for this issue ?

    I used a script that i found on the internet for installing Tomcat, if i find it i will paste it here so it can help you.

    EDIT:

    Actually the script was for starting Tomcat, not installing. My mistake. :) Anyway here is the article. Maybe you will find useful.

    JAVA_OPTS=“-Djava.security.egd=file:/dev/./urandom -Djava.awt.headless=true -Xmx1024m -XX:MaxPermSize=512m -XX:+UseConcMarkSweepGC”

    try this solution given in above comments… it worked for me…

    Hi, does anyone know how to have tomcat serve X web application by default without the need to specify the ports? If I visit my droplet ip (without the :8080) Apache HTTP shows. I had created a droplet with LAMP to begin with, and then installed tomcat.

    Hi. The above suggestions for fixing the slow Tomcat startup by changing your JAVA_OPTS to use a new random number generator are helpful, but not perfect. If you use one of the above strings, you’re ham-handedly changing all your JAVA_OPTS rather than simply adding the one you need. Doing this worked for me instead:

    Inside $CATALINA_BASE/bin/catalina.sh, add the following (I added mine BELOW the existing commented-out “#JAVA_OPTS=” line at line # ~237):

    JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/./urandom"

    This will keep your existing JAVA_OPTS while adding the new random generator. Note: There is no need to uncomment anything. Just add the new line.

    This comment has been deleted

      very nice

      Congratulations… Very nice.

      Great article! Thanks !

      To install tomcat I searched many sites they cant provide complete installation procedure ,this tutorial provide all required procedure for complete installation … thank you thank you very much…

      Hai sir, i installed tomcat 7 as mention above commands, but i am facing HTTP Status 404 - /manager/html , can you please tell me how can i solve this problem.

      Mitchell Anicas
      DigitalOcean Employee
      DigitalOcean Employee badge
      March 30, 2015

      Are you accessing port 8080?

      Very Nice tutorial. I had many problems with the OpenJDK. With the Oracle JDK worked very well.

      Thanks !

      Great Tutorial, Works perfectly fine.

      Nice, its perfect!!!

      under example link WebSocket (JSR356) Examples are not working I am always getting Info: WebSocket connection closed. Can some one help ?

      Mitchell Anicas
      DigitalOcean Employee
      DigitalOcean Employee badge
      July 7, 2015

      Do you have Java 7 or higher installed?

      very nice keep it up

      Thanks for detailed guide sir. i installed tomcat 7 as mention above commands, but i am facing HTTP Status 401 - /manager/html ,many times it is asking authentication and failing. i cleared history also. can you please tell me how can i solve this problem. iam accessing through 8080 port and java version 1.8.0

      Mitchell Anicas
      DigitalOcean Employee
      DigitalOcean Employee badge
      September 28, 2015

      Did you set up the tomcat-users.xml file, as described in the tutorial? If not, be sure to do that then restart Tomcat.

      This comment has been deleted

        Awesome tutorial. Thank you so much. For a new person to UBUNTU i really appreciate the help!

        where can i find lib folder for tomcat7 … i need to add mysql jar to the lib folder of tomcat … but to my surprise /var/lib/tomcat7 doesnt contain any lib folder to add jars. please help

        For some reason I keep getting an error that it took too long to respond.

        I typed the url as follows: http://104.236.140.136:8080

        I currently have the following in my /etc/default/tomcat7 file:

        JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom -Djava.awt.headless=true -Xm$

        I looked in catalina.out and I didn’t see any errors. I do get a couple of warnings such as the ones below but I am not sure if that has anything to do with it.:

        Apr 14, 2016 12:49:29 PM org.apache.catalina.startup.ClassLoaderFactory validateFile WARNING: Problem with directory [/usr/share/tomcat7/common/classes], exists: [false], isDirectory: [false], canRead: [false] Apr 14, 2016 12:49:29 PM org.apache.catalina.startup.ClassLoaderFactory validateFile WARNING: Problem with directory [/usr/share/tomcat7/common], exists: [false], isDirectory: [false], canRead: [false] Apr 14, 2016 12:49:29 PM org.apache.catalina.startup.ClassLoaderFactory validateFile WARNING: Problem with directory [/usr/share/tomcat7/server/classes], exists: [false], isDirectory: [false], canRead: [false] Apr 14, 2016 12:49:29 PM org.apache.catalina.startup.ClassLoaderFactory validateFile WARNING: Problem with directory [/usr/share/tomcat7/server], exists: [false], isDirectory: [false], canRead: [false] Apr 14, 2016 12:49:29 PM org.apache.catalina.startup.ClassLoaderFactory validateFile WARNING: Problem with directory [/usr/share/tomcat7/shared/classes], exists: [false], isDirectory: [false], canRead: [false] Apr 14, 2016 12:49:29 PM org.apache.catalina.startup.ClassLoaderFactory validateFile WARNING: Problem with directory [/usr/share/tomcat7/shared], exists: [false], isDirectory: [false], canRead: [false]

        Here is a link to the entire log file if you are interested: https://www.dropbox.com/s/7zigiddrqblz799/catalina.out?dl=0

        Any ideas?

        It all works fine, and I get the tomcat page at http://server_IP_address:8080. I also configured my web app to listen to port 7777 and made a connector in server.xml so that it redirects port 80 to 7777. However, when I go to http://server_IP_address (no port), I still get the apache welcome screen. Going to http://server_IP_address:8080 I get my web app. How can I configure apache so that it goes to tomcat on port 80? I need that to publish my app in my domain.

        If localhost:8080 get blanck page…

        This could be due to the firewall on the Droplet. LAMP one clicks have all ports except 22 80 and 443 blocking for incoming traffic. Can you confirm that the port is opened on the Droplet for port 8080? This command will list your rules:

        sudo ufw status

        This command will open the port:

        sudo ufw allow 8080

        For more reading I recommend these documents on UFW:

        https://www.digitalocean.com/community/tutorials/ufw-essentials-common-firewall-rules-and-commands https://help.ubuntu.com/community/UFW

        How to save and exit?

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Become a contributor for community

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        DigitalOcean Documentation

        Full documentation for every DigitalOcean product.

        Resources for startups and SMBs

        The Wave has everything you need to know about building a business, from raising funding to marketing your product.

        Get our newsletter

        Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

        New accounts only. By submitting your email you agree to our Privacy Policy

        The developer cloud

        Scale up as you grow — whether you're running one virtual machine or ten thousand.

        Get started for free

        Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

        *This promotional offer applies to new accounts only.