-2

I am trying to get my Apache Web Server to run my Django page using wsgi.py. My virtual environment is at /var/www/myapp/myapp_root/myapp. When I activate the virtual environment I see my user home directory being used. I tried the following wsgi.py changes:

"""
WSGI config for trackx project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/
"""

import os,sys

sys.path.append('/home/myuserid/.local/share/virtualenvs/lib/python3.9/site-packages')

from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trackx.settings')

application = get_wsgi_application()

When I try to start the server - it gives me the following repeating error (over and over):

Python path configuration:
  PYTHONHOME = '/home/myuser/.local/share/virtualenvs/myapp-nygxcPTP/lib/python3.9'
  PYTHONPATH = (not set)
  program name = 'python3'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/usr/local/bin/python3'
  sys.base_prefix = '/home/myuser/.local/share/virtualenvs/myapp-nygxcPTP/lib/python3.9'
  sys.base_exec_prefix = '/home/myuser/.local/share/virtualenvs/myapp-nygxcPTP/lib/python3.9'
  sys.executable = '/usr/local/bin/python3'
  sys.prefix = '/home/myuser/.local/share/virtualenvs/myapp-nygxcPTP/lib/python3.9'
  sys.exec_prefix = '/home/myuser/.local/share/virtualenvs/myapp-nygxcPTP/lib/python3.9'
  sys.path = [
    '/home/my-user/.local/share/virtualenvs/myapp-nygxcPTP/lib/python3.9/lib64/python38.zip',
    '/home/myuser/.local/share/virtualenvs/myapp-nygxcPTP/lib/python3.9/lib64/python3.8',
    '/home/myuser/.local/share/virtualenvs/myapp-nygxcPTP/lib/python3.9/lib64/python3.8/lib-dynload',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f52bfd07880 (most recent call first):
<no Python frame>

My WSGI settings in the http.conf file look like this...

WSGIPythonHome /var/www/myapp/myapp_root/venv

WSGIPythonPath /var/www/myapp/myapp_root/venv/lib/python3.9/site-packages

WSGIDaemonProcess myapp python-home=/home/myuser/.local/share/virtualenvs/myapp-nygxcPTP/lib/python3.9

WSGIProcessGroup myapp
WSGIApplicationGroup %{GLOBAL}

WSGIScriptAlias / /var/www/myapp/myapp_root/myapp/wsgi.py

<Directory /var/www/myapp/myapp_root/myapp>
  <Files wsgi.py>
  Order deny,allow
  Require all granted
  </Files>
</Directory>

Anyone run into this error - any ideas would be great. Have tried changing some of the settings to my actual .local directories in the http configuration, but that seems to create permission errors, and then I still get the same repeating error results.

Todd B
  • 83
  • 1
  • 8
  • Does this answer your question? [Fatal Python error: init\_fs\_encoding: failed to get the Python codec of the filesystem encoding, when trying to start uwsgi](https://stackoverflow.com/questions/65184937/fatal-python-error-init-fs-encoding-failed-to-get-the-python-codec-of-the-file) – yedpodtrzitko Sep 01 '21 at 22:41
  • I saw this - If I were to try and unset the environment variables, where would I do that. Apache is running wsgi.py, would I unset them in the wsgi.py code? – Todd B Sep 01 '21 at 22:58

1 Answers1

0

I found that my paths were not right in wsgi.py.

When I added my root directory, then the setting files was accessed correctly and it loaded wsgi.py successfully.

So wsgi.py now looks like:

import os,sys

sys.path.append('/var/www/myapp/myapp_root')
sys.path.append('/usr/local/lib')
sys.path.append('/home/myuser/.local/share/virtualenvs/myapp-nygxcPTP/lib/python3.9/site-packages/MySQLdb')

sys.path.append('/home/myuser/.local/share/virtualenvs/myapp-nygxcPTP/lib/python3.9/site-packages')

from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')

application = get_wsgi_application()
Todd B
  • 83
  • 1
  • 8