(ve):Django on Ubuntu
- This page was last modified on April 7, 2011, at 14:12.
From (mt) Community Wiki
Contents |
Results
You will install Django on your Ubuntu (ve) Server, and set up one Django test application.
Requirements
Install LAMP on your server and set up at least one domain as a virtual host. See this article for instructions:
Option 1: mod_wsgi instructions
mod_wsgi is the new, more efficient way to run Django applications. Django will soon support only mod_wsgi installations, so this is the recommended installation method.
- Log into your server with a root or sudo user via SSH.
-
Install Django, Python setup tools, mod_wsgi, and Python MySQL support:
apt-get install python-django python-setuptools libapache2-mod-wsgi python-mysqldb
-
Create your Django project directory. For this example, the project will be called testapp, and will be under a new django directory in your example.com domain.
You should follow the instructions here to configure this directory correctly. When you get to the third step in that walkthrough, you can go ahead and generate your Django application then, or skip that step and do it here instead.
Also, you can make the changes to your /etc/apache2/sites-available/example.com file now and then add the additional ones later, or just review the "complete VirtualHost" example at the end of the instructions. -
Move into your Django directory.
cd /var/www/example.com/django/ -
Create a Django application in your project directory.
/usr/lib/python-django/bin/django-admin.py startproject testapp
-
Create your WSGI settings file for your new application. This example creates the file with the name django.wsgi.
vi /var/www/example.com/django/testapp/django.wsgi -
Add the following lines to the file, and save:
django.wsgi
import os, sys sys.path.append('var/www/example.com/django') sys.path.append('/var/www/example.com/django/testapp') os.environ['DJANGO_SETTINGS_MODULE'] = 'testapp.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()- There should be two sys.path.append lines - one to your overall Django directory, and one to your particular application folder.
- Be sure to replace the bold part of testapp.settings with the name for your project.
vi tip: Press "i" to enter "insert mode" so you can type and copy/paste. Press "Esc" to exit "insert mode" when you are done modifying the file. Type ":wq" to save and quit.
-
Open your /etc/apache2/sites-available/example.com file for editing, add the following line between the Virtualhost directives for your domain, and then save your changes:
vi /etc/apache2/sites-available/example.comexample.com<VirtualHost 12.34.56.78:80> ... WSGIScriptAlias /testapp /var/www/example.com/django/testapp/django.wsgi ... </VirtualHost>Notes on this file:
- The first path shown here designates the URL for your app. In this case, it will be http://example.com/testapp/. If you want the app to be your home page, put / instead of /testapp. The second path is the path to the WSGI file we created in the previous step.
If the directory you've designated for your application URL also contains files that do not need to be processed with mod_wsgi - for example, image files, CSS files, your favicon.ico and robots.txt - you should add some additional directives to your /etc/apache2/sites-available/example.com file to make your site more efficient. All of these lines should come right before the WSGIScriptAlias line:
example.com<VirtualHost 12.34.56.78:80> ... Alias /robots.txt /var/www/example.com/html/robots.txt Alias /favicon.ico /var/www/example.com/html/favicon.ico AliasMatch /([^/]*\.css) /var/www/example.com/html/styles/$1 Alias /media/ /var/www/example.com/html/media/ WSGIScriptAlias / /var/www/example.com/django/testapp/django.wsgi ... </VirtualHost>Pick and choose from the example paths as needed, and adjust them to fit your actual server configuration. The first path after Alias is the path to be added to your URL. The second path is the full path to the file or folder. Remember that you only need to make special Alias directives for files and folders that are inside your Django project (the URL path shown in the WSGIScriptAlias line). This will be more necessary if you've designated / as your project folder, and may not be necessary if your application is in a subfolder.
-
Reload your Apache configuration:
/etc/init.d/apache2 reload
- For security, you may now want to change the owner of all your project files to your domain user, as is shown here (replace the example path with the path to your Django directory).
- See Djangoproject.com for further details on Django and mod_wsgi.
That's it! You should now be able to view the Django start page at http://example.com/testapp/, replacing example.com with your own domain name, and testapp with the name of your project.
Complete VirtualHost
<VirtualHost 12.34.56.78:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/html/
ErrorLog /var/www/example.com/logs/error.log
CustomLog /var/www/example.com/logs/access.log combined
WSGIScriptAlias /testapp /var/www/example.com/django/testapp/django.wsgi
<Directory "/var/www/example.com/html">
php_admin_value open_basedir "/var/www/example.com/html/:/tmp/:/var/www/example.com/django/testapp/"
php_admin_value include_path "/var/www/example.com/html/:/tmp/:/var/www/example.com/django/testapp/"
</Directory>
</VirtualHost>
Here's a line-by-line explanation:
- <VirtualHost 12.34.56.78:80> - Begin instructions for this domain. Replace the IP with your IP. The :80 part tells the server to listen on Port 80 for web traffic.
- ServerAdmin webmaster@example.com - Admin email address for this domain - set as desired.
- ServerName example.com - Sets the domain name. Replace with your domain.
- ServerAlias www.example.com - Sets all domain aliases. List as many as you want.
- DocumentRoot /var/www/example.com/html/ - Designates where your web page files will be kept. This can be any path on your server, but the example is standard.
- ErrorLog /var/www/example.com/logs/error.log - Path for error logs. Important for troubleshooting. Can be any path on your server.
- CustomLog /var/www/example.com/logs/access.log combined - Path for access logs. Useful for viewing visitor statistics. Can be any path on your server.
- WSGIScriptAlias /testapp /var/www/example.com/django/testapp/django.wsgi - Makes an alias from the URL http://example.com/testapp/ to the location of your Django WSGI file. Use just / instead of /testapp if you want your app to be your home page.
- php_admin_value open_basedir "/var/www/example.com/html/:/tmp/:/var/www/example.com/django/testapp/" - All directories where you want Apache to be able to serve files. Your document root and /tmp/ are standard. You should also add the path to your Django folder.
- php_admin_value include_path ".:/var/www/example.com/django/testapp/" - All directories where includes (etc.) are allowed. Should include your document root, /tmp, and your application directory. This line may not be necessary, depending on your application.
- </Directory> - End instructions for this directory.
- </Virtualhost> - End instructions for this domain.
Option 2: mod_python instructions
- Log into your server with a root or sudo user via SSH.
-
Install mod_python, Django, and Python support for MySQL.
apt-get install libapache2-mod-python python-django python-mysqldb
- Create your Django project directory. For this example, the project will be called testapp, and will be under a new django directory in your example.com domain.
You should follow the instructions here to configure this directory correctly. When you get to the third step in that walkthrough, you can go ahead and generate your Django application then, or skip that step and do it here instead. Also, you can make the changes to your /etc/apache2/sites-available/example.com file now and then add the additional ones later, or just review the "complete VirtualHost" example at the end of the instructions.
-
Move into your Django directory.
cd /var/www/example.com/django -
Create a Django application in your project directory.
/usr/lib/python-django/bin/django-admin.py startproject testapp
-
Open your /etc/apache2/sites-available/example.com file for editing, add the following lines between the Virtualhost directives for your domain, and then save your changes:
vi /etc/apache2/sites-available/example.comexample.com<VirtualHost 12.34.56.78:80> ... <Location "/testapp"> SetHandler python-program PythonHandler django.core.handlers.modpython PythonDebug On PythonPath "['/var/www/example.com/django'] + sys.path" SetEnv DJANGO_SETTINGS_MODULE examples.settings </Location> ... </VirtualHost>Notes on this file:
- Remember to update the SetEnv line with the name of your own application. Replace the bold part in testapp.settings.
- Your PythonPath should be the full path from the root level of the server to the directory above your project directory.
- The path in the Location line is the path to be appended to your URL. In this case, the application will be hosted on http://example.com/testapp/. If you want the app to be your home page, read the second tip below.
- vi tip: Press "i" to enter "insert mode" so you can type and copy/paste. Press "Esc" to exit "insert mode" when you are done modifying the file. Type ":wq" to save and quit.
- You can make the app your home page by changing the Location to just /, and adding your application directory to the PythonPath, as shown below. If your document root directory also contains files that will not be processed with mod_python - for example, image files, CSS files, your favicon.ico and robots.txt - you should add some additional directives to your /etc/apache2/sites-available/example.com so your files are parsed correctly. These additional Location directives should be added after the first one for Django.
example.com<VirtualHost 12.34.56.78:80> ... <Location "/"> SetHandler python-program PythonHandler django.core.handlers.modpython PythonDebug On PythonPath "['/var/www/example.com/django','/var/www/example.com/django/testapp'] + sys.path" SetEnv DJANGO_SETTINGS_MODULE testapp.settings </Location> <Location "/media"> SetHandler None </Location> ... </VirtualHost>This example assumes that you have static content in http://example.dom/media/ - adjust for your setup as necessary. Remember that you only need to make special Location directives for files and folders that are inside your Django project (the URL path shown in the first Location line). This will be more necessary if you've designated / as your project folder, and may not be necessary if your application is in a subfolder.
-
Reload your Apache configuration.
/etc/init.d/apache2 reload
-
Update your system settings for this project:
python manage.py runserver
- For security, you may now want to change the owner of all your project files to your domain user, as is shown here (replace the example path with the path to your Django directory).
That's it! You should now be able to view the Django start page at http://example.com/testapp/, replacing example.com with your own domain name, and testapp with the name of your project.
Complete VirtualHost
<VirtualHost 12.34.56.78:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/html/
ErrorLog /var/www/example.com/logs/error.log
CustomLog /var/www/example.com/logs/access.log combined
<Location "/testapp">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE testapp.settings
PythonDebug On
PythonPath "['/var/www/example.com/django'] + sys.path"
</Location>
<Directory "/var/www/example.com/html">
php_admin_value open_basedir "/var/www/example.com/html/:/tmp/:/var/www/example.com/django/testapp/"
php_admin_value include_path "/var/www/example.com/html/:/tmp/:/var/www/example.com/django/testapp/"
</Directory>
</VirtualHost>
Here's a line-by-line explanation:
- <VirtualHost 12.34.56.78:80> - Begin instructions for this domain. Replace the IP with your IP. The :80 part tells the server to listen on Port 80 for web traffic.
- ServerAdmin webmaster@example.com - Admin email address for this domain - set as desired.
- ServerName example.com - Sets the domain name. Replace with your domain.
- ServerAlias www.example.com - Sets all domain aliases. List as many as you want.
- DocumentRoot /var/www/example.com/html/ - Designates where your web page files will be kept. This can be any path on your server, but the example is standard.
- ErrorLog /var/www/example.com/logs/error.log - Path for error logs. Important for troubleshooting. Can be any path on your server.
- CustomLog /var/www/example.com/logs/access.log combined - Path for access logs. Useful for viewing visitor statistics. Can be any path on your server.
- <Location "/testapp"> - Begin URL path. The path /testapp will be added to your URL for this application. To make your application be your home page, put just / here.
- SetHandler python-program - Designate this as a Python program.
- PythonHandler django.core.handlers.modpython - Use the Django Python handler.
- SetEnv DJANGO_SETTINGS_MODULE testapp.settings - Designate the correct settings file. Replace testapp with the name of your application.
- PythonDebug On - Useful for troubleshooting. You can turn this off later.
- PythonPath "['/var/www/example.com/django'] + sys.path" - The path in which your project is located. Should include the full path up to the folder above your project folder itself.
- </Location> - End URL path directives.
- <Directory "/var/www/example.com/html"> - Begin instructions for a particular directory. For this purpose, the path should match your DocumentRoot.
- php_admin_value open_basedir "/var/www/example.com/html/:/tmp/:/var/www/example.com/django/testapp/" - All directories where you want Apache to be able to serve files. Your document root and /tmp/ are standard. You should also add the path to your Django folder.
- php_admin_value include_path ".:/var/www/example.com/django/testapp/" - All directories where includes (etc.) are allowed. Should include your document root, /tmp, and your application directory. This line may not be necessary, depending on your application.
- </Directory> - End instructions for this directory.
- </Virtualhost> - End instructions for this domain.