Apache Tomcat is a widely used open-source web server and servlet container designed to run Java applications. It supports Java Servlet, JavaServer Pages (JSP), and other Java-based web technologies, making it a crucial tool for developers.
This guide provides a step-by-step approach to installing Apache Tomcat 9 on Ubuntu 22.04.
Prerequisites
Before proceeding, ensure you have:
- An Ubuntu 22.04 system (or a similar Ubuntu-based distribution)
- A user account with sudo privileges
- Terminal access
- The apt package manager installed (pre-installed on Ubuntu)
Step-by-Step Installation Guide
Step 1: Verify Java Installation
Apache Tomcat requires Java to function. Check if Java is installed with the following command:
java -version
If Java is not installed, you will see an error message.
Note: The steps are also applicable to other Ubuntu versions like 20.04 and Ubuntu-based distributions such as Linux Mint and Elementary OS.
Step 2: Install OpenJDK
If Java is missing, install the default JDK package:
sudo apt install default-jdk
Verify the installation:
java -version
Step 3: Create a Tomcat User and Group
For security reasons, avoid running Tomcat as the root user. Instead, create a dedicated user and group:
sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Step 4: Download Apache Tomcat
Obtain the latest version of Tomcat from the official Apache website:
cd /tmp
curl -O https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.86/bin/apache-tomcat-9.0.86.tar.gz
Step 5: Extract the Tomcat Archive
Create the installation directory and extract the files:
sudo mkdir /opt/tomcat
sudo tar xzvf apache-tomcat-9*tar.gz -C /opt/tomcat --strip-components=1
Step 6: Set Permissions
Grant the Tomcat user permission to execute the necessary scripts:
sudo chown -RH tomcat: /opt/tomcat
sudo sh -c 'chmod +x /opt/tomcat/bin/*.sh'
Step 7: Create a Systemd Service File
To manage Tomcat as a service, create a systemd unit file:
sudo nano /etc/systemd/system/tomcat.service
Add the following content (adjust JAVA_HOME if necessary):
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64"
Environment="CATALINA_HOME=/opt/tomcat"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
Save and exit. Reload the system daemon:
sudo systemctl daemon-reload
sudo systemctl start tomcat
Check if Tomcat is running:
sudo systemctl status tomcat
Step 8: Configure Firewall
Allow traffic through port 8080 to access Tomcat:
sudo ufw allow 8080/tcp
To test, open a browser and navigate to:
http://localhost:8080
Step 9: Enable Web Management Interface
Edit the Tomcat users file:
sudo nano /opt/tomcat/conf/tomcat-users.xml
Add the following lines:
<tomcat-users>
<role rolename="manager-gui"/>
<user username="admin" password="Your_Strong_Password" roles="manager-gui"/>
</tomcat-users>
Save and exit.
Step 10: Configure Remote Access (Optional)
To allow external access, modify the Manager App’s configuration:
sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml
Comment out the following section:
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\d+.\d+.\d+.\d+|::1|0000:1" />
-->
Repeat the same process for the host-manager file:
sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
Restart Tomcat to apply changes:
sudo systemctl restart tomcat
Conclusion
You have successfully installed Apache Tomcat 9 on Ubuntu 22.04. Tomcat is now ready to serve Java-based web applications.
To enhance security, consider setting up SSL encryption, restricting admin access, and updating Tomcat regularly.
For further customization and performance tuning, refer to the official Apache Tomcat documentation. Also, go with Vultr for budget and affordable cloud hosting or vps hosting.
FAQs
1. What is Apache Tomcat used for?
Apache Tomcat is a web server and servlet container designed to deploy and run Java applications.
2. Which Java version is required for Tomcat?
Tomcat 9 requires Java 8 or later (OpenJDK 11 is recommended).
3. How do I restart Tomcat?
Use:
sudo systemctl restart tomcat
4. How do I uninstall Tomcat?
Remove the installation directory:
sudo rm -rf /opt/tomcat
5. How can I secure my Tomcat installation?
- Change the default admin credentials
- Use a firewall to restrict access
- Configure HTTPS for secure communication
- Regularly update Tomcat and Java