ubuntu14.10环境,apache2配置python的cgi

1,安装apache2,命令如下:

sudo apt-get install apache2

2,进入/var/www/目录,创建test.com目录,并修改拥有者权限,再在test.com目录下创建cgi-bin目录,命令分别如下:

sudo mkdir example.com
sudo chown -R $USER example.com
mkdir example.com/cgi-bin

3,创建hello.py文件,编辑内容如下(内容参考链接:http://www.yiibai.com/python/python_cgi_programming.html):

vim hello.py
chmod 755 hello.py
#!/usr/bin/python print "Content-type:text/html " print '<html>' print '<head>' print '<title>Hello Word - First CGI Program</title>' print '</head>' print '<body>' print '<h2>Hello Word! This is my first CGI program</h2>' print '</body>' print '</html>'

4,进入/etc/apache2/sites-available目录下,复制000-default.conf文件到example.com.conf文件,并修改并添加cgi配置:

修改:
DocumentRoot /var/www/example.com
添加: ScriptAlias
/cgi-bin/ /var/www/example.com/cgi-bin/ <Directory "/var/www/example.com/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all AddHandler cgi-script .py AddHandler default-handler .html .htm </Directory>

5,禁用默认cgi配置,在目录/etc/apache2/conf-available/serve-cgi-bin.conf;禁用默认配置000-default.conf;启用example.com.conf配置;并重启apache2

sudo a2disconf serve-cgi-bin.conf
sudo a2dissite 000-default.conf sudo a2ensite example.com.conf sudo service apache2 restart

6,配置sudo编辑/etc/hosts文件:

127.0.0.1    www.example.com

7,访问浏览器www.example.com/cgi-bin/hello.py,应该能看到结果



原文地址:https://www.cnblogs.com/doublehappyi/p/4753225.html