4

Following this tutorial, I was able to set up Django, Gunicorn & nginx inside a virtualenv on an AWS EC2 instance (running Ubuntu 16.04), and then proceed to create an Upstart file to "daemonize" the entire thing.

After activating the virtualenv, I checked that:

  1. Django works - I was able to access my Django project via port 8000 by running the following:

./manage.py runserver 0.0.0.0:8000

When visiting mydomain.com:8000 I was welcomed by the default Django page.

  1. Gunicorn works & is able to serve the Django app - I was able to access my Django app by running this from my project's folder (replace projectname with my actual project name):

gunicorn --bind 0.0.0.0:8000 projectname.wsgi:application

When visiting mydomain.com:8000 this time I was greeted again by the welcome page, and when appending /admin to the end of the URL I was able to see the login screen minus the CSS (because Gunicorn isn't aware of the static CSS files, which is OK for now according to the tutorial).

From here I proceeded to create the following systemd file (when masterfolder is used to show the folder in which all the action takes place, myuser is my system user (ubuntu, as this is an Ubuntu instance) :

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=myuser
Group=www-data
WorkingDirectory=/home/myuser/masterfolder
ExecStart=/home/myuser/masterfolder/myvirtualenv/bin/gunicorn --workers 3 --bind unix:/home/myuser/masterfolder/projectname.sock$

[Install]
WantedBy=multi-user.target`

I encountered two problems:

  1. No socket is being created - when checking the nginx log files outside as well as inside the virtualenv (not entirely sure why they are the same, btw) I saw the same error:

2017/01/17 15:12:43 [crit] 12403#12403: *3 connect() to unix:/home/myuser/masterfolder/projectname.sock failed (2: No such file or directory) while connecting to upstream

Now, note that masterfolder is consistent with the folder hierarchy django-admin creates when I run a startproject:

masterfolder/ ├── manage.py └── projectname ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py

When looking inside masterfolder there is indeed no .sock file at all, never mind one that is named projectname.sock.

  1. Therefore, despite the fact that nginx works, it throws a 502 -

This is how my etc/nginx/sites-availabe/projectname file looks like:

server {
    listen 80;
    server_name www.mydomain.com mydomain.com MYIPADDRESS;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/masterfolder/projectname;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/myuser/mastefolder/projectname.sock;
    }
}

It's symlinked correctly, of course, and after every change I make to try and fix it I run:

sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl restart nginx

I feel as if I'm missing something basic here in the understanding of how all of this infrastructure works together. Feel free to point out errors in my understanding of the process, of course.

1 Answer 1

3

Have you checked permissions of the masterfolder directory?

The permissions should be myuser:www-data.

Also there is a spelling mistake in your nginx config file. Hope that's a typo:)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .