How to run Java servlets on a small vps

wlanboy

Content Contributer
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
    http://download.eclipse.org/jetty/stable-9/dist/
    Code:
    wget http://eclipse.org/downloads/download.php?file=/jetty/stable-9/dist/jetty-distribution-9.0.5.v20130815.tar.gz&r=1
    tar -xzf *.tar.gz
  • Prepare directories and users
    Code:
    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
    Code:
    nano /etc/default/jetty
    Contents:


    JAVA=/usr/bin/java # Path to Java
    NO_START=0 # Start on boot
    JETTY_HOST=127.0.0.1 # 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
    Code:
    cp /opt/jetty/bin/jetty.sh /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)
    Code:
    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
jdbcRealm.properties jetty-http.xml jetty-logging.xml jetty-requestlog.xml jetty-ssl.xml jetty.xml spnego.properties
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 realm.properties

  • 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
    Code:
        <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>
        </Get>
    - HTTP protocol
     


    <New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
    <Set name="secureScheme">https</Set>
    <Set name="securePort"><Property name="jetty.secure.port" 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>
    </Call>
    </New>


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

Code:
<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">
      <Item>test.wlanboy.com</Item>
      <Item>@ConnectorName</Item>
      <Item>localhost</Item>
      <Item>127.0.0.1</Item>
    </Array>
  </Set>
 
Last edited by a moderator:

wlanboy

Content Contributer
Why not install openjdk-7-jre-headless instead?
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.
 

concerto49

New Member
Verified Provider
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.
 

DragonDF

New Member
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:

concerto49

New Member
Verified Provider
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.
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.
 

DragonDF

New Member
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". :)

;)
 

concerto49

New Member
Verified Provider
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". :)


;)
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.
 
Top