Apache Tomcat is an open-source implementation of Java Servlets, JavaServer Pages (JSP), and Java Expression Language. It’s widely used for running web applications that require Java. Here’s a step-by-step guide to installing and configuring Apache Tomcat on a Linux server, with a focus on Ubuntu and other Debian-based systems.
1. Installing Java
Apache Tomcat requires Java to run. If you don’t already have Java installed, use the OpenJDK package, the default Java package on Ubuntu.
Update the system repositories #
Start by updating your repositories to ensure you have access to the latest packages:sudo apt update
sudo apt update
Install Java #
To install the default Java Development Kit (JDK), run:
sudo apt install default-jdk
After the installation, verify it by checking the version:
java -version
2. Setting Up a Tomcat User
It’s not recommended to run Tomcat as the root user. Instead, create a dedicated user for Tomcat with restricted privileges:
sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat
This creates a system user and group named tomcat
with the home directory set to /opt/tomcat
.
3. Downloading Apache Tomcat
Download the latest Tomcat package (version 9.0.34 in this case) from the official website using wget
:
wget -c https://downloads.apache.org/tomcat/tomcat-9/v9.0.34/bin/apache-tomcat-9.0.34.tar.gz
Extract the Tomcat Archive #
After downloading, extract the package to /opt/tomcat
:
sudo tar xf apache-tomcat-9.0.34.tar.gz -C /opt/tomcat
To make future updates easier, create a symbolic link named updated
that points to the Tomcat installation directory:
sudo ln -s /opt/tomcat/apache-tomcat-9.0.34 /opt/tomcat/updated
Update Permissions #
Change ownership of the Tomcat directory so that the tomcat
user has the necessary permissions:
sudo chown -R tomcat: /opt/tomcat/*
sudo sh -c 'chmod +x /opt/tomcat/updated/bin/*.sh'
4. Configuring Tomcat as a Service
To run Tomcat as a systemd service, create a systemd unit file.
Create a Unit File #
Use your preferred text editor to create the service file:
sudo nano /etc/systemd/system/tomcat.service
Add the following configuration. Replace JAVA_HOME
with your Java installation path if it’s different:
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64"
Environment="CATALINA_PID=/opt/tomcat/updated/temp/tomcat.pid"
Environment="CATALINA_HOME=/opt/tomcat/updated/"
Environment="CATALINA_BASE=/opt/tomcat/updated/"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
Environment="JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"
ExecStart=/opt/tomcat/updated/bin/startup.sh
ExecStop=/opt/tomcat/updated/bin/shutdown.sh
User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
Reload the Systemd Daemon #
After creating the unit file, reload the systemd daemon to recognize the new service:
sudo systemctl daemon-reload
Start the Tomcat Service #
Now, start the Tomcat service with:
sudo systemctl start tomcat
Verify Tomcat Status #
Check the status of the Tomcat service:
sudo systemctl status tomcat
Enable Tomcat on Startup #
To ensure Tomcat starts on system boot, enable the service:
sudo systemctl enable tomcat
5. Configuring the Firewall #
If your system has a firewall enabled, you need to allow access to port 8080, the default port for Tomcat.
sudo ufw allow 8080/tcp
6. Verifying the Installation #
Open a web browser and enter the following URL, replacing <YourIPAddress>
with your server’s IP address:
http://your_ip_address:8080
If the installation was successful, you should see the Tomcat default welcome page.
Author’s Final Word #
This setup provides a solid foundation for running Apache Tomcat on a Linux server. You can now deploy web applications that rely on Java Servlets, JSPs, and other Java-based technologies.