The dreaded 502 error

Posted March 1, 2017 5.3k views
NginxPythonDjangoDigitalOcean 1-Click Apps Marketplace

I use Pycharm while creating a website using Django/python. The website works as expected on localhost through Pycharm, so I decided to try and move it to a webserver. I followed this tutorial multiple times but still get the 502 error.

Ngnix error reads:

2017/03/01 04:53:33 [error] 2232#2232: *48 connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client: *client IP*, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "*host IP*"

and Gunicorn error reads:

root@ueba:/home/django/django_project# service gunicorn status
● gunicorn.service - Gunicorn daemon for Django Project
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset:
   Active: inactive (dead) (Result: exit-code) since Wed 2017-03-01 04:42:29 UTC
  Process: 23288 ExecStart=/usr/bin/gunicorn --name=django_project --pythonpath=
 Main PID: 23288 (code=exited, status=1/FAILURE)

Mar 01 04:42:29 ueba systemd[1]: gunicorn.service: Unit entered failed state.
Mar 01 04:42:29 ueba systemd[1]: gunicorn.service: Failed with result 'exit-code
Mar 01 04:42:29 ueba systemd[1]: gunicorn.service: Service hold-off time over, s
Mar 01 04:42:29 ueba systemd[1]: Stopped Gunicorn daemon for Django Project.
Mar 01 04:42:29 ueba systemd[1]: gunicorn.service: Start request repeated too qu
Mar 01 04:42:29 ueba systemd[1]: Failed to start Gunicorn daemon for Django Proj
lines 1-12/12 (END)

if it matters, the file system is:

             *migrations (folder)
             *static (folder)
             *templates (folder)

I feel like I’ve tried multiple suggestions and variations of configs for both the ngnix and gunicorn through docs on OD but still haven’t nailed it out.

Let me know if I need to provide more docs, thanks!

edited by asb
  • @hansen
    I ran
    sudo -E gunicorn -b djangoproject.wsgi:application
    sudo -E gunicorn django

    it returns
    ImportError: No module named pymysql

    I double checked and I have all the MySQL packages in your link already installed.

  • That’s great. Now we know it’s a problem with pymysql.
    Are you using Python2 or Python3? If you’re using Python3, then you need to install this:
    sudo apt-get install python3-pymysql
    And if you’re using Python2, then you need to see which paths are being loaded for the modules:

  • @hansen
    This is what I got for python2 file paths, not sure what i’m looking for:

    [’/home/django/djangoproject’, ’/usr/lib/python2.7’, ’/usr/lib/python2.7/plat-x8664-linux-gnu’, ’/usr/lib/python2.7/lib-tk’, ’/usr/lib/python2.7/lib-old’, ’/usr/lib/python2.7/lib-dynload’, ’/usr/local/lib/python2.7/dist-packages’, ’/usr/lib/python2.7/dist-packages’]

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

You only have to look at your Django project (or Gunicorn more specifically). This has nothing to do with Nginx, right now at least, since you can’t even run Gunicorn.
Try starting it with debug level, in hope to get a better error message than “exit-code”:
gunicorn_django -b --debug --log-level debug
Modify the command, so it fits your project, just leave the –debug –log-level debug

  • I’m assuming that the only thing I change is ‘myapp’ to fit my project, but then get an error:

    usage: gunicorndjango [OPTIONS] [SETTINGS_PATH]
    django: error: unrecognized arguments: –debug

    • Sorry, don’t work with Gunicorn, just remove the –debug but leave the –log-level debug
      If that doesn’t work, then you need to go “nuclear” with –spew

      • Okay, I got quit the long response. Is there something I should look for, or do you want me to post it?

        I should also mention that, while following the YouTube tutorial, when I change everything it says to change other then the database stuff (keeping it sqlite) I can get regular Django errors when loading the webpage (Like “no such table: team_history”). When I change it to MySQL, that’s when I get the 502. I’m not sure what error is closer to getting the webpage to work.

        • Great, then it’s probably something to do with the configuration of MySQL or how it’s loaded (used, code-wise) in the project.
          Can you post the database configuration (EDIT: the config in your project) - both the SQLite and MySQL?
          If we still can’t figure it out, then it’s probably easier to post to and then we write the solution here - in the hope, we find it :-)

          • I just want to use MySQL, the database config I use, that works, to connect to MySQL Workbench on my local machine is:

            ‘default’: {
            'ENGINE’: 'django.db.backends.mysql’,
            'NAME’: 'ueba’,
            'USER’: 'root’,
            'PASSWORD’: ’password’,
            'HOST’: 'localhost’,
            'PORT’: '3306’,

            I installed MySQL on DO, and set that database config to:

            'default’: {
            'ENGINE’: 'django.db.backends.mysql’,
            'NAME’: 'ueba’,
            'USER’: 'root’, (user used to login to MySQL on DO)
            'PASSWORD’: ’password’, (used to login to MySQL on DO)
            'HOST’: '’,
            'PORT’: '3306’,

            My guess is that I want my website to connect to the MySQL on DO, but then populate the DO database from MySQL Workbench?

          • @bdave
            But do you have your MySQL on a different server than Gunicorn?
            If yes, then you should use “private” network on DO, since it’s faster.
            If no, then you should still use 'HOST': 'localhost',

          • @bdave
            And since your project was working fine with SQLite, then please post that configuration, since there must be a difference.
            From what I remember of Django, the configuration is in JSON format, which is very strict, so the comma at the end is not allowed for the final child.
            'PORT': '3306', should be 'PORT': '3306'

          • SQLite settings are the default ones:

            ‘default’: {
            'ENGINE’: 'django.db.backends.sqlite3’,
            'NAME’: os.path.join(BASE_DIR, 'db.sqlite3’),

            With further messing around, I noticed I can make migrations by using 'python migrate’ while using SQLite, but get “ImportError: No module named pymysql” when trying with MySQL.

          • @bdave
            You might be able to get better help by going to a Django-only based forum, since this is getting into something, which I haven’t played with.
            I would recommend this:

  • @hansen
    Thanks for sticking with me thus far and sorry for the long posts, but following the tutorial for ngnix/gunicorn/Django. I managed to get all the way up to running with Gunicorn on my local machine but haven’t gotten past that point. So I do think it’s a gunicorn issue. Running the debug command you provided earlier, it gives me:

    ImportError: No module named validation

    And for good measure, here is my gunicorn config file:

    Description=Gunicorn daemon

    project/uebaenv/bin/gunicorn –workers 3 –bind unix:/home/django/djangoproject/djangoproject.sock django_project.wsgi:application