Here are the general steps to set up Apache Tomcat on a Linux server (e.g., Ubuntu, CentOS).
Tomcat requires Java (either OpenJDK or Oracle JDK). You need to install the Java Development Kit (JDK).
Install OpenJDK 11 (or your preferred version):
On Ubuntu/Debian:
sudo apt update
sudo apt install openjdk-11-jdk
On CentOS/RHEL:
sudo yum install java-11-openjdk-devel
Verify the Java installation:
java -version
You can download the latest version of Tomcat from the official Apache Tomcat website.
Download Tomcat (e.g., Tomcat 9):
On Linux, you can use wget
to download Tomcat:
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz
Extract the downloaded file:
tar -xvzf apache-tomcat-9.0.65.tar.gz
Move the extracted Tomcat folder to a suitable location, like /opt/tomcat
:
sudo mv apache-tomcat-9.0.65 /opt/tomcat
To make it easier to run Tomcat, you can set up environment variables.
Edit your .bashrc
or .profile
file and add the following lines:
export CATALINA_HOME=/opt/tomcat
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Reload the profile:
source ~/.bashrc
To start Tomcat, go to the bin
directory in your Tomcat installation and execute the startup script.
Start Tomcat:
cd /opt/tomcat/bin
./startup.sh
You should see output indicating that Tomcat has started successfully.
Verify Tomcat is running: Open your browser and go to http://<your-server-ip>:8080
. You should see the default Tomcat welcome page.
To run Tomcat as a service, you can create a systemd service file.
Create a service file for Tomcat:
sudo nano /etc/systemd/system/tomcat.service
Add the following content:
[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
Reload systemd and start the service:
sudo systemctl daemon-reload
sudo systemctl start tomcat
Enable Tomcat to start on boot:
sudo systemctl enable tomcat