ubuntu10.10+django+apache2+(mod-python 或 wsgi)

这几天一直在折腾django在apache上的部署,网上的资料鱼龙混杂,特将经验记录下来,希望能帮助更多的人。

mod-python:

1.安装apache,mod-python:

sudo apt-get install apache2 libapache2-mod-python

2.在/etc/apache2/available/目录下建立配置文件:mytest

<VirtualHost *:80>
	ServerName localhost
	DocumentRoot /home/sarlmolapple/workspace/django-site/mytest
	<Directory "/home/sarlmolapple/workspace/django-site">
		Allow from all
		SetHandler python-program
    		PythonHandler django.core.handlers.modpython
    		SetEnv DJANGO_SETTINGS_MODULE mytest.settings
    		PythonDebug On
    		PythonPath "['/home/sarlmolapple/workspace/django-site', '/home/sarlmolapple/workspace/django-site/mytest', '/home/sarlmolapple/workspace/django-site/mytest/mysite'] + sys.path"
	</Directory>
</VirtualHost>

注:django项目为mytest, 路径为:/home/sarlmolapple/workspace/django-site/mytest,

‘/home/sarlmolapple/workspace/django-site/mytest/mysite’为mytest中的app所在路径

3.运行命令:

sudo a2ensite mytest
sudo /etc/init.d/apache2 reload

然后你在浏览器中输入http://localhost就可以看到你想要看到的页面了

WSGI:

1.安装apache,mod-python:

sudo apt-get install apache2 libapache2-wsgi-python

2.在/etc/apache2/available/目录下建立配置文件:mytest

<VirtualHost *:80>
    ServerName wsgi.mytest
    DocumentRoot /home/sarlmolapple/workspace/django-site/mytest
    <Directory /home/sarlmolapple/workspace/django-site/mytest>
        Order allow,deny
        Allow from all
	PythonPath "['/home/sarlmolapple/workspace/django-site', '/home/sarlmolapple/workspace/django-site/mytest', '/home/sarlmolapple/workspace/django-site/mytest/mysite'] + sys.path"
    </Directory>
    WSGIDaemonProcess wsgi.mytest processes=2 threads=15 display-name=%{GROUP}
    WSGIProcessGroup wsgi.mytest
    WSGIScriptAlias / /home/sarlmolapple/workspace/django-site/mytest/apache/django.wsgi
</VirtualHost>

3.在mytest目录下建立apache/django.wsgi

import os
import sys

sys.path.append('/home/sarlmolapple/workspace/django-site')
sys.path.append('/home/sarlmolapple/workspace/django-site/mytest')
sys.path.append('/home/sarlmolapple/workspace/django-site/mytest/mysite')

os.environ['DJANGO_SETTINGS_MODULE'] = 'mytest.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

注: mytest路径为:/home/sarlmolapple/workspace/django-site/mytest,‘/home/sarlmolapple/workspace/django-site/mytest/mysite’为mytest中的app所在路径

4.运行命令:

sudo a2ensite mytest
sudo /etc/init.d/apache2 reload

同理,你在浏览器中输入http://localhost就可以看到你想要看到的页面了。

Posted by 陈着 Mar 12, 2011 04:22:44 PM