Tutorial

How To Install Apache Tomcat 8 on Ubuntu 14.04

Published on June 19, 2015
How To Install Apache Tomcat 8 on Ubuntu 14.04
Not using Ubuntu 14.04?Choose a different version or distribution.
Ubuntu 14.04

Introduction

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 the latest release of Tomcat 8 on your Ubuntu 14.04 server.

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-3 in the initial server setup for Ubuntu 14.04. We will be using the demo user created here for the rest of this tutorial.

Install Java

Tomcat requires that Java is installed on the server, so any Java web application code can be executed. Let’s satisfy that requirement by installing OpenJDK 7 with apt-get.

First, update your apt-get package index:

  1. sudo apt-get update

Then install the Java Development Kit package with apt-get:

  1. sudo apt-get install default-jdk

Answer y at the prompt to continue installing OpenJDK 7.

Now that Java is installed, let’s create a tomcat user, which will be used to run the Tomcat service.

Create Tomcat User

For security purposes, 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.

First, create a new tomcat group:

  1. sudo groupadd tomcat

Then create a new tomcat user. We’ll 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):

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

Now that our tomcat user is set up, let’s download and install Tomcat.

Install Tomcat

The easiest way to install Tomcat 8 at this time is to download the latest binary release then configure it manually.

Download Tomcat Binary

Find the latest version of Tomcat 8 at the Tomcat 8 Downloads page. At the time of writing, the latest version is 8.0.23. 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.

First, change to your home directory:

  1. cd ~

Then use wget and paste in the link to download the Tomcat 8 archive, like this (your mirror link will probably differ from the example):

  1. wget http://mirror.sdunix.com/apache/tomcat/tomcat-8/v8.0.23/bin/apache-tomcat-8.0.23.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:

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

Now we’re ready to set up the proper user permissions.

Update Permissions

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

Change to the Tomcat installation path:

  1. cd /opt/tomcat

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

  1. sudo chgrp -R tomcat conf
  2. sudo chmod g+rwx conf
  3. sudo chmod g+r conf/*

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

  1. sudo chown -R tomcat work/ temp/ logs/

Now that the proper permissions are set up, let’s set up an Upstart init script.

Install Upstart Script

Because we want to be able to run Tomcat as a service, we will set up an Upstart script.

Tomcat needs to know where Java was installed. This path is commonly referred to as “JAVA_HOME”. The easiest way to look up that location is by running this command:

  1. sudo update-alternatives --config java
Output:
There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java Nothing to configure.

The JAVA_HOME will be in the output, without the trailing /bin/java. For the example above, the JAVA_HOME is highlighted in red.

Now we’re ready to create the Upstart script. Create and open it by running this command:

  1. sudo nano /etc/init/tomcat.conf

Paste in the following script, and modify the value of JAVA_HOME if necessary. You may also want to modify the memory allocation settings that are specified in CATALINA_OPTS:

/etc/init/tomcat.conf
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-7-openjdk-amd64/jre
  env CATALINA_HOME=/opt/tomcat

  # Modify these options as needed
  env JAVA_OPTS="-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"
  env CATALINA_OPTS="-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

  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:

  1. sudo initctl reload-configuration

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

  1. 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:

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

You will see the default Tomcat splash page, in addition to other information. Now we will go deeper into the installation of Tomcat.

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:

  1. sudo nano /opt/tomcat/conf/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 (webapps that come with Tomcat). 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 initctl restart tomcat

Access the Web Interface

Now that Tomcat is up and running, let’s access the web management interface in a web browser. You can do this by accessing the public IP address of the server, on port 8080:

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

You will see something like the following image:

Tomcat root

As you can see, there are links to the admin webapps that we configured an admin user for.

Let’s take a look at the Manager App, 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 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 from.

Conclusion

Your installation of Tomcat is complete! Your are now free to deploy your own Java web applications!

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?
 
40 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 comment has been deleted

    There is a little issue on tomcat.conf file, it’s needed to remove all initial spaces in each line to be able to run initctl reload-configuration successfully, that was the only problem here. Thanks for the tutorial!

    I also get this error deploying a new app: Unable to create the directory [/opt/tomcat/webapps/ROOT] So, I guess that is needed to grant tomcat permissions to webapps folder, you can do this executing this command: sudo chown -R tomcat /opt/tomcat/webapp

    thanks, works for me!

    I got

    ~$ sudo initctl reload-configuration
    sudo: initctl: command not found
    

    I had the same problem, it’s an l not a 1.

    I had that problem too, it’s because tomcat can’t start using configuration from tomcat.conf. so my droplet has only 1GB memory and java in provided configuration ask for all that memory.

    so reduce memory consumption: env CATALINA_OPTS=“-**Xms256M **-**Xmx512M **-server -XX:+UseParallelGC”

    also check logs: var/log/upstart/ if it any errors now

    after, that i finally can run and open tomcat start page :)

    try init-checkconf /etc/init/tomcat.conf

    Same, I solved this by re-installing Ubuntu but this time choosing version 14.04. , had the latest version installed.

    …this might be due to initctl missing inside /sbin dir. Are you on Ubuntu 15.10? in 15.10 ubuntu switched from Upstart to systemd ref

    Hi, Very nice guide. But if I follow this, there will be no catalina.out created. Can that be fixed?

    tomcat.out is now in /var/log/upstart/tomcat.log. I’m still wondering where the base directory is (where is the application.log which is specified in logback.xml)

    Very nice and helpful tutorial I had some issues with creating the user for tomcat https://www.mulesoft.com/tcat/tomcat-linux helped me here, also I use JAVA from Oracle, so http://stackoverflow.com/questions/17287542/setting-java-home-path-on-ubuntu helped me here, too. Thanks for writing this Tutorial :-)

    Just in case, if anyone is looking for equivalent “Systemd” service script (e.g. to use on Ubuntu 15.04) , here is one I wrote:


    Filename:

    /etc/systemd/system/tomcat.service

    Content:

    [Unit] Description=Tomcat 8 Server [Service] Type=forking User=tomcat Group=tomcat EnvironmentFile=/etc/conf.d/tomcat ExecStart=/opt/tomcat/bin/catalina.sh run ExecStopPost=/bin/rm -rf /opt/tomcat/temp/* Restart=on-failure [Install] WantedBy=multi-user.target


    The environment file should be created and defined as follows:


    Filename:

    /etc/conf.d/tomcat

    Content:

    JAVA_HOME=/usr/lib/jvm/java-8-oracle CATALINA_HOME=/opt/tomcat JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC


    Relevant service enabling and start commands:

    sudo systemctl enable tomcat

    sudo systemctl start tomcat

    **FIX

    
    JAVA_HOME=/usr/lib/jvm/java-8-oracle
    CATALINA_HOME=/opt/tomcat
    JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom
    CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC
    

    hello.after creating a tomcat user, should not we change the user to tomcat somewhere?What is the password for tomcat user?After running tomcat, what should output be on console?

    Great tutorial! Thanks!

    I found that I could not deploy (server would start but the webpage would not load) unless I gave tomcat rights to the webapps folder too (as we did for conf):

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

    How to uninstall it?

    Important note: when you want to deploy a .war file don’t forget to change permissions… change “root” to “tomcat”, you can do you it that way : chown -R tomcat:tomcat *

    Very nice tutorial, I have installed java,tomcat and my sql server at three different machines with the help of this tutorial and in the installation process i didn’t face single problem.

    WGET mirror is returning 404.

    That’s because 8.0.23 it’s an older version, you just need to change it to 8.0.28 that is tha new version so it will be like this : wget http://mirror.sdunix.com/apache/tomcat/tomcat-8/v8.0.28/bin/apache-tomcat-8.0.28.tar.gz

    Tomcat 8 on Ubuntu 14.04, I had to add address=“0.0.0.0” in order to access site from outside localhost.

    <Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   address="0.0.0.0"
                   redirectPort="8443" />
    
    

    Great set of installation instructions, but I’m completely stumped … I got to the point where I give the “tomcat” user write access to the “conf” directory:

    sudo chgrp -R tomcat conf
    

    I get the following error:

    chgrp: cannot access ‘conf’: No such file or directory
    

    I’ve searched high and low (maybe not effectively) and can’t find the “conf” directory nor the “server.xml” or “modules.xml” files.

    Any ideas as to why this could be happening?

    Answered my own question! … turns out I installed the fulldocs binary instead of the core distribution. Feel silly.

    Great tutorial. Now that I installed tomcat and only the tomcat user can use it, I am facing issues while adding a server in eclipse. I get this error: Could not load Tomcat server configuration at /opt/tomcat/conf. (Permission denied). How do I fix that?

    I’m getting an error as below

    $ sudo initctl start tomcat
    initctl : Unknown Job : tomcat
    $
    

    same here

    Hi,

    I tried make this steps to install two tomcat 7 and 8.

    Here is my issue on stackoverflow: http://stackoverflow.com/questions/34169861/two-tomcat-in-same-server-upstart-script-ubuntu-server.

    Someone have some idea how to do this correctly?

    Thanks.

    Thank you so much. Worked perfectly for me! This is why I love digital ocean.

    Great tutorial. Thx a lot!

    Hi all

    I followed all the steps you are requested to leave with tomcat 8 I’ve done but I can not enter my username and password, this is what I put in /opt/tomcat/conf/tomcat-users.xml.

    <tomcat-users> <user username = “hcortes” password = “losnadie210” roles = “manager-gui, admin-gui” /> </ tomcat-users>

    But when I go to http: // localhost: 8080 / host-manager throws me this error (since I canceled because I do not accept the credentials) 401 Unauthorized

    what is the problem? please help …

    Hi all

    I followed all the steps you are requested to leave with tomcat 8 I’ve done but I can not enter my username and password, this is what I put in /opt/tomcat/conf/tomcat-users.xml.

    <tomcat-users> <user username = “hcortes” password = “losnadie210” roles = “manager-gui, admin-gui” /> </ tomcat-users>

    But when I go to http: // localhost: 8080 / host-manager throws me this error (since I canceled because I do not accept the credentials) 401 Unauthorized

    what is the problem? please help …

    Hi raasaund,

    You have a space in the roles string, this may be the issue.

    Yours: <user username = “hcortes” password = “losnadie210” roles = “manager-gui, admin-gui” />

    Updated: <user username=“hcortes” password=“losnadie210” roles=“manager-gui,admin-gui”/>

    If you are using OracleJDK than OpenJDK will not work. Make sure to download the correct JDK.

    Very nice guide. Thank you for sharing.

    I am using Ubuntu 15.10 which uses systemd. One might want to refer to this link, on the section configuring systemd.

    (https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-centos-7)

    I followed the steps one by one.

    I just changed the Open JDK to my Oracle 8 java version .

    Now when I start the server with sudo initctl start tomcat. I go to tomcat log and I see this problem: org.apache.catalina.startup.catalina load Unable to load server from Configuration [/opt/tomcat/conf/server.xml]

    I browse to /opt/tomcat/conf/ but I did not found the server.xml

    I am new to this world.

    Feb 21, 2016 5:13:40 PM org.apache.catalina.startup.Catalina load WARNING: Unable to load server configuration from [/opt/tomcat/conf/server.xml] Feb 21, 2016 5:13:40 PM org.apache.catalina.startup.Catalina load WARNING: Unable to load server configuration from [/opt/tomcat/conf/server.xml] Feb 21, 2016 5:13:40 PM org.apache.catalina.startup.Catalina start SEVERE: Cannot start server. Server instance is not configured. sudo: no tty present and no askpass program specified

    really really really helpful tutorial !

    I have learned lots of linux commands as well as installing jdk and tomcat in Ubuntu.

    Why do we need to make /opt/tomcat the tomcat user’s home directory?

    Thx a lot for your guide.

    I would like access the webapps deployed on tomcat by using a custom URL without explicit 8080 port. Is it even possible?

    I was able to link my custom domain ( say mydomain.com ) to digital ocean droplet.

    I could access webapp running on tomcat by using mydomain.com:8080.

    I do not want to use 8080 port in my custom url.

    I tried to change the port to 80 in server.xml but couldn’t start the server.

    How do i achieve this?

    Hi, how can I deploy my app without restart tomcat8? This is very important.

    Hi, Please suggest me the steps to create multiple instance of tomcat with in the same server. I have an application running in tomcat, for testing purpose i would go for another instance of same application. Not to changing the application name and deploying in the same server, i would like to go for new tomcat instance, however i have few more application need to be host in different instance.

    Thanks in Advance.

    #sudo initctl start tomcat tomcat start/running, process 10014

    ip_address:8080 No se puede conectar

    I did all the steps, but in this way I came out this errror . Thank you for your help.

    Great tutorial. Thank you.

    Thank you for the tutorial - excellent - really helped me!

    However, I was not able to upload and deploy a WAR file using the Choose File and Deploy buttons on the tomcat management page until I did the following:

    • my <username> was added to the tomcat group: sudo usermod -a -G tomcat <username>
    • tomcat group was given write permission for the /opt/tomcat/webapps directory: sudo chmod g+w /opt/tomcat/webapps
    • the group of the /opt/tomcat/webapps directory was changed to tomcat: sudo chown :tomcat /opt/tomcat/webapps

    Thanks again!

    The mirror location has changed and you need https. This worked for me in Jan 2017:

    wget https://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.39/bin/apache-tomcat-8.0.39.tar.gz
    

    This comment has been deleted

      This comment has been deleted

        This comment has been deleted

          This comment has been deleted

            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.