Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

To enable this without DNS, add the following line to your /etc/hosts file:

Code Pro
127.0.0.1		author.best-comics.net www.best-comics.net www.best-vinyl.net


Apache setup

Make sure you have a working Apache on your local machine. 

...

Code Block
apachectl configtest

Enable mod_proxy, proxy_ajp_module

Make sure your Apache has the modules following module mod_proxy and proxy_ajp_module enabled.

Check the /etc/apache2/httpd.conf file and make sure the following line is lines are not commented:

Code Block
LoadModule proxy_module libexec/apache2/mod_proxy.so
# more line in between ...
LoadModule proxy_ajp_module libexec/apache2/mod_proxy_ajp.so

On a different OS, the path and suffix of the module may be different. 

...

We use virtual hosts configuration in this setup. You must make sure vhosts are enabled. Check the  /etc/apache2/httpd.conf file and make sure the line with the include directive is not commented:

Code Pro
languagetext
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

If you edit the file, you must restart Apache.

...

If vhosts is properly enabled, edit the /etc/apache2/extra/httpd-vhosts.conf file and add something similar to this:

Code Pro
<VirtualHost *:80>
    ServerName author.best-comics.net

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

    ErrorLog "/private/var/log/apache2/projekt.local-error_log"
    CustomLog "/private/var/log/apache2/projekt.local-access_log" common    
</VirtualHost>

This configuration redirects the "author domain" author.best-comics.net to localhost:8080/magnoliaAuthor. The redirection works correctly, however, Tomcat still serves the pages from the context. Web resources do not load and redirections done by Magnolia are incorrect.

...

Expand
titleClick here to expand to see the server.xml


Code Pro
languagexml
titleserver.xml
linenumberstrue
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />


  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />


	<Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />

    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
		<!-- server author from root -->  
		<Context path="" docBase="magnoliaAuthor" />
    
	    <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>
	  
    </Engine>
	
  </Service>
  
</Server>

See line 37 - We ensure the context magnoliaAuthor is served from root. Note that Tomcat can only serve one webapp from root within the same host, and even within the same service.



It works. The same can be done with the context magnoliaPublic instead of magnoliaAuthor.

...

We add two more virtual hosts to Apache. Here is the httpd-vhosts.conf with three virtual hosts, one for the author instance and two for the public instance:

Code Pro
languagetext
linenumberstrue
<VirtualHost *:80>
    ServerName author.best-comics.net

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

<VirtualHost *:80>
    ServerName www.best-comics.net

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

<VirtualHost *:80>
    ServerName www.best-vinyl.net

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

Lines 11-12, 18-19: Note that the hosts www.best-comics.net and www.best-vinyl.net are redirected to localhost on port 8081, whereas author.best-comics.net redirects to localhost on port 8080.

...

This is the adapted Tomcat directory structure:

Code Pro
├── bin
├── conf
├── lib
├── webapps
│   ├── ROOT
│   └── magnoliaAuthor
└── webapps2
    └── magnoliaPublic


  • Create the folder webapps2.
  • Move the webapp magnoliaPublic to webapps2.

...

server.xml

This is the server.xml:

Code Pro
languagexml
linenumberstrue
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />


  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
	<Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>
      <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
		<Context path="" docBase="magnoliaAuthor" />
	    <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>
    </Engine>
  </Service>
  
  <Service name="Catalina2">
    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8444" />
	<Connector port="8010" enableLookups="false" redirectPort="8444" protocol="AJP/1.3" />
    <Engine name="Catalina2" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>
      <Host name="localhost"  appBase="webapps2" unpackWARs="true" autoDeploy="true">
		<Context path="" docBase="magnoliaPublic" />
	    <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>
    </Engine>
  </Service>
  
  
  
</Server>

Note the following:


  • There are two <Service> sections, their name attributes have different values.
  • Every <Service> has its own <Connector>
  • Each connector must have its own distinct ports on the attributes port and redirectPort.
  • The port of the <Connector> is the same port to which we redirect on the Apache virtual host configuration.

...