user10332687
user10332687

Reputation:

Apache/wsgi using wrong python version

I have two versions of python installed on ubuntu -- 3.7 and 2.7. When I add in a "normal" sites-enabled/000-default.conf config, it is trying to use python2.7, so it doesn't find the installed application:

WSGIScriptAlias / home/david/data-py/wsgi.py
<VirtualHost *:80>
    <Directory /home/david/data-py>
       Order allow,deny
       Allow from all
       Require all granted
    </Directory>
</VirtualHost>

ImportError: No module named django.core.wsgi

However, if I specify the python path of my project (which was created using $ virtualenv ., and I start the file with:

WSGIScriptAlias / home/david/data-py/wsgi.py
WSGIPythonHome /home/david/data-py

Then I get another error:

ImportError: No module named site

What does this mean and how can I get the project installed in apache/wsgi?

Upvotes: 8

Views: 5009

Answers (2)

P K
P K

Reputation: 181

I also ran into the same issue about a month ago (caused by python2 vs python3). Here is what I recommend:

  1. Remove django and pip from python2 (this will avoid the confusion)
  2. Python3 comes with inbuilt pip3 - so go ahead and install/update django
  3. Remove the mod_wsgi.so you have installed/setup at OS level
  4. Install the python3-dev/python3-devel+apache-dev/apache-devel package
  5. Try pip3 install mod-wsgi - if it fails use the steps mentioned here (https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html)
  6. More troubleshooting at https://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html

Upvotes: 1

Ron
Ron

Reputation: 1269

I just ran into the exact same issue and it turns out I had installed the wrong version of mod_wsgi on my server (I had the one for python 2 not python 3). You can see what version apache is attempting to use if you look at the tracebacks stored in the apache error.log file (in my case /var/log/apache2/error.log) I solved it as follows (on Ubuntu):

sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi 
sudo apt-get install libapache2-mod-wsgi-py3
sudo service apache2 restart

Also I made sure to install the mod_wsgi package into my virtual environment (I'm not sure whether this was necessary). To do this I did the following:

cd <path to my virtual env bin folder>
sudo ./pip3 install mod_wsgi-httpd 
sudo ./pip3 install mod-wsgi

Upvotes: 6

Related Questions