Before you get started, it’s important to keep in mind that a Spring Boot app is just a web app that’s packaged with an embedded Apache Tomcat Application Server. That means the process of setting up Tomcat to run behind an Apache Web Server is very similar to what you’ll see here. Bottom line: there’s no need to over-complicate this. While the post heading says running a Spring boot web application behind Apache, this article covers most of all J2EE based web applications be it Spring, Struts, Seam or JSF framework. It also broadly applies to any Web Service or Web Application which are bound to a port. A web server is a network service that serves content to a client over the web. This typically means web pages, but any other documents can be served as well. Web servers are also known as HTTP servers, as they use the hypertext transport protocol (HTTP). The Apache HTTP Server, httpd, is an open source web server developed by the Apache Software Foundation. The server-side of Spring-WS is designed around a central class that dispatches incoming XML messages to endpoints. Spring-WS's MessageDispatcher is extremely flexible, allowing you to use any sort of class as an endpoint, as long as it can be configured in the Spring IoC container. In a way, the message dispatcher resembles Spring's DispatcherServlet, the “ Front Controller ” used in. @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if spring-webmvc is on the classpath, this annotation flags the application as a web application and activates key behaviors, such as setting up a DispatcherServlet.
Spring Boot can run as a standalone server, but putting it behind an Apache web server has several advantages, such as load balancing and cluster management. Now with LetsEncrypt it’s easier than ever (and free) to secure your site with SSL.
In this tutorial, we’ll secure an Apache server with SSL and forward requests to a Spring Boot app running on the same machine. (And once you’re done you can add Stormpath’s Spring Boot integration for robust, secure identity
management that sets up in minutes.)
Set Up Your Spring Boot Application
The most basic Spring Boot web application just shows a homepage. Using Maven, this has four files: pom.xml
, Application.java
, RequestController.java
, and home.html
.
The pom.xml
file (in the root folder) declares four things: application details, starter parent, starter web dependency, and the Maven plugin (for convenience in running from the console).
RequestController.java
(src/main/java/com/stormpath/tutorial) maps all requests to the homepage.
2 4 6 8 | publicclassRequestController{ @RequestMapping('/') return'home.html'; } |
Finally home.html
(src/main/resources/static) is just declares a title and message.
2 4 6 8 10 12 14 16 | publicEmbeddedServletContainerFactory servletContainer(){ TomcatEmbeddedServletContainerFactory tomcat=newTomcatEmbeddedServletContainerFactory(); Connector ajpConnector=newConnector('AJP/1.3'); ajpConnector.setPort(9090); ajpConnector.setAllowTrace(false); tomcat.addAdditionalTomcatConnectors(ajpConnector); returntomcat; |
We’re setting the AJP port to 9090
manually. You might want to add a variable to application.properties
and pull it in with @Value
to make it more configurable.
Spring Boot Apache Web Server Download
Restart the app as above and you should see messages that Tomcat is now listening on both port 8080
and 9090
. Note: the Github repository above has the connector code included so you can just use that from the start.
Run the Application on Your Instance
In the screenshots above I’ve been running the web app on my local Windows machine for testing. To get it to run on your instance just do the following.
One last thing. The traffic between Apache and Tomcat is currently unencrypted (HTTP). This can be a problem for some apps (like Stormpath – which requires a secure connection). To fix this, we use something called Tomcat’s RemoteIpValve
. Enable this by adding the following to your application.properties
.