How To Install Tomcat6 With SUN-Java And Apache2 Integration On Ubuntu 10.04 [Lucid Lynx] With Virtual Hosts - Page 2

Want to support HowtoForge? Become a subscriber!
 
Submitted by Jaac (Contact Author) (Forums) on Mon, 2011-01-31 18:53. ::

4.3 Setup a new virtual host in Apache2

Create the folders for our new virtual host:

mkdir /var/www/server1.example.com
mkdir /var/www/server1.example.com/htdocs
mkdir /var/www/server1.example.com/logs

Then create a new site in Apache2:

vi /etc/apache2/sites-available/server1.example.com

Then add:

<VirtualHost *:80>
    JkMount /* default
    ServerName server1.example.com
    ServerAdmin webmaster@server1.example.com
    DocumentRoot /var/www/server1.example.com/htdocs
    ErrorLog /var/www/server1.example.com/logs/error.log
    CustomLog /var/www/server1.example.com/logs/access.log combined
    <Directory /var/www/server1.example.com/htdocs>
        Options -Indexes
    </Directory>
</VirtualHost>

Note: JkMount can be setup in different ways.

JkMount /* will use tomcat for all files
JkMount /*.jsp only for .jsp files
JkMount /folder/* for tomcat files in a specific directory

Enable the new virtual host we created, by running:

a2ensite server1.example.com

 

4.4 Setup a the new virtual host in Tomcat6

Open up the configuration file for Tomcat6:

vi /etc/tomcat6/server.xml

Find the following lines:

<!--
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
-->

And uncomment the connector port, like this:

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

Then scroll down and find:

  </Host>
</Engine>

AFTER the ending </Host> tag, and BEFORE the ending </Engine> tag, insert the following:

<!-- server1.example.com -->
<Host name="server1.example.com" appBase="/var/www/server1.example.com" unpackWARs="true" autoDeploy="true">
    <Context path="" docBase="htdocs" debug="0" reloadable="true"/>
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/var/www/server1.example.com/logs"  prefix="tomcat_access_" suffix=".log" pattern="common" resolveHosts="false"/>
</Host>

Make sure you replace server1.example.com with your details in all the places.

Now lets create a Catalina folder for our new domain:

mkdir /etc/tomcat6/Catalina/server1.example.com

And copy the original context files to new domain:

cp /etc/tomcat6/Catalina/localhost/* /etc/tomcat6/Catalina/server1.example.com/

But remove the ROOT.xml file, because we don't need that one on our new domain:

rm -f /etc/tomcat6/Catalina/server1.example.com/ROOT.xml

 

4.5 Create a test file and test our new setup

Create a new test.jsp file for our new virtual host:

vi /var/www/server1.example.com/htdocs/test.jsp

and add:

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        <h1>Hello World</h1>
        Today is: <%= new java.util.Date().toString() %>
    </body>
</html>

Save and exit the file.

Finally restart the services, so that the new configuration will load:

/etc/init.d/apache2 stop
/etc/init.d/tomcat6 restart
/etc/init.d/apache2 start

Then use your browser to visit http://server1.example.com/test.jsp and verify that you get the test page we just created.

You should also be able to visit http://server1.example.com/examples which is now also available on our virtual host (/docs, /manager/html and /host-manager/html should also work):

Your done!

 

References:

 

Paths:

Apache2 configuration

/etc/apache2/workers.properties
/etc/apache2/conf.d/mod_jk.conf
/etc/apache2/sites-available/server1.example.com
/var/www/server1.example.com/htdocs/

Tomca6 configuration

/etc/tomcat6/tomcat-users.xml
/etc/tomcat6/server.xml
/etc/tomcat6/Catalina/server1.example.com/

Restart Apache2 and Tomcat6

/etc/init.d/apache2 stop
/etc/init.d/tomcat6 restart
/etc/init.d/apache2 start


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Marten (not registered) on Tue, 2011-08-30 08:13.

This works fine, except that in Tomcat 6 now the app is deployed twice! This can cause problems with scheduled job (Quartz jobs), being run twice.

 The Tomcat docs say this about it:

When using automatic deployment, the docBase defined by an XML Context file should be outside of the appBase directory. If this is not the case, difficulties may be experienced deploying the web application or the application may be deployed twice. The deployIgnore attribute can be used to avoid this situation.

Finally, note that if you are defining contexts explicitly in server.xml, you should probably turn off automatic application deployment or specify deployIgnore carefully. Otherwise, the web applications will each be deployed twice, and that may cause problems for the applications.

 

Thus, move the /var/www/server1.example.com/htdocs directory to ie. /var/www/server1.example.com-docsdir/ (that is, move it OUT OF the server1.example.com dir, which is the current 'appBase' in our server.xml)

Submitted by Merrigan (registered user) on Sun, 2011-06-19 10:07.

Thanks, this is THE best Tutorial I have come across for this. Helped me a bunch!

Submitted by runner (not registered) on Wed, 2011-03-09 00:10.

Hello,

 my test.jsp is only accessible over port 8080. I can't find my failure. Any idea?

Submitted by Anonymous (not registered) on Fri, 2011-10-14 00:21.
I have the same issue.
Submitted by David Allwhole (not registered) on Sat, 2011-02-26 12:24.

Wonderful tutorial :)

Many Thanks