How To Install Apache Tomcat on Ubuntu 24.04
Welcome to the Greenhost.Cloud blog! In today’s post, we will guide you through the process of installing Apache Tomcat on Ubuntu 24.04. Apache Tomcat is an open-source application server that allows you to deploy and run Java web applications. With its widespread adoption and robust feature set, Tomcat is a favorite among developers.
Prerequisites
Before we start the installation process, ensure you have the following prerequisites:
- A server or machine running Ubuntu 24.04.
- A non-root user with
sudoprivileges. - Java Development Kit (JDK) installed. Tomcat requires Java to run, so let’s check if it’s installed.
Step 1: Install Java
Apache Tomcat requires JDK to run Java applications. You can install OpenJDK using the following commands:
- First, update your package index:
sudo apt update- Install OpenJDK 11 (or any other version you prefer):
sudo apt install openjdk-11-jdk- Verify the installation:
java -versionYou should see output indicating the installed version of Java.
Step 2: Download Apache Tomcat
- Go to the Apache Tomcat download page to find the latest stable release. As of this writing, the latest version is 9.0.x.
- To download Tomcat, use the following commands. Adjust the version number based on the latest release:
cd /opt
sudo wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.76/bin/apache-tomcat-9.0.76.tar.gz- Extract the downloaded tar.gz file:
sudo tar -xvf apache-tomcat-9.0.76.tar.gz- Rename the extracted folder for easier access:
sudo mv apache-tomcat-9.0.76 tomcatStep 3: Set Permissions
Set the necessary permissions for the Tomcat installation:
sudo chown -R $USER:$USER /opt/tomcatStep 4: Configure Environment Variables
To run Tomcat conveniently, it’s helpful to set environment variables. You’ll need to edit the ~/.bashrc file:
- Open the
.bashrcfile in a text editor:
nano ~/.bashrc- Add the following lines at the end of the file:
export CATALINA_HOME=/opt/tomcat- Save the changes and update your terminal session:
source ~/.bashrcStep 5: Start Apache Tomcat
To start Tomcat, navigate to the bin directory of Tomcat and execute the startup script:
cd $CATALINA_HOME/bin
./startup.shStep 6: Accessing Tomcat
The default port for Tomcat is 8080. You can access the Tomcat server by navigating to http://your_server_ip:8080 in your web browser. You should see the Tomcat welcome page, which confirms that Tomcat is up and running.
Step 7: Set Up Tomcat as a Service (Optional)
To manage Tomcat as a service, we can create a systemd unit file:
- Create a new service file:
sudo nano /etc/systemd/system/tomcat.service- Add the following configuration to the file:
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=simple
User=your_username
Group=your_group
Environment=CATALINA_HOME=/opt/tomcat
Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.targetReplace your_username and your_group with the corresponding values for your system.
- Reload systemd to recognize the new service:
sudo systemctl daemon-reload- Start the Tomcat service:
sudo systemctl start tomcat- Enable it to start on boot:
sudo systemctl enable tomcatConclusion
Congratulations! You have successfully installed Apache Tomcat on your Ubuntu 24.04 server. You can now deploy Java web applications using this powerful application server. If you encounter any issues or have questions, feel free to leave a comment below, or visit the Apache Tomcat documentation for more detailed information.
Stay tuned for more tutorials and guides on Greenhost.Cloud! Happy coding!