6

Issues running django and apache2/mod_wsgi. I keep getting 500 Internal Server Error. I have tried many combinations of fixes to which none have worked. Any help is greatly appreciated. This is my setup:

Ubuntu 16.04
django 1.10.5
apache 2.4.18
python 3.4(virtualenv)
libapache2-mod-wsgi-py3 

My folder structure is:

/home/user/site/venv (virtualenv folder)
    bin
    include
    lib

/home/user/site/mysite
    |- manage.py
    static
    mysite
        |__init__.py
        |settings.py
        |urls.py
        |wsgi.py

site.conf

<VirtualHost *:80>
WSGIDaemonProcess myproject python-home=/home/user/site/venv python-path=/home/user/site/mysite
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/site/mysite/mysite/wsgi.py

        Alias /static /home/user/site/mysite/static
        <Directory /home/user/site/mysite/static>
            Require all granted
        </Directory>

        <Directory /home/user/site/mysite/mysite>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>


</VirtualHost>

wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

application = get_wsgi_application()

apache2/error.log

[mpm_event:notice] [pid 8908:tid 140560009164672] AH00491: caught SIGTERM, shutting down
[wsgi:warn] [pid 9047:tid 139761898837888] mod_wsgi: Compiled for Python/3.5.1+.
[wsgi:warn] [pid 9047:tid 139761898837888] mod_wsgi: Runtime using Python/3.5.2.
[mpm_event:notice] [pid 9047:tid 139761898837888] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[core:notice] [pid 9047:tid 139761898837888] AH00094: Command line: '/usr/sbin/apache2'
[wsgi:error] [pid 9049:tid 139761776183040] mod_wsgi (pid=9049): Target WSGI script '/home/user/site/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[wsgi:error] [pid 9049:tid 139761776183040] mod_wsgi (pid=9049): Exception occurred processing WSGI script '/home/user/site/mysite/mysite/wsgi.py'.
[wsgi:error] [pid 9049:tid 139761776183040] Traceback (most recent call last):
[wsgi:error] [pid 9049:tid 139761776183040]   File "/home/user/site/mysite/mysite/wsgi.py", line 12, in <module>
[wsgi:error] [pid 9049:tid 139761776183040]     from django.core.wsgi import get_wsgi_application
[wsgi:error] [pid 9049:tid 139761776183040] ImportError: No module named 'django'

I have given the permissions to the folders below:

sudo chown -R www-data:www-data /home/user/site/venv
sudo chown -R www-data:www-data /home/user/site/mysite

Any help or criticism I would love thank you in advance.

Venor
  • 347
  • 2
  • 5
  • 17
  • 1
    Do you activate your virtualenv? Otherwise in the global python environment the app can't find the module 'django' because it's installed in your /home/user/site/venv/bin directory. – zhiqiang huang Apr 10 '17 at 18:26
  • Yes it gets loaded by WSGIDaemon from the .conf for apache or at least it is supposed to. In my case, I don't believe it is but I can't seem to figure out why. – Venor Apr 10 '17 at 19:10
  • Try to check your django setting,like echo $DJANGO_SETTINGS_MODULE, make sure you can find your django.py file under that path, because the error shows that the system couldn't find the django module. – zhiqiang huang Apr 10 '17 at 19:27
  • I am able to run the manage.py from the virtualenv. It will not load the virtualenv. I have tried this [link](http://stackoverflow.com/a/38434628/4531243) but this solution did not work for me either. – Venor Apr 10 '17 at 22:38

5 Answers5

16

So after some intense head bashing against the wall. It turns out I needed to compile my own mod_wsgi for the version of python I was using. I was using the standard repo for ubuntu libapache2-mod-wsgi-py3 which is compiled to use with python3.5.2 as it shows in my error.log.

I went here for the most up to date version : mod_wsgi_releases

be sure to use the command below when installing mod_wsgi

.configure --with-python=/your/virtualenv/bin/python(your python_verion here)

Venor
  • 347
  • 2
  • 5
  • 17
  • did you use pip? – Anupam Jun 30 '17 at 10:39
  • 1
    No I downloaded the tar.gz from link I provided above then ran this command (.configure --with-python=/your/virtualenv/bin/python3.4 && make && sudo make install.) after unpacking the tar file. – Venor Jun 30 '17 at 15:00
1

Try using something like this. Just to confirm, is myproject the user group ?

WSGISocketPrefix /var/run/wsgi
WSGIPythonPath /home/user/site/venv/lib/python2.7/site-packages
WSGIDaemonProcess ec2-user processes=1
WSGIProcessGroup ec2-user
WSGIScriptAlias / /home/user/site/mysite/mysite/wsgi.py
rrmerugu
  • 1,826
  • 2
  • 20
  • 26
  • does it throw any error in the console along with 500 ? – rrmerugu Apr 11 '17 at 14:59
  • No apache2 restarted just fine after I put the WSGISocketPrefix outside of the . Still throwing the same error inside of the apache2/error.log – Venor Apr 11 '17 at 15:03
  • I generally use something like this https://github.com/rrmerugu/django-seed , try this implementation – rrmerugu Apr 11 '17 at 15:04
  • So I tried this but a few things. WSGISocketPrefix needs to be outside of the for it to work. Also I changed WSGIPythonPath to be inside of the WSGIDaemonProcess because that is how it is suggested to be used in other documentation. So it looks as follows: `WSGIDaemonProcess ec2-user processes=1 python- path=/home/user/site/venv/python3.4/site-packages WSGIProcessGroup ec2-user WSGIScriptAlias / /home/user/site/mysite/mysite/wsgi.py` Unfortunately, I'm still getting a 500 error. – Venor Apr 11 '17 at 15:16
1

mod_wsgi is the cause of this error because The mod_wsgi module in C code links to the Python library. Thus the version of Python it is compiled for is embedded in the module. It doesn't just execute python program. This means it has to be compiled for the version of Python you want to use. You cannot force it via a virtual environment to use a different Python version [source].

Therefore, you need to uninstall the mod_wsgi module (likely the operating system packaged one) and install mod_wsgi yourself from source code, compiling it against the Python version you want to use.

Follow the steps below to fix the error:

  1. uninstall mod_wsgi [source]:
sudo rm /usr/lib/apache2/modules/mod_wsgi.so
  1. make custom builds of Python and mod_wsgi ourselves [source]:
## A.requirements:
sudo apt update
sudo apt install build-essential checkinstall
sudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
sudo apt install libffi-dev

## B.build mod_wsgi:
apt install apache2 apache2-dev
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.7.1.tar.gz
tar xvfz 4.7.1.tar.gz
cd mod_wsgi-4.7.1

./configure --with-python=[your python path]
## for example: ./configure --with-python=/usr/bin/python3.7

sudo make
sudo make install

You can use which python3.7 to find the path of the Python file

  1. reload apach2:
sudo systemctl reload apache2
Mohammad Nazari
  • 2,535
  • 1
  • 18
  • 29
  • 1
    use `usr/bin/python[version]` insted of address `/usr/local/bin/python[version]` if get error `collect2: error: ld returned 1 exit status apxs:Error: Command failed with rc=65536 . Makefile:31: recipe for target 'src/server/mod_wsgi.la' failed make: *** [src/server/mod_wsgi.la] Error 1 ` – Mohammad Nazari Sep 25 '21 at 11:43
  • dou to the [doc](https://modwsgi.readthedocs.io/en/latest/user-guides/installation-issues.html) i need to reinstall python with --enabled-shared config to make mod_wsgi.so – Mohammad Aqajani Aug 05 '22 at 18:32
0

try which django-admin in your virtual env. If this is different location than the location you desire it to be to work with, install django in your virtual env. Or try pip freeze > requirements.txt in your virtual env to see django is actually there.

Abhishek Menon
  • 753
  • 4
  • 15
  • I checked and it returns /home/user/site/venv/bin/django-admin which is where it should be. I installed django in the virtualenv. which is why I believe it it is not loading the virtualenv correctly. Thank you for the suggestion though. – Venor Apr 11 '17 at 14:15
0

Had the same issue, I fixed it by uninstalling gunicorn and reinstalling it while logged in through virtual environment in my project

Nax
  • 41
  • 4