Setting up a reliable web server for Java-based applications is an essential task for any DevOps engineer, system administrator, or developer. Apache Tomcat is one of the most widely used open-source implementations of the Java Servlet, JavaServer Pages (JSP), and Java Expression Language (EL) technologies. In this guide, we’ll walk you through the process of installing and configuring Apache Tomcat on Ubuntu 22.04 LTS, ensuring you have a secure and efficient environment for deploying Java applications.
Prerequisites
Before getting started, make sure you have:
- Administrative access to the server.
- A basic understanding of the Linux command line and package management.
- Java Development Kit (JDK) installed on your system.
Step 1: Update Your Package List
To ensure your system is up-to-date, run the following command to update your package list and upgrade existing packages:
sudo apt update && sudo apt full-upgrade -y
This command ensures that all packages on your system are current and ready for any new installations.
Step 2: Install Java Development Kit (JDK)
Apache Tomcat requires Java to run. The recommended version for Ubuntu 22.04 LTS is OpenJDK 17. Install it using the following command:
sudo apt install openjdk-17-jdk -y
Verify that Java has been installed by running:
java -version
You should see output similar to openjdk version "17.x.x"
confirming the installation.
Step 3: Download and Install Apache Tomcat
Visit the official Apache Tomcat download page to find the latest stable version. For this guide, we’ll use version 9.0.63. Download and extract it with the following commands:
wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.63/bin/apache-tomcat-9.0.63.tar.gz
tar -xvf apache-tomcat-9.0.63.tar.gz
sudo mv apache-tomcat-9.0.63 /opt/tomcat
This places Tomcat in the /opt/tomcat
directory, which is a standard location for optional software packages.
Step 4: Configure a Tomcat User and Permissions
Create a dedicated tomcat
user and group for security purposes and adjust the ownership of the Tomcat directory:
sudo useradd -m -s /bin/false tomcat
sudo chown -R tomcat:tomcat /opt/tomcat
This ensures that the Tomcat process runs with limited permissions, enhancing the security of your server.
Step 5: Create a Systemd Service File for Tomcat
To manage Tomcat as a service, create a systemd service file:
sudo nano /etc/systemd/system/tomcat.service
Add the following content to the file:
[Unit]
Description=Apache Tomcat 9
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-17-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"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
Save and close the file. Reload the systemd daemon to recognize the new service:
sudo systemctl daemon-reload
Start and enable the Tomcat service:
sudo systemctl start tomcat
sudo systemctl enable tomcat
Verify that Tomcat is running:
sudo systemctl status tomcat
Step 6: Configure the Tomcat Web Interface
To access the Tomcat web interface, edit the tomcat-users.xml
file to create a user with administrative privileges:
sudo nano /opt/tomcat/conf/tomcat-users.xml
Add the following lines inside the <tomcat-users>
tag:
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="securepassword" roles="manager-gui,admin-gui"/>
Replace securepassword
with a strong password of your choice. Save and close the file, then restart Tomcat:
sudo systemctl restart tomcat
Step 7: Access the Tomcat Web Interface
Open a web browser and navigate to http://your-server-ip:8080
. You should see the Tomcat welcome page. Click on “Manager App” or “Host Manager” and log in using the credentials you configured in the tomcat-users.xml
file.
Best Practices
- Secure Your Tomcat Installation: Configure SSL/TLS for encrypted connections and use strong passwords.
- Keep Tomcat Updated: Regularly check for updates to ensure you have the latest security patches.
- Monitor Logs: Regularly review Tomcat logs located in
/opt/tomcat/logs
for any errors or warnings.
Troubleshooting
- Tomcat Not Starting: Check the status with
sudo systemctl status tomcat
and review logs at/opt/tomcat/logs/catalina.out
for error details. - Port Conflicts: Ensure no other services are using port 8080. Adjust the port in the
server.xml
file if necessary.
For additional help, refer to the official Tomcat documentation or community forums.
Conclusion
By following this guide, you’ve successfully installed and configured Apache Tomcat on Ubuntu 22.04 LTS. You now have a powerful, scalable environment for deploying Java-based web applications. Explore advanced configurations, such as integrating Tomcat with a reverse proxy or securing the Tomcat Manager application for further enhancement of your server’s capabilities.
Next Steps:
- Integrate Tomcat with CI/CD tools like Jenkins for automated deployments.
- Implement load balancing for high availability.
- Secure your server with a firewall and SSL/TLS certificates.