jetty server run php code
I want to configure so that jetty runs PHP files, but have been 开发者_JAVA百科unsuccessful so far. I have Jetty WTP tools installed in my Eclipse IDE.
When I start up the Jetty server. I got the exception: java.lang.ClassNotFoundException org.mortbay.servlet.CGI: org.mortbay.servlet.CGI and javax.servlet.UnavailableException: org.mortbay.servlet.CGI. I put a simple php file(index.php) under my WebContent folder. I also downloaded this library and added it to the classpath(Eclipse: build path and add external jar). I have no clue now what I've done wrong. I also compiled php with:
./configure --with-fastcgi=/usr/local
make
sudo make install
And my web.xml file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Sample PHP Application</display-name>
<servlet>
<servlet-name>PHP</servlet-name>
<servlet-class>org.mortbay.servlet.CGI</servlet-class>
<init-param>
<param-name>commandPrefix</param-name>
<param-value>/usr/local/bin/php-cgi-fix</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>PHP</servlet-name>
<url-pattern>/index.php/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.php</welcome-file>
</welcome-file-list>
</web-app>
It could be my php-cgi-fix file, I got the followig text i terminal when i open it:
$ /usr/local/bin/php-cgi-fix ; exit;
/usr/local/bin/php-cgi-fix: line 3: /usr/bin/php-cgi: No such file or directory
logout
Shouldn't be /usr/local/php-cgi instead? Ok I tried that now, but still I got the same ecxeptions when I launch the server.
(I really don't like to use maven too!)
You almost certainly don't want to follow those instructions - they're for Jetty 5 which is very old.
Try following one of these 2 sets of instructions
- http://blog.fedecarg.com/2009/01/04/running-quercus-in-jetty-web-server/
- http://www.owengriffin.com/posts/2010/04/22/Writing_PHP_applications_with_Maven_and_Jetty.html
They take very different approaches, so you'll need to decide which way you want to do.
The first one uses Quercus which is an implementation of PHP written in Java. The second one uses CGI to run the standard PHP implementation.
Report back with any specific issues you have.
Here's what i did to get it running:
Im using jetty-distribution-8.1.5.v20120716
extract jetty-distribution-8.1.5.v20120716.zip to let's say a directory Jetty
i don't think this is necessary, but i didn't want other webapps/servlets to interfere the configuration, so: empty the directories Jetty/contexts, Jetty/contexts-available, Jetty/webapps
optional: to get debugging messages add the following in Jetty/etc/jetty.xml
<Get class="org.eclipse.jetty.util.log.Log" name="log"> <Call name="setDebugEnabled"> <Arg type="boolean">true</Arg> </Call> </Get>
- create a file Jetty/bin/php5-cgi-fix.sh with the content
#!/usr/bin/bash export SCRIPT_FILENAME=$1 /usr/bin/php5-cgi
create the following directories: Jetty/webapps/MYPROJECT, Jetty/webapps/MYPROJECT/WEB-INF, Jetty/webapps/MYPROJECT/cgi-bin
the content of Jetty/webapps/MYPROJECT/WEB-INFO/web.xml is
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4" > <display-name>MYPROJECT</display-name> <servlet> <servlet-name>PHP</servlet-name> <servlet-class>org.eclipse.jetty.servlets.CGI</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>commandPrefix</param-name> <param-value>../../../bin/php5-cgi-fix.sh</param-value> </init-param> <init-param> <param-name>Path</param-name> <param-value>/bin:/usr/bin:/usr/local/bin</param-value> </init-param> <init-param> <param-name>cgibinResourceBase</param-name> <param-value>webapps/MYPROJECT</param-value> </init-param> <!-- <init-param> <param-name>cgibinResourceBaseIsRelative</param-name> <param-value>true</param-value> </init-param> --> <!-- <init-param> <param-name>ENV_yourRequiredEnvironmentVariable</param-name> <param-value>yourValue</param-value> </init-param> --> <!-- Path, other ENV_variables including ENV_SystemRoot, ENV_REDIRECT_STATUS on Windows --> </servlet> <servlet-mapping> <servlet-name>PHP</servlet-name> <url-pattern>*.php</url-pattern> <!-- Any other URL patterns that are needed by your app to be processed by PHP --> </servlet-mapping> <!-- If you want http://yourhost/yourapp to call yourapp/yourapp.php then make a welcome file --> <!-- <welcome-file-list> <welcome-file>index.php</welcome-file> </welcome-file-list> --> </web-app>
create a php file like Jetty/webapps/MYPROJECT/cgi-bin/test.php with content
<?php phpinfo();
- in php.ini set cgi.force_redirect = 0
run jetty:
java -Dorg.eclipse.jetty.servlets.CGI.LEVEL=DEBUG -jar start.jar etc/jetty-requestlog.xml
OR
java -jar start.jar
- test with
http://localhost:8080/MYPROJECT/cgi-bin/test.php
HINTS:
in case you try to use this on windows via e.g. php5-cgi-fix.bat file you have to change the command prefix in web.xml config like this:
cmd.exe /c ../../../bin/php5-cgi-fix.bat
jetty source can be obtained from http://repo1.maven.org/maven2/org/eclipse/jetty/aggregate/jetty-all/ where org/eclipse/jetty/servlets/CGI.java is the cgi servlets code which executes the php scripts
hth
The servlet class has been renamed to org.eclipse.jetty.servlets.CGI
精彩评论