How can I host a Java website/app on my Linode?

Linode Staff

WordPress is cool and all, but I wanna serve up some servlets.

1 Reply

This is a great question! For the most part, hosting a Java application follows the same workflow as setting up any other application but with the exception of using a servlet container as opposed to a "standard" webserver (Apache, nginx, etc).

1. Download the Java Development Kit

You can do this using the following command:

sudo apt install default-jdk

2. Download Tomcat

  • Navigate to the Tomcat download page
  • Under Binary Distributions>Core, right-click on the link to the tar.gz file for the latest version of Tomcat (at the time of writing, v9.0.22) and copy the link address
  • In your Linode, cURL this address:
    curl -O http://ftp.wayne.edu/apache/tomcat/tomcat-9/v9.0.22/bin/apache-tomcat-9.0.22.tar.gz
  • Create the directory /opt/tomcat/, and download Tomcat to it:
    sudo mkdir /opt/tomcat
    sudo tar xzvf apache-tomcat-9*tar.gz -C /opt/tomcat --strip-components=1

3. Configure Tomcat

If you'd like to change the port Tomcat listens over (set to 8080 by default), enable directory listing, or allow for automatic reloads after code changes, you can do so in the conf\server.xml, conf\web.xml, and conf\context.xml files respectively. Keep in mind that this step is not necessary.

4. Start Tomcat

Assuming you installed Tomcat to the aforementioned /opt/tomcat directory, you can start your server with the following commands (note that you may need to do so after using 'chown' to give a non-root user ownership):

cd /opt/tomcat/bin
./catalina.sh start

This will start Tomcat as a background process -- in order to observe logs, you'll need to enter the following:

tail -f logs/catalina.out

You can verify that Tomcat is running by visiting your Linode's IP address via a web browser while specifying the port Tomcat is listening over (by default, port 8080). To shut Tomcat down, run the shutdown.sh shell script in Tomcat's bin directory.

5. Upload your webapp

Create a context root (your app's base directory in which all of your HTML/CSS/images/scripts/etc should reside), under Tomcat's webapps directory named after your app -- for example, for an app named "northstar" you would run:

sudo mkdir /opt/tomcat/webapps/northstar

Once you've done so, create the following subdirectories:

  • <context_root>/WEB-INF - not visible to the web users; this is where your application's web descriptor file goes</context_root>
  • <context_root>/WEB-INF/classes - where you keep all the Java classes (servlet class files)</context_root>

Upload your HTML/servlet/JSP files to the appropriate directories. Start your Tomcat server up again while specifying your app name at the end of your IP address (for example, 127.0.0.1/northstar) and you should see your project displayed at your Linode's IP address. If you need any help with creating DNS records to point a domain of your choice to your Linode's IP address, I'd recommend taking a look at our most essential DNS guide.

From here, you might also want to consider creating a systemd file for Tomcat so that you can manage it via systemctl, creating a non-root user that has ownership of Tomcat and its services for security purposes, and editing your firewall to suit your needs.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct