Saturday, August 13, 2016

Setting up Tomcat + PostgreSQL for multi-tenant web apps in Linux


Have you ever been frustrated with those hard to set up cloud services? Ever been hit by those service's surprise fees? How about their feature limitations? Ever wondered how to setup Tomcat for a multi-tenancy web app? 

Well, my friend, this is your lucky day. Today I'll show you how I setup a local app server with Tomcat + PostgreSQL + Linux to host my Java web applications on a small ASUS machine that I made to be my "server". 

You can actually use similar technique to host your own websites (rather than web apps) out of your bedroom as well. There are quite a bit of small computers, which you can transform into a server. 

Remember, my goal was to host a multi-tenant Java web app on Tomcat, so that an user could type something like this on his browser:
http://tenant1.yourdomain.com

while another user - actually another tenant - could type something like this: 
http://tenant2.yourdomain.com

The companion article showing how to write the Java app itself is Writing a multi-tenancy web app with Spring.

The advantage of this kind of architecture for the developer is that he'll  maintain only one set of source code and deploy (or provision) an independent instance of the app for each of his clients. For a description of the multi-tenancy architecture, please follow these links:



So, here's what you'll need to complete this project:
. Linux 16 LTC
. Java 8
. Tomcat 8.0.30
. PostgreSQL 9.4
. OpenSSH
. WinSCP

If you don't want to install Linux, it's OK, you could easily adapt the steps to install everything on a Windows machine.

For some of the steps you'll may need to Google up for details, but here's the essence of it:

1. Install Ubuntu Linux 16 LTS
There are multiple ways of doing that, but I copied the image structure of files into a USB flash disk and start the install process from there.

2. Install Java 8
a) I don't remember whether or not Ubuntu installs Java automatically, but you can check by opening a terminal window and issuing this command:
java -version
The link to install 

3. Set the JAVA_HOME environment variable
gedit is the graphical editor for Linux, so if you installed Java 8 on /usr/lib/jvm/java-1.8.0-openjdk-amd64 as I did, then issue these commands:
a) open a terminal window
b) sudo gedit /etc/environment
c) insert these lines: 
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
export JAVA_HOME
d) save and exit
e) now reload by issuing this command:
. /etc/environment

4. Install Tomcat 8
You'll need both to install Tomcat 8 and configure it such that the HTTP server listen on port 80, so your visitors don't have to specify the port number from the outside of your network (Tomcat by default listens on port 8080).
a) follow this link to install Tomcat
https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-ubuntu-16-04
b) http://www.2ality.com/2010/07/running-tomcat-on-port-80-in-user.html

5. Configure Tomcat for multi-tenancy
In order for Tomcat to correctly respond appropriately to multiple web address, one for each tenant, you'll need to configure the file server.xml
a) sudo gedit /opt/tomcat/conf/server.xml
b) insert these 2 Host entries:
<host name ="tenant1.yourdomain.com" appBase="webapps_tenant1" unpackwars="true" autoDeploy="true" />
<host name ="tenant2.yourdomain.com" appBase="webapps_tenant1" unpackwars="true" autoDeploy="true" />
c) create the 2 folders accordingly
/opt/tomcat/webapps_tenant1
/opt/tomcat/webapps_tenant2

Here's the folder structure after this step:


6. Configure the "hosts" file
The hosts file contains the mapping table used by the browser to find your app or website. It comes with a single line usually "127.0.0.1 localhost", but you need to add these 2 extra lines, assuming the ip address of your Tomcat machine is 192.168.2.17:
192.168.2.17 tenant1.yourdomain.com
192.168.2.17 tenant2.yourdomain.com

7. Change the DNS for your domain name
This step varies from domain provider to domain provider, so it will require some trial and error, but essentially you'll need to specify the IP address your browser will connect to when the user types a web address such as http://tenant1.blueriversys.com.

8. Configure your Router's routing table
This step varies from router to router, but essentially it tells your router which is the machine that will handle the requests to port 80. 

In my case, it looks like this screenshot:


9. Install and configure SSH (optional)
SSH stands for Secure Shell and it allows you to create a Terminal connection to your Linux machine from your Windows dev machine. This is what will allow you to issue basic commands from Windows as well as use an SCP software for file transfer.
Here's a link that shows how to install and configure an SSH server:
https://help.ubuntu.com/community/SSH/OpenSSH/Configuring

10. Install WindowsSCP in Windows for file transfer (optional)
This step is optional but very handy because, if you take it, you'll be able to transfer the WAR files to the newly installed Linux machine right from your Windows dev machine. This is the only software in this procedure that you'll install in the Windows machine.
The following is a link that contains the install procedure as well as the guide on how to use WindowsSCP for file transfer:
https://winscp.net/eng/docs/guides

11. Copy the WAR files
Now, all that remains is for you to copy the WAR file from your dev machine to the tenant folders you created in step 5.  Make sure you rename the WAR file to ROOT.war as expected by Tomcat for this type of configuration. 

Here's the folder content for "tenant1":


Note: the ROOT folder itself is automatically created by Tomcat when you restart it.

And that's basically it, folks. For testing, just go anywhere outside your LAN network and type in the browser the web address you added to your "hosts" file.

Good luck and let me know in the comments if I can help you any further.

Thanks.

Joe

No comments:

Post a Comment