Назад | Перейти на главную страницу

Как apache выбирает исполняемый файл для cgi-скрипта?

Я проводил несколько экспериментов и пытался понять результаты.

В моем apache conf у меня есть:

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 у меня есть три файла:

hello.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>")

hello.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:

[24 мая, 06:50: 00.681713 2018] [cgi: error] [pid 8481] [client 127.0.0.1:35306] неверный заголовок из сценария 'hello.p1': неверный заголовок: ()

Я предполагаю, что плохой заголовок вызван тем, что сценарий фактически проигнорировал shebang и работал под системным питоном по умолчанию.

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 *****?