user3475724
user3475724

Reputation:

502 error django + nginx + gunicorn - connect() recv()

I've set up a Digital Ocean one-click app (django + nginx + gunicorn are installed and setup https://www.digitalocean.com/community/tutorials/how-to-use-the-django-one-click-install-image). Defaults worked for me, but after I tried to apply code changes via service gunicorn restart I received a 502 error with the following nginx error log line:

 connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client: 178.136.215.70, server: _, request: "GET / HTTP/1.1",upstream: "http://unix:/home/django/gunicorn.socket:/"..

I have looked into similar issues and found that often it's caused by a mistake in ALLOWED_HOSTS, everything is right there, but I also tried to replace 'ip' or 'www.address.com' with single '*' and got a different error:

recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 178.136.215.70, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "192.241.176.184"

EDIT: ok, now it's easier, gunicorn error log is telling us that there is an import error with rest_framework, But it's installed and it's present in Installed app (and spelled right), I'v checked like thousand times so what's really wrong with all that?

     File "/usr/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    django.setup()
  File "/usr/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/lib/python2.7/dist-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/usr/lib/python2.7/dist-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/usr/lib/python2.7/dist-packages/gevent/builtins.py", line 93, in __import__
    result = _import(*args, **kwargs)
  File "/home/django/django_project/blog/models.py", line 5, in <module>
    from rest_framework import serializers
  File "/usr/lib/python2.7/dist-packages/gevent/builtins.py", line 93, in __import__
    result = _import(*args, **kwargs)
ImportError: No module named rest_framework

(the only issue for now)EDIT2: I have reinstalled the app folder, checked again everything. Now it's telling that ImportError: No module named django_ajax. Python 2.7 , Django 1.8, Gunicorn 19.4.5 (https://github.com/yceruto/django-ajax) despite everything seems to be correct enter image description here

EDIT: Résponse of pip freeze:

    Django==1.8
django-filter==1.0.1
-e git://github.com/yceruto/django-ajax@9c122e68f8e7ca92333a1533fa464ee6da0f65c5#egg=djangoajax
djangorestframework==3.5.3
gunicorn==19.6.0
Markdown==2.6.7
netifaces==0.10.5
pkg-resources==0.0.0
psycopg2==2.6.2

Upvotes: 4

Views: 997

Answers (2)

Rahul Reddy Vemireddy
Rahul Reddy Vemireddy

Reputation: 1155

When you do Sudo, the package is installed for the root user and it installed globally. Check this link to understand how sudo pip works. Now that your package is installed globally, it is working.

I still have doubts about which Python is it using. In the error messages, it was trying to access packages under global dist-packages directory. Not from the virtual environment. So, your gunicorn send to be linked to the Python installed globally not the virtual environment one.

Ideally, the errors occurred should point to the dist-packages under the /path/to/virtual_env/virtual_env/lib/python2.7/dist-packages, as this is the python & the library environment that want to use. That seems to be the issue.

If this is the case, can you do the following

DJANGODIR=/path/to/django-project/ 
DJANGO_SETTINGS_MODULE=django-project.settings 

Activate the virtual environment

cd $DJANGODIR
source /path/to/virtualenv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

I think this should solve everything... Please try it out and lemme know if I missed out something

Upvotes: 2

user3475724
user3475724

Reputation:

I am not sure, whether it shall be considered as answer, because I don't understand why exactly it works this way. If someone explains properly I'll mark one's answer as right one . So, this line worked for me

  sudo -H pip install djangorestframework

Despite packages were reported to be installed they were not.And despite they were visible after pip freeze. So I don't know real reason why just pip install didn't work but seemed to work instead(?), although all permissions are correct, and I tried from both root and other username with same privileges.

Upvotes: 0

Related Questions