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 CentOS 7 server.
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 CentOS 7. We will be using the
demo user created here for the rest of this tutorial.
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 yum.
To install OpenJDK 7 JDK using yum, run this command:
- sudo yum install java-1.7.0-openjdk-devel
y at the prompt to continue installing OpenJDK 7.
Note that a shortcut to the JAVA_HOME directory, which we will need to configure Tomcat later, can be found at
Now that Java is installed, let’s create a
tomcat user, which will be used to run the Tomcat service.
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
- 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):
- sudo useradd -M -s /bin/nologin -g tomcat -d /opt/tomcat tomcat
Now that our
tomcat user is set up, let’s download and install Tomcat.
The easiest way to install Tomcat 8 at this time is to download the latest binary release then configure it manually.
Find the latest version of Tomcat 8 at the Tomcat 8 Downloads page. At the time of writing, the latest version is 8.5.37. 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 using
wget using the
yum package manager:
- sudo yum install wget
Then, change to your home directory:
- cd ~
wget and paste in the link to download the Tomcat 8 archive, like this (your mirror link will probably differ from the example):
- wget https://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.37/bin/apache-tomcat-8.5.37.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:
- sudo mkdir /opt/tomcat
- 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.
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:
- cd /opt/tomcat
tomcat group ownership over the entire installation directory:
- sudo chgrp -R tomcat /opt/tomcat
Next, give the
tomcat group read access to the
conf directory and all of its contents, and execute access to the directory itself:
- sudo chmod -R g+r conf
- sudo chmod g+x conf
Then make the
tomcat user the owner of the
- sudo chown -R tomcat webapps/ work/ temp/ logs/
Now that the proper permissions are set up, let’s set up a Systemd unit file.
Because we want to be able to run Tomcat as a service, we will set up a Tomcat Systemd unit file .
Create and open the unit file by running this command:
- sudo vi /etc/systemd/system/tomcat.service
Paste in the following script. You may also want to modify the memory allocation settings that are specified in
# Systemd unit file for tomcat [Unit] Description=Apache Tomcat Web Application Container After=syslog.target network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib/jvm/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=/bin/kill -15 $MAINPID User=tomcat Group=tomcat UMask=0007 RestartSec=10 Restart=always [Install] WantedBy=multi-user.target
Save and exit. This script tells the server to run the Tomcat service as the
tomcat user, with the settings specified.
Now reload Systemd to load the Tomcat unit file:
- sudo systemctl daemon-reload
Now you can start the Tomcat service with this
- sudo systemctl start tomcat
Check that the service successfully started by typing:
- sudo systemctl status tomcat
If you want to enable the Tomcat service, so it starts on server boot, run this command:
- sudo systemctl enable 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.
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
- sudo vi /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> ... </tomcat-users>
You will want to add a user who can access the
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> <user username="admin" password="password" roles="manager-gui,admin-gui"/> </tomcat-users>
Save and quit the tomcat-users.xml file.
By default, newer versions of Tomcat restrict access to the Manager and Host Manager apps to connections coming from the server itself. Since we are installing on a remote machine, you will probably want to remove or alter this restriction. To change the IP address restrictions on these, open the appropriate
For the Manager app, type:
- sudo vi /opt/tomcat/webapps/manager/META-INF/context.xml
For the Host Manager app, type:
- sudo vi /opt/tomcat/webapps/host-manager/META-INF/context.xml
Inside, comment out the IP address restriction to allow connections from anywhere. Alternatively, if you would like to allow access only to connections coming from your own IP address, you can add your public IP address to the list:
<Context antiResourceLocking="false" privileged="true" > <!--<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />--> </Context>
Save and close the files when you are finished.
To put our changes into effect, restart the Tomcat service:
- sudo systemctl restart tomcat
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:
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
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
From the Virtual Host Manager page, you can add virtual hosts to serve your applications from.
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.
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!
Click below to sign up and get $200 of credit to try our products over 60 days!
some add, you must set permission for directory /bin too, before i do this im have error “Failed at step EXEC spawning /usr/share/tomcat8/bin/startup.sh: Permission denied”
This was excellent help for me.
I have a suggestion and an issue.
To search for what Java versions are available, use: $sudo yum search java | grep openjdk | more
One issue: I could not access the default Tomcat website on port 8080.
This is what I did:
Open the firewall: $ sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent $ sudo firewall-cmd --reload
The first command uses --permanent. You are defining this port to be open after a reboot or reload. The second command reload the firewall. You can check on the status of the ports by using: $ sudo firewall-cmd --zone=public --list-ports On my system, I get: 80/tcp 8080/tcp
Environment: Tomcat 8.0.28, Java 1.8.0.
I deployed Tomcat as described, but i ran in problems.
Tomcat didn’t start as a service. I only got an empty log file “catalina.out”.
Okay, let’s start Tomcat in the shell. I ran “./startup.sh” (Which is “./catalina.sh start”) as root. I got the return from the script:
Looks good, but it isn’t true, Tomcat was not started. Again, i got an empty log file.
Next, i tried “./catalina.sh run” and Tomcat started in the actual window. It looked very good. My configuration from “setenv.sh” was working and the Web UI was working too.
Later on i executed “./startup.sh” with Midnight Commander by mistake and Tomcat started!
I asked the same question at Stackoverflow with no repsonse ;-) Stackoverflow
If anyone has trouble with tomcat not starting after
You should comment out or change CATALINA_OPTS in tomcat.service.
Thanks for the tutorial. Works like a charm… (we are using java 1.8)
Is there anyway you can setup 2 tomcat instances in 1 machine ?
I suggest changing the ExecStop command with:
For a clean shutdown.
I follow all the steps and my tomcat server run fine,
i have problem with the latin characters ‘Ñáéí…’ they can not display ok.
Hi, tomcat service is not getting started. How can I check whats going wrong here. sudo systemctl status tomcat ● tomcat.service - Apache Tomcat Web Application Container Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled) Active: activating (auto-restart) (Result: exit-code) since Mon 2017-04-17 15:54:56 UTC; 4s ago Process: 379 ExecStop=/bin/kill -15 $MAINPID (code=exited, status=1/FAILURE) Process: 367 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS) Main PID: 378 (code=exited, status=127)
Apr 17 15:54:56 martmonkey systemd: tomcat.service: control process exited, code=exited status=1 Apr 17 15:54:56 martmonkey systemd: Unit tomcat.service entered failed state. Apr 17 15:54:56 martmonkey systemd: tomcat.service failed. [admin@martmonkey tomcat]$
I am also getting permission denied with tomcat user. tomcat]$ cd bin -bash: cd: bin: Permission denied
Hi, the blog is really good, but I have a little problem, I followed all the instructions, but when i arrive at the step to start tomcat whit the instruction "
$ sudo systemctl start tomcatand than check the status, it says that is inactive. What can I do?? Thanks for helping me