Table of Contents
- Installing Java
- Create Tomcat User
- Installing Tomcat
- Create a Systemd Service File
- Allow Tomcat Through the Firewall
- Configure Tomcat Web Management Console
- Access Tomcat Web Management Console
Tomcat is a widely used open source application and web server developed by the Apache Software Foundation. Tomcat is lightweight, agile, simple to use, and has a very large ecosystem of add-ons. Tomcat is used to serve Java applications and provides a Java HTTP web server environment in which Java code can run. The main goal of the Tomcat server is to provide a good web environment to run Java applications in comparison to other web servers.
This article will walk you through the installation and configuration of Tomcat 8 on a server running Ubuntu 16.04.
- A server running Ubuntu 16.04.
- A non-root user with sudo privileges configured.
Before you begin you will need to update your apt-get package lists. You can update these with the following command:
sudo apt-get update
Java Development Kit (JDK) can be used to run Java web application, you can install Java Development Kit (JDK) package with the following command:
sudo apt-get install default-jdk
Once Java is installed, you can verify the installed Java version using the following command:
You should see the following output:
java version "1.8.0_74" Java(TM) SE Runtime Environment (build 1.8.0_74-b02) Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)
Create Tomcat User
It is recommend that Tomcat run as an unprivileged user for security purposes. You will need to create a new user and group that will run the Tomcat service.
To create a tomcat group, run:
sudo groupadd tomcat
Next, create a new tomcat user and make this user member of the tomcat group with home directory
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Once everything is up-to-date, download the latest version of Tomcat from the Tomcat 8 Downloads page.
wget command to download the Tomcat source:
Extract the Tomcat source to your home directory:
sudo tar -xzvf apache-tomcat-8.5.5.tar.gz
Move the extracted content,
sudo mv apache-tomcat-8.5.5 /opt/tomcat
Next you will need to give proper permission to the tomcat user to access to the Tomcat installation.
sudo chgrp -R tomcat /opt/tomcat sudo chown -R tomcat /opt/tomcat sudo chmod -R 755 /opt/tomcat
Create a systemd Service File
Now you will need to create a systemd service file to manage the Tomcat process so you can run Tomcat as a service.
Create the systemd service file,
tomcat.service, inside the
sudo nano /etc/systemd/system/tomcat.service
Add the following content:
[Unit] Description=Apache Tomcat Web Server After=network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib/jvm/java-8-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' ExecStart=/opt/tomcat/bin/startup.sh ExecStop=/opt/tomcat/bin/shutdown.sh User=tomcat Group=tomcat UMask=0007 RestartSec=15 Restart=always [Install] WantedBy=multi-user.target
Save the file and reload the systemd daemon with the following command:
sudo systemctl daemon-reload
Now start the Tomcat service and check the status:
sudo systemctl start tomcat sudo systemctl status tomcat
● tomcat.service - Apache Tomcat Web Server Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled) Active: active (running) since Fri 2016-10-28 15:32:57 IST; 5s ago Process: 10040 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS) Main PID: 10050 (java) CGroup: /system.slice/tomcat.service └─10050 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.log Oct 28 15:32:57 Node1 systemd: Starting Apache Tomcat Web Server... Oct 28 15:32:57 Node1 systemd: Started Apache Tomcat Web Server.
Configure the Tomcat service to start during boot:
sudo systemctl enable tomcat
Allow Tomcat Through the Firewall
By default Tomcat runs on port 8080, so you will need to allow this port through the firewall.
You can do this by running the following command:
sudo ufw allow 8080
Once you are finished, you can proceed to test Tomcat.
It is time to test Tomcat. Open your web browser and type the URL http://your-server-ip:8080 and you should see the Tomcat default splash page:
Configure Tomcat Web Management Console
You will also need to add a set of login credentials to access the Manager App that comes with Tomcat.
You can do this by editing the
sudo nano /opt/tomcat/conf/tomcat-users.xml
Add a user who can access the manager-gui and admin-gui. To do this, add the following line above </tomcat-users>:
<role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="username" password="password" roles="manager-gui,admin-gui"/>
Save and close the file.
You will also need to edit
context.xml file, if you want to access Tomcat from a remote machine:
To allow access to Manager App open the following file:
sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml
Add / Edit your remote machine IP address as shown below:
To allow access to Host Manager App open the following file:
sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
Add / Edit your remote machine IP address as shown below:
Now save and close the file and restart the Tomcat service:
sudo systemctl restart tomcat
Access Tomcat Web Management Console
Once everything is configured, it is time to access the Tomcat web management console.
To access the Tomcat Manager App, open your web browser and type the URL http://your-server-ip/manager/html. There will be an authentication form that pops up. Input your username and the password that you already set up in
Tomcat Manager Login Page
Tomcat Manager Home Page
To access the Tomcat Host Manager App, open your web browser and type the URL http://your-server-ip/host-manager/html. There will be an authentication form pop up, input your username and password that you already setup in
Tomcat Host Manager Login Page
Tomcat Host Manager Home Page
Congratulations! You have successfully installed Apache Tomcat on your server. You can now deploy Java based applications on the server. Please visit the official Apache Tomcat website for additional help and useful information.