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 16.04 server.
Prerequisites
Before you begin with this guide, you should have a non-root user withsudo
privileges set up on your server. You can learn how to do this by completing ourUbuntu 16.04 initial server setup guide.
Step 1: Install Java
Tomcat requires Java to be installed on the server so that any Java web application code can be executed. We can satisfy that requirement by installing OpenJDK with apt-get.
First, update your apt-get package index:
sudo apt-get update
Then install the Java Development Kit package with apt-get:
sudo apt-get install default-jdk
Now that Java is installed, we can create atomcat
user, which will be used to run the Tomcat service.
Step 2: Install Tomcat
The best way to install Tomcat 8 is to download the latest binary release then configure it manually.
Find the latest version of Tomcat 8 at theTomcat 8 Downloads page. At the time of writing, the latest version is8.5.5, but you should use a later stable version if it is available. Under theBinary Distributionssection, then under theCorelist, copy the link to the "tar.gz".
Next, change to the/tmp
directory on your server. This is a good directory to download ephemeral items, like the Tomcat tarball, which we won't need after extracting the Tomcat contents:
cd /tmp
Usecurl
to download the link that you copied from the Tomcat website:
curl -O http://apache.mirrors.ionfish.org/tomcat/tomcat-8/v8.0.47/bin/apache-tomcat-8.0.47.tar.gz
We will install Tomcat to the/opt/tomcat
directory. Create the directory, then extract the archive to it with these commands:
sudo mkdir /opt/tomcat
sudo tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
Next, we can set up the proper user permissions for our installation.
Step 3: Create a systemd Service File
We want to be able to run Tomcat as a service, so we will set up systemd service file.
Tomcat needs to know where Java is installed. This path is commonly referred to as "JAVA_HOME". The easiest way to look up that location is by running this command:
sudo update-java-alternatives -l
Output
java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64
The correctJAVA_HOME
variable can be constructed by taking the output from the last column (highlighted in red) and appending/jre
to the end. Given the example above, the correctJAVA_HOME
for this server would be:
JAVA_HOME
/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre
YourJAVA_HOME
may be different.
With this piece of information, we can create the systemd service file. Open a file calledtomcat.service
in the/etc/systemd/system
directory by typing:
sudo nano /etc/systemd/system/tomcat.service
Paste the following contents into your service file. Modify the value ofJAVA_HOME
if necessary to match the value you found on your system. You may also want to modify the memory allocation settings that are specified inCATALINA_OPTS
:
/etc/systemd/system/tomcat.service
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=local'
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
User=root
Group=root
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
When you are finished, save and close the file.
Next, reload the systemd daemon so that it knows about our service file:
sudo systemctl daemon-reload
Start the Tomcat service by typing:
sudo systemctl start tomcat
Double check that it started without errors by typing:
sudo systemctl status tomcat
Step 4: Access the Web Interface
Now that we have create a user, we can access the web management interface again in a web browser. Once again, you can get to the correct interface by entering your server's domain name or IP address followed on port 8080 in your browser:
Open in web browser
http://server_domain_or_IP:8080
The page you see should be the same one you were given when you tested earlier:
Let's take a look at the Manager App, accessible via the link orhttp://server_domain_or_IP:8080/manager/html
. You will need to enter the account credentials that you added to thetomcat-users.xml
file. Afterwards, you should see a page that looks like this:
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 orhttp://server_domain_or_IP:8080/host-manager/html/
:
From the Virtual Host Manager page, you can add virtual hosts to serve your applications from.