简体   繁体   English

将php5_module添加到apache服务器的复杂性

[英]intricacies of adding php5_module to an apache server

I have a lot of questions regarding the differences between php files on a server and perl files (or any other for that matter). 我对服务器上的php文件和perl文件(或与此有关的任何其他文件)之间的差异有很多疑问。 But I'm going to try and start off simple and work up. 但我将尝试从简单开始并逐步完善。

First I clean installed OSX Yosemite on my mac. 首先,我在Mac上清理了已安装的OSX Yosemite。 Apache2 comes pre-installed with this OS. 该操作系统已预安装Apache2。 I changed nothing in the httpd.conf and my files are located here: 我没有对httpd.conf进行任何更改,并且文件位于此处:

DocumentRoot "/Library/WebServer/Documents"

I have 3 files welcome.pl, welcome.php and welcome.txt all with the same content. 我有3个具有相同内容的文件welcome.pl,welcome.php和welcome.txt。

$name="Matt";
$email="foo@bar.com";

print "<!DOCTYPE html>\n";
print "<html>\n<body>\n\nWelcome " . $name . "<br>\nYour email address is: " . $email . "\n\n</body>\n</html>";

Except welcome.pl replaces 除了welcome.pl替换

$email="foo@bar.com";

with

$email="foo\@bar.com";

since perl scripts escape the @ sign other than that the only difference is their extension. 由于perl脚本会转义@符号,因此唯一的区别就是扩展名。

If I start my apache server and open three browser windows and go to their respective pages then they all look the same: 如果我启动我的apache服务器并打开三个浏览器窗口并转到它们各自的页面,则它们看起来都是一样的:

HTTP://localhost/welcome.php

(reproduced in Safari and Chrome) (在Safari和Chrome中复制)

Likewise if I open three browser windows and go to their respective file system resource then they all look the same: 同样,如果我打开三个浏览器窗口并转到它们各自的文件系统资源,则它们看起来都相同:

三个文件系统文件的输出相同

(reproduced in Safari and Chrome) (在Safari和Chrome中复制)

However, if I then enable php by uncommenting: 但是,如果我随后通过取消注释启用php:

#LoadModule php5_module libexec/apache2/libphp5.so

in my httpd.conf file, restart the server and open three browser windows and go to their respective pages then welcome.php changes to: 在我的httpd.conf文件中,重新启动服务器并打开三个浏览器窗口,然后转到它们各自的页面,然后welcome.php更改为:

启用通用服务器php

(reproduced in Safari and Chrome) (在Safari和Chrome中复制)

However, again if I go to their respective file system resource then they all look the same. 但是,如果再次访问它们各自的文件系统资源,则它们看起来都一样。 Why does the server parse things differently for the php file? 为什么服务器对php文件的解析方式有所不同?

Now lets change welcome.php to 现在让我们将welcome.php更改为

<?php
$name="Matt";
$email="foo@bar.com";

print "<!DOCTYPE html>\n";
print "<html>\n<body>\n\nWelcome " . $name . "<br>\nYour email address is: " . $email . "\n\n</body>\n</html>";
?>

If I save the file and restart the server then I get: 如果我保存文件并重新启动服务器,则会得到:

欢迎使用带有php标签的php文件

(reproduced in Safari and Chrome) (在Safari和Chrome中复制)

which makes sense, but if I change welcome.pl to: 这很有意义,但是如果我将welcome.pl更改为:

#!/usr/bin/perl
print "Content-type: text/html\n\n";
$name="Matt";
$email="foo\@bar.com";

print "<!DOCTYPE html>\n";
print "<html>\n<body>\n\nWelcome " . $name . "<br>\nYour email address is: " . $email . "\n\n</body>\n</html>";

I still get: 我仍然得到:

欢迎使用Perl解释器注释的Perl文件

(reproduced in Safari and Chrome) (在Safari和Chrome中复制)

However, if I uncomment: 但是,如果我取消评论:

#LoadModule cgi_module libexec/apache2/mod_cgi.so

in the modules section, replace 在“模块”部分中,替换

Options FollowSymLinks Multiviews

with: 有:

Options FollowSymLinks Multiviews ExecCGI

in the "Library/WebServer/Documents" directory directive and add 在“ Library / WebServer / Documents”目录指令中并添加

AddHandler cgi-script .cgi .pl

in the "Library/WebServer/Documents" directory directive then if I restart the server I get: 在“ Library / WebServer / Documents”目录指令中,然后如果我重新启动服务器,则会得到:

已启用ExecCGI和perl CGI句柄的欢迎Perl文件

(reproduced in Safari and Chrome) (在Safari和Chrome中复制)

but now if I replace 但是现在如果我更换

AddHandler cgi-script .cgi .pl

AddHandler cgi-script .cgi .pl php

restart the server and go to localhost/welcome.php 重新启动服务器并转到localhost / welcome.php

then I get an internal server error (reproduced in Safari and Chrome) 然后出现内部服务器错误(在Safari和Chrome中复制)

but then if I replace 但是如果我替换

<?php
$name="Matt";
$email="foo@bar.com";

print "<!DOCTYPE html>\n";
print "<html>\n<body>\n\nWelcome " . $name . "<br>\nYour email address is: " . $email . "\n\n</body>\n</html>";
?>

with

#!/usr/bin/php
<?php
print "Content-type: text/html\n\n";
$name="Matt";
$email="foo@bar.com";

print "<!DOCTYPE html>\n";
print "<html>\n<body>\n\nWelcome " . $name . "<br>\nYour email address is: " . $email . "\n\n</body>\n</html>";
?>

then it's fine again (reproduced in Safari and Chrome). 然后就可以了(在Safari和Chrome中复制)。 I guess my point is there seems to be two types of compilation of php on the server. 我想我的意思是服务器上似乎有两种php编译类型。 One for compiling text files with php extensions and another for running php scripts and returning the output to the client. 一个用于编译带有php扩展名的文本文件,另一个用于运行php脚本并将输出返回给客户端。

I would like to know why this is. 我想知道为什么会这样。 Is the difference only in the server? 区别仅在于服务器吗? Could I pass a php file in the command line as input to the php compiler and get similar results or is the server doing some intermediate work that I'm not seeing? 我可以在命令行中将php文件作为输入传递给php编译器,并得到相似的结果,还是服务器正在执行一些我没看到的中间工作?

It seems to me that you have installed both PHP as a CGI, and as an Apache module ( mod_php5 ). 在我看来,您已经安装 PHP作为CGI和Apache模块( mod_php5 )。

In both cases you need the PHP open tag <?php for a file to "work"; 在这两种情况下,都需要PHP打开标记<?php来使文件“工作”; the error is different in the two cases though, CGI giving you an internal server error and the module displaying the file contents as HTML. 但是,这两种情况下的错误有所不同,CGI会为您提供内部服务器错误,而模块会将文件内容显示为HTML。

You could probably reproduce a similar dual behaviour for Perl files by installing mod_perl . 您可以通过安装mod_perl为Perl文件重现类似的双重行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM