准备进行linux php开发,因为我机器上的apache,mysql,php系统已经安装过了,并且目前都是最新更新,安装这一步骤省略。
apache路径:/usr/sbin/apachectl
php路径:/var/lib/php
并且配置文件也一应俱全,/etc/httpd/conf/httpd.conf 和usr/php.ini存在,心中大喜。
修改php.ini文件的register_globals = off 改为register_globals = On之后赶紧写了一个静态网页test.html,重启apache服务器:
/usr/sbin/apachectl start之后,运行test.html正常,证明apache和php已经安装完毕,并且运行。
下一步,测试下.php动态网页吧,phptest.php其保存在php路径下了/var/www/html/phptest.php,内容很简单:
<?php
phpinfo();
?>
结果很令人失望:phpinfo()根本没有执行,这是为什么呢?
上网查询,很多人都说重新安装apache和php,但是我觉得这个问题不是源自安装,还是配置文件的原因。
重新审视配置文件之后,发现在php.ini里有这么一段话:
; This directive determines whether or not PHP will recognize code between ; <? and ?> tags as PHP source which should be processed as such. It's been ; recommended for several years that you not use the short tag "short cut" and ; instead to use the full <?php and ?> tag combination. With the wide spread use ; of XML and use of these tags by other languages, the server can become easily ; confused and end up parsing the wrong code in the wrong context. But because ; this short cut has been a feature for such a long time, it's currently still ; supported for backwards compatibility, but we recommend you don't use them. ; Default Value: On ; Development Value: Off ; Production Value: Off ; http://www.php.net/manual/en/ini.core.php#ini.short-open-tag
short_open_tag = Off
终于找到真正原因了,short_open_tag这个指令确定了php是否识别<?和?>之间的代码,默认的是On,目前设置的是off,那么就将其修改为:short_open_tag = On,然后再运行phptest.php, php解析正确,运行成功