Apache Tomcat is an open-source implementation of the Java Servlet, JavaServer Pages, Java Expression Language, and Java WebSocket technologies. Installing Apache Tomcat on Ubuntu is a straightforward process that allows you to run Java web applications. This guide will walk you through the steps to install Apache Tomcat on Ubuntu.
Prerequisites
Before you begin, ensure that you have the following:
- A server running Ubuntu.
- A non-root user with sudo privileges.
- Java Development Kit (JDK) installed.
Step 1: Update Your Package Repository
First, update your package repository to ensure you have the latest package information.
sudo apt update
sudo apt upgradeStep 2: Install Java
Apache Tomcat requires Java to run. Install the default JDK package.
sudo apt install default-jdkVerify the installation by checking the Java version.
java -versionStep 3: Create a Tomcat User
For security reasons, it’s best to run Tomcat as a non-root user. Create a new user and group for Tomcat.
sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcatStep 4: Download Tomcat
Visit the Apache Tomcat download page and get the latest version. Alternatively, you can use wget to download it directly.
cd /tmp
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.56/bin/apache-tomcat-9.0.56.tar.gzStep 5: Install Tomcat
Extract the downloaded file and move it to the /opt/tomcat directory.
sudo mkdir /opt/tomcat
sudo tar xzvf apache-tomcat-9.0.56.tar.gz -C /opt/tomcat --strip-components=1Step 6: Update Permissions
Change the ownership of the Tomcat directory to the tomcat user and group.
sudo chown -R tomcat: /opt/tomcat
sudo chmod -R 755 /opt/tomcatStep 7: Create a Systemd Service File
Create a new service file for Tomcat to manage it with systemd.
sudo nano /etc/systemd/system/tomcat.serviceAdd the following content to the file:
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
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
[Install]
WantedBy=multi-user.targetStep 8: Reload Systemd and Start Tomcat
Reload the systemd daemon and start the Tomcat service.
sudo systemctl daemon-reload
sudo systemctl start tomcatEnable Tomcat to start on boot.
sudo systemctl enable tomcatStep 9: Configure Tomcat Web Management Interface
To access the web management interface, configure the tomcat-users.xml file.
sudo nano /opt/tomcat/conf/tomcat-users.xmlAdd the following lines inside the <tomcat-users> tag.
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="password" roles="manager-gui,admin-gui"/>Conclusion
You have successfully installed Apache Tomcat on Ubuntu. You can access the Tomcat web interface by navigating to http://your_server_ip:8080 in your web browser. From here, you can deploy and manage your Java web applications.
