how to set WSGI of appache2 to work with python 3.7?
Asked Answered
R

4

7

I am using ubuntu 16.04 and installed python 3.7 and set it to default using this instructions: Unable to set default python version to python3 in ubuntu when I type python in the console I get python3.7 , i tried to set appache2 to work with python 3.7 using :

sudo add-apt-repository --yes ppa:deadsnakes/ppa
sudo apt-get update --yes
sudo apt-get install --yes python3.7
sudo apt-get install --yes python3-pip
sudo apt-get --yes install python3-pip apache2 libapache2-mod-wsgi-py3 
sudo a2enmod wsgi
sudo apt install --yes python-django-common
sudo apt-get  install --yes python-django

but still I get exceptions of import packages that are already installed in /var/log/apache2/error.log when try to reach out to the server that I don't get at the terminal like this :

Traceback (most recent call last):
 File "/home/ubuntu/my_code/wsgi.py", line 11, in <module>
     from django.core.wsgi import get_wsgi_application
 ImportError: No module named 'django'
 mod_wsgi (pid=75005): Target WSGI script '/home/ubuntu/my_code/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=75005): Exception occurred processing WSGI script '/home/ubuntu/my_code/wsgi.py'.
 Traceback (most recent call last):
  File "/home/ubuntu/my_code/wsgi.py", line 11, in <module>
  from django.core.wsgi import get_wsgi_application

and

mod_wsgi (pid=75005): Target WSGI script '/home/ubuntu/my_code/wsgi.py' cannot be loaded as Python module.

even though i have django installed in python 3.7 another error i get is after service restart :

 mod_wsgi (pid=89300): Call to 'site.addsitedir()' failed for '(null)', stopping.

my wsgiy.py :

import os
import sys

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "servicesite.settings")
path='/home/ubuntu/my_code/'

if path not in sys.path:
  sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'my_code.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

What can be the reason for this error?

Redemptioner answered 26/1, 2021 at 15:46 Comment(6)
Your libapache2-mod-wsgi-py3 also needs to be built against python 3.7. This is why most orgs have moved to standalone WSGI servers like uWSGI and gunicorn.Sheenasheeny
@Sheenasheeny not sure i understand what it means built against python 3.7, i set my default python as python3.7 - type python will give me Python 3.7.9 so why python 3 packages are not installed to python 3.7 ?Redemptioner
The apache module needs to be built for the specific python version you are using. The debian package you are using is going to be built for the OS's default python version, not what you have changed to. You should be able to recompile the module using pip: github.com/GrahamDumpleton/mod_wsgi#installation-into-pythonSheenasheeny
@Sheenasheeny not sure i understand from the GitHub page how to recompile the module using pip can you please elaborate on this ? (maybe in an answer rather then in comment if possible )Redemptioner
@junior_software set the python3.7 priority to high using sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 first and then reboot the system and check the version of python. Install the django with sudo apt install python3-djangoFarmelo
did it as specified in the tutorialRedemptioner
M
3

make custom builds of Python and mod_wsgi ourselves.

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

## Finally:
sudo systemctl reload apache2

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

Mucoid answered 6/9, 2021 at 0:4 Comment(0)
O
5

you need to built python and then built wsgi since the default wsgi is built based on the is system python use this tutorial https://medium.com/@garethbjohnson/serve-python-3-7-with-mod-wsgi-on-ubuntu-16-d9c7ab79e03a

Othelia answered 7/2, 2021 at 18:36 Comment(0)
M
3

make custom builds of Python and mod_wsgi ourselves.

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

## Finally:
sudo systemctl reload apache2

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

Mucoid answered 6/9, 2021 at 0:4 Comment(0)
L
-1

First of all, it seems you messed up installing python and pip. if you have python3-pip installed then you should try sudo pip3 intall django and later check import Django from the python37 interpreter you are using.

another thing is using system python is not good way.

I suggest improve your knowledge following https://realpython.com/intro-to-pyenv/

also using apache with mod_wsgi is pretty old you could try https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

Lennalennard answered 1/2, 2021 at 18:27 Comment(2)
1. i set pip3 as alias to pip3 2. i need to use system interpreter and not pyenvRedemptioner
using system interpreter is not a good practice. good luck anywayLennalennard
M
-2

I had a similar error message, it turned out that SELinux enforcement is the culprit. You can try to turn off SELinux temporary to test.

setenforce 0

// run sestatus to confirm that either it's permissive or disabled
sestatus

If that fixes your issue either you can roll the dice and disable SELinux permanently or find the proper way to fix it.

Did you compile python manually? if that the case, it's a whole different ball game. You might have to recompile python again with a whole slew of flags to get it to work.

Mythology answered 1/2, 2021 at 1:23 Comment(2)
it did no work exceptions still there and home page is not upRedemptioner
Ubuntu doesn't use SELinux, at least out of the box.Damning

© 2022 - 2024 — McMap. All rights reserved.