Post

How to Install Apache Tomcat on Linux



Introduction

Apache Tomcat is a widely used open-source web server and servlet container that allows Java-based web applications to run. In this guide, we will walk through the steps to install and configure Apache Tomcat on a Linux system.

Prerequisites

Before installing Apache Tomcat, ensure that you have the following prerequisites:

  1. Java Development Kit (JDK)
    Apache Tomcat requires Java to run. If you don’t have the JDK installed, follow this guide.
  2. Administrative Privileges
    Ensure you have administrative rights on the Linux machine to install software and configure services.

Step 1: Download and Install Apache Tomcat

  1. Download Tomcat:
  2. Extract Tomcat:
    • Open Terminal and navigate to the directory where you downloaded the .tar.gz file.
    • Extract the file with:
      1
      
       tar -xvzf apache-tomcat-<version>.tar.gz
      
    • Move the extracted Tomcat directory to a preferred location (e.g., /opt):
      1
      
       sudo mv apache-tomcat-<version> /opt
      

Step 2: Configure Environment Variables for Tomcat

Setting environment variables simplifies the process of starting and managing Tomcat. You have to add the CATALINA_HOME environment variable to specify the Tomcat installation directory, which is essential for its proper functioning.

You can set this variable by creating or editing a shell script in the /etc/profile.d/ directory:

1
sudo nano /etc/profile.d/tomcat.sh

Now, paste the following line:

1
export CATALINA_HOME=/opt/apache-tomcat-<version>

Save and close the file (Ctrl + X to save, then Y to confirm, and press Enter to exit). Then, reload the profile by executing the command:

1
source /etc/profile.d/tomcat.sh

Step 3: Start Apache Tomcat

To start Tomcat, navigate to the bin directory and execute the startup script:

1
2
cd /opt/apache-tomcat-<version>/bin
./startup.sh

You should see a message indicating that Tomcat has started.

Step 4: Access Apache Tomcat

To verify that Tomcat is running, open your web browser and go to:

1
<http://localhost:8080>

If everything is set up correctly, you should see the Tomcat welcome page.

Step 5: Creating a Systemd Service for Tomcat (Optional)

To ensure that Tomcat runs as a service and starts automatically after system reboots, you can create a systemd service file.

  1. Create the service file:
    1
    
    sudo nano /etc/systemd/system/tomcat.service
    
  2. Add the following configuration, making sure to adjust the JAVA_HOME path to point to your own JDK installation:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    [Unit]
    Description=Apache Tomcat Web Application Container
    After=network.target
    
    [Service]
    Type=forking
    
    Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64  # Update this path to your JDK installation
    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
    

    Save and close the file (Ctrl + X to save, then Y to confirm, and press Enter to exit).

  3. Reload systemd to register the new service:
    1
    
    sudo systemctl daemon-reload
    
  4. Enable and start the Tomcat service:
    1
    2
    
    sudo systemctl enable tomcat
    sudo systemctl start tomcat
    
  5. You can check the status of Tomcat with:
    1
    
    sudo systemctl status tomcat
    

Step 6: Secure Tomcat (Optional)

By default, Tomcat’s Manager and Host Manager applications are not secure. To secure them, you need to create a user with the appropriate roles.

  1. Open the tomcat-users.xml file:
    1
    
    sudo nano /opt/tomcat/conf/tomcat-users.xml
    
  2. Add the following lines inside the <tomcat-users> tags:
    1
    2
    3
    
    <role rolename="manager-gui"/>
    <role rolename="admin-gui"/>
    <user username="admin" password="password" roles="manager-gui,admin-gui"/>
    

    Replace "admin" and "password" with your desired username and password.

  3. Restart Tomcat to apply the changes:
    1
    
    sudo systemctl restart tomcat
    
  4. You can now access the Tomcat Manager and Host Manager via:
    1
    
    <http://localhost:8080/manager/html>
    

    Log in with the credentials you set in the tomcat-users.xml file.

Conclusion

By following these steps, you should have Apache Tomcat successfully installed and running on your Linux machine. Tomcat is now ready to serve your Java web applications. If you want Tomcat to be more secure or optimized, you can explore additional configuration options, including SSL setup and performance tuning.

© 2024 Java Tutorial Online. All rights reserved.