1

I want to move a Django project from test server to Apache (version 2.2.15 on CentOS). I installed mod_wsgi using [1] and am trying to mount my project as a wsgi application using [2].

I followed the Hello World example (helpful SO thread [3]) by putting the test wsgi.py file in the same directory as my existing Django project file (wsgi.py). This works (e.g., I can access via wget).

<VirtualHost *:80>
   DocumentRoot <abs_path>/myproject/myproject
   WSGIScriptAlias /wsgi  <abs_path>/myproject/myproject/test_wsgi.wsgi
</VirtualHost>

With this in place, I made a modification to the Apache config file to point to my Django project wsgi file (below) based upon the mod_wsgi and Django documentation [2,4].

WSGIPythonPath <abs_path>/myproject/myproject

<VirtualHost *:80>

   DocumentRoot <abs_path>/myproject/myproject
   WSGIScriptAlias /django  <abs_path>/myproject/myproject/wsgi.py

   <Directory <abs_path>/myproject/myproject>
            <Files wsgi.py>
                    # Require all granted
                    Order deny,allow
                    Allow from all
            </Files>
    </Directory>

</VirtualHost>

This throws an "500 Internal Server Error:" "ImportError: No module named django.core.wsgi"


mod_wsgi is compiled against Python 2.7, which has Django installed [5].

ldd mod_wsgi.so 
linux-vdso.so.1 =>  (0x00007fff3ed30000)
libpython2.7.so.1.0 => /usr/local/lib/libpython2.7.so.1.0 (0x00007f641e11d000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f641dee8000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f641dce4000)
libutil.so.1 => /lib64/libutil.so.1 (0x00007f641dae1000)
libm.so.6 => /lib64/libm.so.6 (0x00007f641d85c000)
libc.so.6 => /lib64/libc.so.6 (0x00007f641d4c9000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c14e00000)

Based upon [6], I added path to python2.7 site packages in wsgi.py:

# add the myproject project path into the sys.path
sys.path.append('<PATH_TO_MY_DJANGO_PROJECT>/myproject')
# add the python2.7 site-packages path to the sys.path
sys.path.append('<PATH_TO_python2.7>/Lib/site-packages')

I also added path to python2.7 in httpd.conf:

WSGIPythonPath <PATH_TO_PROJECT>:<PATH_TO_python2.7>/Lib/site-packages

Still, I get "500 Internal Server Error:" "ImportError: No module named django.core.wsgi".

Any advice?


[1] http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide

[2] http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide

[3] Hello World in mod_wsgi

[4] https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/

[5] mod_wsgi isn't honoring WSGIPythonHome

[6] ImportError: No module named django.core.wsgi Apache + VirtualEnv + AWS + WSGI

Community
  • 1
  • 1
lmart999
  • 6,671
  • 10
  • 29
  • 37
  • 1
    Look at your logs in /var/log/apache*.log – elmonkeylp Sep 16 '14 at 18:38
  • Did you set [ALLOWED_HOSTS](https://docs.djangoproject.com/en/1.7/ref/settings/#allowed-hosts) in django settings ? – Slava Bacherikov Sep 16 '14 at 18:49
  • Log shows --- "ImportError: No module named django.core.wsgi." Tried to address by appending to sys.path() as discussed in this link --- http://stackoverflow.com/questions/14927345/importerror-no-module-named-django-core-wsgi-apache-virtualenv-aws-wsgi. Has not worked yet, but looking into this. I did not set ALLOWED_HOSTS. – lmart999 Sep 16 '14 at 19:00

1 Answers1

0

Go read the actual Django documentation. In that you will see requirements for setting up the Python module search path. You should no evidence of having done that.

Also, do not set DocumentRoot to be a parent directory of your Django project. You are opening yourself up to security problems. More so because you are mounting the Django application at a sub URL, which means that your project source code including settings file with database passwords etc is now downloadable by remote users from the root of the site.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • Thanks, Graham. Did that. Elaborated in my post above: getting the "ImportError: No module named django.core.wsgi" reported in other threads. Tried to address it using the recommended by strategies made by you and others previously. – lmart999 Sep 17 '14 at 01:26
  • 1
    Do you really have 'Lib' rather than 'lib' in the path you are specifying? Do you have SELinux enabled? Are the directories accessible/readable to the user that Apache run as? – Graham Dumpleton Sep 17 '14 at 01:50
  • Got it, it was #3: A problem with permission the python 2.7 site-packages. Fixed that, and it worked. – lmart999 Sep 17 '14 at 03:14