0%

Mac下配置CGI环境

Mac下配置CGI环境

最近在学Python CGI编程,环境一直弄不好,Google了一大圈,看了很多博客,后面实践下来,要么就是403,要么就是解析不出来,直接打印出源码,现在在这里记录一下我自己的操作步骤,以便以后在遇到该问题好直接上手

1.在httpd.conf文件中开启CGI配置和虚拟主机

  1. sudo vim /etc/apache2/httpd.config
  2. 取消注释 Include /private/etc/apache2/extra/httpd-vhosts.conf
  3. 取消注释 AddHandler cgi-script .cgi
  4. 取消注释 AddType text/html .shtml
  5. 取消注释 AddOutputFilter INCLUDES .shtml
  6. AddHandler cgi-script .cgi后面添加要支持的cgi文件,比如.pl .py
  7. 重启服务器 sudo apachectl restart

2.在httpd-vhosts.conf文件中设置主机目录

  1. sudo vim /etc/apache2/extra/httpd-vhosts.conf
  2. 注释掉前面的两个实例主机配置
  3. 添加下面的配置
     <VirtualHost *:80>
         DocumentRoot "/Library/WebServer/Documents" #原始的目录
         ServerName localhost
         ErrorLog "/private/var/log/apache2/localhost-error_log"
         CustomLog "/private/var/log/apache2/localhost-access_log" common
     </VirtualHost> 
     <VirtualHost *:80>
         DocumentRoot "/Users/mac/Desktop/PyCharm/cgi/" #自定义目录
         ServerName studycgi  #自定义本地域名
         ErrorLog "/private/var/log/apache2/sites-error_log"
         CustomLog "/private/var/log/apache2/sites-access_log" common
         <Directory "/Users/mac/Desktop/PyCharm/cgi/">
             Options Indexes FollowSymLinks MultiViews ExecCGI #这里必须要有ExecCGI否则会403
             AllowOverride All
             Require all granted
         </Directory> #这里添加权限设置,否则会出现403
     </VirtualHost>

3.设置username.conf

  1. etc/apache2/users/username.conf #这里的username是你的电脑名称
  2. 添加如下配置
         <Directory "/Users/mac/Desktop/PyCharm/cgi/">
             Options Indexes FollowSymLinks MultiViews ExecCGI #这里必须要有ExecCGI否则会403
             DirectoryIndex index.html index.cgi
             AllowOverride None
             Require all granted
             Order allow,deny
             Allow from all
         </Directory>
  3. 重启服务器 sudo apachectl restart

4.设置hosts文件

  1. sudo vim /etc/hosts
  2. 增加 127.0.0.1 studycgi
  3. 在浏览器打开 http://studycgi 即可访问自定义的网站目录

5. 开始就可以撸代码

…………