Mac上搭建Web服务器--Apache
Apache服务器默认的web根目录在:/Library/WebServer/Documents
Apache的配置文件在:/etc/apache2
停止 Apache:sudo apachectl stop
重启 Apache:sudo apachectl restart
- 输入命令启动Apache:sudo apachectl start
- 浏览器输入网址:http://localhost
- 将一个html文件拷贝到
/Library/WebServer/Documents
文件夹下 - 浏览器中访问,测试文件名为test.html的文件,格式:本机IP/静态网页,http://192.168.1.112/test.html
mac上如何配置CGI运行程序
CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户端HTML页面的接口。
Mac上是自带CGI运行程序的,但是直接是不可以用,需要进行相应的修改才可以正常运行。
- 打开httpd.conf文件,直接打开是编辑不了的,需要用root权限,具体操作为:sudo vim /etc/apache2/httpd.conf打开文件。找到LoadModule cgi_module libexec/apache2/mod_cgi.so并取消注释。
- 在AddHandler cgi-script .cgi后面增加.py,表示可以访问.py结尾的python脚本文件,也可以增加.php,表示可以访问.php结尾的脚本文件。
- 配置完成后在/Library/WebServer/CGI-Executables下创建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 World - 这是一个测试程序 </title>') print ('</head>') print ('<body>') print ('<h2> Hello World! 我是用来测试cgi的 </h2>') print ('</body>') print ('</html>')
- 文件创建好后修改权限 ,同样需要root权限用户
sudo chomd 755 hello.py
- 重启appachectl服务
sudo apachectl restart
- 程序在浏览器访问 http://localhost/cgi-bin/hello.py 显示结果如下
Hello World! 我是用来测试cgi的
错误检测:
#!/usr/bin/python没有放在文件头部的时候,执行python hello.py是可以正常得到输出的,但是用./hello.py执行程序就会报错。当楼主将#!/usr/bin/python放在文件头部时,./hello.py就可以正常执行了!