You are currently viewing How to Install and configure Apache Tomcat server on Linux
Install and configure Apache Tomcat server on Linux

How to Install and configure Apache Tomcat server on Linux

In today’s dynamic web environment, efficient and reliable server solutions are essential for developers and system administrators. Apache Tomcat, an open-source implementation of the Java Servlet, JavaServer Pages, and Java Expression Language technologies, has become a very crucial tool in the deployment of Java-based web applications. This guide provides a comprehensive, step-by-step guide on how to install and configure Apache Tomcat server on a Linux system.

Step 1: Install Java

Tomcat requires Java to run. Install the default JDK:

sudo apt update
sudo apt install default-jdk

Step 2: Download and Install Tomcat

Download the latest version of Tomcat from the official Apache Tomcat website. At the time of writing, Tomcat 9 is a stable version. Get the latest Tomcat version

wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.89/bin/apache-tomcat-9.0.89.tar.gz

Extract the Tomcat Archive and move the files to the Tomcat directory:

tar -xzvf apache-tomcat-9.0.89.tar.gz #check on the file name and update as needed.
sudo mv apache-tomcat-9.0.89 /opt/tomcat   #move extracted folder & content to the tomcat folder.

Step 3: Configure Tomcat

  1. Create a Tomcat User: Run the following command to create a Tomcat user:
sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat
  1. Set Permissions: Update and set the required permissions for Tomcat to work as needed.
sudo chown -R tomcat: /opt/tomcat
sudo sh -c 'chmod +x /opt/tomcat/bin/*.sh'
  1. Create a Systemd Service File for Tomcat: Create the following file and add the content below.
sudo nano /etc/systemd/system/tomcat.service

#Add the blow code.

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/default-java
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=10
Restart=always

[Install]
WantedBy=multi-user.target
  1. Reload the Systemd Daemon, Start Tomcat, and finally check the status:
sudo systemctl daemon-reload
sudo systemctl start tomcat
sudo systemctl enable tomcat

sudo systemctl status tomcat

Step 4: Configure Tomcat Web Management Interface

This step is optional and may be skipped. To configure the Tomcat web management Interface, follow the guidelines below:

  1. Edit the Tomcat Users Configuration and add the code below:
sudo nano /opt/tomcat/conf/tomcat-users.xml
#Add the following lines inside the <tomcat-users> tags replacing admin and password with your preferred username and password:

<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="password" roles="manager-gui,admin-gui"/>

By default, the Host Manager is only accessible from localhost. To allow remote access, you need to modify the context.xml file to permit connections from remote IP addresses.

Modify the context.xml for the Host Manager

Edit the context.xml for the Host Manager and perform any of the following operations:

sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml

#Find the following section:
<Context ...>
    <Valve className="org.apache.catalina.valves.RemoteAddrValve"
           allow="127\.\d+\.\d+\.\d+|::1"/>
</Context>
#Comment out the value section to have the following:
<Context ...>
    <!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
           allow="127\.\d+\.\d+\.\d+|::1"/> -->
</Context>

# OR :: add the following and replace YOUR_IP_ADDRESS with your actual IP address or a regex pattern that matches your network.
<Context ...>
    <Valve className="org.apache.catalina.valves.RemoteAddrValve"
           allow="127\.\d+\.\d+\.\d+|::1|YOUR_IP_ADDRESS"/>
</Context>

Modify the context.xml for the Manager Application

Edit the context.xml for the Manager by implementing any of the below options:

sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml
#Find the following section:
<Context ...>
    <Valve className="org.apache.catalina.valves.RemoteAddrValve"
           allow="127\.\d+\.\d+\.\d+|::1"/>
</Context>

#Option 1 : Comment out the value section to have a block like follows
<Context ...>
    <!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
           allow="127\.\d+\.\d+\.\d+|::1"/> -->
</Context>

# OR :: option 2 :: upadte and replace YOUR_IP_ADDRESS with your actual IP address or a regex pattern that matches your network.
<Context ...>
    <Valve className="org.apache.catalina.valves.RemoteAddrValve"
           allow="127\.\d+\.\d+\.\d+|::1|YOUR_IP_ADDRESS"/>
</Context>

Finally, restart Tomcat:

sudo systemctl restart tomcat

To access the Tomcat web interface; Open your browser and navigate to http://your_server_ip:8080. You should be able to access the Tomcat Web Application Manager and the Host Manager using the credentials you configured.

Successfully installing and configuring Apache Tomcat on a Linux server marks a great step toward deploying robust and scalable Java-based web applications. Regular maintenance and updates will keep your server running smoothly and efficiently. With Tomcat’s powerful features and flexibility, you are well-equipped to handle a wide range of web application demands, making it an invaluable tool in your development world. For more information, check out the Tomcat official page.

Make a donation to support us


Web Hosting and email hosting Packages


For web development services, SEO services, Digital marketing strategies, website set up services, web hosting and domain registration; contact Dynamic Technologies.


Related content: