Search code examples
django.htaccessfastcgi

ImportError running Django in FastCGI-mode


I am currently trying to deploy my first django-based site and are running into a few problems as I have very little experience configuring a server.

I've followed a guide provided by the host and my files looks like this now.

The folder structure.

mysite/
    manage.py
    mysite/
        apps
        settings/
            __init__.py
            base.py
            local.py
            production.py
    requirements.txt
    venv
www/
    .htaccess
    mysite.fcgi

.htaccess

AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ mysite.fcgi/$1 [QSA, L]

mysite.fcgi

#!/usr/local/bin/python
import sys
import os

sys.path.append("home/users/myuser")

from django.core.handlers.wsgi import WSGIHandler
from flup.server.fcgi import WSGIServer

os.chdir("/home/users/myuser/mysite/")
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

WSGIServer(WSGIHandler()).run()

The guide provided by the host says that they are running FastCGI, but when adding handler in .htaccess I see they've added fcgid-script and not fastcgi-script. So I've tried both.

When using fcgid-script I get this error from the error log:

mod_fcgid: stderr: ImportError: Could not import settings 'mysite.settings' 
(Is it on  sys.path?): No module named mysite.settings

When using fastcgi-script I get this error from the error log:

File does not exist: /home/users/myuser/www/mysite.fcgi/favicon.ico

Also I am using virtualenv for my project which looks like this.

requirements.txt

Django==1.4.2
Markdown
PIL=1.1.7
MySQL-python
South
flup

If anyone can see what I'm doing wrong, help would be much appreciated. Thank you.

As I'm using virtualenv I had to add this to my .fcgi file to solve the problem

venv = '/home/users/myuser/mysite/venv/bin/activate_this.py'
execfile(venv, dict(__file__=venv))

Solution

  • I don't knwow much about FastCGI but it seems the second option where you are throwing this error File does not exist: /home/users/myuser/www/mysite.fcgi/favicon.ico must be a setting in the Apache config file. The mysite.settings seems to be correct, so maybe find a way to remove the reference to the .ico file (my django settings on Mod-WSGI would be in the apache2 config) or add an ico file to the path where it currently cannot be found.