Technology and Security

Tag: www

building apache tomcat from source

Building Apache Tomcat from Source

Building Apache Tomcat from source is easy, and is the first step to building your own Tomcat mods. Comprehensive instructions are provided by Apache in the BUILDING.txt file. This article will augment those steps with screenshots and a few helper scripts to make the job a bit easier. If you haven’t already done so, provision a plain Linux virtual machine. Directions for this process can be found in this article.

What is Apache Tomcat?

If you are reading this article and do not already know the answer to this question, you are probably in the wrong place. Apache Tomcat is a web container. It is a free open source implementation of the Jakarta Servlet, Jakarta Server Pages, Jakarta Expression Language, Jakarta WebSocket, Jakarta Annotations and Jakarta Authentication specifications. In a nutshell, Tomcat provides an HTTP web server environment for hosting Java code.

Virtual Machine Setup

As mentioned earlier, directions for creating an Ubuntu virtual machine can be found here. We will use that same process to create a standard Linux workstation with the following specifications:

  • OS: Ubuntu 22 LTS (although any modern version will be fine)
    • Minimal installation
  • RAM: 4GB
  • Drive: 20GB
Linux VM Specifications
Linux VM Specifications
Linux Minimal Installation
Linux Minimal Installation

Next we install the tools needed to compile Tomcat by cloning a helper repository maintained by AttainIT Technologies.

$ cd ~
$ sudo apt install git -y
$ git clone https://github.com/AttainIT-Technologies/vm-bootstrap-ubuntu.git
$ cd tomcat-dev
vm-bootstrap-ubuntu repo
vm-bootstrap-ubuntu repo
Clone GitHub Repo
Clone GitHub Repo

Finally we run the bootstrap script which will install java and ant, as well as download the source and setup the Tomcat build directory. The setup script is small and can be viewed here.

$ ./bootstrap
Workstation Setup
Workstation Setup

At this point we have completed our workstation setup and are ready to build Apache Tomcat from source.

Building Apache Tomcat from Source

Now that we have our workstation, we can build the source. Since we cloned the default branch, we have the main branch of Tomcat (most recent updates). Depending on your needs, you could replace this code with one of the other branches for Tomcat v8.x or 9.x releases.

tomcat repo
tomcat repo

Compile the source by running the following commands:

$ cd $TOMCAT_SRC
$ ant
Relocate to Tomcat Source
Relocate to Tomcat Source

The build is a bit like jumping into a time machine, as it still uses Apache Ant for its build tool. Depending on your workstation’s horsepower, the build could take up to several minutes to complete.

Building Tomcat from Source
Building Tomcat from Source

Once complete, you can change into the output directory and see the results.

Apache Tomcat Build Results
Apache Tomcat Build Results

Looking at the build directory should look very familiar, as it is a standard Tomcat distribution as you would normally download directly from Apache.

Apache Tomcat Distribution
Apache Tomcat Distribution

Since our source code has been compiled into executable binaries, lets continue on and launch our web container.

Running Apache Tomcat

Before launching our server, we should first check our version:

$ ./version.sh
Tomcat Version
Tomcat Version

Buried in the results above you can see that we have built Apache Tomcat version 10.1.0-M18-dev. Launch the server by running startup:

$ ./startup.sh
Launch Apache Tomcat
Launch Apache Tomcat

Finally, open the console in a browser:

https://localhost:8080
Web Console
Web Console

Congratulations, you have built the latest milestone release of Apache Tomcat.

Inspecting the Apache Tomcat Source

The bootstrap script also installs a recent copy of Microsoft Visual Studio Code. Use that IDE to inspect the Tomcat source code by running the following commands:

$ cd $TOMCAT_SRC
$ code .
Launch Visual Studio Code
Launch Visual Studio Code
Apache Tomcat Source Code
Apache Tomcat Source Code
AttainIT Loves Apache
Customizing Boostrap Icons

Using Bootstrap Icons

Bootstrap 5 Icons is a “free, high quality, open source icon library with over 1,600 icons.” It is provided by the same Twitter team who built Bootstrap. Since the icons are SVGs you can include them into your HTML in several different ways. This allows for flexibility to support how your project is setup. Using this library we will create a bootstrap button with icon.

Installing Icons

The icon set can be downloaded here. However, there is no need to pull in the whole package. Just copy in the icons you need for your project.

Embedding Icons as SVG

On our projects, we like to import the icons directly as SVG. The following example shows the phone-fill icon being added in a hidden block.

bootstrap 5 embedded svg
bootstrap 5 embedded svg

At this point, the icon is embedded in the page but not visible.

Creating a Bootstrap Button with Icon

Once the icon is embedded it is ready to use. Below is an example of using the embedded icon as part of a button.

<button type="submit" class="btn"><svg style="margin-right: 5px;" class="bi" width="1.5em" height="1.5em"><use xlink:href="#phone-fill"/></svg> Contact Us</button>

This will result in a very plain looking rendered button. Next we will customize the coloring for dark backgrounds.

contact us button
contact us button

Customizing the Bootstrap 5 Icon Color

The are a number of ways to customize the icon color, some are documented on the Bootstrap Usage page. A more direct route which offers more flexibility is to directly update the SVG path. This direct manipulation allows each path to be colored differently. Below the SVG is being updated to a white fill so it will stand out on a dark background.

<symbol id="az-sort" viewBox="0 0 16 16">
    <path fill="#ffffff" d=. . .
</symbol>

When paired with a dark button this will result in a modern and stylish button.

sort button with white icon
sort button with white icon
apache http docker container

Getting Started with httpd on Docker

This article documents how to get started using Apache httpd on Docker. We will create a static website prototype using Windows 10. The website will start as a simple html page. We’ll then integrate the Bootstrap framework to allow us to easily include modern look and feel features. Finally, we will import Font Awesome to give the page some graphic icons.

If you work in IT, sooner or later you are going to find yourself needing to mock up a webpage or two. Maybe you have something to demonstrate to a customer. Or maybe your own website is a bit old and tired and needs a refresh (it happens to the best of us).

Many will follow the path of least resistance and try to get away with hacking a solution together using only a text editor and browser. This works for the most simple of cases, but it won’t take long before you need something a little more robust.

HTTP Server (httpd)

apache httpd web server
apache httpd web server

Apache has a tenured track record and is still the most popular open source web server on the internet. It offers everything you need to quickly get up and running.

Before containers, using a full featured web server on your workstation often came with a few headaches, not the least of which was installation. If you were building on Windows, you also needed to deal with the Control Panel and Firewall settings. Sometimes you already had a web server running and needed to deconflict ports. All of these problems disappear with the ability to quickly launch an isolated environment in a container.

Using Containers

docker logo
docker logo

Docker is a software platform for managing containers. It enables developers to bundle applications into containers—standardized executable artifacts combining source code with operating system libraries and dependencies required to run that code.

We are going to assume you already have it running on your workstation. If not, there is no shortage of tutorials available online. Once the software is installed, Docker Hub is used to locate the official Apache httpd image and start the prototpe.

Getting Started

We are running Windows 10 and using the Windows System for Linux (WSL2 Ubuntu) for our commands. The first step is to create a directory and use a text editor to create the most basic of web pages.

simple html webpage
simple html webpage

After saving our webpage, we will launch a container from the same directory with the following command:

docker run -dit --name my-apache-app -p 8080:80 -v "$PWD":/usr/local/apache2/htdocs/ httpd:2.4

The container launches in just a few seconds and is hosting our simple webpage on localhost port 8080.

docker commands to run httpd
docker commands to run httpd
hello world running in a container
Hello World webpage running in a container

Importing Vendor Frameworks

Next we will import two commonly used software frameworks for building our website prototype: Bootstrap and Font Awesome. First create a vendor directory, and then download and unpack the two frameworks. This will give you a directory structure as follows:

importing web frameworks
importing web frameworks

With the two frameworks installed, edit the original html page. We need to add the necessary links to the framework, as well as additional code to demonstrate the functionality.

updated webpage using bootstrap and font awesome
updated webpage using bootstrap and font awesome

After the file has been updated and saved, simply refresh the browser.

final webpage
final webpage example

Cleaning Up

The container will continue running until you stop it with the following command:

docker container stop my-apache-app

Once completely done, you can delete the container with this command:

docker container rm my-apache-app

Powered by WordPress & Theme by Anders Norén