Post

How to Install Apache Tomcat on MacOS



Introduction

Apache Tomcat is a widely used open-source web server and servlet container for running Java applications. Installing Tomcat on macOS involves several key steps: installing Java, downloading and setting up Tomcat, and configuring environment variables. This guide will walk you through the installation process to help you get Tomcat up and running on your Mac.

Prerequisites

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

  1. Java Development Kit (JDK)
    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 your macOS system.

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., /usr/local/):
      1
      
      sudo mv apache-tomcat-<version> /usr/local/
      

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 your shell configuration file to specify the Tomcat installation directory, which is essential for the proper functioning of Tomcat.

Since different macOS versions use different default shells, we will cover two cases: for those using zsh (the default shell starting with macOS Catalina 10.15) and for those using bash in earlier versions.

1. Setting CATALINA_HOME for zsh

  • Open or create the .zshrc file in your home directory:
    1
    
    nano ~/.zshrc
    
  • Scroll down and paste the following line to set CATALINA_HOME:
    1
    2
    
    # Set CATALINA_HOME to the Tomcat installation directory
    export CATALINA_HOME=/usr/local/apache-tomcat-<version>
    
  • Save and close the file (Ctrl + X to save, then Y to confirm, and press Enter to exit).
  • Reload the profile by executing the command:
    1
    
    source ~/.zshrc
    

2. Set CATALINA_HOME for bash (for OS < Catalina 10.15)

  • Open or create the .bash_profile file in your home directory:
    1
    
    nano ~/.bash_profile
    
  • Scroll down and paste the following line to set CATALINA_HOME:
    1
    2
    
    # Set CATALINA_HOME to the Tomcat installation directory
    export CATALINA_HOME=/usr/local/apache-tomcat-<version>
    
  • Save and close the file (Ctrl + X to save, then Y to confirm, and press Enter to exit).
  • Reload the profile by executing the command:
    1
    
    source ~/.bash_profile
    

Step 3: Start Apache Tomcat

  1. Navigate to Tomcat’s bin Directory
    In Terminal, navigate to the bin directory of Tomcat:
    1
    
    cd $CATALINA_HOME/bin
    
  2. Start Tomcat
    Run the startup script:
    1
    
    ./startup.sh
    

    If everything is set up correctly, you should see a message indicating that Tomcat has started successfully, such as:

    1
    2
    3
    4
    5
    6
    
    Using CATALINA_BASE:   /usr/local/tomcat-<version>
    Using CATALINA_HOME:   /usr/local/tomcat-<version>
    Using CATALINA_TMPDIR: /usr/local/tomcat-<version>/temp
    Using JRE_HOME:        /Library/Java/JavaVirtualMachines/jdk-xx.jdk/Contents/Home
    Using CLASSPATH:       /usr/local/tomcat-<version>/bin/bootstrap.jar:/usr/local/tomcat-<version>/bin/tomcat-juli.jar
    Tomcat started.
    

Step 4: Access Tomcat in the Browser

  1. Open a Web Browser
    Launch your web browser and enter http://localhost:8080 in the address bar.
  2. Check Tomcat Default Page
    You should see the Tomcat welcome page, indicating that Tomcat is successfully installed and running.

Step 5: Stop Apache Tomcat

To stop Tomcat, run the shutdown.sh script from the bin directory:

1
2
cd /usr/local/tomcat-<version>/bin
./shutdown.sh

Step 6: Configure Tomcat (Optional)

For security reasons, Tomcat doesn’t allow access to some of its administration tools by default. If you want to enable the Manager App or Host Manager, you’ll need to create users with the correct roles.

  1. Open the tomcat-users.xml file in the conf directory:

    1
    
    nano /usr/local/tomcat-<version>/conf/tomcat-users.xml
    
  2. Add the following user configuration within the <tomcat-users> section:

    1
    2
    3
    4
    5
    
    <tomcat-users>
        <role rolename="manager-gui"/>
        <role rolename="admin-gui"/>
        <user username="admin" password="admin" roles="manager-gui,admin-gui"/>
    </tomcat-users>
    
  3. Save and close the file. Restart Tomcat for the changes to take effect.

Now, you can access the Manager App and Host Manager by navigating to the following URLs:

  • Manager App: http://localhost:8080/manager
  • Host Manager: http://localhost:8080/host-manager

You will need to log in with the username and password defined in tomcat-users.xml.

Step 7: Automating Tomcat Startup (Optional)

To automatically start Tomcat when your Mac boots, you can create a launchd service. Follow these steps:

  1. Create a plist file for Tomcat in /Library/LaunchDaemons/:

    1
    
    sudo nano /Library/LaunchDaemons/org.apache.tomcat.plist
    
  2. Add the following configuration to the plist file:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "<http://www.apple.com/DTDs/PropertyList-1.0.dtd>">
    <plist version="1.0">
    <dict>
        <key>Label</key>
        <string>org.apache.tomcat</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/local/tomcat9/bin/startup.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <true/>
    </dict>
    </plist>
    
  3. Save and close the file. Now, Tomcat will start automatically when your Mac boots.

Conclusion

You have successfully installed Apache Tomcat on macOS. By following these steps, you’ve set up the required Java environment, downloaded and configured Tomcat, and verified that it’s running correctly. Tomcat is now ready to host your Java web applications. For more advanced configurations and management, refer to the official Tomcat documentation.

© 2024 Java Tutorial Online. All rights reserved.