How to Install Apache Tomcat 9 on Ubuntu 18.04
Apache Tomcat is an open-source implementation of the Java Servlet, JavaServer Pages, and Java Expression Language technologies. It powers numerous large-scale, mission-critical web applications across various industries and sectors. This tutorial will guide you through the process of installing Apache Tomcat 9 on Ubuntu 18.04.
Prerequisites
- Ubuntu 18.04 server instance.
- A user account with sudo privileges.
- Java Development Kit (JDK) installed on your system.
- Basic knowledge of Linux command line.
Main Steps
Follow the steps below to install Apache Tomcat 9 on your Ubuntu 18.04 server:
Step 1: Install Java Development Kit (JDK)
Before installing Tomcat, ensure that you have JDK installed on your system. You can install OpenJDK with the following commands:
sudo apt update
sudo apt install default-jdk
After the installation is complete, verify the Java installation:
java -version
You should see the installed version of Java in the output.
Step 2: Create a Tomcat User
It is a good practice to run Tomcat as its own user. Create a new user for Tomcat with the following command:
sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat
Step 3: Download Tomcat 9
Navigate to the /tmp directory to download the Apache Tomcat tar.gz file:
cd /tmp
curl -O https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.62/bin/apache-tomcat-9.0.62.tar.gz
Extract the downloaded archive:
sudo tar xzvf apache-tomcat-9.0.62.tar.gz -C /opt/tomcat --strip-components=1
Step 4: Change Ownership
Change the ownership of the Tomcat directory to the tomcat user:
sudo chown -R tomcat: /opt/tomcat
Step 5: Configure Systemd for Tomcat
Create a systemd service file for Tomcat to manage it as a service:
sudo nano /etc/systemd/system/tomcat.service
Paste the following content into the file:
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=simple
User=tomcat
Group=tomcat
Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
Step 6: Start Tomcat Service
Reload systemd to apply the changes and then start the Tomcat service:
sudo systemctl daemon-reload
sudo systemctl start tomcat
sudo systemctl enable tomcat
Step 7: Adjust Firewall Settings
If you are using a firewall, allow traffic on port 8080:
sudo ufw allow 8080/tcp
Step 8: Access Tomcat Web Interface
Open your web browser and visit http://your_server_ip:8080. You should see the Tomcat welcome page.
Advanced Configuration
Once Tomcat is installed, you may want to configure it for specific needs:
Changing Default Port
To change the default port Tomcat runs on, edit the server.xml file:
sudo nano /opt/tomcat/conf/server.xml
Look for the following line:
<Connector port="8080" protocol="HTTP/1.1" ... />
Change the port number to your desired port and save the file.
Setting Up Users for Manager App
You can set up users to access the Tomcat Manager app. Open the tomcat-users.xml file:
sudo nano /opt/tomcat/conf/tomcat-users.xml
Add the following lines within the <tomcat-users> tags:
<role rolename="manager-gui"/>
<user username="admin" password="your_password" roles="manager-gui"/>
Replace your_password with a secure password.
Best Practices
- Keep your Tomcat installation up to date to avoid security vulnerabilities.
- Regularly back up your Tomcat configuration and web applications.
- Monitor Tomcat performance and logs for any issues.
- Use a reverse proxy (like Nginx or Apache HTTP Server) in front of Tomcat for better security and performance.
Troubleshooting
If you encounter issues starting the Tomcat service, check the logs located in
/opt/tomcat/logs.
- Use
sudo systemctl status tomcatto check the service status. - Review
catalina.outfor any errors.
Conclusion
In this tutorial, we covered the steps to install Apache Tomcat 9 on Ubuntu 18.04. By following these instructions, you should have a functional Tomcat server ready for hosting Java applications. Remember to configure your installation according to your security needs and performance requirements.
This HTML document provides a comprehensive tutorial on how to install Apache Tomcat 9 on Ubuntu 18.04, including prerequisites, main steps, advanced configurations, best practices, troubleshooting, and conclusion, ensuring that users can easily follow through the installation process.
Verifikasi Teknis
Panduan ini disusun berdasarkan referensi teknis terbaru. Namun, konfigurasi server dapat bervariasi. Lihat sumber referensi asli →