Mac下配置CGI环境
最近在学Python CGI编程,环境一直弄不好,Google了一大圈,看了很多博客,后面实践下来,要么就是403,要么就是解析不出来,直接打印出源码,现在在这里记录一下我自己的操作步骤,以便以后在遇到该问题好直接上手
1.在httpd.conf文件中开启CGI配置和虚拟主机
sudo vim /etc/apache2/httpd.config
- 取消注释
Include /private/etc/apache2/extra/httpd-vhosts.conf
- 取消注释
AddHandler cgi-script .cgi
- 取消注释
AddType text/html .shtml
- 取消注释
AddOutputFilter INCLUDES .shtml
- 在
AddHandler cgi-script .cgi
后面添加要支持的cgi文件,比如.pl .py
- 重启服务器
sudo apachectl restart
2.在httpd-vhosts.conf文件中设置主机目录
sudo vim /etc/apache2/extra/httpd-vhosts.conf
- 注释掉前面的两个实例主机配置
- 添加下面的配置
<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
etc/apache2/users/username.conf
#这里的username是你的电脑名称- 添加如下配置
<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>
- 重启服务器
sudo apachectl restart
4.设置hosts文件
sudo vim /etc/hosts
- 增加
127.0.0.1 studycgi
- 在浏览器打开
http://studycgi
即可访问自定义的网站目录
5. 开始就可以撸代码
…………