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
3
Answers
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.Please ckeck if you have inserted those ligne in apache2.conf:
Can you please share wsgi.py + the beginning of the apache log file
Add below outside your VirtualHost:
WSGIPythonHome /var/www/path/to/myenv