I'm new to Django and have a question about Django application design.
I want to write one application and have multiple instances of it running, each under a different URL. Each instance would store its data in its own tables and each instance would have a different set of users. The different instances would be accessed from different URLs, but from the same domain and run the same application code behind the scenes.
For example if I had two instances, they would be accessed from the following URLs:
http://www.domain.com/instance1
http://www.domain.com/instance2
I've got my models defined, and I can run a single application, single instance, with multiple users just fine. However it's not clear to me how to support multiple instances.
I've taken a look at the sites framework, which is almost what I want, but I need everything to be hosted under the same domain.
Any tips on how I can run multiple instances of a single application with Django?
You could simply copy the django project to another place and create a new apache site.
For example:
Create a virtual host in /etc/apache2/sites-available/domain.eu_first
<VirtualHost *:80>
ServerName domain.eu
WSGIScriptAlias /first /var/www/first/wsgi
<Directory /var/www/first>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
and
a virtual host in /etc/apache2/sites-available/domain.eu_second
<VirtualHost *:80>
ServerName domain.eu
WSGIScriptAlias /second /var/www/second/wsgi
<Directory /var/www/second>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Now copy your project to first and second. And activate your apache sites by a2ensite domain.eu_first and a2ensite domain.eu_second
You have to proof that you have used only relativ links in your project.
Answer is unverified so just check it out.
Related
So I am converting the Django tutorial to fully work with Apache instead of using the built-in "runserver" command. I got step one working; getting Apache to serve the static files (css). Now I need to get it to serve the static files for the admin.
My code so far in the httpd.conf file for Apache 2.4.
#static files for site
Alias /static/ "C:/mysite/polls/static/"
<Directory "C:/mysite/polls/static">
Require all granted
</Directory>
#static files for admin
Alias /static/ "C:/Python27/Lib/site-packages/django/contrib/admin/static/"
<Directory "C:/Python27/Lib/site-packages/django/contrib/admin/static">
Require all granted
</Directory>
Obviously having the same alias for the 2nd block does not work and the CSS will not load for the admin. The site (1st block) loads fine. Now this page in the Django tutorial details the entire process on how to make it work. I just cannot figure it out. Maybe I am doing a syntax error and I have read countless posts about this both here and elsewhere.
The doc mentions 3 ways to do it. I want to do it the 2nd way; by way of using the alias directive: "Use an Alias directive, as demonstrated above, to alias the appropriate URL (probably STATIC_URL + admin/) to the actual location of the admin files."
Now I don't understand the exact part where it says "STATIC_URL + admin/). I tried various variations of that but it won't work. My link to the admin page is exactly this:
http://127.0.0.1/admin/
Can we figure this out in specific to WINDOWS and DJANGO 1.6? I know prior to DJANGO 1.4 there was a different way using "ADMIN_MEDIA_PREFIX" in the settings.py file. That way is deprecated now and I want to use the alias. And bonus. How do aliases exactly work?
Thanks all.
EDIT:
Link that mentions how to do it. Under "Serving the admin files" I need to figure out the 2nd way using alias.
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi/
This is what the "static" app is for. You should do manage.py collectstatic on deployment, and this collects all your static content - both for your apps and for the built-in/contrib ones - into one place, which is where you point your Apache alias to.
But if you really want to hard code it, STATIC_URL + admin just means exactly that: the value of STATIC_URL, suffixed with "admin", so Alias /static/admin.
This is the code that finally works:
Alias /static/admin "C:/Python27/Lib/site-packages/django/contrib/admin/static/admin/"
<Directory "C:/Python27/Lib/site-packages/django/contrib/admin/static/admin">
Require all granted
</Directory>
Alias /static/ "C:/mysite/polls/static/"
<Directory "C:/mysite/polls/static">
Require all granted
</Directory>
Note that ORDER matters greatly. I have to Alias the admin static BEFORE aliasing the site static. Sounds like a cascading type issue and makes sense the more specific gets precedent.
Also I had my link incorrect for the admin. It was ending in ../admin/static/. It should go deeper into ../admin/static/admin/.
Finally 2 areas working to serve static files. The admin comes first and then the site static 2nd.
Thanks to all and this should be really documented and might be a pitfall for some.
Regarding this documentation page from the Django website,
https://docs.djangoproject.com/en/1.2/howto/static-files/
where it says for development "With that said, Django does support static files during development. You can use the django.views.static.serve() view to serve media files."
So my question is, if I use this method, How much work is required to move to apache.
Currently I have a symbolic link to my image folder in the /var/www folder, and in the Django settings I have set the media url to :
MEDIA_URL = 'http://127.0.0.1:80/Images/'
This seems like a fairly easy hack but my project is going to get very big (with lots of css, js and pdfs) and I doubt if this method is good.
My approach was to have apache itself intercept the static files urls and serve them directly without invoking django at all. So my apache config looked something like this:
<VirtualHost *:80>
ServerName www.myproject.com
Alias /static /docs/my_website/static
<Directory /docs/my_website/static>
Order allow,deny
Allow from all
</Directory>
Alias /favicon.ico /docs/my_website/static/images/icons/favicon.ico
Include "/13parsecs/conf/django.conf"
</VirtualHost>
Then you can just keep doing whatever you're doing in the dev environment, and when you get to apache it won't invoke django at all for static content, which is what you want.
This is a perfectly good way of doing things. The only change you'll need to make is to put the actual URL of your site, rather than the localhost IP.
Don't "move to Apache", start using it in the first place. None of the software needed has licensing fees and runs on almost any platform, so the only excuse you could have is "I'm too lazy".
After doing a bit of googling, I found these projects to help with serving static files:django-static with Nginx, and django-staticfiles.
Is there anybody that has had experience with one, or preferably both of these approaches, and that can recommend one or the other, or a 3rd?
The usual way to handle static files is actually not sending them through django, but let the web server (e.g. apache or ngingx) handle them. I provide a small example for mod_wsgi, based on official django docs, found here.
Alias /media/ /usr/local/wsgi/static/media/
<Directory /usr/local/wsgi/static>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /usr/local/wsgi/scripts/django.wsgi
<Directory /usr/local/wsgi/scripts>
Order allow,deny
Allow from all
</Directory>
The first statement makes sure all files in /media will be served through apache directly, not django. The second statement is for setting up the django site itself. Now, using this media files do not go through django processing, which is often painfully slow.
The reason static file servers exist is mainly for development or very minimalistic rollouts.
Currently I am having three sites for example let it be site1, site2 and site3 . Each site require authentication. Both site1 and site2 take the same database let it be "Portfolio" database and site3 is having a different database let it be "site3specific" database.
I am planning to have a Common Account database for keeping the login credentials of users for the all different sites available. So that each sites (i.e. site1, site2 and site3) will make use of the Common Account database for authenticating the user login. I am planning to keep the user details in a separate database since all the three sites in development, testing and live environment can share the same user credentials without redundancy. Also each site may have its own specific data that we may be having or entering differently in development, staging and live environments.
Also there is a possibility of sharing some data between sites.
Could anyone please tell me how can I achieve these task in django + Apache + mod_wsgi.
Please advice whether I need to have a globally shared settings file , model file and urls file. IF then how my globally shared settings files need to be modified . Please advice.
This is how we currently operate.
Each site has its own VirtualHost entry in the httpd.conf, and each app has its own django.wsgi config file which looks something like this (you can probably use a simpler one):
import os, sys, site, glob
prev_sys_path = list(sys.path)
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
site.addsitedir(glob.glob(os.path.join(root_dir, 'venv/lib/python*/site-packages'))[0])
sys.path.append('/usr/local/django-apps')
sys.path.append('/usr/local/django-apps/AppName')
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
os.environ['DJANGO_SETTINGS_MODULE'] = 'AppName.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
The VirtualHost needs to contain entries like this:
SetEnv DJANGO_ENV ${environment
WSGIDaemonProcess appname user=apache group=apache processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup appname
WSGIScriptAlias / /usr/local/django-apps/AppName/apache/django.wsgi
<Directory /usr/local/django-apps/AppName/apache>
Order deny,allow
</Directory>
From there, the database set up is dependent on what database engine you're using.
Hope this helps.
You have to look at your requirements, and see if all sites would perhaps require and if so respect a single sign-on (sso) service. If that is the case, then you might need to look at how sessions are transfered between sites as sessions are SITE_ID specific. So, just making it work may be a great start, but looking at the big picture before you dig too deal in might be a good idea.
I set the same session name in these sites (a.xx.com/b.xx.com/c.xx.com -> sesssion name=xx.com). In my Django project, I used three settings files for each site and used the manager.py to separate these sites. The last step, start them up separately.
I have a shared hosting account for my ColdFusion websites. One of my customers needs CFChart graphics for his statistics. I've programmed them and they run ok on my own development server, but they don't show up online. The reason is that ColdFusion puts the generated images into /CFIDE which is outside of my part of the file system, and not accessible for me in a shared hosting environment.
IMG SRC="/CFIDE/GraphData.cfm?graphCache=wc50&graphID=Images/4990209100100002.PNG"
The hoster uses IIS on a Windows machine and CF7. He has tried several things (configuration-wise), but so far, nothing helped.
What can we do?
We have a site that creates statistical charts on a schedule. CFChart allows you to store the data to a variable (the "name" attribute). Then use CFFile to write the chart to any location within your webroot. We use it for Flash charts, but I've tested it with PNG as well, and it works fine.
I'm not sure how you'd go about adding this to IIS, but, I've used this on apache to solve the same issue:
Alias /CFIDE /var/www/html/CFIDE
<Directory /var/www/html/CFIDE>
Order deny,allow
Deny from all
</Directory>
<Files ~ "^GraphData.cfm$">
Order allow,deny
Allow from all
</Files>
I believe it would be possible to use the techniques described in this blog post:
link text
And store the image in a location where the browser could get to it.