1
votes

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

1
Look at your logs in /var/log/apache*.logelmonkeylp
Did you set ALLOWED_HOSTS in django settings ?Slava Bacherikov
Log shows --- "ImportError: No module named django.core.wsgi." Tried to address by appending to sys.path() as discussed in this link --- stackoverflow.com/questions/14927345/…. Has not worked yet, but looking into this. I did not set ALLOWED_HOSTS.lmart999

1 Answers

0
votes

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.