Tomcat: Difference between revisions

From Alpine Linux
m (Categorized: Web Server)
Line 49: Line 49:


More detailed description how [https://www.moreofless.co.uk/static-content-web-pages-images-tomcat-outside-war/ serving static content] with tomcat works.
More detailed description how [https://www.moreofless.co.uk/static-content-web-pages-images-tomcat-outside-war/ serving static content] with tomcat works.
[[Category:Web Server]]

Revision as of 03:24, 21 September 2017

Tomcat is a web server with a built-in Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket interpreter that can also serve static HTML pages.

This How-To describes how to set-up Tomcat to deliver static HTML pages as well as prepared WAR packages.

Installation

Java

Tomcat requires the Java Runtime Environment, which is available from the Alpine repository as openjdkN (where N stands for the version). Install the most up to date version of Java, 8 at the time of writing (September 2017).

apk add openjdk8

Tomcat

Tomcat has to be downloaded from the Apache Tomcat homepage. You need the Binary Distribution Core, download it, save it in a place where you can easily find it and extract it into a folder of your choice. As most other manuals for Tomcat use /usr/local/tomcat, this manual will assume that Tomcat is extracted there as well and call it $TOMCAT$ from now on.

cd /usr/local/tomcat

tar xvzf apache-tomcat-9.0.0.M26.tar.gz

Testing

Installation

Tomcat should run without any further configuration. Switch to $TOMCAT$/bin and execute the following command:

cd /usr/local/tomcat/bin

sh catalina.sh version

You should see a list of folders, the installed tomcat version, OS name, processor architecture and so on. If this fails, either the installed version of tomcat is somehow incompatible with the installed version of Java or one of the two programs was not properly installed.

Tip: Tomcat consists of several components, Catalina is the part responsible for running the server.

Server

If the first test succeeded, you can test the server itself by running catalina.sh with the start command.

sh catalina.sh start

Several lines of text should appear, the last line should read Tomcat started.

Now, point a browser on the same computer that Alpine runs on to http://127.0.0.1:8080/. You should see a page that says If you are seeing this, you successfully installed Tomcat. Congratulations!.

Tip: Make sure you included the :8080 after the IP adress. 8080 is the standard port of Tomcat, while your browser will search the server on port 80 unless you specify another port.

Running Tomcat

There are two ways to publish documents with Tomcat: Either a dynamic java application (for example a wiki, a forum, or a blog) or as static pages (HTML, images). Tomcat is specialized on dynamic applications, which are usually deployed as WAR file.

Dynamic Content

Installing a WAR file is easy: Simply copy it to $TOMCAT$/webapp. Tomcat will automatically install it to a folder with the same name as the WAR file (whatever is written before the .WAR extension). For example, if you install jspwiki.war, you can open the wiki on https://127.0.0.1:8080/jspwiki/.

Static Content

To deliver static content, you need to edit the file server.xml in the folder $TOMCAT$/conf.

Find the <Host appBase="webapps"> section in the server.xml, and add the entry <Context docBase="/home/stuff" path="/static" /> directly before the closing </Host>. Replace /home/stuff with the folder where you will save your static files. The /static is the part of the URL you need to open your static files, it can be changed to whatever you like.

More detailed description how serving static content with tomcat works.