3

My computer runs Ubuntu 12.04 and I followed this tutorial to get started with Django: http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/

I tried to start in /srv/www/ my alex project by running this command sudo django-admin.py startproject alex

Then created apache folder and django.wsgi file (/srv/www/alex/apache/django.wsgi)

tree for /srv/www/alex/

.
├── apache
│   └── django.wsgi
├── alex
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

2 directories, 6 files

alex apache site:

<VirtualHost *:80>

    ServerName alex.djangoserver
    DocumentRoot /srv/www/alex

    <Directory /srv/www/alex>
        Order allow,deny
        Allow from all
    </Directory>

    WSGIDaemonProcess alex.djangoserver processes=2 threads=15 display-name=%{GROUP}
    WSGIProcessGroup alex.djangoserver

    WSGIScriptAlias / /srv/www/alex/apache/django.wsgi

</VirtualHost>

The apache error log result:

[error] [client 127.0.0.1] mod_wsgi (pid=28076): Exception occurred processing WSGI script '/srv/www/floqq/alex/django.wsgi'.
[error] [client 127.0.0.1] Traceback (most recent call last):
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__
[error] [client 127.0.0.1]     self.load_middleware()
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 39, in load_middleware
[error] [client 127.0.0.1]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 184, in inner
[error] [client 127.0.0.1]     self._setup()
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
[error] [client 127.0.0.1]     self._wrapped = Settings(settings_module)
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 95, in __init__
[error] [client 127.0.0.1]     raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[error] [client 127.0.0.1] ImportError: Could not import settings 'alex.settings' (Is it on sys.path?): No module named alex.settings

UPDATE

apache/django.wsgi

import os
import sys

path = '/srv/www'
if path not in sys.path:
    sys.path.insert(0, '/srv/www')

os.environ['DJANGO_SETTINGS_MODULE'] = 'alex.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
1
  • for development you shouldn't need to use Apache; instead just use the built-in development server by running ./manage.py runserver and access your site on localhost:8000
    – Lie Ryan
    Jul 7, 2012 at 6:19

4 Answers 4

3

In your wsgi file you have:

sys.path.insert(0, '/srv/www')

os.environ['DJANGO_SETTINGS_MODULE'] = 'alex.settings'

which will search for settings.py in /srv/www/alex/.

That fails because, according to your posted tree, settings.py is in /srv/www/alex/alex/.

So you have 3 options:

  1. change the inserted path
  2. change the directory sturcture, or
  3. refactor /srv/www/alex/ into a package containing alex.settings.
1
  • so I tried setting sys.path.insert(0, '/srv/www/alex/alex') but still no success. I did not create the directory structure, django-admin.py startproject alex did
    – Alex
    May 8, 2012 at 10:13
2

You shouldn't use sudo with any commands that you need during development; because you don't do development as root and it will lead to other problems down the road with permissions.

If you are new to django one of the first things that's different about it than more traditional development frameworks (like PHP) is that you don't need a web server or database server to get started. Django comes with everything you need during development, including a web server which you start with the runserver command.

You should also use the official tutorial to get you started. Once you are comfortable with the tutorial then you will have a better understanding of how to deploy projects and can follow other posts easily.

Apache and mod_wsgi is best when you are deploying projects, and not optimal (or necessary) during development.

I suggest you start with the following steps:

  • First, you should execute sudo apt-get install python-virtualenv, which will allow you to create a separate environment for your python/django work. This is the only command you should run as root, and it will only be done once.

  • Now, to get started. Open a terminal and type in the following as your normal user:

    $ virtualenv --no-site-packages django_project

    This will create a new environment which you can use for your first project. Once that command has executed, type this:

    $ source django_project/bin/activate

    This activates the new virtual environment. You'll notice your prompt now has (django_project) listed. This lets you know you are in the virtual environment. Finally, you should install django:

    (django_project) $ pip install django

Now that django is installed and you can start with the tutorial. In case you close your terminal (or open a new tab or terminal window), you can reactivate the virtual environment by executing source django_project/bin/activate. You can get back to your normal shell by typing deactivate in the virtual environment.

Hope that helps.

2
  • Thank you for the info @Burhan Khalid, it's more than I could ever ask. Thank you, it is very useful
    – Alex
    May 8, 2012 at 11:31
  • One note. I'd recommend instead of installing virtualenv via apt, install setuptools instead (sudo apt-get install python-setuptools), then use easy_install to get virtualenv (sudo easy_install virtualenv). The apt versions of python packages are rarely kept up-to-date, so it's never really a good idea to install them that way. May 8, 2012 at 14:55
1

Can you post your wsgi.py? Maybe the problem is right there

My wsgi.py, maybe could help you.

import os
import sys

sys.path.append('/Applications/MAMP/htdocs/Django-1.4')
sys.path.append('/Applications/MAMP/htdocs/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meishi.settings")
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
0
1

I followed the same tutorial and had the same problem, but found an SO and some wsgi docs that got me further down the track. The key was adding 2 paths to the wsgi file search path list, though only one will actually point to the right settings.py, depending on your file structure after shuffling things around per other advice, ...

base = os.path.dirname(os.path.dirname(__file__))
base_parent = os.path.dirname(base)
sys.path.append(base)
sys.path.append(base_parent)

My pages serve up fine now... minus the css and other static/media files. But that should be easy to fix with a bit more digging.

1
  • Thanks for the info, currently I abandoned the python projects for the lack of time. I have to finish/improve some of my current PHP projects. But I'll have it in mind when I'll start again with Python (hopefully, soon)
    – Alex
    Jul 7, 2012 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.