php是一种脚本语言,但是是运行在服务端的,与之对应的js也是脚本语言,却是运行在客户端的。所以要运行php页面,必须要搭起一个服务端才行,这里选择的是常用的apache tomcat。
1、首先安装php的运行环境
http://www.php.net/downloads.php,就像用java开发一样,需要按照jdk虚拟环境,那么php运行,也需要一个环境,java叫jvm,php叫cgi。
2、然后配置cgi的path环境变量 ,在电脑的环境变量path添加cgi的安装路径 D:\php-7.1.0-nts-Win32-VC14-x64\
3、添加cgi的servlet的支持,在php的安装路径下,找到php.ini-development文件,然后复制一份改成php.ini,然后右击修改php.ini文件。
然后打开这些注释:
<servlet>
<servlet-name>cgi</servlet-name>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
<init-param>
<param-name>cgiPathPrefix</param-name>
<param-value>cgibin</param-value>
<param-name>passShellEnvironment</param-name>
<param-value>true</param-value>
</init-param>
<init-param> //这是后续加上去的
<param-name>executable</param-name>
<param-value>D:\javaWeb\php-7.1.0-nts-Win32-VC14-x64\php-cgi.exe</param-value>
</init-param>
<load-on-startup>5</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cgi</servlet-name>
<url-pattern>/cgibin/*</url-pattern>
</servlet-mapping>
//这些参数也要打开
cgiPathPrefix - The CGI search path will start at the web application root directory + File.separator + this prefix. The default cgiPathPrefix is WEB-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 is perl.
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.
最后,在Dreamweaver中新建一个php文件,然后关联站点到Apache tomcate的文件夹:
D:\javaWeb\apache-tomcat-7.0.73\webapps\php\cgibin\
url :http://localhost:8080/php/cgibin/
连接成功后,F12运行,就可以看到php内容了。
如果没有确定是否连上,可以文件的面板上,右击->上传,然后在F12运行。