I use the word run in the loosest possible sense as the Servlet application will actually be driven from inside Tomcat which will be proxied to from Xampp or more specifically the Apache web server that comes bundled with Xampp.
Even though Xampp can be installed on windows, mac and linux, this article will focus on installing it on linux and using it as a proxy (reverse proxy) to Tomcat, which will be hosting the servlet application.
Table Of Contents
It comes with a handy control panel, upon installation, that can be used for the management thereof.
As stated above it also comes bundled with a variety of services typically found together in web development making it a one stop shop for developers inclined to use the stack.
The example code in this article was built and run using:
- Java 8
- Maven 3.3.9
- STS (3.9.0.RELEASE)
- Ubuntu 16.04
- Tomcat 8.5.x
To confirm the correct version of Java:
To confirm the correct version of Maven:
The sample code provided for this article will only need to be built as the goal of this article is to illustrate how to install and configure Xampp to proxy requests to Tomcat, hence the focus will not be on the code used to demonstrate a working application but rather the setup itself.
One can download Xampp for linux here. Once downloaded change the permissions of the downloaded file to allow us to execute it from the command line:
chmod a+x xampp-linux-x64-126.96.36.199-installer.run sudo ./xampp-linux-x64-7.1.10-0-installer.run
Once executed the following sequence of screenshots showcase some of the vital steps of the install wizard which neatly guides you through the installation process.
- Ensure you select
XAMPP Core Files.
- Of importance on the launch screen is the
Manage Serverstab which will present you with a screen showing the running or at least runnable processes bundled with the suite. You will be able to manage (start / stop / configure) them from there.
A closer inspection of the control panel reveals the following services:
With other installs (windows) you will have the luxury of a Tomcat process, however we do not have such luck.
Before going further some clarification regarding proxies and their use.
Typically one can distinguish between two main types of proxy servers,
forward proxy serversand
reverse proxy servers.
A healthy explanation between the two exists here but for those not wanting to suffer the extra hyperlink
click I will try to distill it in a few words below:
- A forward proxy acts as an intermediary between the client and a destination / target server where the client will request something explicitly from the destination server via the proxy. The proxy will then forward the request on to the destination server. The client has to have knowledge of the proxy in this case, and thus be configured to use it.
- A reverse proxy, however, appears to the client just like any web server and in fact the client will request something from the reverse proxy, whilst unbeknownst to the client, the reverse proxy will delegate the request on to anther server that will ultimately satisfy the request, this is in fact what we will be doing in this article.
Why go to all this trouble? They say in computer science most problems can be solved by an extra layer of indirection and a proxy is an extra layer of indirection. What this gives us is:
- The ability to load balance requests between an array of target servers behind the scenes, thus allowing us to scale in the face of high load.
- Offload certain tasks before hitting application servers, like SSL offloading (certificate negotiation).
- Extra security by being able to intercept malicious requests before hitting the application servers.
The first step is to ensure that the AJP protocol connector is enabled for Tomcat. This can be done by navigating to the tomcat install / download folder and navigating into the
conffolder and editing the
server.xml file and ensure the following line is present and not commented out.
Tomcat AJP connector configuration
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
The next step is to ensure that the Apache web server in Xampp is configured to use the
mod_proxy module and that the
httpd-vhosts.conf is set to be loaded.
This can be confirmed / configured by clicking on the
configure button of the Apache web server and on the
Open conf filebutton. This will open the
httpd.conf file located in
Ensure the following lines appear and are not commented out.
Apache Web server httpd.conf settings
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_connect_module modules/mod_proxy_connect.so LoadModule proxy_ftp_module modules/mod_proxy_ftp.so LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule proxy_ajp_module modules/mod_proxy_ajp.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule proxy_express_module modules/mod_proxy_express.so .... # Virtual hosts Include etc/extra/httpd-vhosts.conf
The final step is to ensure that the virtual hosts configuration is correct for the
Open the above-mentioned file for edit
sudo vim /opt/lamp/etc/extra/httpd-vhosts.conf and insert the following text:
<VirtualHost *:80> DocumentRoot "******" ServerName localhost ProxyRequests Off ProxyVia Off ProxyPass /chat ajp://127.0.0.1:8009/chat ProxyPassReverse /chat ajp://127.0.0.1:8009/chat </VirtualHost>
The above-mentioned configuration will ensure that any requests to the context path
chat on the default port
80 served on
localhost by Apache web server will proxy the request via AJP on port
8009 to Tomcat.
Download and extract the sample project (websocket-example) to somewhere on your file system. Navigate to the project root folder and execute:
mvn clean install package.
Once complete, navigate into the
target folder and copy the packaged
chat.war to the
webapps folder of your Tomcat installation.
Navigate into the
binfolder of Tomcat and execute
./startup.sh. The following snippet illustrates this:
jean-jay@jeanjay-SATELLITE-L750D:~/runtimes/apache-tomcat-8.5.23-xampp$ ls -al total 128 drwxr-xr-x 9 jean-jay jean-jay 4096 Sep 28 11:31 . drwxrwxr-x 13 jean-jay jean-jay 4096 Nov 5 20:15 .. drwxr-xr-x 2 jean-jay jean-jay 4096 Sep 28 11:31 bin drwxr-xr-x 3 jean-jay jean-jay 4096 Nov 6 13:06 conf drwxr-xr-x 2 jean-jay jean-jay 4096 Sep 28 11:31 lib -rw-r--r-- 1 jean-jay jean-jay 58153 Sep 28 11:31 LICENSE drwxr-xr-x 2 jean-jay jean-jay 4096 Nov 6 13:06 logs -rw-r--r-- 1 jean-jay jean-jay 1774 Sep 28 11:31 NOTICE -rw-r--r-- 1 jean-jay jean-jay 7241 Sep 28 11:31 RELEASE-NOTES -rw-r--r-- 1 jean-jay jean-jay 16416 Sep 28 11:31 RUNNING.txt drwxr-xr-x 2 jean-jay jean-jay 4096 Sep 28 11:31 temp drwxr-xr-x 8 jean-jay jean-jay 4096 Nov 6 13:11 webapps drwxr-xr-x 3 jean-jay jean-jay 4096 Nov 6 13:06 work jean-jay@jeanjay-SATELLITE-L750D:~/runtimes/apache-tomcat-8.5.23-xampp$ cd bin jean-jay@jeanjay-SATELLITE-L750D:~/runtimes/apache-tomcat-8.5.23-xampp/bin$ ./startup.sh Using CATALINA_BASE: /home/jean-jay/runtimes/apache-tomcat-8.5.23-xampp Using CATALINA_HOME: /home/jean-jay/runtimes/apache-tomcat-8.5.23-xampp Using CATALINA_TMPDIR: /home/jean-jay/runtimes/apache-tomcat-8.5.23-xampp/temp Using JRE_HOME: /usr Using CLASSPATH: /home/jean-jay/runtimes/apache-tomcat-8.5.23-xampp/bin/bootstrap.jar:/home/jean-jay/runtimes/apache-tomcat-8.5.23-xampp/bin/tomcat-juli.jar Tomcat started. jean-jay@jeanjay-SATELLITE-L750D:~/runtimes/apache-tomcat-8.5.23-xampp/bin$
Accordingly you can stop Tomcat by issuing the command
./shutdown.shfrom within that same
binfolder to shutdown the service.
Starting Apache can be done from the Xampp control panel which itself can be started by executing the following on the command line:
sudo /opt/lampp/manager-linux-x64.run Once up, navigate to the
Manage Serverstab and start Apache.
We also browsed to a Java Servlet application hosted on Tomcat via Xampp’s Apache web server. We discussed several of the reasons as to why this would be done as well as the details regarding the configuration used to facilitate this setup.
You can download the full source code of this example here: Java Servlet XAMPP Tutorial