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
| "Warning!" These instructions are for using PHP with Jetty 5.x only. |
Jetty and PHP
Running PHP applications using the Jetty CGI servlet.
TODO: Find a FASTCGI servlet which by all accounts will have a significant impact on throughput and performance
Jetty as a general web server
You have Jetty configured to serve static files from the root context, and you want *.php files to be processed by PHP. Add the CGI servlet into the context as shown below.
TODO: Find a way to configure jetty so that *.php/some/path/info is passed to PHP with SCRIPT_NAME=*.php and PATH_INFO=/some/path/info
PHP as a webapp
Your php app needs special environment variables for configuration or uses PATH_INFO information.
For example, yourapp.php/path/info expects yourapp.php to be processed by PHP with PATH_INFO=/path/info
Assuming jetty is configured to auto-discover web applications in a webapps directory and your php application files are in a directory yourapp.
Create a directory webapps/yourapp/WEB-INF and the standard webapps/yourapp/WEB-INF/web.xml
mod_rewrite
If your app makes use of Apache's mod_rewrite functionality they have a look at URL Rewrite Filter and add the configuration to yourapp/WEB-INF/web.xml
htaccess Authentication
WEB-INF/jetty-web.xml can be used as normal to configure the HTAccessHandler. The index form is required because you need the authentication check to occur before the PHP scripts are called.
PHP Configuration
For this purpose PHP must be compiled as a cgi binary.
Refer to the PHP documentation for security considerations but be mindful that they are likely to be referencing the default Apache configuration and your Jetty configuration is likely to be highly customised.
php.ini
- doc_root If you are only using the general web server arrangement then doc_root can be set to the ResourceBase of your root context. If you want to also use the webapp arrangement then leave doc_root blank and take any necessary security precautions (use cgi.force_redirect or remove the php binary/link from your cgi-bin)
- cgi.fix_pathinfo Try using this setting (=0 or 1) if PATH_INFO is not as you expect
php-cgi-fix
In both scenarios above the command prefix is pointing to a script php-cgi-fix that works around the fact that PHP running as cgi uses a non-standard CGI environment variable SCRIPT_FILENAME to reference the script, rather than taking the first argument. In certain circumstances PHP will get a bad SCRIPT_FILENAME, then use SCRIPT_NAME to find and parse the file. This produces the expected HTML but also provides a "Not Found" header, (Status: 404) which can confuse some browsers.
Since Jetty 5.1.5 the CGI servlet sets SCRIPT_FILENAME, but not correctly in all cases |
Testing
The phpinfo() function is very handy for testing your configuration as it will dump lots of information about the configuration.
Save the following to a file phpinfo.php into your webserver directory and navigate your browser to http://<yourjettyhost>/phpinfo.php and check the results are what you expect.