AH01215: (8) Exec format error: exec of '/var/www/python/hello.py' failed: /var/www/python/hello.py

AH01215: (8) Exec format error: exec of '/var/www/python/hello.py' failed: /var/www/python/hello.py

我正在尝试 运行 python 作为 apache 服务器中的 CGI。 Python 版本为 2.7.12。 这是我的 apache 配置文件

<VirtualHost *:80>
   <Directory /var/www/python>
   Options +ExecCGI
   AddHandler cgi-script .cgi .py
   Order allow,deny
   Allow from all
   </Directory>
   DocumentRoot /var/www/python
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

我的 python 脚本 /var/www/python/hello.py 看起来像这样

print('Content-Type: text/html; charset=utf-8\n')
print("Hello, World!")

当我访问 url 时出现内部服务器错误,我从 error.log 文件中获取了详细信息,它说

[Sun Dec 11 09:53:40.694909 2016] [cgi:error] [pid 6812] [client 127.0.0.1:36282] AH01215: (8)Exec format error: exec of '/var/www/python/hello.py' failed: /var/www/python/hello.py [Sun Dec 11 09:53:40.695312 2016] [cgi:error] [pid 6812] [client 127.0.0.1:36282] End of script output before headers: hello.py

PHP 脚本在服务器上仍然运行良好。 如何解决 py 文件的这个问题?

您的脚本需要 "shebang" 行,例如

#!/usr/bin/env python

作为第一行。另外,确保脚本可以使用 chmod.

执行

你应该使用:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
print('Content-Type: text/html; charset=utf-8\n')
print("ąęśłłłóąś UTF answer")