Mac apache php 和CGI 的配置

今天看了很多关于PHP和CGI 在Mac上apache 的配置的教程,按照起步骤操作了还是不行,最后通过尝试配置成功了,下面我将为大家总结一些其配置流程,希望能帮到大家!!!!

# 1. Mac上Php的配置
# (1)查看Apache的版本信息 在终端上输入apachectl -v

Server version: Apache/2.4.29 (Unix)

Server built:   Jan 17 2018 18:20:31

#(2)查找apache 配置文件
#在目录/etc/apache2/下,打开Finder,选择"前往"-"前往文件夹",输入"/etc/apache2/",

#找到其中的"httpd.conf"文件,先拷贝一份出来到桌面

#(3)以文本编辑的形式打开 查找 #LoadModule php7_module libexec/apache2/libphp7.so 将其前面的‘#’去掉

# 查找到 
“#
 #ServerName www.example.com:80

在其下面添加一行 ServerName localhost:80


#(4)自定义目录配置
在"httpd.conf"文件找到
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
并将其改为 (/Users/admin/Sites)自己定义的路径可以根据自己的需要自行修改
DocumentRoot "/Users/admin/Sites"
<Directory "/Users/admin/Sites">

找到
#
Options FollowSymLinks Multiviews
MultiviewsMatch Any
改为
#
Options Indexes FollowSymLinks Multiviews

MultiviewsMatch Any

修改完毕后 将httpd.conf 替换/etc/apache2/路径下的httpd.conf文件


#(5)创建测试文件 验证 配置php是否成功
# 在终端上 输入 cp /Library/WebServer/Documents/index.html.en /Users/admin/Sites/index.php 
# 用文本编辑模式打开index.php,然后在后面添加<?php phpinfo(); ?>
# 启动 apache  在终端上输入 sudo apachectl -k restart

# 打开浏览器  输入 localhost  




# 2.配置CGI 
在上述操作中拷贝在桌面的httpd.conf 文件上继续修改  
找到#LoadModule cgi_module libexec/apache2/mod_cgi.so 去掉前面的‘#’


查找<Directory "/Library/WebServer/CGI-Executables">
改为<Directory "/Users/admin/Sites/Cgi">  注意:自定义的路径(/Users/admin/Sites/Cgi)必须在前面修改的根目录下(DocumentRoot "/Users/admin/Sites")
<Directory "/Users/admin/Sites/Cgi">
    AllowOverride None
    Options ExecCGI
    Order deny,allow
    Allow from all
</Directory>


查找 AddHandler cgi-script .cgi 
将其改为
AddHandler cgi-script .cgi .py .pl .sh


查找 ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"
将其改为
ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Users/admin/Sites/Cgi/$1"
最后保存替换替换/etc/apache2/路径下的httpd.conf文件
重新启动 apache  在终端上输入 sudo apachectl -k restart


在自定的工作目录下(我的是/Users/admin/Sites/Cgi)下放入hello.py 文件
文件内容如下
#!/usr/bin/python
# -*- coding: UTF-8 -*-


print "Content-type:text/html"
print
print '<html>'
print '<head>'
print '<meta charset="utf-8">'
print '<title>Hello!</title>'
print '</head>'
print '<body>'
print '<h2>Hello test Word! </h2>'
print '</body>'
print '</html>'


打开浏览器  输入 localhost/cgi-bin/hello.py 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值