ubuntu apache python

安装 LAMP

1
sudo tasksel install lamp-server

安装 mod_python

1
sudo apt-get install libapache2-mod-python

为了开发和调试的方便,我们把apache默认目录改到我们的用户目录下
以下“你的名字”指的是你当前使用的系统用户名
如果你不清楚的话,可以执行命令 whoami 查看你当前的用户名

执行

1
cd

回到你的用户目录

创建你的工作目录

1
mkdir workhome


修改apache配置文件

1
sudo gedit /etc/apache2/sites-available/default

把所有 /var/www 改成 /home/你的名字/workhome"
在第10行左右,有 Options Indexes FollowSymLinks MultiViews
在后面加上 +ExecCGI
变成 Options Indexes FollowSymLinks MultiViews +ExecCGI

修改apache用户及用户组

1
sudo gedit /etc/apache2/envvars

www-data改成你的名字
两个都要改

建立mod_python配置文件

1
sudo gedit /etc/apache2/mods-available/python.conf

加入

1
2
3
4
<IfModule mod_python.c>
  AddType text/html .py
  AddHandler cgi-script .cgi .py
</IfModule>

保存退出

重新启用mod_python

1
sudo a2enmod python

重启apache

1
sudo /etc/init.d/apache2 restart

测试python

1
gedit workhome/test.py

内容:

1
2
3
#!/usr/bin/python
print "Content-type: text/html\n\n"
print "Hello, Python."

浏览器打开 http://localhost/test.py
应该不会提示你下载test.py了,正常情况会输出 Hello, Python.

python works

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
This entry was posted in Technologies and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>