我试图在Mac上设置Python在我的本地apache服务器上运行。
在 httpd.conf
,我已经
<VirtualHost *:80>
DocumentRoot "/Users/ptamzz/Sites/python/sandbox.ptamzz.com"
<Directory "/Users/pritams/Sites/python/sandbox.ptamzz.com">
Options Indexes FollowSymLinks MultiViews ExecCGI
AddHandler cgi-script .py
Require all granted
</Directory>
DirectoryIndex index.py
ServerName sandbox.ptamzz.com
ErrorLog "/var/log/apache2/error-log"
CustomLog "/var/log/apache2/access-log" common
</VirtualHost>
在我的文档根目录下,我的 index.py
归档
#!/usr/bin/python
print "Content-type: text/html"
print "<html><body>Pritam's Page</body></html>"
但当我在浏览器上加载页面时,python代码却原封不动地返回。
我到底缺少什么?
在Apache中执行python(cgi)。
系统 : OSX yosmite 10.10.3 , 默认为apache。
在http配置中未加评论
LoadModule cgi_module libexec/apache2/mod_cgi.so
虚拟主机条目
<VirtualHost *:80>
# Hook virtual host domain name into physical location.
ServerName python.localhost
DocumentRoot "/Users/sj/Sites/python"
# Log errors to a custom log file.
ErrorLog "/private/var/log/apache2/pythonlocal.log"
# Add python file extensions as CGI script handlers.
AddHandler cgi-script .py
# Be sure to add ** ExecCGI ** as an option in the document
# directory so Apache has permissions to run CGI scripts.
<Directory "/Users/sj/Sites/python">
Options Indexes MultiViews FollowSymLinks ExecCGI
AddHandler cgi-script .cgi
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
index.py文件
#!/usr/bin/env python
print "Content-type: text/html"
print
print "<html><body>Test Page</body></html>"
文件是可以执行的,使用
chmod a+x index.py
重启了apache并输出
对于Apache (2.4)以后的版本,Sojan V Jose的答案大部分还是适用的,只是我得到了一个授权失败,可以通过替换来解决。
Order allow,deny
Allow from all
替换为:
Require all granted