blob: ecc8ad904eb7de2fa3b2c28ad3d24bf219265bc3 [file]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Configuring Jetty for FastCGI</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><meta name="keywords" content="jetty, servlet, servlet-api, cometd, http, spdy, websocket, eclipse, maven, java, server, software"><link rel="home" href="index.html" title="Jetty : The Definitive Reference"><link rel="up" href="fastcgi.html" title="Chapter&nbsp;17.&nbsp;FastCGI Support"><link rel="prev" href="fastcgi.html" title="Chapter&nbsp;17.&nbsp;FastCGI Support"><link rel="next" href="advanced-extras.html" title="Chapter&nbsp;18.&nbsp;Provided Servlets, Filters, and Handlers"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><script type="text/javascript" src="js/shCore.js"></script><script type="text/javascript" src="js/shBrushJava.js"></script><script type="text/javascript" src="js/shBrushXml.js"></script><script type="text/javascript" src="js/shBrushBash.js"></script><script type="text/javascript" src="js/shBrushJScript.js"></script><script type="text/javascript" src="js/shBrushSql.js"></script><script type="text/javascript" src="js/shBrushProperties.js"></script><script type="text/javascript" src="js/shBrushPlain.js"></script><link type="text/css" rel="stylesheet" href="css/shCore.css"><link type="text/css" rel="stylesheet" href="css/shThemeEclipse.css"><link type="text/css" rel="stylesheet" href="css/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small">
Version: 9.2.21.v20170120</span></td><td style="width: 50%"><script type="text/javascript"> (function() {
var cx = '016459005284625897022:obd4lsai2ds';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script><gcse:search></gcse:search></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Configuring Jetty for FastCGI</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="fastcgi.html"><i class="icon-chevron-left"></i> Previous</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;17.&nbsp;FastCGI Support<br><a accesskey="p" href="index.html"><i class="icon-home"></i> Home</a></th><td width="20%" align="right">&nbsp;<a accesskey="n" href="advanced-extras.html">Next <i class="icon-chevron-right"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/">Contact the core Jetty developers at
<span class="website">www.webtide.com</span></a></h5><p>
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ...
scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="configuring-fastcgi"></a>Configuring Jetty for FastCGI</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="configuring-fastcgi.html#configuring-fastcgi-http">Configuring Jetty to Proxy HTTP to FastCGI</a></span></dt><dt><span class="section"><a href="configuring-fastcgi.html#configuring-fastcgi-spdy">Configuring Jetty to Proxy SPDY to FastCGI</a></span></dt></dl></div><p>
In this section you will see how to configure Jetty to serve WordPress via FastCGI.
</p><p>
The first step is to have WordPress installed in your server machine, for example under
<code class="code">/var/www/wordpress</code>. For more information about how to install WordPress,
please refer to the <a class="link" href="https://codex.wordpress.org/Installing_WordPress" target="_top">WordPress Installation Guide</a>.
</p><p>
The second step is to install <code class="code">php-fpm</code> and make sure it is configured to listen
on a TCP socket; typically it is configured to listen to <code class="code">localhost:9000</code>.
</p><p>
The third step is to install Jetty, for example under <code class="code">/opt/jetty</code>.
Refer to <a class="xref" href="quick-start-getting-started.html#jetty-downloading" title="Downloading Jetty">Downloading Jetty</a> for more information about how to install Jetty.
</p><p>
The fourth step is to create a Jetty base directory (see <a class="xref" href="startup-base-and-home.html" title="Managing Jetty Base and Jetty Home">Managing Jetty Base and Jetty Home</a>),
called in the following <code class="code">$JETTY_BASE</code>, where you setup the configuration needed to support
FastCGI in Jetty:
</p><div class="screenexample"><pre class="screen">
$ mkdir -p /usr/jetty/wordpress
$ cd /usr/jetty/wordpress
</pre></div><p>
Therefore <code class="code">$JETTY_BASE=/usr/jetty/wordpress</code>.
</p><p>
The fifth step is to deploy the web application that provides the proxying of client requests to
the FastCGI server, <code class="code">php-fpm</code>.
Typically this is done by deploying a <code class="code">*.war</code> file in the <code class="code">$JETTY_BASE/webapps</code>
directory, but in case of FastCGI there is really nothing of this web application that you have to write:
all the work is already done for you by the Jetty developers.
Therefore you just need to deploy a Jetty XML file that configures the web application directly.
Copy and paste the following content as <code class="code">$JETTY_BASE/webapps/jetty-wordpress.xml</code>
</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: xml;toolbar: false">
<![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure class="org.eclipse.jetty.servlet.ServletContextHandler">
<New id="root" class="java.lang.String">
<Arg>/var/www/wordpress</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?p=$path</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>
]]>
</script></div><p>
Explanation of this file content:
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>
At line 6 it is specified the WordPress installation directory, in this example
<code class="code">/var/www/wordpress</code> (as defined in the first step).
</p></li><li class="listitem"><p>
At line 9 it is specified the context path at which WordPress will be served, in this example
at the root context path <code class="code">/</code>.
</p></li><li class="listitem"><p>
At line 10 it is specified the resource base of the context, also set to the WordPress installation
directory. This allows Jetty to serve static resources directly from the WordPress installation
directory.
</p></li><li class="listitem"><p>
At line 12 it is specified the welcome file as <code class="code">index.php</code>, so that
Jetty can perform the proper redirects in case of URIs ending with the <code class="code">/</code>
character.
</p></li><li class="listitem"><p>
At line 15 it is specified the <code class="code">TryFilesFilter</code>, a Servlet Filter that has been inspired
by the <a class="link" href="http://wiki.nginx.org/HttpCoreModule#try_files" target="_top">try_files</a> functionality
offered by Nginx.
This filter tries to serve the resource from the file system first, and if the resource is not found
it forwards the request as <code class="code">index.php?p=$path</code>, which will match the proxy servlet
defined below. Refer to the
<a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/fcgi/server/proxy/TryFilesFilter.html" target="_top">TryFilesFilter</a>
documentation for further information.
</p></li><li class="listitem"><p>
At line 29 it is specified Jetty's <code class="code">DefaultServlet</code> to serve static content such as
CSS files, JavaScript files, etc. <code class="code">DefaultServlet</code> will serve these files by looking
in the resource base of the context, defined at line 10 (see above).
</p></li><li class="listitem"><p>
At line 47 it is specified the <code class="code">FastCGIProxyServlet</code>, a Servlet that proxies HTTP
requests arriving from clients to FastCGI requests to the FastCGI server.
</p></li><li class="listitem"><p>
At line 52 it is specified the TCP address of the FastCGI server (<code class="code">php-fpm</code>), where
HTTP requests are forwarded as FastCGI requests.
</p></li><li class="listitem"><p>
At line 60 it is specified once again the WordPress installation directory, so that the
<code class="code">FastCGIProxyServlet</code> can pass this information to the FastCGI server.
</p></li><li class="listitem"><p>
At line 64 it is specified a regular expression that matches request URIs performed to this
servlet, in addition to the standard URL mapping defined by Servlet at line 49.
Refer to the
<a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/fcgi/server/proxy/FastCGIProxyServlet.html" target="_top">FastCGIProxyServlet</a>
documentation for further information.
</p></li></ul></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="configuring-fastcgi-http"></a>Configuring Jetty to Proxy HTTP to FastCGI</h3></div></div></div><p>
In order to configure Jetty to listen for HTTP requests from clients and forward them to
the FastCGI server as FastCGI requests, you need to specify a <code class="code">start.ini</code> file
that configures the Jetty modules that are needed for Jetty to proxy HTTP to FastCGI.
The minimal <code class="code">start.ini</code> file is the following:
</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: plain;toolbar: false">
<![CDATA[
# The 'deploy' module enables Jetty to look up in the 'webapps' directory for files to deploy.
--module=deploy
# The 'fcgi' module enables FastCGI support.
--module=fcgi
# The 'http' module enables a HTTP connector that listens for client requests on port 8080.
--module=http
jetty.port=8080
]]>
</script></div><p>
At this point, you can start Jetty (see <a class="xref" href="startup.html" title="Chapter&nbsp;9.&nbsp;Starting Jetty">Chapter&nbsp;9, <i>Starting Jetty</i></a>), hit <code class="code">http://localhost:8080</code>
with your browser and enjoy WordPress:
</p><div class="screenexample"><pre class="screen">
$ cd $JETTY_BASE
$ java -jar /opt/jetty/start.jar
</pre></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="configuring-fastcgi-spdy"></a>Configuring Jetty to Proxy SPDY to FastCGI</h3></div></div></div><p>
In order to configure Jetty to listen for SPDY requests from clients that are SPDY enabled
and forward them to the FastCGI server as FastCGI requests, you need to specify a
<code class="code">start.ini</code> file that configures the Jetty modules that are needed for Jetty to
proxy SPDY to FastCGI.
</p><p>
Remember that for SPDY to work you will need to run over SSL, and you will need the NPN boot jar
in the boot classpath, as explained in <a class="xref" href="spdy-configuring.html" title="Configuring SPDY">Configuring SPDY</a>.
</p><p>
Since now your site will run over SSL, you need to make sure that the WordPress URL is also
configured so.
If you have followed the steps of the <a class="xref" href="configuring-fastcgi.html#configuring-fastcgi-http" title="Configuring Jetty to Proxy HTTP to FastCGI">Configuring Jetty to Proxy HTTP to FastCGI</a>,
your WordPress site is served at <code class="code">http://localhost:8080</code>.
You will need to change that to be <code class="code">https://localhost:8443</code> from the WordPress
administration web interface, or follow the
<a class="link" href="http://codex.wordpress.org/Changing_The_Site_URL" target="_top">WordPress instructions</a>
to do so without using the administration web interface.
</p><p>
The minimal <code class="code">start.ini</code> file is the following:
</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: plain;toolbar: false">
<![CDATA[
# The 'deploy' module enables Jetty to look up in the 'webapps' directory for files to deploy.
--module=deploy
# The 'fcgi' module enables FastCGI support.
--module=fcgi
# The 'spdy' module enables a SPDY connector that listens for client requests on port 8443.
--module=spdy
spdy.port=8443
]]>
</script></div><p>
Note that we specified the <code class="code">spdy.port</code> to be <code class="code">8443</code>.
</p><p>
At this point, you will need the NPN boot jar and the SSL certificate.
In this example, you can obtain them very easily using the built-in feature of the Jetty start
mechanism that downloads required dependencies, see <a class="xref" href="startup.html" title="Chapter&nbsp;9.&nbsp;Starting Jetty">Chapter&nbsp;9, <i>Starting Jetty</i></a>.
However, remember that the example SSL certificate that is downloaded will not be valid for your
website and that it must not be used in production. It is provided only for testing and example
purposes.
To leverage the automatic download of required dependencies, issue this command:
</p><div class="screenexample"><pre class="screen">
$ cd $JETTY_BASE
$ java -jar /opt/jetty/start.jar --create-files
</pre></div><p>
At this point, you can start Jetty (see <a class="xref" href="startup.html" title="Chapter&nbsp;9.&nbsp;Starting Jetty">Chapter&nbsp;9, <i>Starting Jetty</i></a>), hit <code class="code">http://localhost:8080</code>
with your browser and enjoy WordPress via SPDY using a SPDY enabled browser:
</p><div class="screenexample"><pre class="screen">
$ cd $JETTY_BASE
$ java -Xbootclasspath/p:lib/npn/npn-boot-&lt;version&gt;.jar -jar /opt/jetty/start.jar
</pre></div><p>
If you don't have a SPDY enabled browser, WordPress will still be available over plain HTTPS.
</p></div></div><script type="text/javascript">
SyntaxHighlighter.all()
</script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="fastcgi.html"><i class="icon-chevron-left"></i> Previous</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="fastcgi.html"><i class="icon-chevron-up"></i> Top</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="advanced-extras.html">Next <i class="icon-chevron-right"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&nbsp;17.&nbsp;FastCGI Support&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="icon-home"></i> Home</a></td><td width="40%" align="right" valign="top">&nbsp;Chapter&nbsp;18.&nbsp;Provided Servlets, Filters, and Handlers</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout">
See an error or something missing?
<span class="callout"><a href="http://github.com/jetty-project/jetty-documentation">Contribute to this documentation at
<span class="website"><i class="icon-github"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2015-03-13T11:47:52-05:00)</i></span></div></p><script xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1149868-7']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script></body></html>