简体   繁体   English

在Ubuntu中执行.cgi文件

[英]Execute a .cgi file in Ubuntu

I am running Apache/PHP under Ubuntu 我在Ubuntu下运行Apache / PHP

When I run the .cgi file, by going at http://localhost/mycgi.cgi , the browser will display the code instead of running it. 当我运行.cgi文件时,通过访问http://localhost/mycgi.cgi ,浏览器将显示代码而不是运行它。

How do I make the browser execute the CGI file instead of showing its contents? 如何让浏览器执行CGI文件而不是显示其内容?

Add these lines to your apache2.conf file 将这些行添加到apache2.conf文件中

<Directory /usr/local/apache2/htdocs/somedir>
    Options +ExecCGI
</Directory>

AddHandler cgi-script .cgi .pl

Obviously, CGI execution is not set up properly. 显然, CGI执行没有正确设置。 Did you try the tutorial ? 你有没有尝试过这个教程

Usually, all CGI scripts are put into a certain location such as cgi-bin . 通常,所有CGI脚本都放在某个位置,例如cgi-bin Only the files present in that folder are executed at all. 只执行该文件夹中存在的文件。 Otherwise, they are treated as normal files and the web server simply returns them as source. 否则,它们被视为普通文件,Web服务器只是将它们作为源返回。

The tutorial shows you the alternatives that you can use to make the scripts being executed instead of just being returned as text files. 教程向您展示了可用于使脚本执行而不是仅作为文本文件返回的替代方法。

Make sure that the shebang line in your .cgi or .pl file is correct. 确保.cgi或.pl文件中的shebang行正确无误。 The shebang line tells Apache where the Perl interpreter is (the Perl interpreter is the perl.exe file). shebang行告诉Apache Perl解释器的位置(Perl解释器是perl.exe文件)。

The Apache tutorial says: Apache教程说:

Your first CGI program The following is an example CGI program that prints one line to your browser. 您的第一个CGI程序以下是一个示例CGI程序,它将一行打印到您的浏览器。 Type in the following, save it to a file called first.pl, and put it in your cgi-bin directory. 输入以下内容,将其保存到名为first.pl的文件中,并将其放在cgi-bin目录中。 #!/usr/bin/perl print "Content-type: text/html\\n\\n"; #!/ usr / bin / perl print“Content-type:text / html \\ n \\ n”; print "Hello, World."; 打印“Hello,World。”;

So here the shebang line is #!/usr/bin/perl 所以这里的shebang线是#!/usr/bin/perl

However , if perl.exe is located in a different folder, then #!/usr/bin/perl needs to be changed! 但是 ,如果perl.exe位于不同的文件夹中,则需要更改#!/usr/bin/perl This is true even for Windows users who, normally, can completely ignore and omit the shebang line. 即使对于通常可以完全忽略并省略shebang行的Windows用户也是如此。 (Why the tutorial doesn't mention this fact, i don't know - i guess they didn't realise that some people always ignore the shebang line?) (为什么教程没有提到这个事实,我不知道 - 我猜他们没有意识到有些人总是忽略了shebang线?)

For example, when I installed Perl it was automatically suggested that i install Perl in C:\\Program Files (x86)\\Perl64 Inside my Perl64 folder is folder called 'bin' and inside that is perl.exe 例如,当我安装Perl时,会自动建议我在C:\\Program Files (x86)\\Perl64安装Perl在我的Perl64文件夹中是名为'bin'的文件夹,里面是perl.exe

So i had to change the shebang line to: #!C:/Program Files (x86)/Perl64/bin/perl.exe before i could get cgi scripts to work with Apache. 所以我必须将shebang行更改为: #!C:/Program Files (x86)/Perl64/bin/perl.exe然后才能获得与Apache一起使用的cgi脚本。

This foxed me on Amazon Ubuntu 14.04. 这让我厌倦了亚马逊Ubuntu 14.04。 I had a configuration that had 'worked' for a few years: 我的配置已经“工作”了几年:

sudo a2enmod cgi
sudo service apache2 restart

Sorted this out for some existing Perl scripts with a .cgi extension and the existing virtual host configuration. 对于一些具有.cgi扩展名的现有Perl脚本和现有虚拟主机配置,对此进行了排序。 Also wrong [not this problem but associated with this upgrade]: 也错了[不是这个问题,但与此升级相关]:

virtual.host

needed to become 需要成为

virtual.host.conf

before it was recognised as valid. 在被认为有效之前。

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

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