Apache2.4终于运行python脚本了。。。但是为什么呢?

2024-06-17 10:12:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我花了很长时间才想出如何让Apache2.4运行我的“你好,世界!”python脚本。我终于弄清楚了必须在命令行中运行的命令序列才能使脚本正常工作。不幸的是,当我运行这些命令时,我仍然不明白发生了什么。我想知道我的剧本为什么有用。我知道这些都在文档中,但到目前为止,我发现很难理解其中所写的内容。在

下面是我使用的命令列表。在

  1. sudo apt-get install apache2
  2. sudo a2dismod mpm_event
  3. sudo a2enmod mpm_prefork
  4. sudo service apache2 restart
  5. sudo a2enmod cgi
  6. sudo service apache2 restart

如有任何关于第2、3和5步的意见,我们将不胜感激。在

之后我创造了脚本.py在/usr/lib/cgi-bin中:

#! /usr/bin/python
print "Content-type: text/html\n\n"
print "Hello, world!"

由于某种原因脚本.py是绝对必要的。没有它们代码是不可能运行的。在

最后我运行:

^{pr2}$

当我打电话给http://localhost/cgi-bin/script.py时,我得到了我的问候,世界!在

我甚至不需要修改apache2.conf,服务于cgi-宾.conf或000-默认.conf在

如果有更明显的/更好的/正确的方法来使用Apache24运行python脚本,我真的很想学习它。在

一些人建议将AddHandler cgi script.py.cgi添加到/etc/apache2/confenabled/serve cgi中-宾.conf如果在Apache上运行脚本时遇到问题。但出于某种原因,这对我的情况没有任何影响。为什么?在

我用的是Ubuntu 14.04。在


Tags: py命令脚本binusrconfservice世界
2条回答
  1. The event Multi-Processing Module (MPM) is designed to allow more requests to be served simultaneously by passing off some processing work to supporting threads, freeing up the main threads to work on new requests. http://httpd.apache.org/docs/2.2/mod/event.html

  2. This Multi-Processing Module (MPM) implements a non-threaded, pre-forking web server. Each server process may answer incoming requests, and a parent process manages the size of the server pool. It is appropriate for sites that need to avoid threading for compatibility with non-thread-safe libraries. It is also the best MPM for isolating each request, so that a problem with a single request will not affect any other. http://httpd.apache.org/docs/current/mod/prefork.html

(五)

a2enmod is a script that enables the specified module within the apache2 configuration. http://manpages.ubuntu.com/manpages/lucid/man8/a2enmod.8.html

名称a2enmod代表apache2 enable module。在

For some reason the first two lines of the script.py are absolutely necessary.

第一个命令告诉apache如何执行cgi脚本。毕竟,还有其他服务器端语言,比如php、perl、ruby等,apache如何知道您使用的是哪种服务器端语言?在

第二行输出一个HTTP头,这是您可以使用的最简单的头。协议体的头必须是http的输出方式。在

sudo chmod +x /usr/lib/cgi-bin/script.py 

why do I need this? how come it is not executable by default?

除非管理员授予执行文件的权限,否则无法执行该文件。这是出于安全原因。在

If there is a more obvious/better/correct way to run a python script using Apache24, I would really love to learn it.

您列出的大多数命令都是用来设置apache配置的。您不应该每次执行cgi脚本时都运行这些命令。一旦配置了apache,您所要做的就是启动apache,然后请求一个web页面。在

P.S. Some people recommend adding:

AddHandler cgi-script .py .cgi 

to /etc/apache2/conf-enabled/serve-cgi-bin.conf if you encounter a problem when running a script on Apache. But for some reason it doesn't make any difference in my case. Why?

请看这里:

AddHandler handler-name extension [extension]

Files having the name extension will be served by the specified handler-name. This mapping is added to any already in force, overriding any mappings that already exist for the same extension. For example, to activate CGI scripts with the file extension .cgi, you might use:

AddHandler cgi-script .cgi

Once that has been put into your httpd.conf file, any file containing the .cgi extension will be treated as a CGI program. http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addhandler

因此,当您添加AddHandler行时,它似乎正在重写某个执行相同操作的配置设置。在

回复评论

ScriptInterpreterSource Directive

This directive is used to control how Apache httpd finds the interpreter used to run CGI scripts. The default setting is Script. This causes Apache httpd to use the interpreter pointed to by the shebang line (first line, starting with #!) in the script http://httpd.apache.org/docs/current/mod/core.html

在同一页上,有这样的指示:

CGIMapExtension Directive

This directive is used to control how Apache httpd finds the interpreter used to run CGI scripts. For example, setting CGIMapExtension sys:\foo.nlm .foo will cause all CGI script files with a .foo extension to be passed to the FOO interpreter.

mpm代表Multi-Processing Module;基本上,您将基于event的方法替换为{};这是Apache内部使用的,通常不会影响性能以外的任何东西(每个MPM都有不同的性能特征),但是有些东西与某些MPM不兼容,然后您需要更改它们。在

cgi模块是提供Common Gateway Interface的附加模块;默认情况下,它不再包含在Apache中。在

脚本的第一行是shebang;它告诉Unix/Linux内核使用什么程序作为解释器;也就是说,“请使用/usr/bin/python来运行这个文件”。文件扩展名在*nix w.r.t可执行性中没有任何意义。在

第二行是标题。CGI规范指出output shall be headers followed by an empty line,后跟内容。1头是必需的:Content-Type。这里您告诉web服务器和浏览器,下面是text/html类型的文档。'\n'代表换行符。(严格来说,你应该写

print "Content-type: text/html\n\n",

在那里加一个逗号,否则你会得到一个新行太多)。在

默认情况下,*nix中的文件没有打开^{} execute bit-这是一个安全特性;需要有意识的决定才能使某些内容可执行。在


至于首选方法,因为您控制了服务器,所以可以将Apachemod_wsgi与任何web框架一起使用-PyramidFlaskDjango等;WSGI应用程序比CGI效率高很多。在

相关问题 更多 >