61

I am trying to publish my site on an Amazon's EC2 Instance, and I keep getting a 500 error. I really don't know why.

//Log Files

    [Sun Feb 17 23:12:48.066802 2013] mod_wsgi (pid=2102): Target WSGI script '/srv/www/app/poka/apache/wsgi.py' cannot be loaded as Python module.
    [Sun Feb 17 23:12:48.066840 2013] mod_wsgi (pid=2102): Exception occurred processing WSGI script '/srv/www/app/poka/apache/wsgi.py'.
    [Sun Feb 17 23:12:48.066864 2013] Traceback (most recent call last):
    [Sun Feb 17 23:12:48.066889 2013] File "/srv/www/mysite/poka/apache/wsgi.py", line 26, in <module>
    [Sun Feb 17 23:12:48.066920 2013] from django.core.wsgi import get_wsgi_application
    [Sun Feb 17 23:12:48.066945 2013] ImportError: No module named django.core.wsgi

//Apache Config Files

    WSGIScriptAlias / /srv/www/app/mysite/apache/wsgi.py

    WSGIDaemonProcess mysite python-path=/srv/www/app/mysite:/home/ec2-user/.virtualenvs/mysite-main/lib/python2.7/site-packages
    WSGIProcessGroup mysite

    <Directory /srv/www/app/mysite/apache/>
    <Files wsgi.py>
    Order deny,allow
    Allow from all
    </Files>
    </Directory>

    <Directory /srv/www/app/mysite/apache/>
    Order deny,allow
    Allow from all
    </Directory>

    <Directory /home/ec2-user/app/mysite/static>
    Order deny,allow
    Allow from all
    </Directory>

    <Directory /home/ec2-user/app/mysite/media>
    Order deny,allow
    Allow from all
    </Directory>

//wsgi.py

    import os
    import sys
    import site

    site.addsitedir('/home/ec2-user/.virtualenvs/mysite-main/lib/python2.7/site-packages')
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

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

    path = '/srv/www/app/mysite'

    if path not in sys.path:
        sys.path.append(path)
peterh
  • 11,875
  • 18
  • 85
  • 108
abisson
  • 4,365
  • 9
  • 46
  • 68

12 Answers12

52

I know that this is an old thread but I've just bumped into the same issue and I don't think that this is caused by a missing package. As the Django core distribution contains the correct wsgi handler already.

The problem here is that when wsgi.py is executed it's missing the packages of the site-packages from your virtualenv. (If you have activated your virtualenv, and done pip install django then everything is fine. You have the necessary django packages).

As far as I'm concerned, I fixed the issue modifying the sys.path in my Path/to/Project/Project/wsgi.py file.

You have to append your project dir and your virtualenv site-packages to the sys.path List. Here is my wsgi.py file contained in my project (Talking about the wsgi.py created with django-admin.py start-project)... that I had to modify in order to make it work with Apache

# =====================
# wsgi.py file begin 

import os, sys
# add the hellodjango project path into the sys.path
sys.path.append('<PATH_TO_MY_DJANGO_PROJECT>/hellodjango')

# add the virtualenv site-packages path to the sys.path
sys.path.append('<PATH_TO_VIRTUALENV>/Lib/site-packages')

# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")

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

# wsgi.py file end
# ===================

Make sure:

  1. you added mod_wsgi to the Apache modules dir mod_wsgi must be compiled for the OS, Apache and Python version you have

  2. added the load module command into your httpd.conf to load mod_wsgi module LoadModule wsgi_module modules/mod_wsgi.so

  3. configured Django specifics in your httpd.conf or any conf you include in your httpd.conf

Based on the documentation How to use Django with Apache and mod_wsgi

WSGIScriptAlias / <PATH_TO_PROJECT>/hellodjango/hellodjango/wsgi.py
WSGIPythonPath <PATH_TO_PROJECT>:<PATH_TO_VIRTUALENV>/Lib/site-packages

<Directory <PATH_TO_PROJECT>/hellodjango/hellodjango> 
  <Files wsgi.py>
    Order deny,allow
    Require all granted
  </Files>
</Directory>

Hope this helps. It worked for me.

Rabah B
  • 521
  • 4
  • 5
  • 7
    The **wsgi.py** update seems to fix it for me, except that my `site-packages` had a different path (and not with a capital L in lib): `lib/python3.6/site-packages`. Your path may be different, fx. if you have a different python version, so I suggest checking this. Also I'm not sure what `PATH_TO_PROJECT/hellodjango` means. Is it supposed to be the directory where **settings.py** is? – miyalys Mar 15 '17 at 14:33
  • perfect!! after so many answers saying the same thing ie. Django not installed in the current virtualenv, finally something worthwhile – coda Mar 31 '19 at 17:11
  • Follow this to locate site-packages - https://stackoverflow.com/questions/122327/how-do-i-find-the-location-of-my-python-site-packages-directory – Robert Ranjan Jan 11 '22 at 18:47
30

I had the same issue. My libapache2-mod-wsgi was python 2.x and not python 3.x

found the solution here: https://stackoverflow.com/a/28118284/2489042

credits to @nima

$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3

Warning from @alxs before you copy/paste these commands: If there are python 2 projects running on the server that use wsgi and apache, the above commands will effectively shut them down.

hanesh
  • 470
  • 5
  • 12
  • 1
    From Review: Hi, while links are great way of sharing knowledge, they won't really answer the question if they get broken in the future. Add to your answer the essential content of the link which answers the question. In case the content is too complex or too big to fit here, describe the general idea of the proposed solution. Remember to always keep a link reference to the original solution's website. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – sɐunıɔןɐqɐp Nov 14 '19 at 14:40
7

Add this to the Apache configuration file:

WSGIPythonHome /home/ec2-user/.virtualenvs/mysite-main
Matt
  • 8,758
  • 4
  • 35
  • 64
  • It didn't do the trick.... however I did a "ldd mod_wsgi.so" and it points to Python 2.6, but my virtual env is Python 2.7... – abisson Feb 18 '13 at 02:01
4

For me, this indicated Django wasn't installed on the sever. Fixed via

pip install Django

In general, make sure you installed requirements:

pip install -r requirements.txt

If using a virtual environment, activate it before installing requirements.

Zags
  • 37,389
  • 14
  • 105
  • 140
  • 12
    This defeats the purpose of using virtualenv. – kjyv Jan 03 '15 at 20:32
  • Often not that simple: Consider the example of a shared dev or staging server where multiple branches of a django project must be run on various virtual hosts -- each of which run fine in a virtual environment. – fiacre Aug 16 '15 at 13:24
  • Had the same issue on Amazon ec2, had installed Django under the Ubuntu username, but it needed to be installed under root for Apache2 to pick it up for some reason. – Alex Stewart Oct 10 '17 at 08:30
  • @kjyv pip installs into virtual environments too... – Zags Oct 27 '22 at 12:38
4

I had a similar error just now. It turns out that our Django code was developed on python 3.5, but for some reasons the people who deployed our server setup virtualEnv with python 2.7. We redeployed with python 3.5 and everything worked for us

Below was the error message I received:

$ python serviceStartup.py 
Traceback (most recent call last):
  File "serviceStartup.py", line 10, in <module>
    from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi

Hope this will help with anyone seeing a similar error message!

Lingster
  • 977
  • 10
  • 21
  • 1
    I got this error after installing WSGI with libapache2-mod-wsgi in debian. When using python 3 the correct package is libapache2-mod-wsgi-py3 – datagutten Jun 22 '19 at 08:28
1

You've configured everything very well my friend, just need to give the apache user permission to access both project and virtualenv dirs.

Example:

sudo chown -R www-data:www-data /home/ubuntu/projects
sudo chown -R www-data:www-data /home/ubuntu/virtualenv

This solved my problem with ImportError: No module named django.core.wsgi (virtualenvs folders) and ImportError: No module named <project-name>.settings (projects folders)

Cássio
  • 181
  • 2
  • 8
0

At first glance, I am sorry for my English. I also faced this issue, and I have solved it by changing 'wsgi.py' file to:

import os
import django
from django.core.handlers.wsgi import WSGIHandler


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eisentask.settings.production")
django.setup(set_prefix=False)

application = WSGIHandler()
0

I am using Centos and the nginx and gunicorn for my project... I had this problem... wsgi file is not reachable maybe it's bcz of the your current directory I changed my current directory and It worked... my project name is coda so I typed

cd coda

and then

gunicorn --bind  0.0.0.0:8001 coda.wsgi
emeh
  • 89
  • 3
0

In case someone came here using AWS Lightsail bitnami..

The problem is that 'python install packages in a directory that apache do not see'.

so: packages get installed in '/home/bitnami/.local/lib/python3.8/site-packages/' and Apache looks in '/opt/bitnami/python/lib/python3.8/site-packages/'.

The temporary solution I followed is copying packages to Apache eyes folder with this command 'cp -r /home/bitnami/.local/lib/python3.8/site-packages/* /opt/bitnami/python/lib/python3.8/site-packages/'

Mohamed Sayed
  • 844
  • 5
  • 15
0

Hi I got the same issue in Ubuntu 20.04 server. I fixed it by installing apache2-dev

sudo apt-get install apache2-dev

In mod_wsgi documentation its important to have both apache2 and apache2-dev installed.

Karthikaeyan
  • 655
  • 1
  • 9
  • 13
0

Run these commands:

$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3
vimuth
  • 5,064
  • 33
  • 79
  • 116
-2

For me it was some variables that needed to be setted (for windows) :

set PYTHONHOME=F:\path\to\python
set PYTHONPATH=F:\path\to\python
Stephane L
  • 2,879
  • 1
  • 34
  • 44