您的位置:首页 > 运维架构 > Tomcat

Linux下配置Tomcat6和PHP

2015-12-05 17:41 393 查看
初步接触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[/code] 
在自己学习,可以将其设置为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可以继续执行,当然自己要承担其中可能出现的安全问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux Tomcat6 php