Search This Blog

Sunday, 21 March 2010

Tomcat on Linux Part 2 - Configuration

Automatic Startup
As this installation is based on the generic multi-platform you'll have to create a script that'll allow you to start tomcat on boot. Using your preferred text editor type out a script similar to this
#Tomcat startup script
exec $CATALINA_HOME/bin/
Save the file as tomcat and change the file ownership to root and chmod it to 755 to make it executable.
$ sudo chown root.root tomcat
$ sudo chmod 755 tomcat
This next step will depend on the distribution you are using, you can copy the file into the directory containing your init scripts so that it starts on boot e.g. /etc/rc.d/init.d or use your distributions start up application manager. In Ubuntu go to the start up applications found in System>Prefrences>Startup Applications and add the tomcat file you just created in there.
Now restart your computer and test Tomcat in a web-browser as before to make sure it worked. At this point in your browser Tomcat is most likely still using the default port of 8080 where in the address bar it says http://lochalhost:8080. Copy / move this file to /etc/init.d/ to run as a service.
Changing the Port address
One thing you may want to change is the port number that Tomcat runs on. This maybe due to you having another instance of Tomcat running or another server already using that port. Another reason that you may choose to change this is if it interferes with something else such as one person I know who had a printer and discovered that after installing and running Tomcat the printer wouldn't work, it was later discovered that the printer uses the HTTP protocol on port 80. The port that the server uses can be changed in the server.xml file found in apache-tomcat-6.0.20/conf . Use your prefered text editor to change the port number like so.
$ sudo gedit ~/apache-tomcat-6.0.26/conf/server.xml
Once you have changed the port number to something of your choosing that you feel is more appropriate (there is plenty of material on the Internet covering port numbers but for a brief description and list of port numbers visit save the file. Now start up the server and test it works like before but now with the new port number e.g. http://lochalhost:9915 .
Change the default Web Application and temp Directory
We looked at how to change the port address to prevent potential conflicts, now we're going to change the default Web Application Directory. Why would you want to do that? Well by default the directory resides with in the Tomcat root directory with all the other directories and files that make it work. If you decide to update Tomcat you run the risk of loosing your own files if they are kept inside Tomcat. Another reason you may want to change the default directory is if you want to run more than one instance of Tomcat and need both to access and share your own files. To be able to do this you can set the CATALINA_BASE environment variable to point to your desired directory while CATALINA_HOME points to the root Tomcat directory. To do this, in a terminal type
$ mkdir ~/dist (create a new directory with the name of your choice)
$ cd dist (cd into that directory)
$ cp -R ~/apache-tomcat-6.0.20/conf (copy the conf directory from tomcat)
$ mkdir common logs temp server shared webapps work (create these new directories)
The reason for copying the conf directory and creating these new directories as well as the webapps is that this folder will be used to hold our personal stuff, logs and configurations so that any future Tomcat upgrade won't write over them and upgrading won't be a painful process. Now that you've created your new directory you'll have to set up the CATALINA_BASE variable.
$ CATALINA_HOME=~/apache-tomcat-6.0.20
This will point the relevant environment variables to you're personal files including webapps and the root tomcat directory for it to run. You may want to add this line to your start up script otherwise these settings will be lost the next time you boot. You're start up script should now look something a bit like this
#Tomcat startup script
exec $CATALINA_HOME/bin/
When you start up tomcat with these settings you might notice that CATALINA_TMPDIR automatically points to ~/dist/temp, you can change the location of the temp directory if you so choose using the same method as above.
These configuration options can be defined for the environment in which you're using the service and on personal preference. Next we'll look at configuring Tomcat to improve performance.