Step by step instructions for installing Django with Apache and mod_wsgi on Ubuntu 10.04.
PART 1 – Prepare the server
Update the server
> sudo apt-get update > sudo apt-get upgrade
Install Apache and mod_wsgi
> sudo apt-get install apache2 libapache2-mod-wsgi
Install setup tools and pip
> sudo apt-get install python-setuptools > sudo apt-get install python-pip
Install Django
> sudo pip install django
Create a folder for storing our sites
I’ll be placing our sites in the /srv/www directory. The /srv directory should already exist so we just need to create the /www directory
> sudo mkdir /srv/www
PART 2 – Add host entries for testing
We will set up two domains for testing the configuration
- one for testing that WSGI is working, and
- one for testing that Django is working.
My test virtual machine’s IP address is 172.16.52.130 so I’ll set up the following in my local hosts file (not on the server)
> sudo nano /etc/hosts
And add the following
172.16.52.130 djangoserver 172.16.52.130 wsgi.djangoserver 172.16.52.130 hello.djangoserver
PART 3 – Test WSGI is working
Create our wsgi test site content
> sudo mkdir /srv/www/wsgi > sudo nano /srv/www/wsgi/app.wsgi
And add the content
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]
Create a new apache site
> sudo nano /etc/apache2/sites-available/wsgi
And add the content
<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>
And activate the site
> sudo a2ensite wsgi > sudo /etc/init.d/apache2 reload
Then open your web browser and browse to
http://wsgi.djangoserver
You should see a ‘Hello World!’ message
PART 4 – Test Django is working
Create a new Django site
> cd /srv/www > sudo django-admin.py startproject hello
Create a wsgi file for the site
> sudo mkdir /srv/www/hello/apache > sudo nano /srv/www/hello/apache/django.wsgi
And add the content
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 ( )
Create a new apache site
> sudo nano /etc/apache2/sites-available/hello
And add the content
<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>
And activate the site
> sudo a2ensite hello > sudo /etc/init.d/apache2 reload
Then open your web browser and browse to
http://hello.djangoserver
You should see the Django default installation message.
Notes
NOTE 1 – Running in daemon mode
Our test Django site is configured to run in daemon mode – because of these two lines:
WSGIDaemonProcess hello.djangoserver processes=2 threads=15 display-name=%{GROUP} WSGIProcessGroup hello.djangoserver
So if we modify the code then rather than restarting apache we can just touch the wsgi file and the changes will be picked up:
> sudo touch /srv/www/hello/apache/django.wsgi
NOTE 2 – Specify the application module name
It appears to be a good idea to specify the application module when specifying the DJANGO_SETTINGS_MODULE. So rather than writing this:
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
We should write this:
os.environ['DJANGO_SETTINGS_MODULE'] = 'hello.settings'
Useful Commands
Error log file
If you get errors, check the apache log file
> tail /var/log/apache2/error.log
Test using development mode
If your app does not seem to be working using wsgi, then check if it is working via the development server.
> cd /srv/www/hello > python manage.py runserver 0:8080
The in your web browser go to
http://hello.djangoserver:8080
References
An improved WSGI script for use with Django.
http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html
Modwsgi – Integration With Django
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
How do I stop getting ImportError: Could not import settings ‘mofin.settings’ when using django with wsgi?
http://stackoverflow.com/questions/1411417/how-do-i-stop-getting-importerror-could-not-import-settings-mofin-settings-whe
Configuration problems with django and mod_wsgi
http://stackoverflow.com/questions/2587251/configuration-problems-with-django-and-mod-wsgi
相关推荐
标题 "Installing Nginx With PHP5 And MySQL Support On Debian Squeeze" 涉及到的是在Debian Squeeze操作系统上安装Nginx web服务器、PHP5脚本语言支持以及MySQL数据库的过程。这是一个基础的LEMP(Linux、Nginx、...
Apache OpenOffice是一款开源的办公软件套件,它包含多个组件,如文字处理、电子表格、演示文稿、绘图和数据库管理工具,是Microsoft Office的一个有力替代品。在这个"Apache_OpenOffice_4.1.10_Linux_x86-64_...
Installing,_troubleshooting,_and_repairing_wireless_networks_by,_troubleshooting,_and_repairing_wireless_networks_by
在Ubuntu 10.04环境下安装NS2(网络模拟器2.34)是一个涉及多个步骤的过程,这个过程对于那些需要进行网络模拟和性能分析的IT专业人员来说至关重要。NS2是一个开源的网络模拟软件,它允许用户模拟各种网络环境,测试...
Apache 2.4 安全加固。 Table of Contents Overview ...............................................................................................3.4 Set Ownership on Apache Directories and Files (Scored)
CIMCO FOR INSTALLING FOR MASTERCAM
Developing an Apache/mod_wsgi configuration 376 Creating the WSGI script for the marketr project 378 Creating an Apache VirtualHost for the marketr project 379 Activating the new Apache ...
Practical Mod Perl <br> Copyright Preface What You Need to Know Who This Book Is For How This Book Is Organized Reference Sections Filesystem Conventions Apache ...
This book is designed as an Ubuntu 15.04 server reference, covering the Ubuntu servers and their support applications. Server tools are covered as well as the underlying configuration files and system...
第 1 章 在在裸裸机机上上安安装装 1.1. 在裸机上安装集群 1.2. 使用网络自定义在裸机上安装集群 1.3. 在受限网络中的裸机上安装集群
A_shell_script_for_one_key_installing_all_basic_so_one-key-installer-for-developer
Downloading and running Apache Geronimo 12 Geronimo Administration Console 14 Information portlet 15 Java System Info portlet 15 Server Logs portlet 15 Web Server portlet 16 JMS Server portlet ...
Django is an amazingly powerful system for creating modern, dynamic websites. But programming Django hasn’t always been easy–until now....G Installing Python, Django, and Your Tools
[edubt]Installing_and_Removing_Software.pdf [edubt]Installing_Ubuntu.pdf [edubt]Introducing_the_BASH_Shell.pdf [edubt]In_Depth__Calc.pdf [edubt]In_Depth__Evolution.pdf [edubt]In_Depth__Impress.pdf ...
[edubt]Installing_and_Removing_Software.pdf [edubt]Installing_Ubuntu.pdf [edubt]Introducing_the_BASH_Shell.pdf [edubt]In_Depth__Calc.pdf [edubt]In_Depth__Evolution.pdf [edubt]In_Depth__Impress.pdf ...