在GAE上搭建PHP环境并开启URL重写

from 博客园_首页 http://www.cnblogs.com/eslizn/archive/2012/07/31/php-on-the-google-app-engine.html

GAE不直接支持php非常遗憾,但是可以利用Quercus来让GAE支持php,其原理是将php预编译为java文件再执行.

 

1.下载quercus:

版本当然最新的最好,因为原则上来说新版本对php支援程度更高,但是在自己测试的时候发现最新的4.0.25存在一点问题,于是换用4.0.18版本.

选择WAR格式的文件下载,利用Winrar解压,将WEB-INF\lib\的jar拷贝至GAE工程下的war\WEB-INF\lib\目录

 

2.配置Quercus:

在appengine-web.xml中配置对php文件的支持:

    <static-files>        <exclude path="/**.php" />    </static-files>    <resource-files>        <include path="/**.php" />    </resource-files>

在web.xml中添加一个servlet:

    <servlet>        <servlet-name>Quercus Servlet</servlet-name>        <servlet-class>com.caucho.quercus.servlet.GoogleQuercusServlet</servlet-class>    </servlet>

添加对php文件的映射:

    <servlet-mapping>        <servlet-name>Quercus Servlet</servlet-name>        <url-pattern>*.php</url-pattern>    </servlet-mapping>

 

3.实现URL重写(通过UrlRewriteFilter实现):

下载UrlRewriteFilter,将urlrewritefilter-*.jar拷贝在工程的war\WEB-INF\lib\目录下

在web.xml中添加URL过滤

    <filter>        <filter-name>UrlRewriteFilter</filter-name>        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>UrlRewriteFilter</filter-name>        <url-pattern>/*</url-pattern>        <dispatcher>REQUEST</dispatcher>        <dispatcher>FORWARD</dispatcher>    </filter-mapping>

在工程的war\WEB-INF目录下新建一个Url重写配置文件:urlrewrite.xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"        "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd"><urlrewrite>    <rule enabled="true" match-type="regex">      <note>UrlRewrite</note>      <condition type="request-filename" operator="notfile" name="notfile" next="and"/>      <condition type="request-filename" operator="notdir" name="notdir" next="and"/>      <from>/(.*)</from>      <to last="true" type="forward">/index.php</to>    </rule></urlrewrite>

这条规则就等同于.htaccess中的:

RewriteCond %{SCRIPT_FILENAME} !-fRewriteCond %{SCRIPT_FILENAME} !-dRewriteRule ^(.*)$ index.php/$1

注意:这条规则可能会导致GAE本地管理http://localhost:8888/_ah/admin/失效,由于时间关系就不再修正.

 

4.测试:

在工程的war\目录下新建一个index.php文件:

<?phpecho '<pre>';print_r($_SERVER);?>

由于我已经将index.php设置为welcome文件,所以直接打开http://localhost:8888/

效果如图所示:

 

附上一些参考资料:

http://blog.caucho.com/2009/04/28/quercus-on-the-google-app-engine/

http://blog.caucho.com/2009/05/31/quercus-on-google-app-engine/

http://tuckey.org/urlrewrite/#documentation

PHPer们还在犹豫什么,赶紧上吧~

本文链接

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s