How To Install Tomcat On Linux – Java Application Server Setup

Running Apache Tomcat on Linux requires installing Java first, then extracting the Tomcat archive and configuring environment variables. If you’re wondering how to install tomcat on linux, this guide walks you through every step with clear commands and practical tips. You’ll have a working Tomcat server in under 30 minutes, even if you’re new to Linux administration.

Tomcat is a popular open-source servlet container used to run Java web applications. Many developers and sysadmins rely on it for deploying apps like Jenkins, Jira, or custom Java-based projects. This tutorial covers installation on Ubuntu, Debian, CentOS, and RHEL distributions.

Prerequisites For Installing Tomcat On Linux

Before you start, make sure your Linux system meets these requirements:

  • A user account with sudo or root privileges
  • At least 2 GB of RAM (4 GB recommended for production)
  • Stable internet connection to download packages
  • Basic familiarity with the terminal and command-line tools

You also need to know your Linux distribution and package manager. Ubuntu and Debian use apt, while CentOS and RHEL use yum or dnf. Check your OS version with cat /etc/os-release.

How To Install Tomcat On Linux

This section covers the complete installation process. Follow each step carefully to avoid common pitfalls. We’ll install Java first, then download and configure Tomcat.

Step 1: Install Java Development Kit (JDK)

Tomcat requires Java 8 or later. OpenJDK 11 is recommended for most setups. Run these commands based on your distribution:

For Ubuntu/Debian:

sudo apt update
sudo apt install openjdk-11-jdk -y

For CentOS/RHEL:

sudo yum install java-11-openjdk-devel -y

Verify the installation:

java -version

You should see output similar to openjdk version "11.0.18". If not, check your package manager or add the appropriate repository.

Step 2: Create A Tomcat User

Running Tomcat as root is a security risk. Create a dedicated system user:

sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat

This creates a user named tomcat with a home directory at /opt/tomcat and no login shell. The -r flag makes it a system account.

Step 3: Download Apache Tomcat

Visit the official Apache Tomcat downloads page to get the latest stable version. As of this writing, Tomcat 10.1.x is current. Copy the link for the tar.gz file under “Core”.

Download it using wget or curl:

cd /tmp
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.20/bin/apache-tomcat-10.1.20.tar.gz

If you prefer a different version, replace the URL accordingly. Always verify the checksum from the official site.

Step 4: Extract The Archive

Extract the downloaded file to /opt/tomcat:

sudo tar -xzf apache-tomcat-10.1.20.tar.gz -C /opt/tomcat/

This creates a directory like /opt/tomcat/apache-tomcat-10.1.20. For easier management, create a symbolic link:

sudo ln -s /opt/tomcat/apache-tomcat-10.1.20 /opt/tomcat/latest

Now you can reference /opt/tomcat/latest in scripts and commands.

Step 5: Set Proper Permissions

Give the tomcat user ownership of the installation directory:

sudo chown -R tomcat: /opt/tomcat
sudo chmod +x /opt/tomcat/latest/bin/*.sh

The second command makes all shell scripts in the bin folder executable. This is required for starting and stopping Tomcat.

Step 6: Configure Environment Variables

Tomcat needs to know where Java is installed. Edit the setenv.sh script (create it if it doesn’t exist):

sudo nano /opt/tomcat/latest/bin/setenv.sh

Add these lines:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export CATALINA_HOME=/opt/tomcat/latest
export CATALINA_BASE=/opt/tomcat/latest

Adjust JAVA_HOME to match your JDK installation path. Find it with update-alternatives --list java or dirname $(dirname $(readlink -f $(which java))).

Make the script executable:

sudo chmod +x /opt/tomcat/latest/bin/setenv.sh

Step 7: Start Tomcat Manually

Test the installation by starting Tomcat:

sudo -u tomcat /opt/tomcat/latest/bin/startup.sh

Check the logs to confirm it started successfully:

tail -f /opt/tomcat/latest/logs/catalina.out

You should see a line like Server startup in [xxx] milliseconds. Open a web browser and navigate to http://your-server-ip:8080. You’ll see the default Tomcat welcome page.

Step 8: Create A Systemd Service

For automatic startup and easier management, create a systemd service file:

sudo nano /etc/systemd/system/tomcat.service

Add the following content:

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

[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

Reload systemd and enable the service:

sudo systemctl daemon-reload
sudo systemctl enable tomcat
sudo systemctl start tomcat

Check the status:

sudo systemctl status tomcat

You should see active (running). Now Tomcat starts automatically on system boot.

Step 9: Configure Firewall

If you have a firewall enabled, allow traffic on port 8080:

For Ubuntu/Debian (ufw):

sudo ufw allow 8080/tcp
sudo ufw reload

For CentOS/RHEL (firewalld):

sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

Test remote access by visiting http://your-server-ip:8080 from another machine.

Post-Installation Configuration

After completing the basic installation, you’ll want to secure and customize Tomcat for production use. These steps are optional but highly recommended.

Configure Tomcat Users And Roles

Edit the tomcat-users.xml file to add admin and manager users:

sudo nano /opt/tomcat/latest/conf/tomcat-users.xml

Add these lines inside the <tomcat-users> block:

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

Replace your-strong-password with a secure password. Save the file and restart Tomcat:

sudo systemctl restart tomcat

Enable Remote Access To Manager Apps

By default, the Manager and Host Manager apps only allow localhost access. To enable remote access, edit two files:

For Manager app:

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

Comment out the Valve section that restricts IP addresses:

<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

For Host Manager app:

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

Do the same commenting. Restart Tomcat after making changes.

Optimize Memory And Performance

Edit the setenv.sh file again to add JVM options:

sudo nano /opt/tomcat/latest/bin/setenv.sh

Add these lines:

export CATALINA_OPTS="-Xms512m -Xmx1024m -XX:+UseG1GC"

Adjust the heap sizes based on your server’s available RAM. The -Xms flag sets initial heap, -Xmx sets maximum heap.

Common Troubleshooting Tips

Even with careful setup, you might encounter issues. Here are solutions to frequent problems:

  • Port 8080 already in use: Run sudo netstat -tulpn | grep :8080 to find the process. Change Tomcat’s port in server.xml or stop the conflicting service.
  • Permission denied errors: Verify ownership with ls -la /opt/tomcat/latest/. The tomcat user should own everything.
  • Java not found: Double-check JAVA_HOME in setenv.sh. Use echo $JAVA_HOME as the tomcat user to test.
  • Service fails to start: Check logs with journalctl -u tomcat -n 50 or look at catalina.out.

Frequently Asked Questions

What Is The Difference Between Tomcat And Apache HTTP Server?

Apache HTTP Server is a general-purpose web server that serves static content. Tomcat is a servlet container designed to run Java web applications. They can work together, but Tomcat alone handles both static and dynamic Java content.

Can I Install Tomcat Without Root Access?

Yes, you can extract the archive to your home directory and run it as a non-root user. However, you won’t be able to use systemd for automatic startup or bind to ports below 1024 without extra configuration.

How Do I Change The Default Port From 8080 To 80?

Edit /opt/tomcat/latest/conf/server.xml and change the Connector port="8080" to port="80". You’ll need root privileges to bind to port 80. Alternatively, use a reverse proxy like Nginx.

Is Tomcat 10 Compatible With Older Java Versions?

Tomcat 10 requires Java 11 or later. For Java 8 compatibility, use Tomcat 9.x instead. Check the official documentation for version-specific requirements.

How Do I Deploy A WAR File To Tomcat?

Copy your .war file to /opt/tomcat/latest/webapps/. Tomcat automatically deploys it. You can also use the Manager app’s web interface for deployment.

Conclusion

You now know exactly how to install tomcat on linux from scratch. The process involves installing Java, downloading the Tomcat archive, setting up a dedicated user, and configuring systemd for automatic startup. With the additional security and performance tweaks, your Tomcat server is ready for development or production use.

Remember to regularly update Tomcat and Java for security patches. Monitor your server’s logs and resource usage to ensure smooth operation. If you run into any issues, the troubleshooting section above covers the most common problems. Happy deploying!