1. 程式人生 > >讓Tomcat執行PHP的幾種方式

讓Tomcat執行PHP的幾種方式

環境:
作業系統:IBM aix5.3(當然linux環境和windows下肯定是沒問題的)

1 執行php當然首選apache這裡就不講apache方式了,可見於http://www.blogjava.net/freeman1984/archive/2011/06/10/352043.html,但是在aix下php和apache的結合始終沒有裝好,linux下我試過一點問題沒有。有成功的案例可以給個連結看看,謝謝!

下面介紹兩種單獨使用tomcat執行php的:
-------------------------------------------------------------------------------------------


1 使用tomcat內建的cgi支援(需要安裝php環境,去官網下載就行了http://www.php.net/downloads.php,要注意的是,我在aix上5.X裝不上,用4.X的沒問題)
    相關文件可見於tomcat的安裝目錄webapps下面的doc檢視cgi-howto.html介面裡面有介紹,當然tomcat5.X和6.X有不同的地方:
  5.X步驟:
1 :Rename$CATALINA_BASE/server/lib/servlets-cgi.renametojarto$CATALINA_BASE/server/lib/servlets-cgi.jar.
  這個大家應該知道意思。
2.Remove the XML comments from around the CGI servlet and servlet-mapping configuration in$CATALINA_BASE/conf/web.xml
.
(也就是新增cgi的servlet支援,開啟註釋即可)
  具體如下:

        cgi
        org.apache.catalina.servlets.CGIServlet
          debug
          0
          cgiPathPrefix
          WEB-INF/cgi
         passShellEnvironment   
          true   
         5
還有後面的mapping



        cgi
        /cgi-bin/*

3 新增several servlet init parameters which can be used to configure the behaviour of the CGI servlet(新增幾個servlet的引數)


  • cgiPathPrefix- The CGI search path will start at the web application root directory + File.separator + this prefix. The default cgiPathPrefix isWEB-INF/cgi(php檔案放置的位置)
  • debug- Debugging detail level for messages logged by this servlet. Default 0.
  • executable- The of the executable to be used to run the script. Default isperl.(新增解析php的引擎)
  • parameterEncoding- Name of the parameter encoding to be used with the GCI servlet. Default isSystem.getProperty("file.encoding","UTF-8").
  • passShellEnvironment- Should the shell environment variables (if any) be passed to the CGI script? Default isfalse.

    我們需要新增executable 即可:windows下我的php引擎位置:D:\PHP\php-cgi.exe(aix下為/usr/local/php4/bin/php),因此在第二步裡面新增下面引數:
       
       executable
       D:\PHP\php-cgi.exe 
      
tomcat6.X下面沒有第一步。
4,測試,新增一個web應用,這裡使用ROOT即可,編寫一個index.php放到webapps/ROOT/WEB-INF/cgi/下面內容:

啟動tomcat,訪問http://localhost:8080/cgi-bin/index.php即可。
---------------------------------------------------------------------------------

另外一種方式是使用quercus:
    Quercus是Caucho公司採用純Java開發的一個PHP5引擎。基於開源授權協議GPL釋出。Quercus自帶很多個PHP模組和擴充套件如 PDF,PDO,MySQL和JSON。可以利用這個引擎在一些JavaEE應用容器(Resin,GlassFish等)中執行PHP程式,也可以在 PHP指令碼中呼叫一些Java服務如JMS。Quercus會預先把PHP檔案編譯成.java檔案,然後再執行。http://quercus.caucho.com/
   當然可以使用resin作為伺服器執行也可以,我們這裡使用tomcat,需要jdk1.5以上(我試了1,4貌似不行,估計和我下載的quercus有關係),下面是步驟:
1 我們把php專案當做一個web專案放在webapps下,並在裡面建WEB-INF\lib目錄
2 從http://quercus.caucho.com/。下載quercus-4.0.18.zip ,提取裡面的web.xml放到
  WEB-INF下面,提取裡面的cdi-16.jar,resin.jar放到WEB-INF\lib下面。
3 啟動tomcat,
4 像訪問j2ee web應用一樣訪問php應用即可。我們現在aix生產環境用的就是這種,比較穩定。

其他方式兄弟們可以擴充。