在 Apache 中使用 Anaconda Python 时,脚本输出在 headers 之前结束

End of script output before headers when using Anaconda Python in Apache

我正在尝试使用 cgi 在 Ubuntu 上 运行 Ananaconda Python 2.7 脚本,但我在 var/log/apache2/error.log 中收到错误 500 和以下错误消息。

 No such file or directory: AH01241: exec of '/var/www/html/app/getcapabilities.py' failed
 End of script output before headers: getcapabilities.py

下面是我的 Apache apache2.conf 的样子。

<Directory /var/www/html/*>
  Options +ExecCGI
  AddHandler cgi-script .py
</Directory>

命令which python给了我路径:/home/myuser/anaconda2/bin/python

下面是我的 Python 文件的样子。 Python 脚本 运行 在 Windows XAMPP Apache 中很好。

#!/home/myuser/anaconda2/bin/python
print ("Content-Type: text/xml\n\n")
print ('\n')
print ('\n')
print ('hello')

我也关注了 this step,但对我不起作用。

我如何解决这个问题并能够在网络浏览器上 运行 python 脚本?

问题是由于 Windows 和 Linux 中的行尾不同。该文件以 CRLF 结尾,它应该更改为 LF 行结尾。我的文本编辑器 Pycharm 可以在右下角或文件菜单中更改它,如下所示。所以将它更改为 Unix 和 OS X 行分隔符解决了这个问题。

有关 Pycharm 的更多信息,请查看此 link

如果您使用 git,这会在您提交存储库时自动修复。

编辑: 我最近发现需要使 python 文件可执行。即使我修复了行尾,我也遇到了问题,通过选中文件权限中的复选框使文件可执行解决了这个问题。