wsgi_mod 无法在 CentOs 上使用 python3.10 托管 Django

wsgi_mod fails to host Django with python3.10 on CentOs

我的目标是使用 python3.10

在 CentOs 7 上托管 Django 应用程序

我已经成功下载并配置了 Python、sqlite 以与 manage.py runserver 一起使用 但是当我尝试用 apache 托管它时它不起作用。

Apache 抛出错误:ImportError: /usr/local/lib/python3.10/lib-dynload/_sqlite3.cpython-310-x86_64-linux-gnu.so: undefined symbol: sqlite3_trace_v2

已安装的版本

Apache.conf

WSGIScriptAlias / /var/www/portal/portal/wsgi.py
WSGIPythonPath /var/www/portal/:/var/www/venv/lib/python3.10/site-packages
<VirtualHost *:80>
  ServerName 192.168.1.25
  Alias /static /var/www/portal/static/
  DocumentRoot /var/www/portal

  <Directory /opt/portal/static>
    AllowOverride All
    Require all granted
    Allow from all
  </Directory>

  <Directory /var/www/portal/portal>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>
</VirtualHost>

完整追溯:

Traceback (most recent call last):
   File "/var/www/portal/portal/wsgi.py", line 22, in <module>
     application = get_wsgi_application()
   File "/var/www/venv/lib/python3.10/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
     django.setup(set_prefix=False)
   File "/var/www/venv/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
     apps.populate(settings.INSTALLED_APPS)
   File "/var/www/venv/lib/python3.10/site-packages/django/apps/registry.py", line 116, in populate
     app_config.import_models()
   File "/var/www/venv/lib/python3.10/site-packages/django/apps/config.py", line 304, in import_models
     self.models_module = import_module(models_module_name)
   File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
     return _bootstrap._gcd_import(name[level:], package, level)
   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
   File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
   File "<frozen importlib._bootstrap_external>", line 883, in exec_module
   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
   File "/var/www/venv/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in <module>
     from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
   File "/var/www/venv/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
     class AbstractBaseUser(models.Model):
   File "/var/www/venv/lib/python3.10/site-packages/django/db/models/base.py", line 141, in __new__
     new_class.add_to_class("_meta", Options(meta, app_label))
   File "/var/www/venv/lib/python3.10/site-packages/django/db/models/base.py", line 369, in add_to_class
     value.contribute_to_class(cls, name)
   File "/var/www/venv/lib/python3.10/site-packages/django/db/models/options.py", line 235, in contribute_to_class
     self.db_table, connection.ops.max_name_length()
   File "/var/www/venv/lib/python3.10/site-packages/django/utils/connection.py", line 15, in __getattr__
     return getattr(self._connections[self._alias], item)
   File "/var/www/venv/lib/python3.10/site-packages/django/utils/connection.py", line 62, in __getitem__
     conn = self.create_connection(alias)
   File "/var/www/venv/lib/python3.10/site-packages/django/db/utils.py", line 208, in create_connection
     backend = load_backend(db["ENGINE"])
   File "/var/www/venv/lib/python3.10/site-packages/django/db/utils.py", line 113, in load_backend
     return import_module("%s.base" % backend_name)
   File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
     return _bootstrap._gcd_import(name[level:], package, level)
   File "/var/www/venv/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py", line 15, in <module>
     from sqlite3 import dbapi2 as Database
   File "/usr/local/lib/python3.10/sqlite3/__init__.py", line 57, in <module>
     from sqlite3.dbapi2 import *
   File "/usr/local/lib/python3.10/sqlite3/dbapi2.py", line 27, in <module>
     from _sqlite3 import *
 ImportError: /usr/local/lib/python3.10/lib-dynload/_sqlite3.cpython-310-x86_64-linux-gnu.so: undefined symbol: sqlite3_trace_v2

我没有找到解决此问题的方法,但我已将 python 降级到 3.9.12,现在可以使用了。 重现步骤以使其适用于 python 3.9.12

INSTALLATION REQUIRED PACKAGES:
yum update 
yum groupinstall "Development Tools" -y
yum install wget httpd httpd-devel openssl-devel libffi-devel bzip2-devel -y
wget https://www.python.org/ftp/python/3.9.12/Python-3.9.12.tgz
tar xvf Python-3.9.12.tgz
wget https://www.sqlite.org/2019/sqlite-autoconf-3280000.tar.gz
tar zxvf sqlite-autoconf-3280000.tar.gz


SQLITE INSTALLATION:
cd ~/sqlite-autoconf-3280000
./configure
make
make install


PYTHON INSTALLATION:
cd ~/Python-3.9.12
./configure --enable-loadable-sqlite-extensions --enable-shared --with-ssl


SET UP LD_LIBRARY_PATH:
vi ~/.bashrc 
    export LD_LIBRARY_PATH=/usr/local/lib
source ~/.bashrc


CREATE VIRTUAL ENV: 
cd /opt
python3.9 -m venv venv


MOD_WSGI INSTALLATION FOR CURRENT VERSION OF PYTHONA:
source ./venv/bin/activate
pip install mod_wsgi
mod_wsgi-express install-module > /etc/httpd/conf.modules.d/02-wsgi.conf


UPDATE SQLITE (TODO: find better solution. Now sqlite verison works with 
installed django, after updating django you have to manually change it again, 
Right now can t find better solution):
source /opt/venv/bin/activate
pip install pysqlite3
pip install pysqlite3-binary
vi /opt/venv/lib/python3.9/site-modules/django/db/backends/sqlite3/base.py
    change 'from sqlite3 import ...' to 'from pysqlite3 import ...'