How To Install Apache Tomcat on Ubuntu 24.04 LTS
Apache Tomcat stands as a cornerstone in the world of web application development, offering a robust and flexible environment for Java-based web applications. As an open-source implementation of the Java Servlet, JavaServer Pages, Java Expression Language, and WebSocket technologies, Tomcat provides developers with a powerful platform to build and deploy dynamic web applications.
This comprehensive guide will walk you through the process of installing Apache Tomcat on Ubuntu 24.04 LTS, ensuring you have a fully functional and secure environment for your Java web applications. Whether you’re a seasoned system administrator or a developer looking to set up your own server, this article will provide you with the knowledge and steps necessary to get Tomcat up and running smoothly.
What is Apache Tomcat?
Apache Tomcat is an open-source web server and servlet container developed by the Apache Software Foundation. It implements several Java EE specifications, including Java Servlet, JavaServer Pages (JSP), Java Expression Language, and WebSocket, providing a “pure Java” HTTP web server environment for Java code to run.
Key features and benefits of Apache Tomcat include:
- Lightweight and efficient performance
- Easy configuration and management
- Extensive documentation and community support
- Compatibility with a wide range of Java frameworks
- Built-in security features
Compared to other web servers like Apache HTTP Server or Nginx, Tomcat specializes in serving Java applications. While it can serve static content, its primary strength lies in its ability to run Java servlets and render JSP pages, making it an ideal choice for Java-based web applications.
Prerequisites
Before we begin the installation process, ensure that your system meets the following requirements:
- Ubuntu 24.04 LTS installed and updated
- Root or sudo access to the system
- Basic knowledge of Linux command-line operations
- A stable internet connection for downloading packages
You’ll also need to have some essential tools installed, such as wget and unzip, which we’ll use during the installation process.
Updating Ubuntu 24.04 LTS
It’s crucial to start with an up-to-date system. Open a terminal and run the following commands:
sudo apt update
sudo apt upgrade -y
This will update the package lists and upgrade all installed packages to their latest versions. The “-y” flag automatically answers “yes” to any prompts during the upgrade process.
Installing Java Development Kit (JDK)
Apache Tomcat requires Java to run. We’ll install OpenJDK, the open-source implementation of the Java Platform.
First, check if Java is already installed:
java -version
If Java is not installed, proceed with the installation of OpenJDK:
sudo apt install default-jdk -y
After installation, verify the Java version:
java -version
Next, set up the JAVA_HOME environment variable. Open the /etc/environment
file:
sudo nano /etc/environment
Add the following line to the file:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Save and close the file. Then, reload the environment variables:
source /etc/environment
Downloading and Installing Apache Tomcat
Now, let’s download and install Apache Tomcat. At the time of writing, Tomcat 10.x is the latest stable version compatible with Ubuntu 24.04 LTS.
First, create a directory for Tomcat:
sudo mkdir /opt/tomcat
Download the latest version of Tomcat:
wget https://downloads.apache.org/tomcat/tomcat-10/v10.1.x/bin/apache-tomcat-10.1.x.tar.gz
Extract the archive and move it to the /opt/tomcat directory:
sudo tar xzvf apache-tomcat-10.1.x.tar.gz -C /opt/tomcat --strip-components=1
Create a dedicated user for Tomcat:
sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat
Change the ownership of the Tomcat directory:
sudo chown -R tomcat: /opt/tomcat
Configuring Apache Tomcat
With Tomcat installed, we need to configure it properly. First, let’s set up the necessary environment variables. Create a new file named tomcat.service
:
sudo nano /etc/systemd/system/tomcat.service
Add the following content to the file:
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
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
Save and close the file. Next, let’s configure the server.xml file to adjust Tomcat’s settings:
sudo nano /opt/tomcat/conf/server.xml
Here, you can modify settings such as the port number (default is 8080) and other server configurations based on your requirements.
To set up users and roles, edit the tomcat-users.xml
file:
sudo nano /opt/tomcat/conf/tomcat-users.xml
Add user roles and credentials as needed. For example:
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="your_password" roles="manager-gui,admin-gui"/>
Remember to use a strong, unique password for security.
Creating a Systemd Service File
To manage Tomcat as a service, we’ll create a systemd service file. We’ve already created this file in the previous step, so now we just need to reload systemd and start the Tomcat service:
sudo systemctl daemon-reload
sudo systemctl start tomcat
sudo systemctl enable tomcat
Check the status of the Tomcat service:
sudo systemctl status tomcat
Accessing Tomcat Web Interface
To access Tomcat’s web interface, you may need to configure your firewall to allow traffic on Tomcat’s port (default 8080). If you’re using UFW (Uncomplicated Firewall), run:
sudo ufw allow 8080/tcp
Now, open a web browser and navigate to:
http://your_server_ip:8080
You should see the Apache Tomcat welcome page. If you encounter any issues, check your firewall settings and ensure Tomcat is running correctly.
Securing Apache Tomcat
Security is crucial for any web server. Here are some steps to enhance Tomcat’s security:
- Change default ports: Edit server.xml and modify the Connector port.
- Implement SSL/TLS: Configure Tomcat to use HTTPS for encrypted connections.
- Set up access controls: Restrict access to sensitive directories and files.
- Use strong passwords: Ensure all user accounts have robust, unique passwords.
- Keep Tomcat updated: Regularly check for and apply security updates.
Deploying Web Applications
To deploy web applications on Tomcat, you can use the Tomcat Web Application Manager or manually deploy WAR files.
To access the Web Application Manager, navigate to:
http://your_server_ip:8080/manager/html
You’ll need to log in with the credentials set in tomcat-users.xml
.
For manual deployment, copy your WAR file to the /opt/tomcat/webapps/
directory. Tomcat will automatically deploy the application.
Monitoring and Maintaining Tomcat
Regular monitoring and maintenance are essential for optimal Tomcat performance:
- Check Tomcat status:
sudo systemctl status tomcat
- View logs: Check files in
/opt/tomcat/logs/
for errors and performance data. - Performance tuning: Adjust JVM settings in the tomcat.service file for better performance.
Troubleshooting Common Issues
Here are solutions to some common Tomcat issues:
- Address already in use: Check for conflicting services on the same port.
- Out of memory errors: Increase the allocated memory in the CATALINA_OPTS environment variable.
- Permission problems: Ensure proper ownership and permissions on Tomcat directories.
Upgrading Apache Tomcat
To upgrade Tomcat, follow these steps:
- Check for updates on the official Apache Tomcat website.
- Back up your current Tomcat installation and configurations.
- Download and extract the new version.
- Stop the Tomcat service, replace the old installation, and start the service again.
Congratulations! You have successfully installed Apache Tomcat. Thanks for using this tutorial for installing the Apache Tomcat on Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official Apache website.