How to Configure Tomcat to Use Port 80 in Amazon EC2 Linux Instances

As a data scientist or software engineer, you might come across a situation where you need to run Apache Tomcat on port 80 in an Amazon EC2 Linux instance. However, you’ll notice that Linux typically restricts ports below 1024 for root user access. This blog post aims to provide a step-by-step guide on how to configure Tomcat to use port 80 on your EC2 instance.

How to Configure Tomcat to Use Port 80 in Amazon EC2 Linux Instances

As a data scientist or software engineer, you might come across a situation where you need to run Apache Tomcat on port 80 in an Amazon EC2 Linux instance. However, you’ll notice that Linux typically restricts ports below 1024 for root user access. This blog post aims to provide a step-by-step guide on how to configure Tomcat to use port 80 on your EC2 instance.

Step 1: Update and Install Tomcat

Start by ensuring your Linux instance is up to date:

sudo yum update -y

Next, install Tomcat:

sudo yum install tomcat -y

Step 2: Modify Tomcat’s Server.xml File

By default, Tomcat uses port 8080. You need to change this to port 80. Open the server.xml file:

sudo nano /usr/share/tomcat/conf/server.xml

Find the following line:

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />

Change the port="8080" to port="80" and save the file.

Step 3: Configure Linux to Allow Tomcat to Use Port 80

As mentioned, Linux restricts ports below 1024 for the root user. To allow Tomcat to use port 80, we’ll use authbind. Install it using:

sudo yum install authbind -y

Next, give Tomcat permission to use port 80:

sudo touch /etc/authbind/byport/80
sudo chown tomcat:tomcat /etc/authbind/byport/80
sudo chmod 500 /etc/authbind/byport/80

Then, edit the Tomcat service file to use authbind:

sudo nano /usr/lib/systemd/system/tomcat.service

Find the line that starts with ExecStart and change it to:

ExecStart=/usr/bin/authbind --deep /usr/libexec/tomcat/server start

Save and close the file.

Step 4: Restart Tomcat

Finally, restart Tomcat to apply the changes:

sudo systemctl restart tomcat

You should now be able to access your Tomcat server on port 80.

Conclusion

This guide provides a solution to running Tomcat on port 80 in Amazon EC2 Linux instances. While Linux restricts lower ports for root access, authbind allows us to bypass this restriction, enabling Tomcat to operate on port 80. This method is preferred over running Tomcat as root, which poses security risks.

Remember to adjust your EC2 security group settings to allow incoming traffic on port 80. You should now be able to access your Tomcat server on port 80 from your browser using your EC2 instance’s public IP address or domain name.

Please Note: This tutorial assumes you are working with a fresh EC2 instance. If you have existing configurations or applications, make sure to backup your data before making these changes.

Keywords: Amazon EC2, Linux, Tomcat, port 80, authbind, server configuration, data science, software engineering


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.