环境: CentOS 6.9, httpd 2.2.15
安装httpd方式
yum install httpd
配置/etc/httpd/conf/httpd.conf
加载CGI模块
LoadModule cgid_module modules/mod_cgi.so设置CGI脚本映射路径
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"设置CGI访问权限
<Directory "/var/www/cgi-bin">
AllowOverride None
Options +ExecCGI
Order allow,deny
Allow from all
</Directory>
- 设置CGI支持的脚本
AddHandler cgi-script .cgi .py
重启httpd
service httpd restart
编写Python脚本
- 添加Python脚本放到/var/www/cgi-bin/目录下
#!/usr/bin/python
# -*- coding: UTF-8 -*-
print "Content-type:text/html"
print
print '<html>'
print '<head>'
print '<title>Test</title>'
print '</head>'
print '<body>'
print '<h2>Hello CGI</h2>'
print '</body>'
print '</html>'
- 设置Python脚本755权限
必须给/var/www/cgi-bin/目录下的Python脚本添加执行权限,否则Python是无法被CGI正常调用的。