This works – Apache + Tomcat + mod_jk

Good YouTube : http://www.youtube.com/watch?v=Njx1V4ZW_g0

Good YouTube w/LoadBalance : http://www.youtube.com/watch?v=yNuuoQLw0tA

This is a compact step-by-step guide for installing Tomcat with Apache and mod_jk on a Ubuntu 10.10 system considering following components

Apache 2, MySQL 5, PHP 5, PhpMyAdmin, Sun Java JDK 1.6, Tomcat 6.0, Tomcat-Admin, mod_jk

Important note: This is just the first version of this guide. It does not consider any previous configured or installed components


…Greyed out indicates should try, not tested…


http://java.qbytesworld.com/post/-Ubuntu-Server-Setup-.aspx

Server setup would allow for skipping steps:

  • 1 as we do not want MySQL on the same server and Apache2 is in setup (needs cleaned up)
  • 2 as we do not care about PHP (and already skipping)
  • 4 as we have it in the Setup

1 LAMP – Apache, MySQL, PHP (Add Tomcat)

$ sudo apt-get install tasksel
$ sudo tasksel

Remember your MySQL password.

Now create a test page


$ sudo nano /var/www/info.php

and insert following code

<?php
phpinfo();
?>

Restart Apache

/etc/init.d/apache2 restart

and check if everything went fine


2 PHPMyAdmin

Install via apt-get

$ sudo apt-get install phpmyadmin

Choose Apache, and insert credentials

Now create a soft-link to make phpmyadmin-sources available in /var/www

$ sudo ln -s /usr/share/phpmyadmin /var/www/phpmyadmin

Now open a browser and see if it’s working

http://[MY_IP]/phpmyadmin

(use the credentials you defined shortly before)


3 JAVA

Due Sun’s java is not provided by the repository anymore, we have to add it manually:

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:sun-java-community-team/sun-java6
$ sudo apt-get install sun-java6-jdk

Usually this should already be everything what we have to do. Try it by typing

$ sudo java -version

4 TOMCAT – Commons and Admin/Manager

Install both tomcat-common and tomcat-admin via apt-get

$ sudo apt-get install tomcat6
$ sudo apt-get install tomcat6-admin
$ sudo apt-get install tomcat6 tomcat6-admin tomcat6-common tomcat6-docs tomcat6-examples tomcat6-user

For accessing the Tomcat-admin we need to add our own manager-user

$sudo nano /etc/tomcat6/tomcat-users.xml

And add the new user

<role rolename="admin"/>
<role rolename="manager"/>
<user username="admin" password="my_secret" roles="admin,manager"/>

Finally restart Tomcat

$ sudo /etc/init.d/tomcat6 restart

Try if it works

http://[MY_IP]:8080/manager/html


5 Mod JK

Install via apt-get

$ sudo apt-get install libapache2-mod-jk

Now create our own workers.properties file for Apache.
Open or create

$ sudo nano /etc/apache2/workers.properties

and paste:

workers.tomcat_home=/var/lib/tomcat6
#workers.java_home=/usr/lib/jvm/java-6-sun
workers.java_home=/usr/lib/jvm/default-java
ps=/
# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
worker.default.lbfactor=1
Now tell the Apache to use this worker
$ sudo nano /etc/apache2/apache2.conf

and add following lines

<IfModule>
JkWorkersFile /etc/apache2/workers.properties
# Where to put jk shared memory
# Update this path to match your local state directory or logs directory
JkShmFile /var/log/apache2/mod_jk.shm
# Where to put jk logs
# Update this path to match your logs directory location (put mod_jk.log next to access_log)
JkLogFile /var/log/apache2/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
LockFile ${APACHE_LOCK_DIR}/accept.lock
# Select the timestamp log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkRequestLogFormat "%w %V %T"
</IfModule>

Finally configure which URLs Apache should pass through the Tomcat

Serving our Tomcat pages
$ sudo nano /etc/apache2/sites-enabled/000-default
JkMount /mytomcatwebappname* worker1
JkMount /docs* worker1
JkMount /examples* worker1
JkMount /manager* worker1

JkMount /sample* worker1
This means, that every URL that contains the key mytomcatwebappname gets handled by Tomcat instead of Apache.

(If you want to pass all requests to the Apache (instead of individual ones), just remove the line
DocumentRoot /var/www
and add following JkMounts:

JkMount / worker1
JkMount /* worker1
)

Serve some Java JSP Site called QBW-Web-Parent.com

$ sudo nano /etc/apache2/sites-available/QBW-Web-Parent.com

<VirtualHost *:80>
DocumentRoot /var/lib/tomcat6/webapps/QBW-Web-Parent
ServerName QBW-Web-Parent.com
JkMount /*.jsp worker1
JkMount /* worker1
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/lib/tomcat6/webapps/QBW-Web-Parent
ServerName QBW-Web-Parent.com
JkMount /*.jsp worker1
JkMount /* worker1
SSLEngine On
# SSL Engine Switch:
# Enable/Disable SSL for this virtual host.
SSLEngine on
# A self-signed (snakeoil) certificate can be created by installing
# the ssl-cert package. See
# /usr/share/doc/apache2.2-common/README.Debian.gz for more info.
# If both key and certificate are stored in the same file, only the
# SSLCertificateFile directive is needed.
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
</VirtualHost>

=====================================================

And last but not least, we have to enable the redirect port 8443 on Tomcat

$ sudo nano /etc/tomcat6/server.xml

And uncomment / insert / update that following line is present

<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:

<!– QBW-Web-Parent.com –>
<Host
name="QBW-Web-Parent.com"
appBase="webapps/QBW-Web-Parent"
unpackWARs="true"
autoDeploy="true">
<Context
path=""
docBase=""
debug="1"
reloadable="true"/>
<Valve
className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
prefix="tomcat_access_"
suffix=".log"
pattern="common"
resolveHosts="false"/>
</Host>

Now we only have to restart first the Tomcat and then the Apache

$ sudo /etc/init.d/tomcat6 restart
$ sudo /etc/init.d/apache2 restart

Et voila, now it should work as expected.


http://www.javathinking.com/2007/10/tomcat-with-apache2-virtual-hosts/

http://www.debuntu.org/2006/02/22/7-virtual-hosting-using-apache-2

http://www.tequilafish.com/2007/08/02/apache-virtualhost-on-ubuntu/

6 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 /* defaultServerName server1.example.comServerAdmin webmaster@server1.example.comDocumentRoot /var/www/server1.example.com/htdocsErrorLog /var/www/server1.example.com/logs/error.logCustomLog /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


6 Setup a 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):


SSL on Apache2

http://java.qbytesworld.com/post/apache-tomcat-ubuntu-1104.aspx


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/

Tomca6 webapp path

/var/lib/tomcat6/webapps/QBW-Web-Parent

Tomca6 Cache

/var/cache/tomcat6/Catalina/localhost

Restart Apache2 and Tomcat6

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


Information via: http://blog.cwill-dev.com/2011/04/07/installing-apache2-and-tomcat6-with-mod_jk-on-ubuntu-10-10/

Installing Apache2 and Tomcat6 with mod_jk on Ubuntu 10.10


Tag Cloud