1、更新服务
sudo apt-get update
sudo apt-get upgrade
安装Apache和mod_wsgi
sudo apt-get install apache2 libapache2-mod-wsgi
安装工具setuptools 和pip
sudo apt-get install python-setuptools
sudo apt-get install python-pip
安装Django
sudo pip install django
创建站点目录
sudo mkdir /srv/www
2、添加host进行测试
sudo nano /etc/hosts
127.0.0.1 djangoserver
127.0.0.1 wsgi.djangoserver
127.0.0.1 hello.djangoserver
3、测试WSGI是否正常工作
sudo mkdir /srv/www/wsgi
sudo nano /srv/www/wsgi/app.wsgi
添加以下内容:
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
创建一个新的apache站点
sudo nano /etc/apache2/sites-available/wsgi
添加以下内容:
<VirtualHost *:80>
ServerName wsgi.djangoserver
DocumentRoot /srv/www/wsgi
<Directory /srv/www/wsgi>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /srv/www/wsgi/app.wsgi
</VirtualHost>
激活站点
sudo a2ensite wsgi
sudo /etc/init.d/apache2 reload
http://wsgi.djangoserver
效果如下:
4、测试Django是否正常工作
创建Django工程
cd /srv/www
sudo django-admin.py startproject hello
配置Django的wsgi文件
sudo mkdir /srv/www/hello/apache
sudo nano /srv/www/hello/apache/django.wsgi
添加以下内容:
import os
import sys
path = '/srv/www'
if path not in sys.path:
sys.path.insert(0, '/srv/www')
os.environ['DJANGO_SETTINGS_MODULE'] = 'hello.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
创建一个新的apache站点
sudo nano /etc/apache2/sites-available/hello
添加以下内容:
<VirtualHost *:80>
ServerName hello.djangoserver
DocumentRoot /srv/www/hello
<Directory /srv/www/hello>
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess hello.djangoserver processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup hello.djangoserver
WSGIScriptAlias / /srv/www/hello/apache/django.wsgi
</VirtualHost>
激活站点
sudo a2ensite hello
sudo /etc/init.d/apache2 reload
http://hello.djangoserver
效果如下:
原文地址:
http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/