Apache with Tomcat and mod_jk
  Apr 01, 2010
tomcat logo

Coming soon...



The Java Development kit (JDK) is an important dependency for Tomcat. Unfortunately, due to licensing restrictions, the files needed for installation are only available to download manually. Just play along and download all the files.

  1. # java -version
  2. # pkg_info | grep jdk

If you did not get any results in either grep, you will need to install the Java Development Kit (JDK)

Dependency: Java Development Kit (JDK)

  1. # cd /usr/ports/java/jdk16
  2. # make install clean

You will most likely end on with a stalled JDK installation and a screen full of links to download various JDK components. Make sure you go to each link and download the respective component. Double check that the filenames match exactly! I would strongly recommend putting all these files in a directory for later use to save you some time in the future.

  1. # mkdir ~/jdk16

To give you an idea on what you will need (at the time of this writing...)

  • bsd-jdk16-patches-5.tar.bz2
  • tzupdater-1_3_25-2009u.zip
  • jdk-6u3-fcs-mozilla_headers-b05-unix-24_sep_2007,jar
  • jdk-6u3-fcs-src-b05-unix-24_sep_2007,jar
  • jdk-6u3-fcs-bin-b05-unix-24_sep_2007,jar
  • diablo-caffe-freebsd7-i386-1.6.0_07-b02.tar.gz

Once you have all the files downloaded, copy them over to /usr/ports/distfiles (where port archives are stored).

  1. # cp ~/jdk16/* /usr/ports/distfiles
  2. # cd /usr/ports/java/jdk16
  3. # make install clean

Install tomcat, apache22, & mod_jk

Now that we have tomcat on our system, install the Apache webserver and the apache/tomcat connector.

  1. # cd /usr/ports/www/tomcat6
  2. # make install clean
  3. # cd /usr/ports/www/apache22
  4. # make install clean distclean
  5. # cd /usr/ports/www/mod_jk-apache2
  6. # make install clean

Enable everything to start at system boot.

  1. # echo 'tomcat60_enable="YES"' >> /etc/rc.conf
  2. # echo 'tomcat60_java_home="/usr/local/diablo-jdk1.6.0"' >> /etc/rc.conf
  3. # echo 'tomcat60_stdout_log="/var/log/tomcat6.log"' >> /etc/rc.conf
  4. # echo 'tomcat60_stderr_log="/var/log/tomcat6.log"' >> /etc/rc.conf

Install apache 2.2, tomcat 6, & mod_jk

  1. # cd /usr/ports/www/apache22
  2. # make install clean
  3. # cd /usr/ports/www/tomcat6
  4. # make install clean
  5. # cd /usr/ports/www/mod_jk
  6. # make install clean distclean
  7. # rehash

Configure tomcat 6

  1. # touch /var/log/tomcat6.log
  2. # chgrp www /var/log/tomcat6.log
  3. # chmod g+w /var/log/tomcat6.log
  4. # /usr/local/etc/rc.d/tomcat6 start
  5. # tail -n 30 /var/log/tomcat6.log
  6. # sockstat -l4 | grep java
  7. www     java    4824   27   tcp4    *:8180     *:*
  8. www     java    4824   43   tcp4    *:8009     *:*
  9. www     java    4824   46   tcp4    127.0.0.1:8005   *:*

Wait about 10 seconds and go to http://tomcat_hostname_or_ip:8180 or http://localhost:8180 if you are on the Tomcat box. If you got a generic Tomcat page similar to the one pictured below, all is well in the world. If you have problems, check out /var/log/tomcat6.log for errors and use /usr/local/etc/rc.d/tomcat6 stop and /usr/local/etc/rc.d/tomcat6 start to restart the Tomcat server, as tomcat can restart slowly and trip over it's own port when trying to reconnect when issued a /usr/local/etc/rc.d/tomcat6/restart!

Tomcat Welcome Screenshot

Running Tomcat in Users' Home Directories

Give Apache permission to serve out files from users' home directories.

  1. # vi /usr/local/etc/apache22/httpd.conf
  2. ... ... ... ... ...
  3. Include etc/apache22/extra/httpd-userdir.conf
  4. ... ... ... ... ...
  5. # apachectl restart

Give Tomcat a host to serve up requests for.

  1. # vim /usr/local/apache-tomcat-6.0/conf/server.xml
  2.       </Host>
  3. ... ... ... ... ...
  4.       <Host name="10.10.20.105">
  5.         <Listener className="org.apache.catalina.startup.UserConfig"
  6.          directoryName="public_html"
  7.          userClass="org.apache.caralina.startup.PasswdUserDatabase"/>
  8.       </Host>
  9. ... ... ... ... ...
  10.     </Engine>
  11.   </Service>
  12. </Server>

Restart the server so changes take effect. Also, watch the logs for any garbage!

  1. # /usr/local/etc/rc.d/tomcat6 restart
  2. # tail -n 30 /var/log/tomcat6.log
  3. # sockstat -l4 | grep java
  4. www     java    4824   27   tcp4    *:8180     *:*
  5. www     java    4824   43   tcp4    *:8009     *:*
  6. www     java    4824   46   tcp4    127.0.0.1:8005   *:*

Make a test file to make sure you can parse a .jsp file correctly in an unspecified user's directory.

  1. # su some_user
  2. $ mkdir -p ~/public_html
  3. $ vi ~/public_html/hello.jsp
  4. TESTING: <% out.println("Hello World!"); %>!

Now open up a web browser and visit http://hostname_or_ip:8180:/~some_user/hello.jsp or http://localhost:8180:/~some_user/hello.jsp if you are physically on the server. You should be able to parse any .jsp file in the web root directory (/home/some_user/public_html in our case) and any file in /home/some_user/public_html/servlets.

Configure apache 2.2

  1. # echo 'apache22_enable="YES"' >> /etc/rc.conf
  2. # kldload accf_http
  3. # echo 'acct_http_load="YES"' >> /boot/loader.conf
  4. # /usr/local/etc/rc.d/apache22 start

Verify that the webserver is working correctly by entering the new webserver's IP address in a browser on the local network and make sure you get a page that says It Works!.

http://localhost would work if you are in a browser on the new webserver box.

Now do some generic Apache configuration.

  1. # vi /usr/local/etc/apache22/httpd.conf
  2. ... ... ... ... ...
  3. ServerAdmin user@emailaddr.com
  4. ... ... ... ... ...
  5. Include etc/apache22/extra/httpd-default.conf
  6. ... ... ... ... ...
  7. # vi /usr/local/etc/apache22/extra/httpd-default.conf
  8. .
  9. ... ... ... ... ...
  10. ServerTokens Prod
  11. ... ... ... ... ...
  12. ServerSignature Off
  13. ... ... ... ... ...

Apache VHOST

  1. # cd /usr/local/etc/apache22/extra
  2. # mv httpd-vhosts.conf httpd-vhosts.conf.original
  3. # mkdir /var/log/httpd
  4. # vi /usr/local/etc/apache22/extra/httpd-vhosts.conf
  5. NameVirtualHost *:80
  6. <VirtualHost *:80>
  7.     ServerAdmin webmaster@example.com
  8.     DocumentRoot "/usr/local/www/apache22/data/example.com"
  9.     ServerName www.example.com
  10.     ServerAlias example.com web.example.com
  11.     ErrorLog "/var/log/httpd/example.com-error_log"
  12.     CustomLog "/var/log/httpd/example.com-access_log" common
  13. ... ... ... ... ...
  14.     JkMount /∗.jsp
  15.     JkMount /servlets/∗
  16. ... ... ... ... ...
  17. </VirtualHost>
  18. # mkdir /var/log/httpd
  19. # mkdir -p /usr/local/www/apache22/data/example.com
  20. # apachectl restart
  21. # vi /usr/local/www/apache22/data/example.com/test.jsp
  22. TESTING: <% out.println("Hello World!"); %>!

Don't forget that each vhost hostname must have a corresponding <Host> directive in Tomcat's conf/server.xml file to answer requests properly.

  1. # vim /usr/local/apache-tomcat-6.0/conf/server.xml
  2.       </Host>
  3. ... ... ... ... ...
  4.       <Host name="example.com"
  5.             appBase="/usr/local/www/apache22/data/example.com"
  6.             unpackWARs="true" autoDeploy="true">
  7.         <Context path="" docBase="public_html" debug="0" reloadable="true"/>
  8.         <Valve className="org.apache.catalina.valves.AccessLogValve"
  9.            directory="/var/log/tomcat6"
  10.            prefix="tomcat_access_"
  11.            suffix=".log" pattern="common" resolveHosts="false"/>
  12.       </Host>
  13. ... ... ... ... ...
  14.     </Engine>
  15.   </Service>
  16. </Server>
  17. # mkdir /var/log/tomcat6
  18. # chgrp www /var/log/tomcat6
  19. # chmod g+w /var/log/tomcat6
  20. # /usr/local/etc/rc.d/tomcat6 restart
  21. # tail -n 30 /var/log/tomcat6.log
  22. # sockstat -l4 | grep java
  23. www     java    4824   27   tcp4    *:8180     *:*
  24. www     java    4824   43   tcp4    *:8009     *:*
  25. www     java    4824   46   tcp4    127.0.0.1:8005   *:*

Now go to http://example.com/test.jsp (but with the hostname you chose!) in a web browser. You should see the text "TESTING: Hello World!" printed out if everything went smoothly.

Configure mod_jk

  1. # cd /usr/local/etc/apache22
  2. # cp workers.properties.sample workers.properties
  3. # vi workers.properties
  4. worker.list=localhost
  5.  
  6. worker.localhost.port=8009
  7. worker.localhost.host=localhost
  8. worker.localhost.type=ajp13
  9. worker.localhost.lbfactor=1
  10.  
  11. workers.tomcat_home=/usr/local/apache-tomcat6.0
  12. workers.java_home=/usr/local/diablo-jdk1.6.0
  13. # vi /usr/local/etc/apache22/Includes/mod_jk.conf
  14. LoadModule jk_module    libexec/apache22/mod_jk.so
  15. JkWorkersFile /usr/local/etc/apache22/workers.properties
  16. JkLogFile /var/log/mod_jk.log
  17. JkShmFile /var/log/mod_jk-runtime-status
  18. JkLogLevel error
  19. # apachectl restart



Post a New Comment

Name

Message

Security
Code

        (case insensitive & space between words)