0

I tried to set up a Django app with Apache and mod_wsgi, but ran into a problem that I have no ideas where is the cause. The app works fine with the command "python manage.py runserver", but when I tried to run it with Apache, I got the following errors in the Apache error log file.

Current thread 0x00007fb4880ad940 (most recent call first):
<no Python frame>
Python path configuration:
  PYTHONHOME = '/data/anaconda3/envs/partsdb'
  PYTHONPATH = (not set)
  program name = 'python3'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/usr/bin/python3'
  sys.base_prefix = '/data/anaconda3/envs/partsdb'
  sys.base_exec_prefix = '/data/anaconda3/envs/partsdb'
  sys.platlibdir = 'lib64'
  sys.executable = '/usr/bin/python3'
  sys.prefix = '/data/anaconda3/envs/partsdb'
  sys.exec_prefix = '/data/anaconda3/envs/partsdb'
  sys.path = [
    '/data/anaconda3/envs/partsdb/lib64/python38.zip',
    '/data/anaconda3/envs/partsdb/lib64/python3.8',
    '/data/anaconda3/envs/partsdb/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'

I have the following lines in an Apache conf file.

WSGIPythonHome /data/anaconda3/envs/partsdb
WSGIPythonPath /data/partsdb/partsdb
WSGIScriptAlias / /data/partsdb/partsdb/wsgi.py
<Directory "/data/partsdb/partsdb">
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

I also replaced the following two lines in the Apache conf file

WSGIPythonHome /data/anaconda3/envs/partsdb
WSGIPythonPath /data/partsdb/partsdb

with the following two lines, but got the same errors.

WSGIDaemonProcess partsdb python-path=/data/partsdb/partsdb python-home=/data/anaconda3/envs/hla3db_venv
WSGIProcessGroup partsdb

The file /data/partsdb/partsdb/wsgi.py just contains the following lines of codes.

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'partsdb.settings')

application = get_wsgi_application()

Upon a brief debugging, I found out that the errors were from this line in wsgi.py.

from django.core.wsgi import get_wsgi_application

My machine's OS is redhat 8, and the Apache version is 2.4.37. Thanks for any info/hints.

Shiping
  • 1,203
  • 2
  • 11
  • 21

1 Answers1

0

Try the following code:

WSGIDaemonProcess partsdb python-path=/data/partsdb/partsdb python-home=/data/anaconda3/envs/hla3db_venv
WSGIProcessGroup partsdb
WSGIApplicationGroup %{GLOBAL}

WSGIPythonHome /data/anaconda3/envs/partsdb
WSGIPythonPath /data/partsdb/partsdb

WSGIScriptAlias / /data/partsdb/partsdb/wsgi.py
<Directory "/data/partsdb/partsdb">
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

Alias /static /path/to/directory/defined/as/STATIC_ROOT/
# The first part is defined by STATIC_URL in settings.py
<Directory /path/to/directory/defined/as/STATIC_ROOT>
    Require all granted
</Directory>

<VirtualHost *:80>
    ServerName mysite.app
    ServerAdmin webmaster@localhost
    # You do not need to specify DocumentRoot
    # I have seen advice not to do so
    ErrorLog ${APACHE_LOG_DIR}error.log
    CustomLog ${APACE_LOG_DIR}access.log combined

</VirtualHost>

Please note I used this for a single app running in daemon mode as suggested in the documentation, on a Ubuntu server. Check the permissions on the folders, in my case I had to change permissions to allow apache to read and write to the folders the site was in.

You can also look at the answer Graham Dumpleton gave a few years back here: Apache with virtualenv and mod_wsgi : ImportError : No module named 'django'