Step-by-Step Guide: Install Tomcat on a Linux System
Step 1: Preparations
Before installing Tomcat on Linux, ensure that Java is installed on your system. If not, we will use the OpenJDK package, which is the default Java Development Kit. Open the terminal on your Ubuntu system and first update the package sources with the following command:
sudo apt update
This ensures that you get the latest version of the OpenJDK package when installing Java on your system. Then, install Java with this command:
sudo apt install default-jdk
Check the Java installation to ensure it was successful:
java -version
Step 2: Set Up Tomcat User
It is not recommended to run Tomcat under a root account. Therefore, we need to create a new user under which the Tomcat server will run on our system. Use the following command to create the user:
sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat
Here, the user “tomcat” is associated with the directory “/opt/tomcat” where the Tomcat service will run.
Step 3: Download Tomcat Package
After creating the user, we need to download the Tomcat package to install Tomcat on Linux. Use the “wget” command to download the Tomcat package from the official website:
wget -c https://downloads.apache.org/tomcat/tomcat-9/v9.0.34/bin/apache-tomcat-9.0.34.tar.gz
Step 4: Install Tomcat on Linux
Once the tar archive is downloaded on your system, extract it to the directory “/opt/tomcat” with the following command:
sudo tar xf apache-tomcat-9.0.34.tar.gz -C /opt/tomcat
Create a symbolic link pointing to the Tomcat installation directory to facilitate future updates:
sudo ln -s /opt/tomcat/apache-tomcat-9.0.34 /opt/tomcat/updated
Change permissions in the Tomcat directory with the following commands:
sudo chown -R tomcat: /opt/tomcat/*
sudo sh -c 'chmod +x /opt/tomcat/updated/bin/*.sh'
Step 5: Configure Tomcat Service
After installation, you need to configure Tomcat to run it as a service. Create a systemd unit file under “/etc/systemd/system” with a text editor of your choice:
sudo nano /etc/systemd/system/tomcat.service
Insert the following content into the file and save it:
[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
Update the system daemon to apply the changes:
sudo systemctl daemon-reload
Start the Tomcat service with the following command:
sudo systemctl start tomcat
Check the status of the Tomcat service:
sudo systemctl status tomcat
Step 6: Allow Port 8080
To allow Tomcat to communicate on port 8080, adjust the firewall configuration:
sudo ufw allow 8080/tcp
Step 7: Verify Installation
To verify the successful installation of Tomcat, open your web browser and enter your server’s IP address followed by “:8080”, e.g., “http://:8080”. You should see the Tomcat homepage.
Conclusion
Tomcat is a powerful tool for deploying Java Servlets and JSPs. With this guide, you should be able to install Tomcat on your Linux system and make basic configurations. You can make further customizations to your Tomcat server to meet your requirements. For any questions or suggestions, feel free to reach out to us in the comments.