Django plugged into Apache not working like Django

2019-09-05 03:00发布

I have encountered a hodgepodge of errors trying to bring my django site into production with Apache. Having finally gotten mod_wsgi sorted and Apache at least seeming to be trying to load the site I'm seeing a number of confusing problems in the debug details when the homepage fails to load.

None of these errors emerge when I fire up the site using python manage.py runserver 10.10.10.214:8080 - it works 100% fine that way

  1. Import errors - I cannot from x import y - for some reason it will only allow me to do from x import * An example was #from django.conf.urls import patterns, include, url gave an error - I had to change to from django.conf.urls.defaults import *
  2. Permission errors - for example, I had to chmod the DB file which worked fine before. No big deal I suppose but I just wondered why
  3. DB field errors: Cannot resolve keyword 'user_id' into field. Choices are: end_date, id, singleDate, start_date, user user_id worked just fine in standalone mode.
  4. Apache log was giving errors like TemplateSyntaxError: Caught ImportError while rendering: No module named staticfiles which I just lazily bypassed by commenting out django.contrib.staticfiles in settings.py. This solved nothing, needless to say

It almost felt like the files were being handled on a standalone basis and not within the django framework. Has anyone experienced these kind of teething problems before? I was foolishly thinking I could just plug in. I have a (unfounded) optimistic feeling it must just be a directive in some file.

Cheers, Arthur

1条回答
Evening l夕情丶
2楼-- · 2019-09-05 03:11

Are you sure the target system has the same version of Django installed?

Is the mod_wsgi even compiled for the version of Python you want to use? It is a common mistake that people don't realise that mod_wsgi is actually for a different Python version and so not using the same Python installation as you expect and therefore using different versions of packages.

As to the permissions errors, this is because the code when run under Apache is running as the Apache user by default. It will not have access rights the same as you do when run manually. You also have to be careful on making assumptions about what the working directory of the process is.

In order to try and sort out things out, you may be better to try and use mod_wsgi-express in your development environment. This way development environment is closer to your target system.

查看更多
登录 后发表回答