user3624702
user3624702

Reputation: 123

How To Use PostgreSQL with a Django Application on CentOS 7 with Python 3.4 and Apache2

I'm running a CentOS 7 server and I managed to get a Django site up and running by doing the following:

The previous steps successfully installed a new Django 1.8 project and I was able to view the It worked! page in my browser.

The problems start when I modify the settings.py file to use PostgreSQL 9.4 as my project's database. I change the default DATABASES dictionary to the following:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'myproject',
        'USER': 'myprojectuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

When I press refresh in my browser, I then get an Internal Server Error from Apache and the error_log says:

[] mod_wsgi (pid=1049): Target WSGI script '/var/www/django/projects/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[] mod_wsgi (pid=1049): Exception occurred processing WSGI script '/var/www/django/projects/mysite/mysite/wsgi.py'.
[] Traceback (most recent call last):
[]  File "/var/www/django/projects/mysite/mysite/wsgi.py", line 16, in <module>
[]     application = get_wsgi_application()
[]   File "/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[]     django.setup()
[]   File "/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
[]     apps.populate(settings.INSTALLED_APPS)
[]   File "/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages/django/apps/registry.py", line 78, in populate
[]     raise RuntimeError("populate() isn't reentrant")
[] RuntimeError: populate() isn't reentrant
[] mod_wsgi (pid=1049): Target WSGI script '/var/www/django/projects/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[] mod_wsgi (pid=1049): Exception occurred processing WSGI script '/var/www/django/projects/mysite/mysite/wsgi.py'.
[] Traceback (most recent call last):
[]   File "/var/www/django/projects/mysite/mysite/wsgi.py", line 16, in <module>
[]     application = get_wsgi_application()
[]   File "/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[]     django.setup()
[]   File "/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
[]     apps.populate(settings.INSTALLED_APPS)
[]   File "/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages/django/apps/registry.py", line 78, in populate
[]     raise RuntimeError("populate() isn't reentrant")
[] RuntimeError: populate() isn't reentrant

I've tried everything I can think of to resolve it. I think the problem is specific to the use of Python 3.4 and maybe not having the correct version of mod_wsgi to work with it.

I've tried installing mod_wsgi via pip but I'm not sure how to get Apache to use that version and not the system wide version. That may not even be what's causing the problem.

I've tried nearly every Q&A on this site with no luck so any help will be appreciated,

Thanks.

EDIT

Here's my VirtualHost configuration for Apache2:

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com

DocumentRoot /var/www/django/projects/mysite/mysite

WSGIScriptAlias / /var/www/django/projects/mysite/mysite/wsgi.py

WSGIDaemonProcess example.com python-path=/var/www/django/projects/mysite:/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages

WSGIProcessGroup example.com

  <Directory /var/www/django/projects/mysite/mysite>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>
</VirtualHost>

Upvotes: 1

Views: 521

Answers (1)

user3624702
user3624702

Reputation: 123

The problem was that the version of mod_wsgi installed by yum install mod_wsgi was not compatible with Python 3.4.

The solution is to install mod_wsgi via pip3.4 install mod_wsgi then run the command mod_wsgi-express install-module. Make sure both of the previous commands are run outside of your virtual environment.

You will then have to edit the file /etc/httpd/conf.d/wsgi.conf to reflect your set up.

My wsgi.conf file looks similar to this:

LoadModule wsgi_module /etc/httpd/modules/mod_wsgi-py34.cpython-34m.so

WSGIPythonHome /var/www/venv
WSGIDaemonProcess apache user=apache group=apache
WSGIProcessGroup apache
WSGISocketPrefix /var/run/wsgi

Alias /apps /var/www/apps/

<Directory /var/www/apps/>

  Options ExecCGI MultiViews Indexes
  MultiViewsMatch Handlers

  AddHandler wsgi-script .py
  AddHandler wsgi-script .wsgi

  DirectoryIndex index.html index.php index.py app.wsgi

  Order allow,deny
  Allow from all

</Directory>

Reference: https://gist.github.com/jmorton/558f7079ed2159156277 (Thanks Paulo Scardine)

Upvotes: 1

Related Questions