cgi配置文件路径:/etc/apache2/conf-available/serve-cgi-bin.conf
<IfModule mod_alias.c>
<IfModule mod_cgi.c>
Define ENABLE_USR_LIB_CGI_BIN
</IfModule>
<IfModule mod_cgid.c>
Define ENABLE_USR_LIB_CGI_BIN
</IfModule>
<IfDefine ENABLE_USR_LIB_CGI_BIN>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Require all granted
</Directory>
</IfDefine>
</IfModule>
改为自己的路径:
<IfModule mod_alias.c>
<IfModule mod_cgi.c>
Define ENABLE_USR_LIB_CGI_BIN
</IfModule>
<IfModule mod_cgid.c>
Define ENABLE_USR_LIB_CGI_BIN
</IfModule>
<IfDefine ENABLE_USR_LIB_CGI_BIN>
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/var/www/cgi-bin">
AllowOverride None
AddHandler cgi-script .cgi .py
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Require all granted
</Directory>
</IfDefine>
</IfModule>
这里:ScriptAlias /cgi-bin/ /var/www/cgi-bin/ 和 < Directory “/var/www/cgi-bin”> 改为存放.py和.cgi文件的路径
这里:AddHandler cgi-script .cgi .py是说,可以在cgi中使用.cgi和.py的脚本文件
重启apache:
service apache2 restart
在网页中输入:你的ip地址/cgi-bin/你的.py即可看到页面
如:cgi-bin/he.py:
#!/usr/bin/env python3
# -*- coding:utf-8 -*-"
print ("Content-type:text/html")
print()
print ('<html>')
print ('<head>')
print ('<title>hello</title>')
print ('</head>')
print ('<body>')
print ('<h2>Hello world! This is my first CGI program</h2>')
print ('<h3>"helo world!</h3>')
print ('</body>')
print ('</html>')
页面: