0

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

1

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.

  • Thanks for your answer and comments. Really helpful. But I will still look first for single instance case. That would be a learning too. Also let me try two virtual host pointing to 8080. @Whats is this DocumentRoot refer too here ? – Javanator Aug 5 '14 at 16:26
  • the document root here was actually just from the example i took :) if you are proxying / then it does not really matter, i would set it to "/var/empty". For the single instance version i would try see if the problem is in tomcat or an upper layer. make sure to have "NameVirtualHost *:80" in your apache config ... tcpdump on localhost port 8080 , open the file with wireshark and see if the servername is making into tomcat or not ... and tomcat is not really my field :) – przRocco Aug 5 '14 at 16:28
  • NameVirtualHost *:80" has no effect and will also be removed from next version. – Javanator Aug 5 '14 at 17:22
  • but the good news is two virtual hosts things worked. I created two virtual host and ProxyPass / localhost:8080/www.mytestdomain.com and ProxyPass / localhost:8080/test.mytestdomain. And respective appbase directory in CATALINA_HOME and HOST entry in server.xml of tomcat/conf. – Javanator Aug 5 '14 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.