Domain – Multiple Domains Single Tomcat 7 – Apache Mod_proxy

apache-2.2domaintomcattomcat7

I have been struggling with this for a day and more now. As i am new to server setup stuff.
Been following many tutorial and docs on net regarding the same, many hit and run, But got no luck.

Here is what i did till now.

  1. I got my vps running and it has Apache running at port 80.

  2. I installed Tomcat 7.0.55 at port 8080 and it is up and running.

Now we have to route traffic from Apache to Tomcat

  1. Following

    https://www.digitalocean.com/community/tutorials/how-to-use-apache-http-server-as-reverse-proxy-using-mod_proxy-extension

modifying the 000-default.conf of apache to look like this.

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

<VirtualHost *:*>
   ProxyPreserveHost On

   # Servers to proxy the connection, or;
   # List of application servers:
   # Usage:
   # ProxyPass / http://[IP Addr.]:[port]/
   # ProxyPassReverse / http://[IP Addr.]:[port]/
   # Example: 
   ProxyPass / http://localhost:8080/
   ProxyPassReverse / http://localhost:8080/

   ServerName localhost
</VirtualHost>

This successfully redirected my each request to tomcat.

  1. Now running my registered domain name in browser. www.mytestdomain.com. i am landing on the default tomcat page. and writing the server ip in browser address bar is also landing me to the same tomcat page.

5 Now i need www.mytestdomain.com to point to my app deployed in tomcat at $CATALINA_HOME/webapps/www.mytestdomain.com

6 And other test.mytestdomain.com to point to my app deployed in tomcat at $CATALINA_HOME/webapps/test.mytestdomain.com

7 And xx.xx.xx.xx my ip to land at default tomcat page only.

That what i want to achieve and not able to do that.

8 Now following

http://tomcat.apache.org/tomcat-7.0-doc/virtual-hosting-howto.html

I updated my $CATALINA_HOME/conf/server.xml to have one more host. After updating my server.xml file look

  <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">

    <!-- SingleSignOn valve, share authentication between web applications
         Documentation at: /docs/config/valve.html -->
    <!--
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
    -->

    <!-- Access log processes all example.
         Documentation at: /docs/config/valve.html
         Note: The pattern used is equivalent to using pattern="common" -->
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access_log." suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />

  </Host>

  <Host name="www.mytestdomain.com"  appBase="webapps-new"
        unpackWARs="true" autoDeploy="true">

    <!-- SingleSignOn valve, share authentication between web applications
         Documentation at: /docs/config/valve.html -->
    <!--
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
    -->

    <!-- Access log processes all example.
         Documentation at: /docs/config/valve.html
         Note: The pattern used is equivalent to using pattern="common" -->
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="www.mytestdomain.com_log." suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />

  </Host>
  1. I created new appBase directory mkdir $CATALINA_HOME/webapps-new

  2. Within $CATALINA_HOME/webapps-new folder i created symlink for my webapp.
    ln -s ../webapp/www.mytestdomain.com ROOT

  3. cd $CATALINA_HOME/webapps-new/ROOT/WEB_INF/

  4. Created context.xml in META-INF file with content as below.

  1. Restarted Apache and Restarted Tomcat – [ok]

  2. Results. –> www.mytestdomain.com in browser is hitting my tomcat webapp properly.

  3. But test.mytestdomain.com and xx.xx.xx.xx my ip in browser is also hitting my same webapp as for www.mytestdomain.com . They should have gone to the default host.

I had tried many other tricks but not luck.

Any help would be appreciated. This is very much possible that i know.

Thanks

Best Answer

This sounds rather complicated to me and not the way I would try to run it.

In general I try to separate as much as I can environments from each other, running on different servers ideally , but i understand this is not something you can do with a single VPS.

You should at least separate tomcat instances, this would reduce the mess and makes everything more clear.

I would

1) Start 2 different tomcat instances ( achievable in different ways depending on which distribution you are running) on different ports , prod:8080 and test:8180

2) Enable NameVirtualHost in your Apache configuration and proxy the right servername to the right tomcat instance

# Ensure that Apache listens on port 80
Listen 80

# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.mytestdomain.com

# Other directives here

ProxyPreserveHost On

ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/

</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/example2
ServerName test.mytestdomain.com

# Other directives here
ProxyPreserveHost On

ProxyPass / http://localhost:8180/
ProxyPassReverse / http://localhost:8180/
</VirtualHost>

this way your environment are completely separate and you can apply changes to test without affecting prod , not just inside your code but also at the tomcat configuration level and apache configuration level.