Apache Tomcat is a popular open-source web server and servlet container used to serve Java applications. In this guide, we will walk you through how to install and configure Apache Tomcat 9 on Ubuntu 20.04 LTS. Whether you're hosting Java applications on a Windows VPS UK or any other server, Tomcat provides a robust and scalable platform for web applications.

Prerequisites

Before you begin, ensure you have the following:

Step 1: Update Your System

Before installing any software, update your system to ensure all packages are up to date:

sudo apt update && sudo apt upgrade -y

Step 2: Install Java

Apache Tomcat requires Java to run. Install OpenJDK, the open-source version of Java, by running the following command:

sudo apt install default-jdk -y

Verify the installation by checking the Java version:

java -version

Step 3: Create a Tomcat User

For security reasons, it is recommended to run Tomcat under a non-root user. Create a new Tomcat user and group:

sudo groupadd tomcat
sudo useradd -m -g tomcat -s /bin/bash tomcat

Step 4: Install Apache Tomcat 9

Download the latest version of Apache Tomcat 9 from the official Apache Tomcat website. You can use the following wget command to download the Tomcat archive:

cd /tmp
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.62/bin/apache-tomcat-9.0.62.tar.gz

Extract the archive to the /opt directory:

sudo tar -xvf apache-tomcat-9.0.62.tar.gz -C /opt

Rename the extracted directory for convenience:

sudo mv /opt/apache-tomcat-9.0.62 /opt/tomcat

Step 5: Set Permissions for Tomcat

Set the correct permissions for the Tomcat directories:

sudo chown -R tomcat:tomcat /opt/tomcat
sudo chmod -R 755 /opt/tomcat

Step 6: Configure Tomcat as a Systemd Service

To run Tomcat as a service, create a new systemd service file:

sudo nano /etc/systemd/system/tomcat.service

Add the following configuration 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.target

Reload the systemd daemon to apply the changes:

sudo systemctl daemon-reload

Start the Tomcat service and enable it to start on boot:

sudo systemctl start tomcat
sudo systemctl enable tomcat

Step 7: Configure Firewall (Optional)

If you have a firewall running on your server, allow traffic on port 8080, which Tomcat uses by default:

sudo ufw allow 8080/tcp

Step 8: Access the Tomcat Web Interface

Tomcat should now be running. Open your web browser and navigate to http://your-server-ip:8080. You should see the Apache Tomcat welcome page.

Step 9: Secure Tomcat with Let's Encrypt SSL (Optional)

If you want to secure your Tomcat instance using SSL, you can configure Let's Encrypt. First, install Certbot:

sudo apt install certbot -y

Obtain an SSL certificate using Certbot:

sudo certbot certonly --standalone -d your-domain.com -d www.your-domain.com

After obtaining the certificate, configure Tomcat to use SSL by editing the server.xml file in the /opt/tomcat/conf directory. Add or modify the following section:

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
            <Certificate certificateFile="/etc/letsencrypt/live/your-domain.com/fullchain.pem"
                         certificateKeyFile="/etc/letsencrypt/live/your-domain.com/privkey.pem"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>

Conclusion

By following these steps, you have successfully installed and configured Apache Tomcat 9 on Ubuntu 20.04 LTS. Whether you’re hosting Java applications on a Windows VPS UK, Windows VPS Italy, or another Windows Virtual Dedicated Server Hosting, Apache Tomcat provides a powerful and flexible platform for running web applications.

Hai trovato utile questa risposta? 0 Utenti hanno trovato utile questa risposta (0 Voti)