简体   繁体   English

麻烦在apache下运行python脚本为cgi(ubuntu 12)

[英]Trouble running python script as cgi under apache (ubuntu 12)

Disclosure: I searched a lot, and I don't think my question (for my configuration) is answered here. 披露:我搜索了很多,我不认为我的问题(对于我的配置)在这里得到了解答。 For example run python script as cgi apache server doesn't answer it. 例如, 运行python脚本,因为cgi apache服务器没有回答它。

So: I have a simplest script possible: 所以:我有一个最简单的脚本:

#!/usr/bin/env python

print "Content-type: text/html"
print ""

print "<h1>Hello from Python!</h1>"

When I run it in a browser, it literally displays itself instead of expected Hello from Python! 当我在浏览器中运行它时,它实际上显示自己而不是Python的预期Hello!

I did the following to make it run: 我做了以下操作让它运行:

a) it is executable by everyone; a)每个人都可以执行; It runs in a shell perfectly. 它完美地运行在一个外壳中。

b) it is in a virtual directory that has the following configuration (in/etc/apache2/sites-available/my_cgi_dir): b)它位于具有以下配置的虚拟目录中(在/ etc / apache2 / sites-available / my_cgi_dir中):

 <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/ <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/my_cgi_dir/> Options Indexes +ExecCGI FollowSymLinks MultiViews AddHandler cgi-script .cgi .py AllowOverride None Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 

As you see it has 如你所见

  Options Indexes +ExecCGI FollowSymLinks MultiViews 

and

  AddHandler cgi-script .cgi .py 

c) I made sure apache has python support by running sudo apt-get install libapache2-mod-python c)我通过运行sudo apt-get install libapache2-mod-python确保apache具有python支持

d) Yes I did restart apache. d)是的我确实重启了apache。

Still, I just see the script's source instead of "Hello Python". 不过,我只是看到脚本的源而不是“Hello Python”。

What am I missing? 我错过了什么?

Please help. 请帮忙。


PS: if that might help, here is what I am running: PS:如果这可能会有所帮助,这就是我正在运行的:

Linux ip-172-31-37-178 3.2.0-40-virtual #64-Ubuntu SMP Mon Mar 25 21:42:18 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux Linux ip-172-31-37-178 3.2.0-40-virtual#64-Ubuntu SMP Mon Mar 25 21:42:18 UTC 2013 x86_64 x86_64 x86_64 GNU / Linux

Server Version: Apache/2.2.22 (Ubuntu) 服务器版本:Apache / 2.2.22(Ubuntu)

Python 2.7.3 Python 2.7.3

What could also cause these symptoms, is that you don't have the apache2 cgi module loaded. 什么也可能导致这些症状,是你没有加载apache2 cgi模块。 This will also generate a log message in /var/log/apache2/access.log with an HTTP 304 error: 这也将在/var/log/apache2/access.log生成一条带有HTTP 304错误的日志消息:

192.168.2.3 - - [26/Jul/2014:11:56:34 +0200] "GET /cgi-bin/hello.py HTTP/1.1" 304 179 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"

Check loaded modules with: 检查加载的模块:

apache2ctl -M

Look for: 寻找:

cgid_module (shared)

If it's not loaded, load it with: 如果没有加载,请加载:

a2enmod cgid

Then restart apache2: 然后重启apache2:

service apache2 reload

Then refresh your browser and purge your browser cache (CTRL + F5). 然后刷新浏览器并清除浏览器缓存(CTRL + F5)。 And/or restart your browser, to be sure it's requesting the actual page, instead of using the browser cache. 和/或重新启动浏览器,以确保它正在请求实际页面,而不是使用浏览器缓存。

try this 试试这个

    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
    </Directory>

cgi script cgi脚本

import cgi
import cgitb; cgitb.enable()
print "Content-type: text/html\n\n"
print "<h1>Hello from Python!</h1>"

Why don't you configure like this? 你为什么不这样配置? here. 这里。

ScriptAlias /cgi-bin/ /var/www/my_cgi_dir/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

In my case, I made a mistake with my ScriptAlias directive. 就我而言,我的ScriptAlias指令犯了一个错误。 I uncommented the original one, but forgot to configure a new one. 我取消了原来的一个,但忘了配置一个新的。

As soon as I correctly changed and saved my sites-available/default config file from this: 一旦我正确地更改并保存了我的sites-available/default配置文件:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">

.. to this: ..对此:

ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/var/www/cgi-bin">

.. and reloaded apache2, it just worked: it stopped displaying my scripts as text, and started running them as a script. ..并重新加载apache2,它只是工作:它停止以文本形式显示我的脚本,并开始将它们作为脚本运行。 Also, it no longer displayed /var/www/cgi-bin as a directory in the browser, but now correctly displays the error: 此外,它不再将/var/www/cgi-bin为浏览器中的目录,但现在可以正确显示错误:

Forbidden 
You don't have permission to access /cgi-bin/ on this server.

I was having the same problem and tried all of the above. 我遇到了同样的问题并尝试了以上所有方法。 Nothing helped. 什么都没有帮助。 In the end it turned out to be a really stupid mistake. 最后,结果证明这是一个非常愚蠢的错误。 Since many people seem to have problems with this, I will post it anyway: 由于很多人似乎都有这方面的问题,我会发布它:

I had not sym-linked my default site from "sites-available" to "sites-enabled" as described at the top of the apache.conf file: 我没有将我的默认站点从“sites-available”sym链接到“sites-enabled”,如apache.conf文件顶部所述:

[...] Configuration files in the mods-enabled/ and sites-enabled/ directories contain particular configuration snippets which manage modules or virtual host configurations, respectively. [...] mods-enabled /和sites-enabled /目录中的配置文件包含分别管理模块或虚拟主机配置的特定配置片段。

They are activated by symlinking available configuration files from their respective *-available/ counterparts. 它们通过符合各自* -available /对应的可用配置文件进行激活来激活。 These should be managed by using our helpers a2enmod/a2dismod, a2ensite/a2dissite. 这些应该通过使用我们的助手a2enmod / a2dismod,a2ensite / a2dissite来管理。 See their respective man pages for detailed information. 有关详细信息,请参见各自的手册页。 [...] [...]

Thus, all my edits to the default file were not read by apache. 因此,apache无法读取我对默认文件的所有编辑。 Once I made the symlink, it worked. 一旦我创建了符号链接,就可以了。

This worked for me,, as @flyking_ suggested, follow with some extra steps in the same order. 这对我有用,正如@flyking_建议的那样,按照相同的顺序执行一些额外的步骤。

I had to change in the directory - I am using raspberrypi (NOOB version linux) /etc/apache2/sites-available/default 我不得不在目录中更改 - 我正在使用raspberrypi(NOOB版本linux)/ etc / apache2 / sites-available / default

maintain the ones in these adding +ExecCGI and also Addhander cgi-script .cgi .py as below 维护这些添加+ ExecCGI以及Addhander cgi-script .cgi .py中的那些 ,如下所示

<Directory /var/www/>
       Options Indexes +ExecCGI FollowSymLinks MultiViews
       AddHandler cgi-script .cgi .py
       AllowOverride None
      Order allow,deny
      allow from all

Then restart apache 然后重启apache

service apache2 restart

This should restart the service, and verify whether the python script runs fine without errors in the terminal, before launching it in the browser. 这应该重新启动服务,并在浏览器中启动之前验证python脚本是否正常运行而终端没有错误。

If no errors, then the sampe script would run fine. 如果没有错误,则sampe脚本运行正常。

import cgi
import cgitb; cgitb.enable()
print "Content-type: text/html\n\n"
print "<h1>Hello from Python!</h1>"

Imp: later my script worked fine even without both the imports. Imp:后来我的脚本工作正常,即使没有导入。

Note: Clear the cache in the browser, or load it afresh with ctrl+F5. 注意:清除浏览器中的缓存,或使用ctrl + F5重新加载。 Hopefully this should solve. 希望这应该解决。

If this doesnt solve, then try this as @user2449877 suggested Check loaded modules with: 如果这没有解决,那么试试这个@ user2449877建议检查加载的模块:

 apache2ctl -M

Look for: 寻找:

 cgid_module (shared)

If it's not loaded, load it with: 如果没有加载,请加载:

  a2enmod cgid    

Restart apache and then refresh browser 重启apache然后刷新浏览器

In my case (which is a shared unix machine), I also need to create a hidden file ".htaccess" with the following: 在我的情况下(这是一个共享的unix机器),我还需要创建一个隐藏文件“.htaccess”,其中包含以下内容:

Options +ExecCGI
SetHandler cgi-script 

It is also important to set the permissions accordingly (755 - group and other with no writing but execution permissions) as well as having: 同样重要的是设置权限(755 - 组和其他没有写入但执行权限)以及具有:

#!/usr/bin/python

as a first line of the python CGI script. 作为python CGI脚本的第一行。

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

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