1

On my local environment, with Python 2.7.10, my Django project seems to run perfectly well using .manage.py runserver. But when I tried to deploy the project to my Debian Wheezy server using the same version of python 2.7.10, it encountered 500 internal server error. Upon checking my apache log, I found the error to be alternating between these two:

[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201] mod_wsgi (pid=1973): Target WSGI script '/var/www/proj/proj/proj_wsgi.py' cannot be loaded as Python module.
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201] mod_wsgi (pid=1973): Exception occurred processing WSGI script '/var/www/proj/proj/proj_wsgi.py'.
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201] Traceback (most recent call last):
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]   File "/var/www/proj/proj/proj_wsgi.py", line 21, in <module>
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]     application = get_wsgi_application()
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]     django.setup()
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]     apps.populate(settings.INSTALLED_APPS)
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/apps/registry.py", line 78, in populate
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201]     raise RuntimeError("populate() isn't reentrant")
[Fri Oct 23 23:31:41 2015] [error] [client 176.10.99.201] RuntimeError: populate() isn't reentrant

AND this one:

[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201] mod_wsgi (pid=1973): Target WSGI script '/var/www/proj/proj/proj_wsgi.py' cannot be loaded as Python module.
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201] mod_wsgi (pid=1973): Exception occurred processing WSGI script '/var/www/proj/proj/proj_wsgi.py'.
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201] Traceback (most recent call last):
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/proj/proj/proj_wsgi.py", line 21, in <module>
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     application = get_wsgi_application()
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     django.setup()
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     apps.populate(settings.INSTALLED_APPS)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     app_config.import_models(all_models)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     self.models_module = import_module(models_module_name)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     __import__(name)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/contrib/auth/models.py", line 41, in <module>
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     class Permission(models.Model):
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/models/base.py", line 139, in __new__
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     new_class.add_to_class('_meta', Options(meta, **kwargs))
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/models/base.py", line 324, in add_to_class
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     value.contribute_to_class(cls, name)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/models/options.py", line 250, in contribute_to_class
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     return getattr(connections[DEFAULT_DB_ALIAS], item)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/utils.py", line 240, in __getitem__
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     backend = load_backend(db['ENGINE'])
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/utils.py", line 111, in load_backend
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     return import_module('%s.base' % backend_name)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     __import__(name)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]   File "/var/www/ven/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 24, in <module>
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201]     raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
[Fri Oct 23 23:30:52 2015] [error] [client 176.10.99.201] ImproperlyConfigured: Error loading psycopg2 module: /var/www/ven/lib/python2.7/site-packages/psycopg2/_psycopg.so: undefined symbol: PyUnicodeUCS2_AsUTF8String

I have tried many solutions, such as all these links below via Google but still to no avail. Django stops working with RuntimeError: populate() isn't reentrant Django populate() isn't reentrant

I tried moving to python 2.7.3 and the django project managed to work but I need some encoding features in pickle contained in the 2.7.10 version so I need to use that.

I have even tried reinstalling a brand new Django 1.8.5 project from scratch on python 2.7.10 but it did not work, giving out the same errors.

My proj_wgsi.py is:

    import os
    import sys
    import site
    from django.core.wsgi import get_wsgi_application

    # Add the site-packages of the chosen virtualenv to work with
    site.addsitedir('/var/www/ven/lib/python2.7/site-packages')

    # Add the app's directory to the PYTHONPATH
    sys.path.append('/var/www/proj')
    sys.path.append('/var/www/proj/proj')


    # Activate your virtual env
    activate_env=os.path.expanduser('/var/www/ven/bin/activate_this.py')
    execfile(activate_env, dict(__file__=activate_env))

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


    application = get_wsgi_application()

My virtual host conf in apache in /etc/apache2/sites-enabled/000-default is

<VirtualHost *:80>
    ServerName 128.133.218.444
    ServerAdmin webmaster@localhost
    ServerAlias 128.133.218.444
    WSGIDaemonProcess 128.133.218.444 python-path="/var/www/proj:/var/www/ven/lib/python2.7/site-packages"
    WSGIProcessGroup 128.199.218.180
    WSGIScriptAlias / /var/www/proj/proj/proj_wsgi.py process-group=128.199.218.180
    WSGIPassAuthorization On
    DocumentRoot /var/www/proj

    #<Directory />
    #   Options FollowSymLinks
    #   AllowOverride None
    #</Directory>
    #<Directory /var/www/>
    #   Options Indexes FollowSymLinks MultiViews
    #   AllowOverride None
    #   Order allow,deny
    #   allow from all
    #</Directory>
    <Directory /var/www/proj>
        Order allow,deny
        Allow from all
    </Directory>
    <Directory /var/www/proj/proj/static>
        Order deny,allow
        Allow from all
    </Directory>
    <Directory /var/www/proj/proj/media>
            Order deny,allow
            Allow from all
    </Directory>
    <Directory /var/www/proj/proj>
        <Files wsgi.py>
            Order allow,deny
            allow from all
        </Files>
    </Directory>

    #ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    #<Directory "/usr/lib/cgi-bin">
    #   AllowOverride None
    #   Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    #   Order allow,deny
    #   Allow from all
    #</Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    #ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I have been trying to solve this issue for a couple of days so any help will be highly appreciated.Thank you!

Community
  • 1
  • 1
  • 1
    I've seen the `RuntimeError: populate() isn't reentrant` error happen when database drivers like psycopg2 are missing. – Brandon Taylor Oct 23 '15 at 19:31
  • @Brandon I did install psycopg2. This error doesn't happen in python 2.7.3 though. It only happened after I upgraded python to 2.7.10. – user2970242 Oct 23 '15 at 19:49
  • What I would do in that case is start commenting out the exception handlers in `registry.py` until you get to the actual problem. – Brandon Taylor Oct 23 '15 at 19:51
  • In the past when I've had issues like this, it's an error in my code that I didn't anticipate, which I didn't see until I ran `runserver` in place on the production box to test the deployment. Are you able to do that? – trpt4him Oct 23 '15 at 19:51
  • I'd comment out this: https://github.com/alsoicode/django/blob/master/django/apps/registry.py#L78 and any exception handler after that until you can find out why the real exception is being swallowed. – Brandon Taylor Oct 23 '15 at 19:53
  • I wonder if there's some library in the chain doing the wrong version compare... – Wayne Werner Oct 23 '15 at 20:55
  • @Brandon Thank you for your guide. I did this and managed to solve the issue. =) – user2970242 Oct 25 '15 at 05:48

3 Answers3

0

Try to recreate your virtualenv.

Use it if already not using.

sudo apt-get install python-virtualenv

This looks like an error associated with postgres-python installation

virtualenv /some/dir/path
source /some/dir/path/bin/activate

Prepare django and psycopg2 and whatever other libraries you use

pip install psycopg2 django # possibly other libraries

This http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/ link as details on how to associate your virtualenv with apache. I would suggest you to try uwsgi as well. It plays really nice with DJango.

iankit
  • 8,806
  • 10
  • 50
  • 56
  • Thank you for your answer. I did as what you said and updated the question with an answer. Your approach worked! – user2970242 Oct 25 '15 at 05:50
0

The "undefined symbol" error suggests that something might be wrong with your psycopg2 install. I suggest you uninstall and re-install it and make sure it is not using any pip cache or anything but does a clean build.

Mad Wombat
  • 14,490
  • 14
  • 73
  • 109
0

Thank you very much everyone for helping to guide me solve this challenge. I have managed to solve the problem. First, for this error:

ImproperlyConfigured: Error loading psycopg2 module: /var/www/ven/lib/python2.7/site-packages/psycopg2/_psycopg.so: undefined symbol: PyUnicodeUCS2_AsUTF8String

I recompiled the python v2.7.10 with ./configure --enable-unicode=ucs4. And then I recreated a new virtual environment with the recompiled version.

Second error in regards to the module, I found out that some libraries as Wayne Werner said were of the wrong versions and were unable to run properly to be compatible with the django project. I just made sure I upgraded libraries(pip install -U [libraries])

Thank you everyone again!