我一直在做一些实验,并试图了解其结果。
在我的 Apache 配置中我有:
balter@balterbox:/etc/apache2$ cat sites-enabled/000-default.conf
.
.
.
<Directory /var/www/html>
AllowOverride None
Order allow,deny
allow from all
Options +ExecCGI
AddHandler cgi-script .pl .php .py .p1 .p2 .sh
</Directory>
在我的 /var/www/html 中我有三个文件:
你好.py
balter@balterbox:/var/www/html$ cat hello.py
#!/usr/bin/env python
from __future__ import print_function
print ("Content-Type: text/html")
print()
print("<html><body><h1>Hello from python</h1></body></html>")
你好.p1
balter@balterbox:/var/www/html$ cat hello.p1
#!/usr/bin/env python
print ("Content-Type: text/html")
print()
print("<html><body><h1>Hello from python</h1></body></html>")
你好.p2
balter@balterbox:/var/www/html$ cat hello.p2
#!/home/balter/conda/bin/python
print ("Content-Type: text/html")
print()
print("<html><body><h1>Hello from python</h1></body></html>")
当我这样做时会发生以下情况curl
:
balter@balterbox:/var/www/html$ curl localhost/hello.py
<html><body><h1>Hello from python</h1></body></html>
balter@balterbox:/var/www/html$ curl localhost/hello.p1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator at
webmaster@localhost to inform them of the time this error occurred,
and the actions you performed just before this error.</p>
<p>More information about this error may be available
in the server error log.</p>
<hr>
<address>Apache/2.4.29 (Ubuntu) Server at localhost Port 80</address>
</body></html>
balter@balterbox:/var/www/html$ curl localhost/hello.p2
<html><body><h1>Hello from python</h1></body></html>
apache日志中的错误是:
[2018 年 5 月 24 日星期四 06:50:00.681713] [cgi:error] [pid 8481] [客户端 127.0.0.1:35306] 来自脚本“hello.p1”的格式错误标头:错误标头:()
我假设坏的标题是因为脚本实际上忽略了shebang并在默认系统python下运行。
balter@balterbox:/var/log/apache2$ /usr/bin/python -V
Python 2.7.15rc1
balter@balterbox:/var/log/apache2$ /usr/bin/env python -V
Python 3.6.4 :: Anaconda, Inc.
balter@balterbox:/var/log/apache2$ which python
/home/balter/conda/bin/python
有没有一种简单的方法可以让 apache 直接使用/usr/bin/env *****
?