Configuring Jetty to run Magento

Yesterday I wrote a blog post on my surprising finding that Jetty was able to out-perform, or at minimum keep up (depending on how you looked at the numbers), Nginx when it comes to static files.  Today I wanted to get it up and running and able to serve Magento requests.

It turns out that it is stupid easy.  That said, it was stupid hard figuring out how stupid easy it was.

To get Magento running with Jetty

  1. Set up PHP-FPM as you would with Nginx, making sure that you use TCP and not Unix sockets
  2. Download Jetty.  Do not use the RPM, if you are using CentOS.  Yum does not have all the components you need.  Use the full download from the Jetty page on the Eclipse site.  Install into /usr/share/java/jetty
  3. Configure Jetty.

So let’s look at number 3.

There are two things you need to know about this.

First of all is that FastCGI is not enabled by default.  You need to add it to its start.d directory.

# export JETTY_HOME=/usr/share/java/jetty
# cd /usr/share/java/jetty/etc 
# java -jar $JETTY_HOME/start.jar --add-to-start=fcgi
INFO: fcgi initialised in ${jetty.base}/start.ini
INFO: Base directory was modified

FastCGI is now installed.  Now you need to configure a default context.  Create a file called $JETTY_HOME/etc/webapps/fcgi.xml (or whatever.xml) and add the following.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.servlet.ServletContextHandler">
 
    <New id="root" class="java.lang.String">
        <Arg>/var/www/magento19/magento/</Arg>
    </New>
 
    <Set name="contextPath">/</Set>
    <Set name="resourceBase"><Ref refid="root" /></Set>
    <Set name="welcomeFiles">
        <Array type="string"><Item>index.php</Item></Array>
    </Set>
 
    <Call name="addFilter">
        <Arg>org.eclipse.jetty.fcgi.server.proxy.TryFilesFilter</Arg>
        <Arg>/*</Arg>
        <Arg>
            <Call name="of" class="java.util.EnumSet">
                <Arg><Get name="REQUEST" class="javax.servlet.DispatcherType" /></Arg>
            </Call>
        </Arg>
        <Call name="setInitParameter">
            <Arg>files</Arg>
            <Arg>$path /index.php</Arg>
        </Call>
    </Call>
 
    <Call name="addServlet">
        <Arg>
            <New class="org.eclipse.jetty.servlet.ServletHolder">
                <Arg>default</Arg>
                <Arg>
                    <Call name="forName" class="java.lang.Class">
                        <Arg>org.eclipse.jetty.servlet.DefaultServlet</Arg>
                    </Call>
                </Arg>
                <Call name="setInitParameter">
                    <Arg>dirAllowed</Arg>
                    <Arg>false</Arg>
                </Call>
            </New>
        </Arg>
        <Arg>/</Arg>
    </Call>
 
    <Call name="addServlet">
        <Arg>org.eclipse.jetty.fcgi.server.proxy.FastCGIProxyServlet</Arg>
        <Arg>*.php</Arg>
        <Call name="setInitParameter">
            <Arg>proxyTo</Arg>
            <Arg>http://localhost:9000</Arg>
        </Call>
        <Call name="setInitParameter">
            <Arg>prefix</Arg>
            <Arg>/</Arg>
        </Call>
        <Call name="setInitParameter">
            <Arg>scriptRoot</Arg>
            <Arg><Ref refid="root" /></Arg>
        </Call>
        <Call name="setInitParameter">
            <Arg>scriptPattern</Arg>
            <Arg>(.+?\\.php)</Arg>
        </Call>
    </Call>
 
</Configure>

THAT  is a lot of stuff.  Well, actually it’s not, but it’s very verbose.  There are a few things you need to know

  1. //New[@id=”root”] – this is document root for this web application
  2. //Set[@name=”contextPath”] – This is the base URI where your application lives.  http://example.com/ would be /, whereas http://example.com/blog/ would be /blog/
  3. Find org.eclipse.jetty.fcgi.server.proxy.TryFilesFilter.  That is the equivalent of Nginx’s try_files setting.  Here, if it doesn’t find the file, route it to index.php.
  4. The last addServlet item is the FastCGI component.  I trust you can see the two important settings.

And there you have it.  A Jetty instance configured to run Magento.

Oh, and how does this compare to Nginx in terms of Magento throughput?

2016-06-03_1250

Leave a Reply

Your email address will not be published. Required fields are marked *