Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple instances of django on a single domain

I'm looking for a good way to install multiple completely different Django projects on the same server using only a single domain name. The point is that I want to browse to something like:

http://192.168.0.1/gallery/ # a Django photo gallery project
http://192.168.0.1/blog/ # a blogging project

This way, I can develop and test multiple django projects on the same server by just referring to different URLs. (note: I don't think this Django Sites module is what I am looking for because the projects need to be distinct). As an example, PHP kind of behaves in this way as I can install something like php-gallery and phpmyadmin on the same server, just with different URL paths.

Does anyone know of any good resources of how to setup multiple Django projects under multiple URLs on a single server using Apache (with either mod_python or mod_wsgi)? Things I'd be interested in knowing is how to setup the apache.conf, possible virtualenv setup, and changes to the urls.py to accommodate this. Most of the Django deployment examples that I see are for one application per domain or subdomain.
Any advice is much appreciated.

Thanks,
Joe

like image 470
Joe J Avatar asked Jul 12 '10 21:07

Joe J


4 Answers

I've been in situations where I couldn't use subdomains, and the way to handle this with Django is pretty simple actually.

Pretty much everything in your settings file will be just like a regular Django app, with the exception of making sure these settings include your project path:

MEDIA_URL = 'http://192.168.0.1/gallery/media/'
ADMIN_MEDIA_PREFIX = '/gallery/admin_media/'
SESSION_COOKIE_PATH = '/gallery'
LOGIN_REDIRECT_URL = '/gallery/'
LOGIN_URL = '/gallery/accounts/login/'
LOGOUT_URL = '/gallery/accounts/logout/'

The SESSION_COOKIE_PATH is critical to prevent all your apps on the same domain from rewriting each others cookies.

The above instructions should cover the Django side, but there's still more work to do on the web server side. For example, if you use apache+mod_wsgi you'll need to make sure each project has their own wsgi script that is loaded like this:

WSGIScriptAlias /gallery /path/to/gallery/apache/gallery.wsgi
Alias /gallery/media /path/to/gallery/media
Alias /gallery/admin_media /path/to/gallery/venv/lib/python2.6/site-packages/django/contrib/admin/media

etc.

like image 103
Van Gale Avatar answered Nov 15 '22 21:11

Van Gale


In your question, you seem to be using projects and apps interchangeably. They mean separate things in Django. A project includes the setup file, database configuration, and overall urlconf, and is what you want at the root of your domain. An app is an individual functional piece of code that (generally) does one task.

If you want to deploy multiple apps, you want to create a single project and copy each app into the project directory. If you look at the tutorial, you'll see how to include an app in the urlconf. Simply repeat that for each one, making sure that the regexes are correct.

The key point here is that you get apache working for your overall django project, and then you use Django's internal urlconf to set up where each app may be accessed. Don't try to run multiple projects under the root of the same url - that's almost certainly a sign that you're doing it wrong.

If you're referring to running multiple projects under a single domain, we solve this problem is with subdomains.

Since the Django projects we're building are (generally) designed to live at the root of the domain when they're actually deployed, if you use app1.example.com and app2 etc., you can test like you will be deploying, in the root of each domain. You can configure subdomains exactly as you would configure top level domains, and then moving to your final deploy is easy.

If you're trying to actually deploy applications like that, create a single overarching Django project and use the urlconfs to include each Django app at a different sub url.

like image 26
Paul McMillan Avatar answered Nov 15 '22 21:11

Paul McMillan


Others have covered use of multiple applications within one Django project. If however you meant projects and/or only have one application in each project, then the simple answer is to use a separate WSGIScriptAlias directive for each project if using mod_wsgi. Each such project may optionally be delegated to a mod_wsgi daemon process group so as to allow each to be separately restarted without restarting the whole of Apache, but daemon mode is an extra thing that can be done and not the solution itself.

like image 29
Graham Dumpleton Avatar answered Nov 15 '22 20:11

Graham Dumpleton


Let's get straight on the terminology.

Most examples you see on the web are for one Django project per domain. Each project can hold several applications.

From here on I'll assume you are referring to deploying several projects on a single domain. (Otherwise - your question is nullified).

This can easily be solved with proper deployment per directory (this depends on what deployment method you are using), and ensuring your URLs are not assuming that they exist on the domain root.

like image 34
Yuval Adam Avatar answered Nov 15 '22 20:11

Yuval Adam