HOWTO : Installing Web Services with | |
Linux / Tomcat / Apache / Struts / Postgresql / OpenSSL / JDBC / JNDI / DBCP |
|
Welcome to LinuxJava.net | |
Created by Oscar Carrillo - Updated on 05/10/2005 @ 18:04 |
| You use these instructions at your own risk. I am not responsible for any damage or loss to your data, or damage or loss to your business, etc. |
Table Of Contents
When I first tried to build a webapp, it was very difficult to understand how all these open source technologies connect together. I find these tools to be very powerful and well-written, but daunting to configure them for a useful project. I hope this page assists people in getting these technologies to work together. I hope this creates a larger user base and thus ultimately making these technologies better. This is my small contribution to these projects that many others contribute their valuable time and resources to.
My previous hosting abilities came to an end, and I needed a new home for my Documentation Project. A special thanks to ServePath for supporting my Documentation Project by donating the co-location of my server. They were very helpful, especially in providing me extra IPs so I could test out SSL deployment. They do various hosting scenarios, including Tomcat and Apache hosting. Check out ServePath hosting and if you do decide you like them, please use this link for ordering as it provides me as a reference, and I get a small referral fee.
I have added a lot to this page lately. I have added install scripts that basically do the actions that I describe below. You can download them and run them to install the individual packages. In addition, I have added the configuration scripts for Apache ("httpd.conf", "ssl.conf"), and Tomcat ("server.xml"). And I am providing a sample webapp that uses all the relevant tools listed on this page. It's basically a reference implementation for showing how to combine these tools (Apache/OpenSSL/Tomcat/Postgresql/JDBC/JNDI/Struts) to deploy a webapp using the MVC model. I also hope to add Realms configuration and Tiles to the mix. Download all my install scripts, config files, misc files here
If you live in the San Francisco Bay Area and are a Struts user, consider joining the Struts User Group. Yahoo Groups Forum here: Struts User Group (Yahoo).We had a very good presentation on August 6th that was an introduction to Struts. Follow the link and you will be able to download the slides and source code used for the presentation. The talk was also video recorded and we'll be putting that up shortly as well. We had another very excellent presentation on November 5th by Craig McClanahan on "Struts and Java Server Faces". It's also video taped and should be up shortly. Stay tuned...
I've made corrections recently on this page, and there may be more so check back periodically. I have made significant changes/additions to this document and have used the LATEST packages for all. If you wish to view the "old" version, you can go to the archive. This newer version is much better though.
If this document has helped you or did not quite work for you, or if you found any errors, please send me an email at oscar <AT> linuxjava <DOT> net.
There is a security vulnearability in openssl and I list the latest here. The revised version was released on 03/17/2004, so if you did this tutorial before then, you will want to download Openssl and re-compile Openssl and Apache. |
Coming Soon: A Hibernate Tutorial
I have a mix of packages from source and binaries. I find my mix of source and binaries works well.
The system I installed this setup on is RedHat 9+updates/kernel 2.6.1-mm5 RAID(1), on this hardware. Beware that some people have reported problems with threading, unless the LD_KERNEL_ASSUME=2.2.5 environment variable is set, which is in my daemon scripts(but commented out). I think threading is fine now w/o this. I suspect that if you do all the RedHat updates and compile your own kernel, that you won't need to set the LD_KERNEL_ASSUME as mentioned above. So far this setup has worked without a hiccup though it hasn't had much stress testing. I plan to do 2.4 vs 2.6 kernel comparisons for this install.
If you're interested in burning multiple CDs in Linux simultaneously see my CD / DVD burning HOWTO
If you're interested in playing my favorite FPS game in Linux Gore in Linux
Java binaries (bytecode) usually install with no problems for obvious reasons.
But for others, I like the ability to get the new source code (especially if there's a major bug or security flaw) and re-compile.
Recent binaries are hard to come by for some platforms.
I am in the process of updating all this information for more current packages with a few more details and prudent measures.
I'm contemplating on doing the following HOWTOs:
- Linux kernel 2.4 vs 2.6 comparisons
- Tomcat Realms
- Tiles
|
A Legend of my text formatting |
| Green means a command you give at the console |
| Blue means configuration settings you must add |
| Italics means configuration settings you must find |
| Bold denotes paths such as /usr/local/src |
| I use these background colors |
| to aid in differentiating between lines |
| Get all appropriate files from web and place them in /usr/local/src/. |
Make sure NO rpms are installed for these packages, except for openssl which you may want to keep for other dependencies. That's fine as Apache will only be pointing to the new SSL libraries.
Now for a description of my build environment. I put all the packages, install scripts, and configuration files in "/usr/local/src/webapps/". Make this directory and put these files in there or somewhere else to keep your files organized. Get all these files here. All the descriptions below are basically contained in these files.
Edit and add the lines below to "/etc/profile"
Make sure you logout and login for this to take effect. If you're in XWindow you might need to quit out of XWindow and exit primary shell. Then login again and start XWindow again (startx). It's really good to get this straight from the beginning.
| JAVA_HOME=/usr/local/java/java |
| CATALINA_HOME=/usr/local/tomcat |
| PATH=$JAVA_HOME/bin:$PATH:$HOME/bin:/sbin:/usr/sbin |
| CLASSPATH=$CATALINA_HOME/bin/bootstrap.jar:$JAVA_HOME/lib/tools.jar:$CATALINA_HOME/common/lib/servlet.jar:/usr/local/pgsql/share/java/postgresql.jar:../lib/struts.jar:. |
| Now add the PATH JAVA_HOME CATALINA_HOME & CLASSPATH if any aren't in the export line |
| export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC JAVA_HOME CATALINA_HOME CLASSPATH |
| This will output your environment variables |
| env |
|
| mkdir /usr/local/java |
| cd /usr/local/java |
| sh jdk-1_5_0_04-linux-i586.bin |
| The jdk directory is then extracted |
| mv jdk-1_5_0_04 /usr/local/java |
| ln -s jdk-1_5_0_04 java |
| Now the java directory will be in /usr/local/java/java. I do it like this so I can keep all my different JDKs/JREs in one directory and then just change the symbolic link to point to the current one. |
|
| tar xvfz jakarta-tomcat-4.1.29.tar.gz |
| mv jakarta-tomcat-4.1.29 /usr/local/ |
| cd /usr/local |
| ln -s jakarta-tomcat-4.1.29 tomcat |
|
| tar xvfz jakarta-ant-1.6.0 |
| mv jakarta-ant-1.6.0 /usr/local |
| cd /usr/local |
| ln -s jakarta-ant-1.6.0 ant |
| ln -s /usr/local/ant/bin/ant /usr/local/bin/ant |
|
| tar xvfz openssl-0.9.7d.tar.gz |
| mv openssl-0.9.7d /usr/local/src/ |
| cd /usr/local/src/openssl-0.9.7d |
|   |
| ./config |
| make |
| make test |
| make install | #This will install in /usr/local/ssl |
|
| tar xvfz httpd-2.0.48.tar.Z |
| cd httpd-2.0.48 |
| ./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite \ |
|                 --enable-ssl --with-ssl=/usr/local/ssl --enable-proxy |
| make |
| make install | #(places in /usr/local/apache2) |
|
| Build mod_jk |
| cd /usr/local/src/jakarta-tomcat-connectors-jk-1.2.5-src/jk/native |
| ./buildconf.sh |
| ./configure --with-apxs=/usr/local/apache2/bin/apxs |
| make |
| cp apache-2.0/mod_jk.so /usr/local/apache2/modules | Installs mod_jk in correct location |
|
| I put these lines in http.conf just before NameVirtualHost |
| <IfModule !mod_jk.c>
  LoadModule jk_module modules/mod_jk.so
</IfModule> |
- mkdir /usr/local/htdocs/nonsecure
- mkdir /usr/local/htdocs/nonsecure/myhost.mydomain
- mkdir /usr/local/htdocs/nonsecure/host2.domain
- #Look for similar lines.
- #Make sure you have lines that say this in conf/http.conf:
- Listen myhost.mydomain:80
- #Same with this line:
- #Make sure you do this line because it will listen
- #and try to do http on the https port as well.
- ServerName myhost.mydomain:80
- #Same with this line:
- NameVirtualHost myhost.mydomain
- #I also do this to help people get to right page:
- #Make sure you have VirtualHost like this
< >VirtualHost myhost.mydomain:80>
ServerAdmin me@myhost.mydomain
DocumentRoot /usr/local/htdocs/nonsecure/myhost.mydomain
ServerName myhost.mydomain:80
ErrorLog logs/myhost.mydomain_public-error_log
CustomLog logs/,yhost.mydomain_public-access_log common
# Static files
Alias /mywebapp "/usr/local/tomcat/webapps/mywebapp"
< >Directory "/usr/local/tomcat/webapps/mywebapp">
Options Indexes FollowSymLinks
DirectoryIndex index.jsp
< >/Directory>
# Deny direct access to WEB-INF and META-INF
< >Location "/mywebapp/WEB-INF/*">
AllowOverride None
deny from all
< >/Location>
< >Location "/mywebapp/META-INF/*">
AllowOverride None
deny from all
< >/Location>
#Add these lines too
#It's likely you don't want people to access your
#secure site via plain old http. So I tell it to go
#somewhere else where I may have useful but unsecured
#site
JkMount /mywebapp_nonsecure/*.do ajp13
JkMount /mywebapp_nonsecure/*.jsp ajp13
JkMount /mywebapp_nonsecure ajp13
JkMount /mywebapp_nonsecure/* ajp13
< >/VirtualHost>
Make sure you add these lines too
JkWorkersFile "/usr/local/tomcat/conf/jk/workers.properties"
JkLogFile "/usr/local/tomcat/logs/mod_jk.log"
But Do Not use a "Include mod_jk.conf"
Because it will then be global
- mkdir /usr/local/htdocs/secure
- mkdir /usr/local/htdocs/secure/myhost.mydomain
#Now make changes in ssl.conf
#Look for similar lines and make sure it looks like this:
< >VirtualHost _default_:443>
# General setup for the virtual host
DocumentRoot "/usr/local/htdocs/secure/myhost"
ServerName myhost.mydomain:443
ServerAdmin me@myhost.mydomain
ErrorLog logs/error_log
TransferLog logs/access_log
#This is dangerous I just realized (01-02-2004)
#It causes a loop in redirection. I've only noticed
#the problem when still using localhost.
#The URL gets the domain appended in recursive fashion
#Get your site up first by leaving it commented out.
#You can try adding it later if you need it.
#Redirect / https://myhost.mydomain/mywebapp
# Static files
Alias /mywebapp "/usr/local/tomcat/webapps/mywebapp"
< >Directory "/usr/local/tomcat/webapps/mywebapp">
Options Indexes FollowSymLinks
DirectoryIndex index.jsp
< >/Directory>
# Deny direct access to WEB-INF and META-INF
< >Location "/mywebapp/WEB-INF/*">
AllowOverride None
deny from all
< >/Location>
< >Location "/mywebapp/META-INF/*">
AllowOverride None
deny from all
< >/Location>
JkMount /mywebapp/*.do ajp13
JkMount /mywebapp/*.jsp ajp13
JkMount /mywebapp ajp13
JkMount /mywebapp/* ajp13
Someone else outlined this in his install document and has some info on other packages such as Oracle, JFreeChart, and iText
Please see http://myweb.cableone.net/kdubuisson/Install.PDF
| cd /usr/local/ssl/bin |
| ./openssl genrsa -out myhost.mydomain.key 1024 |
| ./openssl req -new -key myhost.mydomain.key -out myhost.mydomain.csr |
| #Answer the questions you are prompted with |
| ./openssl x509 -days 365 -req -in myhost.mydomain.csr \ |
|     -signkey myhost.mydomain.key -out myhost.mydomain.cert | #For 1 year |
| cp myhost.mydomain.* /usr/local/apache2/conf | #copy them over |
| #edit http.conf file |
| SSLCeritifcateFile /usr/local/apache2/conf/myhost.mydomain.cert |
| SSLCertificateKeyFile /usr/local/apache2/conf/myhost.mydomain.key |
|
| Directly below this line in "/usr/local/tomcat/conf/server.xml" |
| < >Server port="8005" shutdown="SHUTDOWN" debug="1"\> |
| Add these lines: |
| < >Listener className="org.apache.ajp.tomcat4.config.ApacheConfig"
modJk="/usr/local/apache2/modules/mod_jk.so" workersConfig="/usr/local/tomcat/conf/jk/workers.properties"/> |
| For Tomcat 5, the configuration is slightly different |
| < >Listener className="org.apache.jk.config.ApacheConfig
modJk="/usr/local/apache2/modules/mod_jk.so" workersConfig="/usr/local/tomcat/conf/jk/workers.properties"/> |
|   |
| Directly below this line in "/usr/local/tomcat/conf/server.xml" |
| < >Host name="localhost" debug="0" appBase="webapps"> |
| Add this line |
| < >Listener className="org.apache.ajp.tomcat4.config.ApacheConfig" append="true" forwardAll="false" modJk="/usr/local/apache2/modules/mod_jk.so" /> |
|   |
| Comment out JK2 connector, and uncomment AJP1.3 connector (jk). Will show details later |
| Change instances of localhost to whatever your domain name is |
Create file "/usr/local/tomcat/conf/jk/workers.properties" with these contents:
# Setting Tomcat & Java Home
workers.tomcat_home=/usr/local/tomcat
workers.java_home=/usr/local/java/java
ps=/
worker.list=ajp13
worker.ajp13.port=8009
worker.ajp13.host=localhost
worker.ajp13.type=ajp13
|
| Postgres wants the latest version of bison so remove the older RPM and install it from scratch |
| Look in my install scripts for removing bison RPM and installing from scratch. |
| cd /usr/local/src |
| tar xvfz postgresql-7.3.4.tar.gz |
| cd postgresql-7.3.4 |
| ./configure --with-tcl --with-java |
| gmake |
| gmake install |
| chown postgres.postgres /usr/local/pgsql/data |
|
| cd /usr/local/src |
| tar xvfz pgaccess-0.98.8.20030520.tar.gz |
| cp -R pgaccess /usr/local/pgaccess |
| cd pgaccess |
| make clean |
| make all |
| cat "/usr/local/pgaccess/lib" >> /etc/ld.so.conf |
| ldconfig |
|
| I like to keep my postgresql data in /usr/local/pgsql. The daemon scripts below point to that location |
| mkdir /usr/local/pgsql/data |
| chown postgres.postgres /usr/local/pgsql/data |
| chmod 0700 /usr/local/pgsql/data |
| /etc/rc.d/init.d/postgresql start | Test it by doing this |
|
| You can use these steps to import the database for the sample web app that I provide with the install scripts |
| su |
| su postgres | You'll need switch to postgres user, so that you have access unless your user already has access |
| pg_dump nameOfDatabase > nameOfDatabase.psql | To dump a single database |
| pg_dumpall > allDatabases.psql | To dump all your databases |
| createdb -T template0 nameOfDatabase | First you have to create database on new server |
| psql nameOfDatabase | optionally create new user in psql shell |
|       CREATE USER username; |
| \q | Quit out of psql |
| Copy .psql file over to your new server if necessary. Make sure postgres or your user has access to file |
| If database exist already, and you're uploading a newer version. |
| dropdb nameOfDatabase | Get rid of old db first |
| psql nameOfDatabase < nameOfDatabase.psql | Now, add the new one |
|
| The jar file works in RedHat 9, and probably works with RedHat 7.3 but may not. |
#I'm not sure if this is the best way to get this working, but I do this
| ln -s /usr/local/pgsql/share/java/postgresql.jar /usr/local/tomcat/common/lib/postgresql.jar |
| I use the dbcp-commons connection pool. |
Launch class at startup |
| This is in /usr/local/tomcat/webapps/mywebapp/WEB-INF/web.xml |
<!-- If you need to start or do something when the app launches. Here's the config for a servlet to launch at startup -->
<servlet>
< >servlet-name>setup< >/servlet-name>
< >servlet-class>mywebapp.SetupServlet< >/servlet-class>
< >init-param>
< >param-name>test< >/param-name>
< >param-value>test value< >/param-value>
< >/init-param>
< >load-on-startup>1< >/load-on-startup>
< >/servlet>
|
|
| This is a no-brainer. |
| Just put the struts.jar (and other jars if there are more) and tld so your webapp can get to it. I also put it in my path, as noted above with: "../lib/struts.jar". This way I can build from /usr/local/tomcat/webapps/mywebapp/WEB-INF/classes/. You'll need a copy of these in EACH webapp you deploy. |
| cp *.jar /usr/local/tomcat/webapps/mywebapp/WEB-INF/lib/ |
| cp *.tld /usr/local/tomcat/webapps/mywebapp/WEB-INF |
|
| Here's a brief explanation of unusual features in my daemon scripts |
tomcatd |
| #You can add this line for status in tomcatd. It outputs all tomcat processes |
| ps ax --width=1000 | grep "[o]rg.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}' |
postgresql |
| Here's a brief explanation of unusual features in my daemon scripts |
| You can make these changes in a daemon script to allow it to connect with jdbc |
| Edit /etc/rc.d/init.d/postgresql. JDBC needs to connect via tcp/ip, so |
| add "-i" next to "-p ${PGPORT}" so that the line contains this: |
| '-i -p ${PGPORT}' |
Download all the daemon scripts |
chkconfig | This installs scripts to start at startup and shutdown when system is halted |
| After you've copied all daemon scripts to /etc/rc.d/init.d, make sure you've changed the chkconfig line to reflect the appropriate runlevels |
| First make sure they are disabled, or else chkconfig sometimes gets confused |
| /sbin/chkconfig --del tomcatd | |
| /sbin/chkconfig --del httpd |
| /sbin/chkconfig --del postgresql |
| Now add them |
| /sbin/chkconfig --add tomcatd |
| /sbin/chkconfig --add httpd |
| /sbin/chkconfig --add postgresql |