天天看點

Linux下配置Tomcat6和PHP

初步接觸php,因為以前用過tomcat,是以想用tomcat+php學習一下。網上搜到的tomcat+php的配置教程都是在windows系統上,經過一番折騰之後,發現Linux與windows的配置方法差不多,其間也是曆經痛苦,記錄一下。

我用的Linux系統是CentOS,tomcat和php都是用yum安裝的,PHP版本是5.3.3,php.ini被放到了/etc/php.ini。以前使用tomcat都是在內建開發環境裡面,很多事情IDE都給做了,現在又重新學習了一遍,當然也因為之前學的不紮實。

首先使用tomcat的web.xml檔案進行配置tomcat,在其中添加一個處理CGI請求的servlet和相應的映射。在/etc/tomcat6/下有全局web.xml,也可以在自己網站根目錄下建專用的web.xml,修改全局web.xml需要重新開機tomcat生效。其中在全局web.xml中有各字段的含義和預設值。

<servlet>
        <servlet-name>cgi</servlet-name>
        <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
        <init-param>
          <param-name>debug</param-name>
          <param-value>0</param-value>
        </init-param>
        <init-param>
          <param-name>clientInputTimeout</param-name>
          <param-value>200</param-value>
        </init-param>
        <init-param>
          <param-name>passShellEnvironment</param-name>
          <param-value>true</param-value>
        </init-param>
        <init-param>
          <param-name>executable</param-name>
          <param-value>/usr/bin/php-cgi</param-value>
        </init-param>
       <init-param>
          <param-name>cgiPathPrefix</param-name>
          <param-value></param-value>
        </init-param>
        <load-on-startup>5</load-on-startup>
    </servlet>
<servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
           

由于CGIServlet的使用權限限制,要在tomcat的context.xml中對context加入字段<Context reloadable="true" privileged="true">

另外,還有配置一下PHP。在php.ini中找到cgi.force_redirect,可以看到其是為了保證php在網站中運作的安全性。

; cgi.force_redirect is necessary to provide security running PHP as a CGI under
; most web servers.  Left undefined, PHP turns this on by default.  You can
; turn it off here AT YOUR OWN RISK
; **You CAN safely turn this off for IIS, in fact, you MUST.**
; http://www.php.net/manual/en/ini.core.php#ini.cgi.force-redirect
           

在自己學習,可以将其設定為0,cgi.force_redirect=0;在設定為1時,需要設定cgi.redirect_status_env,提供一個環境變量,比如PWD:

; if cgi.force_redirect is turned on, and you are not running under Apache or Netscape
; (iPlanet) web servers, you MAY need to set an environment variable name that PHP
; will look for to know it is OK to continue execution.  Setting this variable MAY
; cause security issues, KNOW WHAT YOU ARE DOING FIRST.
; http://www.php.net/manual/en/ini.core.php#ini.cgi.redirect-status-env
cgi.redirect_status_env = “PWD”
           

這個環境變量告訴PHP可以繼續執行,當然自己要承擔其中可能出現的安全問題。