Saturday, September 20, 2014

Liferay Tomcat as windows service

When Liferay Portal is bundled with Tomcat, it can be started as a windows service, so it can be automatically started at computer startup, without being logged in as windows user and without manually start through "startup.bat".
The procedure applies to a normal Tomcat installation as well, also without Liferay Portal.

1) First of all, we need to be sure that the global variable "%JAVA_HOME%" is set. If it is not set, we have to set it in Control Panel -> System and Security -> System -> Advanced System Settings -> Environment Variables -> System Variables -> New. Insert "JAVA_HOME" as variable name, and as value insert:

    C:\{JAVA_JDK_DIR}/jre/bin/server/jvm.dll

Replace {JAVA_JDK_DIR} with your JDK installation path.

2) Open command prompt (i.e. from menu Start -> Run -> "cmd") and navigate to liferay tomcat folder.

    C:\{folder_liferay_portal_tomcat.x.x.x}/bin

Be sure that in this folder are present the files "tomcatX.exe" e "tomcatXw.exe", where "X" stands for the tomcat version.

3) From the command prompt execute the following command:

    service.bat install myservice

In this way Tomcat service is installed in the windows service registry, in this case with the name "myservice" that you can change as you like. To verify that service is installed, go to Start->Run, type "services.msc", and check if the service "Apache Tomcat tomcatX" is present as system service.

4) From command prompt execute the command:

    tomcatXw //ES//myservice

where "X" is the Tomcat Version, as already told before.
(i.e.: tomcat7w //ES//myservice)
This will open a configuration window of the Tomcat Service.

5) Go to "JAVA" section and check that the checkbox "Use default" is NOT set and the Java Virtual Machine path is

    C:\{JAVA_JDK_DIR}/jre/bin/server/jvm.dll

6) In the same section go to "Java Options" and insert this values under the existing values:

    -XX:MaxPermSize=256m
    -Dfile.encoding=UTF8
    -Duser.timezone=GMT
    -Djava.security.auth.login.config={liferay_dir}\conf\jaas.config
    -Dorg.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES=false

this is the standard configuration for Java 32 bit version.
Replace {liferay_dir} with your liferay directory (if your are in a liferay portal context)

If your server supports Java 64 bit version, the initial Java configuration values are:

    -XX:MaxPermSize=512m
    -XX:+UseParNewGC
    -XX:+UseConcMarkSweepGC
    -XX:+CMSParallelRemarkEnabled
    -XX:SurvivorRatio=20
    -XX:ParallelGCThreads=4
    -Dcom.sun.management.jmxremote=true
    -Dcom.sun.management.jmxremote.ssl=false
    -Dcom.sun.management.jmxremote.authenticate=false
    -Dcom.sun.management.jmxremote.port=9090
    -XX:+CMSPermGenSweepingEnabled
    -XX:+CMSClassUnloadingEnabled

Note: It is important to be sure that between lines there are no empty spaces! You can change configuration values based on your needs.

7) In the section java there are "Initial Memory Pool" and "Maximal Memory Pool" fields. Set this fields with this values:

(for 32 bit Java version):
    Initial Memory Pool: 512
    Maximal Memory Pool: 1024

(for 64 bit Java version):
    Initial Memory Pool: 2048 
    Maximal Memory Pool: 2048

Note: you can modify memory values based on your context and your needs, These values seems to be good enough for a Liferay Portal that supports Vaadin portlets.

Save this settings and restart the service. Apache Tomcat tomcatX will be executed as a system service as soon as Windows is started.

Note: To keep the java configuration declared in "startup.bat" file, in the tomcat folder directory, we have to modify parameters in the tomcat service configuration window, in the "JAVA" section, assigning the values as needed. If you notice problems starting service, you have to modify section "STARTUP" too, in the configuration window of the tomcat service (see step 4 for how to open the configuration window). "Mode" field has to be set with the value "Java". In this way we can start the service automatically with the desired configurations.

No comments:

Post a Comment

(c) Copyright 2020 - MyTroubleshooting.com