Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django / Pyodbc / Apache - Connection error when using Apache

I'm trying to connect to a SQL Server backend from a Django app using the django-pyodbc-azure package.

I'm able to connect and return data using the Django manage.py shell (both ORM and pyodbc queries) and development server (HttpResponse in a view function), but when I attempt to use Apache and mod_wsgi, I receive the error:

('08S01', '[08S01] [unixODBC][FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist (20009) (SQLDriverConnect)')

I'm using Red Hat Enterprise Linux 7, Python 3.3.5, Pyodbc 3.0.7, Django 1.7, Apache 2.4.6, Mod_WSGI 4.4.5, SQL Server 2014, FreeTDS 0.91 (using dsn-less), and Django-Pyodbc-Azure 1.2.0


Modwsgi.conf:

<VirtualHost *:80>
    WSGIDaemonProcess xxx.xxx.xxx.xxx python-path=/path/to/mysite.com:/path/to/virtualenv/lib/python3.3/site-packages processes=2 threads=15
    WSGIProcessGroup xxx.xxx.xxx.xxx

    WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py

    <Directory /path/to/mysite.com/mysite>
    <Files wsgi.py>
        Require all granted
    </Files>
    </Directory>
</VirtualHost>


wsgi.py:

"""
WSGI config for mysite project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()


In settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'HOST': 'hostname',
        'NAME': 'dbname',
        'USER': 'dbuser',
        'PASSWORD': 'dbpassword',
        'PORT': '1433',
        'OPTIONS': {
            'driver': 'FreeTDS',
            'host_is_server': True,
            'unicode_results': True,
            'extra_params': 'tds_version=7.4'
        }
    }
}


When using manage.py runserver I can view data in a browser.

From the interpreter I can also do:

In [1]: import pyodbc
In [2]: conn = pyodbc.connect('DRIVER=FreeTDS;SERVER=servername;PORT=1433;DATABASE=dbname;UID=dbuser;PWD=dbpassword')
In [3]: cursor = conn.cursor()
In [4]: cursor.execute("select top 5 test_names from test_table;").fetchone()
Out[4]: ('John', )


But for some reason the initial pyodbc database connection attempt fails when I try to use Apache.
Any help on this is much appreciated.

like image 793
clank Avatar asked Apr 27 '26 00:04

clank


1 Answers

Have you tried running

sudo setsebool -P httpd_can_network_connect=1

on the machine?

like image 139
Erez A. Korn Avatar answered Apr 28 '26 13:04

Erez A. Korn