9

Few days ago I decided to update python from version 2.7 to 3.7. This is my current setup:

Ubuntu 16.04
Python 3.7.7
Django 3.0.6
Apache/2.4.18

Using command python -m venv --system-site-packages /var/www/path/to/myenv I've created the virual environment, after activation of this environment I've created a new project. The path to the environment looks like this /var/www/path/to/myenv and the path to project looks like this /var/www/path/to/myenv/myproject. Configuration of myproject.conf looks like this:

<VirtualHost *:80>
    ServerName myproject.com
    ServerAlias www.myproject.com
    WSGIDaemonProcess myproject processes=2 threads=15 display-name=%{GROUP} python-home=/var/www/path/to/myenv python-path=/var/www/path/to/myenv/myproject
    WSGIProcessGroup candyhand

    WSGIScriptAlias /   /var/www/path/to/myenv/myproject/myproject/wsgi.py

    <Directory /var/www/path/to/myenv/myproject/myproject/>
    <Files wsgi.py>
        Require all granted
    </Files>
    </Directory>

    <Directory /var/www/path/to/myenv/myproject/>
        Require all granted
    </Directory>

    CustomLog /var/www/path/to/myenv/myproject/logs/apache_access.log combined
    ErrorLog /var/www/path/to/myenv/myproject/logs/apache_error.log

    Alias /static/ /var/www/path/to/myenv/myproject/static/
    <Directory /var/www/path/to/myenv/myproject/>
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>


    Alias /media/ /var/www/path/to/myenv/myproject/media/
    <Directory /var/www/path/to/myenv/myproject/>
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

But i've got error 500 from apache server. Here is the log of the apache server:

mod_wsgi (pid=9495): Target WSGI script '/var/www/path/to/myenv/myproject/myproject/wsgi.py' cannot be loaded as Python module.
[Wed May 20 16:25:08.145621 2020] [wsgi:error] [pid 9495]  mod_wsgi (pid=9495): Exception occurred processing WSGI script '/var/www/path/to/myenv/myproject/myproject/wsgi.py'.
[Wed May 20 16:25:08.145788 2020] [wsgi:error] [pid 9495]  Traceback (most recent call last):
[Wed May 20 16:25:08.145864 2020] [wsgi:error] [pid 9495]   File "/var/www/path/to/myenv/myproject/myproject/wsgi.py", line 12, in <module>
[Wed May 20 16:25:08.145885 2020] [wsgi:error] [pid 9495]      from django.core.wsgi import get_wsgi_application
[Wed May 20 16:25:08.145945 2020] [wsgi:error] [pid 9495]  ImportError: No module named 'django'

I configured VirtualHost according this documentation, but maybe I made a mistake somewhere, thank you for your advice.

P.S. python manage.py runserver command runs well

remram
  • 4,805
  • 1
  • 29
  • 42
Zagorodniy Olexiy
  • 2,132
  • 3
  • 22
  • 47
  • Try using python 3.8 or 3.6, as 3.7 is causing some problems with django – Paaksing May 20 '20 at 15:22
  • did you replace the python 2 mod wsgi with the python 3 version? see [here](https://stackoverflow.com/a/31564325/1233289) and [here](https://stackoverflow.com/a/41006418/1233289) – AdonisN May 20 '20 at 16:00
  • @AdonisN yes, my mod_wsgi updated, here is version mod_wsgi/4.3.0 – Zagorodniy Olexiy May 20 '20 at 16:27
  • try explicitly specifying the virtual env in your wsgi and see if you get a different error. You do that by adding the following to the top of the wsgi file: `import sys` `sys.path.insert(0, '/var/www/path/to/myenv/lib/python3.7/site-packages/')` you can also add this to the wsgi file which will print out what paths it's seeing `print(sys.path)` in the logs – AdonisN May 20 '20 at 16:31
  • i've just tried and an error the same – Zagorodniy Olexiy May 20 '20 at 16:45
  • what was the output of the print(sys.path)? – AdonisN May 20 '20 at 20:06
  • @AdonisN `['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/var/www/path/to/myenv/lib/python3.7/site-packages/', '/root/.local/lib/python3.7/site-packages', '/usr/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']` – Zagorodniy Olexiy May 21 '20 at 07:17
  • 1
    can you confirm that you have django installed under /var/www/path/to/myenv/lib/python3.7/site-packages/. check if a django folder exists in there – AdonisN May 21 '20 at 12:12
  • 1. Do you really need Python 3.7 instead current actual 3.8 version? 2. Do you really need Apache instead of Nginx? It is better solution then use Apache. – zo0M Jun 14 '20 at 23:08

3 Answers3

1

The issue is most likely that python -m venv does not generate an activate_this.py within your virtualenv, please have a look at the documentation at https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html#daemon-mode-multiple-applications

"When needing to activate the Python virtual environment from within the WSGI script file as described, it is preferred that you be using the either virtualenv or virtualenvwrapper to create the Python virtual environment. This is because they both provide the activate_this.py script file which does all the work of setting up sys.path. When you use either pyvenv or python -m venv with Python 3, no such activation script is provided."

EDIT

Just figured out that mod_wsgi v4.6.1 seems to handle the virtual environment created by python -m venv properly, but mod_wsgi has to use the exact same python version as your virtualenv (mod_wsgi does not take the python interpreter from virtualenv, just check the python version within your wsgi.py to ensure mod_wsgi is using the correct one). If its the wrong interpreter version you must reinstall mod_wsgi after updating your global python package to the correct version number.

NiMeDia
  • 995
  • 1
  • 15
  • 27
0

Please ckeck if you have inserted those ligne in apache2.conf:

WSGIPythonPath /usr/local/lib/python3.7/dist-packages
WSGILazyInitialization On
WSGIApplicationGroup %{GLOBAL}

Can you please share wsgi.py + the beginning of the apache log file

Houda
  • 671
  • 6
  • 16
0

Add below outside your VirtualHost: WSGIPythonHome /var/www/path/to/myenv

Beck
  • 1