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
To solve the error, install the module by running the pip install Django command. Open your terminal in your project's root directory and install the Django module. Copied! After you install the Django package, you should be able to import it and use it.
Django can be installed easily using pip . In the command prompt, execute the following command: pip install django . This will download and install Django. After the installation has completed, you can verify your Django installation by executing django-admin --version in the command prompt.
Simply type python -m django --version or type pip freeze to see all the versions of installed modules including Django.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With