【问题标题】:Django application mod_wsgi error with apacheDjango 应用程序 mod_wsgi 与 apache 错误
【发布时间】:2021-03-14 05:31:22
【问题描述】:

我在 AWS ec2 上使用 ubuntu 20.04 lts 托管 Django 应用程序。我可以在各种端口上使用 venv 运行该应用程序。我想将应用程序与 apache 绑定,以便我的用户可以仅在默认端口上使用公共 DNS 无端口访问我的应用程序。所以我已经安装了 apache2, mod_wsgi。 我的虚拟主机配置。

<VirtualHost *:80>
  ServerAdmin test@example.com
  DocumentRoot /var/www/html/app_folder

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
  
  Alias /static /var/www/html/app_folder/static

  <Directory /var/www/html/app_folder/static>
    Require all granted
  </Directory>

  Alias /static /var/www/html/app_folder/media

  <Directory /var/www/html/app_folder/media>
    Require all granted
  </Directory>


  <Directory /var/www/html/app_folder/main_app>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>

  WSGIDaemonProcess app_folder python-path=/var/www/html/app_folder python-home=/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py

  WSGIProcessGroup app_folder
  WSGIScriptAlias / /var/www/html/app_folder/main_app/wsgi.py

</VirtualHost>

所以在配置了这个虚拟主机后,我重新启动了我的 apache2,但我在公共 dns 上什么也没有。相反,我在 /var/log/apache2/error.log 上收到以下错误日志

[Wed Dec 02 08:50:05.967958 2020] [wsgi:warn] [pid 121300] (13)Permission denied: mod_wsgi (pid=121300): Unable to stat Python home /home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py. 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/.local/lib/python3.8/site-packages/django/__init__.py'
  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/.local/lib/python3.8/site-packages/django/__init__.py'
  sys.base_exec_prefix = '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py'
  sys.executable = '/usr/bin/python3'
  sys.prefix = '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py'
  sys.exec_prefix = '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py'
  sys.path = [
    '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py/lib/python38.zip',
    '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py/lib/python3.8',
    '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py/lib/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 0x00007f0e7fa1bc40 (most recent call first):
<no Python frame>
  • 那我该怎么办???
  • I need to upload the Django site and make it live with apache
  • 那么我怎样才能让服务器上线呢?
  • 如何仅使用 http://public_ip 查看我的网站?
  • 我已经阅读了很多博客,但我无法让网站与 apache 一起运行!!!
  • 请查看并提出解决方案。

【问题讨论】:

    标签: django apache ubuntu


    【解决方案1】:

    我认为您在虚拟主机配置中设置了错误的 python 路径。 这应该是这样的:

    WSGIDaemonProcess app_folder python-path=/home/ubuntu/.local/lib/python3.8/site-packages
    

    【讨论】:

    • 感谢您的回答,但它对我不起作用,它只是给了我错误。
    • 你是不是换了python home再试了一遍?
    • 我认为你可以跳过脚本中的python home。
    【解决方案2】:

    通过 apache 服务 python 或 Django 应用程序有时会变得很忙,但可以做到。我已经用 apahce 和 nginx 配置了 Django。好吧,当谈到 django 时,我认为我们应该使用 gunicorn 来服务 django 应用程序。我为此切换到了 nginx。我创建了一个服务文件,该文件通过 gunicorn 运行应用程序,3 个工作人员使用应用程序 virtualenv。下面是服务文件。

    [Unit]
    Description=Softopark gunicorn service by: Softopark.com
    After=network.target
    
    [Service]
    User=nginx
    Group=nginx
    WorkingDirectory=/usr/share/nginx/html/softopark_cms_2
    Environment="/home/ec2-user/Env/cms5-yYbXgITO/bin"
    ExecStart=/home/ec2-user/Env/cms5-yYbXgITO/bin/gunicorn --workers 3 --bind 0.0.0.0:8000 cms_project.wsgi
    
    [Install]
    WantedBy=multi-user.target
    

    像魅力一样工作

    【讨论】:

      【解决方案3】:

      我也尝过同样的苦涩经历。我猜这是 apache/mod_wsgi 的问题,它不在 python3.9 上,而是在 python3.8 上,这是 ubuntu 20.04 的默认 python 包。

      不幸的是,我没有什么聪明/容易的建议。我想,解决方案是针对 python 3.9 从源代码构建 mod_wsgi 模块。但是,这不是一个简单的过程,而且我自己从未做过,所以我无法真正确认它的有效性。

      您也可以查看this link

      【讨论】:

      • 感谢@pantelis-natsiavas 的回答,我正在使用 django 和 apache,但 apache 没有得到我的青睐,所以我切换到了 nginx。认为有时会出现一些问题,但我对 nginx 很满意。代理重定向和使用 mod_wsgi 很困难,需要时间。
      猜你喜欢
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 1970-01-01
      • 2014-05-21
      • 2017-02-18
      • 1970-01-01
      相关资源
      最近更新 更多