Tuesday, April 12, 2022

[SOLVED] wsgi_mod fails to host Django with python3.10 on CentOs

Issue

My goal is to host Django app on CentOs 7 with python3.10

I've manage to download and configure Python, sqlite to work with manage.py runserver but it dosn't work when i try to host it with apache.

Apache throws error: ImportError: /usr/local/lib/python3.10/lib-dynload/_sqlite3.cpython-310-x86_64-linux-gnu.so: undefined symbol: sqlite3_trace_v2

Installed Versions

  • System: CentOs 7
  • Python: 3.10.4
  • sqlite: 3.28.0
  • mod_wsgi: 4.9.0
  • Apache: 2.4.6

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>

Full Traceback:

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

Solution

I didn't found any solution for this problem but I have downgraded python to 3.9.12 and it works now. Steps to reproduce to make it work with 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 ...'

    


Answered By - bazzuk123
Answer Checked By - Marie Seifert (WPSolving Admin)