Wednesday, November 28, 2012

Setting Tomcat for development and production

In this post I will describe how to setup great Tomcat installation.


Installation

Download Tomcat for your environment from official download site. Don't download Windows Service Installer even if you plan to use Tomcat as service.

Unzip Tomcat to desired directory.

Changing logging engine to Logback

After installing Tomcat I will change logging to use Logback following this instructions.
  • copy everything from tomcat-slf4j-logback/bin to destination/bin (this will overwrite tomcat-juli.jar)
  • copy logging configuration in tomcat-slf4j-logback/conf to destination/conf
  • delete logging.properties from destination/conf
  • copy logback-core.jar and logback-access.jar to destination/lib
Change Valve in server.xml with:
 <Valve className="ch.qos.logback.access.tomcat.LogbackValve" quiet="true" filename="${catalina.home}/conf/logback-access-localhost.xml" />  

After that you should run tomcat with startup.bat to check that everything works.

Setting Tomcat users

In directory conf/ you should edit tomcat-users.xml. Add this inside tomcat-users tags:
 <role rolename="manager-gui"/>  
 <role rolename="admin-gui"/>  
 <user username="tomcat-mng" password="*****" roles="manager-gui,admin-gui"/>  

Adding Tomcat to Eclipse

Add new Tomcat server as usually. First add Server runtime enviroment (Windows->Preferences), and then add new Server using that runtime.
You should adjust Publishing and Timeouts as preferred. I use "Never publish automatically" , start 450s and stop 150s. 
After that you should set "Launch Configuration", in VM arguments you should add:
 -Djuli-logback.configurationFile="<absolute tomcat home path>\conf\logback.xml"  
In Server project on your workspace you should check that server.xml has right Valve set.

Now you can start server to check that everything is OK.

Running Tomcat as Windows Service

You should install Tomcat as a service using service.bat. Advanced setup is described here.

 service.bat install  

You should run this command as an admin. This will add Tomcat to your services, but will not enable monitor service and edit configuration service. So I use this command:

 tomcat7w.exe //MS//   

You install service with specific name:

 service.bat install [service_name]  

Then you need to rename tomcat7.exe and tomcat7w.exe to [service_name].exe and [service_name]w.exe respectively. You should run this programs as administrator.

Now you can change setup so Tomcat will be started automatically. Also you should add
 -Djuli-logback.configurationFile="<absolute tomcat home path>\conf\logback.xml"  
to Java Options.

Redirecting Stdout and Strerr

When you run Tomcat in production you should redirect stdout and stderr to null.

No comments:

Post a Comment