Apache2.4 终于是 运行 python 脚本...但是为什么呢?
Apache2.4 is finally running python script... but why?
我花了很长时间才弄清楚如何制作 Apache2.4 运行 我的 "Hello, world!" python 脚本。我终于弄清楚了我必须在命令行中 运行 什么命令序列才能使脚本工作。不幸的是,当我 运行 这些命令时,我仍然不明白 发生了什么。我想知道 为什么 他们让我的脚本工作。我知道这一切都在文档中,但到目前为止我发现很难理解那里写的内容。
这是我使用的命令列表。
sudo apt-get install apache2
sudo a2dismod mpm_event
sudo a2enmod mpm_prefork
sudo service apache2 restart
sudo a2enmod cgi
sudo service apache2 restart
如果您对第 2、3 和 5 步提出任何意见,我们将不胜感激。
之后我在 /usr/lib/cgi-bin 中创建 script.py:
#! /usr/bin/python
print "Content-type: text/html\n\n"
print "Hello, world!"
出于某种原因,script.py 的前两行是绝对必要的。没有它们,代码就无法 运行。
最后我运行:
sudo chmod +x /usr/lib/cgi-bin/script.py #why do I need this? how come it is not executable by default?
sudo service apache2 restart
当我调用 http://localhost/cgi-bin/script.py 时,我得到了我的 Hello, world!
我什至不需要修改 apache2.conf、serve-cgi-bin.conf 或 000-default.conf
如果有更多 obvious/better/correct 的方法来 运行 使用 Apache24 的 python 脚本,我真的很想学习它。
P.S。如果在 Apache 上 运行 运行脚本时遇到问题,有人建议将 AddHandler cgi-script .py .cgi 添加到 /etc/apache2/conf-enabled/serve-cgi-bin.conf。但出于某种原因,这对我的情况没有任何影响。为什么?
P.P.S。我使用 Ubuntu 14.04.
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
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
5)
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启用模块。
For some reason the first two lines of the script.py are absolutely
necessary.
第一个告诉 apache 如何执行你的 cgi 脚本。毕竟,还有其他服务器端语言,如 php、perl、ruby 等。apache 应该如何知道您使用的是哪种服务器端语言?
第二行输出一个 HTTP header,这是您可以使用的最简单的 header。 header要求在请求的body之前输出——这就是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,然后请求一个网页。
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;基本上,您用 prefork
替换了基于 event
的方法;这是 Apache 内部使用的,通常不会影响任何超出性能的东西(每个都有不同的性能特征),但有些东西与某些 MPM 不兼容,然后你需要改变它们。
cgi
模块是一个附加模块,提供Common Gateway Interface;它不再默认包含在 Apache 中。
脚本的第一行是shebang;它告诉 Unix/Linux 内核使用什么程序作为解释器;那是; "use /usr/bin/python
to run this file please"。文件扩展名在 *nix w.r.t 可执行性中没有任何意义。
第二行是header。 CGI 规范说 output shall be headers followed by an empty line,然后是内容。 1 header 是强制性的:Content-Type
。在这里,您告诉网络服务器和浏览器,接下来是 text/html
类型的文档。 '\n'
代表一个换行符。 (技术上你应该写
print "Content-type: text/html\n\n",
那里有一个逗号,否则换行太多了)。
*nix 中的文件默认没有 +x
execute bit - 这是一项安全功能;需要有意识的决定才能使某些东西可执行。
至于首选方法,因为您可以控制服务器,所以可以将 Apache mod_wsgi
与任何 Web 框架一起使用 - Pyramid, Flask, Django,等等; WSGI 应用程序比 CGI 更高效。
我花了很长时间才弄清楚如何制作 Apache2.4 运行 我的 "Hello, world!" python 脚本。我终于弄清楚了我必须在命令行中 运行 什么命令序列才能使脚本工作。不幸的是,当我 运行 这些命令时,我仍然不明白 发生了什么。我想知道 为什么 他们让我的脚本工作。我知道这一切都在文档中,但到目前为止我发现很难理解那里写的内容。
这是我使用的命令列表。
sudo apt-get install apache2
sudo a2dismod mpm_event
sudo a2enmod mpm_prefork
sudo service apache2 restart
sudo a2enmod cgi
sudo service apache2 restart
如果您对第 2、3 和 5 步提出任何意见,我们将不胜感激。
之后我在 /usr/lib/cgi-bin 中创建 script.py:
#! /usr/bin/python
print "Content-type: text/html\n\n"
print "Hello, world!"
出于某种原因,script.py 的前两行是绝对必要的。没有它们,代码就无法 运行。
最后我运行:
sudo chmod +x /usr/lib/cgi-bin/script.py #why do I need this? how come it is not executable by default?
sudo service apache2 restart
当我调用 http://localhost/cgi-bin/script.py 时,我得到了我的 Hello, world!
我什至不需要修改 apache2.conf、serve-cgi-bin.conf 或 000-default.conf
如果有更多 obvious/better/correct 的方法来 运行 使用 Apache24 的 python 脚本,我真的很想学习它。
P.S。如果在 Apache 上 运行 运行脚本时遇到问题,有人建议将 AddHandler cgi-script .py .cgi 添加到 /etc/apache2/conf-enabled/serve-cgi-bin.conf。但出于某种原因,这对我的情况没有任何影响。为什么?
P.P.S。我使用 Ubuntu 14.04.
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
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
5)
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启用模块。
For some reason the first two lines of the script.py are absolutely necessary.
第一个告诉 apache 如何执行你的 cgi 脚本。毕竟,还有其他服务器端语言,如 php、perl、ruby 等。apache 应该如何知道您使用的是哪种服务器端语言?
第二行输出一个 HTTP header,这是您可以使用的最简单的 header。 header要求在请求的body之前输出——这就是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,然后请求一个网页。
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;基本上,您用 prefork
替换了基于 event
的方法;这是 Apache 内部使用的,通常不会影响任何超出性能的东西(每个都有不同的性能特征),但有些东西与某些 MPM 不兼容,然后你需要改变它们。
cgi
模块是一个附加模块,提供Common Gateway Interface;它不再默认包含在 Apache 中。
脚本的第一行是shebang;它告诉 Unix/Linux 内核使用什么程序作为解释器;那是; "use /usr/bin/python
to run this file please"。文件扩展名在 *nix w.r.t 可执行性中没有任何意义。
第二行是header。 CGI 规范说 output shall be headers followed by an empty line,然后是内容。 1 header 是强制性的:Content-Type
。在这里,您告诉网络服务器和浏览器,接下来是 text/html
类型的文档。 '\n'
代表一个换行符。 (技术上你应该写
print "Content-type: text/html\n\n",
那里有一个逗号,否则换行太多了)。
*nix 中的文件默认没有 +x
execute bit - 这是一项安全功能;需要有意识的决定才能使某些东西可执行。
至于首选方法,因为您可以控制服务器,所以可以将 Apache mod_wsgi
与任何 Web 框架一起使用 - Pyramid, Flask, Django,等等; WSGI 应用程序比 CGI 更高效。