django project on apache2 + mod_wsgi - TemplateDoesNotExist - django

I just installed mod_wsgi on server with apache2.
I also installed django.
I created project and app inside project (in fact I uploaded app from my local machine).
The url-view mapping in my app works but when view tries to render any template there is TemplateDoesNotExist exception.
All my templates are in /[project]/[app]/templates/[app] (there are also some sub directories in this directory such "elements" and "errors" with specific templates)
Anyway it seems that django/wsgi doesn see the templates in my app - on my local machine {with django developement server) it works ok and django finds templates in my [app]/templates directory.
I am render templates using this path "[app]/base.html" or "[app]/elements/some_elements.html"
in my httpd.conf:
WSGIScriptAlias /wsgi /var/src/[project]/[project]/wsgi.py
WSGIPythonPath /var/src/[project]/
<Directory /var/src/[project]>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

Do you have TEMPLATE_DIRS in settings.py pointing at the correct location for this server?

Related

Apache is not serving static files from Django app

I don't know what's wrong in my virtualhost for django project but the simply question is no matter what modification I do over this file stills output the same in error log from apache and not load any css or js files, what I can see is that Apache is looking for static and media file in the root web folder: /var/www
[Fri May 30 00:58:08 2014] [error] [client 192.168.1.145] File does not exist: /var/www/static, referer: http://192.168.1.143/dgp/login/
I set up virtual host file as follows:
WSGIPythonPath /var/www/dgp_python2_7/bin/python2.7:/var/www/dgp_python2_7/lib/python2.7/site-packages
WSGIScriptAlias /dgp /var/www/dgp/dgp/dgp/wsgi.py
<VirtualHost 127.0.0.1:80>
ServerName www.dgp.dev
ServerAlias dgp.dev
AliasMatch ^/([^/]*\.css) /var/www/dgp/dgp/static/$1
Alias /media/ /var/www/dgp/dgp/media/
Alias /static/ /var/www/dgp/dgp/static/
Alias /images/ /var/www/dgp/dgp/images/
<Directory /var/www/dgp/dgp/static/>
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/dgp/dgp/media/>
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/www/dgp/dgp/error.log
CustomLog /var/www/dgp/dgp/access.log combined
</VirtualHost>
And in settings.py STATIC_ROOT with '/var/www/dgp/dgp/static/' where is located all the css content.
How can I tell apache or Django to looking for the proper directory '/var/www/dgp/dgp/static/'? It's driving me crazy, I don't understand how something so elemental in development it's so complex for production.
Regards!
Edit with the solution
The really problem was that I didn't disable the default site for Debian Apache (that is the version I'm working for) and has another method for stablish virtualhost, at beginning we have to disable default site with the follow command: a2dissite defaultand everything works now like a charm!
You can tell where your static files are being looked for in your project's rendered html. Just view the source in your browser and look for a stylesheet or javascript include, what is the full path to the file?
My guess, you have to run Django's collect static script, which will collect all the static scripts in all of your project's app and put them into one location. This is a core part of deploying Django projects and unavoidable if you use multiple "apps" in your project.
in your terminal go to the Django projects root folder and type this:
python manage.py collectstatic
Read more at https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/

Trouble with Apache, mod_wsgi, and Django configuration

Just set up a 64 bit ubuntu EC2 instance using the Bitnami DjangoStack image.
So far I have installed a few python dependencies and removed the Project django app which was created by default. I created a new app with 'django-admin.py startproject projectname'. I then followed the instructions here: http://wiki.bitnami.org/Components/Django, attempting to setup apache.
Here is my projectname.conf file:
Alias /static "/opt/bitnami/apps/django/lib/python2.7/site-packages/django/contrib/admin/static"
<Directory '/opt/bitnami/apps/django/lib/python2.7/site-packages/django/contrib/'>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias /URL_mount_point "/opt/bitnami/apps/django/scripts/projectname.wsgi"
<Directory '/opt/bitnami/apps/django/scripts'>
Order allow,deny
Allow from all
</Directory>
Here is my projectname.wsgi
import os, sys
sys.path.append('/opt/bitnami/apps/django/django_projects')
sys.path.append('/opt/bitnami/apps/django/django_projects/projectname')
os.environ['DJANGO_SETTINGS_MODULE'] = 'projectname.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Here are the last three lines of my httpd.conf:
Include "/opt/bitnami/apache2/conf/ssi.conf"
Include "/opt/bitnami/apache2/conf/bitnami/httpd.conf"
Include "/opt/bitnami/apps/django/conf/projectname.conf"
After doing this and restarting apache, hitting mydomain.com/projectname still comes up with a 404 (the Bitnami landing page comes up just fine at mydomain.com).
Am I missing something here? Are my paths in projectname.wsgi incorrect (I have not strayed from the default Bitnami directory structure). Or is there some additional step I am missing here?
You should be accessing:
http://mydomain.com/URL_mount_point
Since it appears you have not shown your original configuration, hard to say whether that is the issue or whether is a typo of sorts.
I added this to my httpd.conf
WSGIScriptAlias / /opt/bitnami/apps/django/scripts/projectname.wsgi
<Directory '/opt/bitnami/apps/django/django_projects/projectname'>
Order allow,deny
Allow from all
</Directory>
and that seemed to fix it.

Apache not able to load stylesheets and scripts in Django's static directory

I am developing a site using Django. Up until now, when I have used it in a developement environment it was working fine, loading all stylesheets and scripts normally. However, I have currently shifted over to a production environment using Apache Web Server with mod_wsgi for serving Django pages. Now when I am viewing the site, it is reporting that the css and js files couldn't be loaded and were returned with an error code 403 - Access Denied. My web site is the directory "C:\Users\rohitm\Web Applications\WebApp". I have installed Apache 2.2 at "C:\Program Files\Apache Software Foundation\Apache2.2". I have edited the httpd.conf file under the conf sub-directory in the Apache installation folder as follows:
Alias /static/ "C:/Users/rohitm/Web Applications/WebApp/static"
<Directory "C:/Users/rohitm/Web Applications/WebApp/static">
Order allow,deny
Allow from all
</Directory>
Alias /static/images/ "C:/Users/rohitm/Web Applications/WebApp/static/images"
<Directory "C:/Users/rohitm/Web Applications/WebApp/static/images">
Order allow,deny
Allow from all
</Directory>
Alias /static/javascript/ "C:/Users/rohitm/Web Applications/WebApp/static/javascript"
<Directory "C:/Users/rohitm/Web Applications/WebApp/static/javascript">
Order allow,deny
Allow from all
</Directory>
Alias /static/stylesheets/ "C:/Users/rohitm/Web Applications/WebApp/static/stylesheets"
<Directory "C:/Users/rohitm/Web Applications/WebApp/static/stylesheets">
Order allow,deny
Allow from all
</Directory>
Alias /static/admin/ "C:/Users/rohitm/Web Applications/WebApp/static/admin"
<Directory "C:/Users/rohitm/Web Applications/WebApp/static/admin">
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / "C:/Users/rohitm/Web Applications/WebApp/apache/django.wsgi"
<Directory "C:/Users/rohitm/Web Applications/WebApp/apache">
Order allow,deny
Allow from all
</Directory>
I have created a file django.wsgi in a folder apache under my WebApp folder. The contents are as follows:
import os
import sys
sys.path.append('C:/Users/rohitm/Web Applications/')
sys.path.append('C:/Users/rohitm/Web Applications/WebApp/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'WebApp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
The static files settings in settings.py are as follows
STATIC_ROOT = 'C:/Users/rohitm/Web Applications/WebApp/static/'
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
After running the manage.py collectstaic command, the static folder has 4 sub folders - admin, images, stylesheets and javascript. The stylesheet folder contains a file main.css while the javascipt folder contains a file jquery.min.js. The admin folder has all the admin related css and js present there. Now whenever I am viewing the site via http://localhost, the css and javascript files are not loaded, while the browser console (Chrome - Crtl+Shift+J) reports that the files failed to load as the server responded with an error 403 (Forbidden).
Please help urgenty.
Remove the trailing slash on first path given to Alias directive, or alternatively add a trailing slash to second path. The use of trailing slash should be balanced, ie., not on either, or on both.
You also only need the Alias/Directory block for the top directory where static files are. You don't need to enumerate it for all the sub directories. Thus use:
Alias /static "C:/Users/rohitm/Web Applications/WebApp/static"
<Directory "C:/Users/rohitm/Web Applications/WebApp/static">
Order allow,deny
Allow from all
</Directory>
Note now that using '/static' and not '/static/'.
I would guess that the user that Apache is running under does not have access to the C:/Users/rohitm/Web Applications/WebApp/static folder.
In general, it's probably best to put those files somewhere central, rather than in your personal home directory.

setting up two Django websites under Apache with WSGI

I've set up a django website as described in the django docs: https://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/
Now I want to setup another version of the site (different source dir, different database) to run on the same server. There are active users and flex apps who use app #1, so I want to keep app #1 access unchanged. I also rather not change the urls.py at all even for app #2.
I was thinking of different port for app #2
For example
http://192.168.1.1/load_book/123/ will load book from app #1
http://192.168.1.1:444/load_book/123/ will load book from app #2
I'm a complete noob to Apache and WSGI... how do I set it up?
What do you mean by they have the same URLs? The same hostname, perhaps?
Let's say you've got 2 apps:
http://example.com/your_app
http://example.com/my_app
These can both be Django apps, served by WSGI, on the same Apache instance. Using either Directory or Location directives in your apache conf to specify the .wsgi loader file as described in the django docs linked above:
<Location /your_app>
WSGIScriptAlias /your_app /path/to/mysite/apache/your_app/django.wsgi
...
</Location>
<Location /my_app>
WSGIScriptAlias /my_app /path/to/mysite/apache/my_app/django.wsgi
...
</Location>
The only real gotcha is that you'll need to tell your_app and my_app that they are no longer on the document root of the host. To do this, add a base_url parameter to your settings.py and prefix all of the entries in your urls.py with this param. This will ensure when the request comes through Apache, your python app can route it accordingly.
For an easy example of how this is done, have a look at the code for Bookworm, a Django app.
You can attatch the wsgi application to different sub-paths under the same domain. If you do this the paths to the views inside Django will still be the same. You do not have to modify the urls.py. In the following example Django will regard /site1 as the root of project1.
Check out http://code.google.com/p/modwsgi/wiki/InstallationInstructions for documentation on mod_wsgi.
<VirtualHost *:80>
ServerName www.example.com
WSGIDaemonProcess example
WSGIProcessGroup example
WSGIScriptAlias /site1 /home/django/project1/deploy/wsgi.py
<Directory /home/django/project1/deploy>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias /site2 /home/django/project2/deploy/wsgi.py
<Directory /home/django/project2/deploy>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Now the two sites will run in the same daemon process using different python sub-interpreters.

I need help on configuring mod_wsgi and Django

Apache & mod_wsgi are configured correctly (I've created a hello
world .html apache file and a hello world mod_wsgi application with
no problems). I now need my Django app to work with my django.wsgi
file. What makes me think that it's not recognizing my wsgi file is that I
went into my django.wsgi file I created and completely deleted all of
the code in the file and restarted Apache and it still gives me the
same page (a listing of the files from Django app, not my actual
Django application. Configuring Apache and mod_wsgi went really well
but I'm at a loss of how to fix this. Here are some details:
Here is my current django.wsgi file:
import os
import sys
sys.path.append('/srv/www/duckling.org/store/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'store.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I've tried a few different versions of the django.wsgi file
(including a version like the one over at http://www.djangoproject.com/).
This version of my wsgi is from here:
http://library.linode.com/frameworks/django-apache-mod-wsgi/ubuntu-10...
Also, here is my vhost apache configuration file below. I think these
are the main files that are suppose to do the job for me. Let me know if
you see any errors in what I'm doing and what else I might do to fix
this. The django app runs fine on the django's built-in development
server so I'm thinking it might have something with my paths.
No errors in my apache error.log file as well. It's acting as there's
no problem at all, which is not the case...the project isn't loading,
like I said just a listing of my files and directories of my Django
project. Here is my apache config file:
<VirtualHost 1.2.3.4:80>
ServerAdmin hi#duckling.org
ServerName duckling.org
ServerAlias www.duckling.org
DocumentRoot /srv/www/duckling.org/store/
<Directory /srv/www/duckling.org/store/>
Order Allow,Deny
Allow from all
</Directory>
Alias /static/ /srv/www/duckling.org/store/static/
<Directory /srv/www/duckling.org/store/static>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias store/ /srv/www/duckling.org/store/wsgi-scripts/django.wsgi
<Directory /srv/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
And here are versions of the stack that I'm using, I saw over at the
mod_wsgi site that you all would like the versions of what I'm using
on the server:
Apache/2.2.14 (Ubuntu) PHP/5.3.2-1ubuntu4.5 with Suhosin-Patch
mod_python/3.3.1 Python/2.6.5 mod_wsgi/2.8
thanks,
j.
For a start, you should definitely not keep your Django files under your DocumentRoot. There's no need for them to be there, and it's a potential security risk - as you've seen, your current misconfiguration allows Apache to serve up your files directly: an attacker could guess that and download your settings.py, complete with your database password.
So, get rid of that DocumentRoot directive completely, as well as the first Directory section which allows direct access to /srv/www/duckling.org/store/. (You probably don't need the one serving up /srv/www/wsgi-scripts either.) That should make things a bit better.
By the way, this configuration will serve your website under duckling.org/store - is that what you want? If you want it under the root, you should just use:
WSGIScriptAlias / /srv/www/duckling.org/store/wsgi-scripts/django.wsgi