How to run Java servlets on a small vps

Discussion in 'Tutorials and Guides' started by wlanboy, Sep 21, 2013.

  1. wlanboy

    wlanboy Content Contributer

    May 16, 2013
    I really like Ruby but sometimes you need/want to run some Java apps on your vps. This is quite easy because you can strip down the Java VM to use quite a small amount of RAM.

    But if you need to support servlets, websockets, JMX or JNDI lookups you need a container, or at least a servlet container.

    You should not try to run the big application servers on a small vps - just by looking to the footprint of Jboss, Glassfish or Websphere.

    But you can easily run Jetty on a small vps.

    So let's install Jetty:

    • Install Java

      apt-get install openjdk-7-jdk (wopping 363MB)

      mkdir /usr/java

      For 64bit:
      ln -s /usr/lib/jvm/java-7-openjdk-amd64 /usr/java/default

      For 32bit
      ln -s /usr/lib/jvm/java-7-openjdk-i386/ /usr/java/default

    • Download Jetty
      tar -xzf *.tar.gz
    • Prepare directories and users
      mkdir /opt
      mv jetty-distribution-9.0.5.v20130815 /opt/jetty
      useradd jetty -U -s /bin/false
      chown -R jetty:jetty /opt/jetty
    • Configure Jetty defaults
      nano /etc/default/jetty

      JAVA=/usr/bin/java # Path to Java
      NO_START=0 # Start on boot
      JETTY_HOST= # Listen to following ip
      JETTY_ARGS=jetty.port=8085 # Web port of jetty
      JETTY_USER=jetty # User to run under

    • Configure Jetty as a service
      cp /opt/jetty/bin/ /etc/init.d/jetty
      chmod +x /etc/init.d/jetty
      update-rc.d jetty defaults
      service jetty restart
    • Remove demo apps (because they are not secure)
      cd /opt/jetty/webapps
      rm -rf test.d/ test.war test.xml async-rest.war
      rm -rf /opt/jetty/webapps.demo

    If you look to the footprint of Jetty:

    PID User Command Swap USS PSS RSS
    7358 jetty /usr/bin/java -Djetty.state 37456 40388 40492 41464

    no one can say that Jetty is bloated.

    Maybe enough to bring back some Java to your vps.

    PS: One Jetty based project: Bombermine.

    Configuration of Jetty

    All config files are located in "/opt/jetty/etc"

    /opt/jetty/etc# ls
    README.spnego jetty-deploy.xml jetty-jmx.xml jetty-proxy.xml jetty-spdy.xml jetty.conf spnego.conf jetty-http.xml jetty-logging.xml jetty-requestlog.xml jetty-ssl.xml jetty.xml
    jetty-annotations.xml jetty-https.xml jetty-lowresources.xml jetty-rewrite.xml jetty-started.xml keystore test-realm.xml
    jetty-debug.xml jetty-ipaccess.xml jetty-monitor.xml jetty-setuid.xml jetty-stats.xml krb5.ini webdefault.xml
    jetty-demo.xml jetty-jaas.xml jetty-plus.xml jetty-spdy-proxy.xml jetty-xinetd.xml

    • jetty-http.xml
      Host and port of the http port
    • jetty-https.xml
      Port and certs for SSL
    • jetty.xml
      All major settings like: 
      - Thread pool
          <Get name="ThreadPool">
            <Set name="minThreads" type="int"><Property name="threads.min" default="10"/></Set>
            <Set name="maxThreads" type="int"><Property name="threads.max" default="200"/></Set>
            <Set name="idleTimeout" type="int"><Property name="threads.timeout" default="60000"/></Set>
            <Set name="detailedDump">false</Set>
      - HTTP protocol

      <New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
      <Set name="secureScheme">https</Set>
      <Set name="securePort"><Property name="" default="8443" /></Set>
      <Set name="outputBufferSize">32768</Set>
      <Set name="requestHeaderSize">8192</Set>
      <Set name="responseHeaderSize">8192</Set>
      <Set name="sendServerVersion">true</Set>
      <Set name="sendDateHeader">false</Set>
      <Set name="headerCacheSize">512</Set>

      <!-- Uncomment to enable handling of X-Forwarded- style headers-->
      <Call name="addCustomizer">
      <Arg><New class="org.eclipse.jetty.server.ForwardedRequestCustomizer"/></Arg>

    Best place to set the virtual hosts: Within the war file:

    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
      <Set name="contextPath">/ibadmin</Set>
      <Set name="war"><Property name="jetty.webapps" default="."/>/ibadmin.war</Set>
      <Set name="virtualHosts">
        <Array type="String">
    Last edited by a moderator: Sep 21, 2013
    MannDude likes this.
  2. nunim

    nunim VPS Junkie

    May 15, 2013
    Why not install openjdk-7-jre-headless instead?
  3. wlanboy

    wlanboy Content Contributer

    May 16, 2013
    To be able to compile.

    I like to pull the sources do call ant and everything is done automatically.

    But if you just need a runtime environment, you can use "openjdk-7-jre-headless" instead.
  4. jcaleb

    jcaleb New Member

    May 15, 2013
    it's the app that you will install on top on app server that are memory hog.
  5. concerto49

    concerto49 New Member Verified Provider

    May 5, 2013
    Since it's Jetty, you can take out the modules you don't need. Play around with the startup script. Also the thread pool etc would be another factor. Jetty is quite neat and due to it being NIO based, saves resources.

    Running Tomcat with NIO (default in Tomcat 8) isn't a bad option either.

    Most Linux OS come with Tomcat (but usually an old version).

    Something I'd watch out for is that Java sometimes doesn't want to run on a single core allocated VPS on OpenVZ (and even on others). It just thinks there's more cores in the system.
  6. DragonDF

    DragonDF New Member

    Oct 22, 2013
    With this tuto is possible to install LIFE RAY and run it with a VPS with 1Gb?

    This is one thing I do not like in Java. A lot of RAM for apps.


    And I like the idea to use TOMCAT, too.
    Last edited by a moderator: Oct 22, 2013
  7. concerto49

    concerto49 New Member Verified Provider

    May 5, 2013
    Liferay is a web app installed inside an application server such as Jetty or Tomcat.

    Java isn't a RAM hog. Blame the developers. You can write a program that uses a lot less memory easily.
  8. DragonDF

    DragonDF New Member

    Oct 22, 2013
    But Liferay is not a 3x Joomla in code to spend all it asks in RAM.

    It is sure that Liferay has a lot of features. I used it only as an example that when I have to run something and it is in JAVA, the first thing I imagine is: "A LOT OF RAM". :)

  9. jcaleb

    jcaleb New Member

    May 15, 2013
    For Liferay I think you need more than 1GBRAM
  10. peterw

    peterw New Member

    Jun 14, 2013
    That is true. In Java it is easy to build a web app but the easy way (using a lot of libs) is consuming a lot of RAM.
  11. concerto49

    concerto49 New Member Verified Provider

    May 5, 2013
    That is due to poor coding not Java. This is because Java traditionally targeted enterprises that are running large blade servers. All those developers had gigabytes of memory. Compare this to php that is popular around shared hosting with limited memory. Doesn't mean Java itself can't do the same.
    vRozenSch00n likes this.