首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Python:CGI本地配置方法 Mac Apache

Python:CGI本地配置方法 Mac Apache

作者头像
菜菜不吃蔡
发布于 2018-08-03 16:39:38
发布于 2018-08-03 16:39:38
2.1K1
举报
文章被收录于专栏:编程语言编程语言

CGI本地配置 Mac,记录一下,网上的看了好多,不怎么好理解,通俗易懂的走个流程吧。

Mac环境下 Apache是支持CGI的,本人理解的意思就是说:平时我们打开Python文件,在浏览器上显示的是py文件内容,通过配置,就可以让py文件在Apache上运行,然后我们访问的时候就是程序的反馈信息。

开始配置流程:

1.找到要修改的文件(在find中,直接command+shift+G,然后粘贴路径,查找,就找到要修改的文件了)

代码语言:javascript
复制
/etc/apache2/httpd.conf   

2.修改httpd.conf,我是直接用sublime text修改的,直接将文件拖到sublime中,如下图所示:

(1)command+F 搜索mod_cgi.so,然后将前面的#删除

代码语言:javascript
复制
LoadModule cgi_module libexec/apache2/mod_cgi.so

(2)command+F 搜索AddHandler,然后将这一行,用下面这行替换

代码语言:javascript
复制
AddHandler cgi-script .cgi .pl .py .sh

(3)command+F 搜索ScriptAlias,然后添加下面一行(一般这个默认都是注释的,我们直接添加一句,也可以把他删除了)

代码语言:javascript
复制
ScriptAlias   /Documents/  /Library/WebServer/Documents/ 

添加后就是下面这样了,注意最下面第二行就是我添加的

代码语言:javascript
复制
<IfModule alias_module>
    #
    # Redirect: Allows you to tell clients about documents that used to 
    # exist in your server's namespace, but do not anymore. The client 
    # will make a new request for the document at its new location.
    # Example:
    # Redirect permanent /foo http://www.example.com/bar

    #
    # Alias: Maps web paths into filesystem paths and is used to
    # access content that does not live under the DocumentRoot.
    # Example:
    # Alias /webpath /full/filesystem/path
    #
    # If you include a trailing / on /webpath then the server will
    # require it to be present in the URL.  You will also likely
    # need to provide a <Directory> section to allow access to
    # the filesystem path.

    #
    # ScriptAlias: This controls which directories contain server scripts. 
    # ScriptAliases are essentially the same as Aliases, except that
    # documents in the target directory are treated as applications and
    # run by the server when requested rather than as documents sent to the
    # client.  The same rules about trailing "/" apply to ScriptAlias
    # directives as to Alias.
    #ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"
    ScriptAlias   /Documents/  /Library/WebServer/Documents/  
</IfModule>

(4)在刚才搜索的下面有个CGI的Directory需要修改

代码语言:javascript
复制
#<Directory "/Library/WebServer/CGI-Executables">
  #  AllowOverride None
   # Options None
   # Require all granted
#</Directory>

就是这个字典,我把他直接注释了,然后用下面这个字典替换。

代码语言:javascript
复制
<Directory "/Library/WebServer/Documents">  
    AllowOverride None  
    Options +ExecCGI  
    Order allow,deny  
    Allow from all  
</Directory>  

修改完毕,保存。

4.重启Apache

代码语言:javascript
复制
sudo /usr/sbin/apachectl restart    

5.在下面路径下写一个用来运行的test.py文件

代码语言:javascript
复制
/Library/WebServer/Documents
代码语言:javascript
复制
import cgi


def main():
    print "Content-type: text/html\n"
    print "你的东西配置好了,开始py"


main()

6.(注意)给test.py授权

代码语言:javascript
复制
chmod 777 
/Library/WebServer/Documents/test.py

7.搞定了,这时候在浏览器打开:

http://localhost/test.py

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年06月06日,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档