简体   繁体   中英

Django mod_wsgi Apache error 403 Forbidden on Ubuntu 22.04

Django mod_wsgi on Apache application works fine in Ubuntu 20.04 and previous versions with the configuration mentioned below, but when I do the same configuration in Ubuntu 22.04 it gets 403 Forbidden error . and Permission denied: mod_wsgi in the error log.

I tried changing permission on all files it's doesn't work,

Apache config:

<VirtualHost *:80>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <Directory /home/ubuntu/mydjango/mydjango>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

        alias /static /home/ubuntu/mydjango/static
        <Directory /home/ubuntu/mydjango/static>
                Require all granted
        </Directory>

        WSGIDaemonProcess myapp python-home=/home/ubuntu/mydjango/venv python-path=/home/ubuntu/mydjango
        WSGIProcessGroup myapp
        WSGIScriptAlias / /home/ubuntu/mydjango/mydjango/wsgi.py
</VirtualHost>

Apache Error Log:

Current thread 0x00007fb606719780 (most recent call first):
  <no Python frame>
[Sun Jun 05 05:59:07.594727 2022] [wsgi:warn] [pid 37044:tid 140419768883072] (13)Permission denied: mod_wsgi (pid=37044): Unable to stat Python home /home/ubuntu/mydjango/venv. Python interpreter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
Python path configuration:
  PYTHONHOME = '/home/ubuntu/mydjango/venv'
  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 = '/home/ubuntu/mydjango/venv'
  sys.base_exec_prefix = '/home/ubuntu/mydjango/venv'
  sys.platlibdir = 'lib'
  sys.executable = '/usr/bin/python3'
  sys.prefix = '/home/ubuntu/mydjango/venv'
  sys.exec_prefix = '/home/ubuntu/mydjango/venv'
  sys.path = [
    '/home/ubuntu/mydjango/venv/lib/python310.zip',
    '/home/ubuntu/mydjango/venv/lib/python3.10',
    '/home/ubuntu/mydjango/venv/lib/python3.10/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'

By default Apache2 run as www-data user that does not have permission (read/execute) to home directories. So you should set the correct permissions also on the home directory:

sudo chmod 755 /home/ubuntu

Or you can put the project into eg /var/www that is readable/executable by 'others' by default, so Apache2 can run the project from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM