The dreaded 502 error

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!


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

[‘/home/django/django_project’, ‘/usr/lib/python2.7’, ‘/usr/lib/python2.7/plat-x86_64-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’]

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 I ran sudo -E gunicorn -b django_project.wsgi:application and sudo -E gunicorn django_project.wsgi:application

it returns ImportError: No module named pymysql

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

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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