Need some help completing deployment of Django site on Nginx from local computer

February 12, 2017 769 views
Django Nginx Ubuntu 16.04

I followed this tutorial for setting up uwsgi and Nginx on Ubunutu

https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-ubuntu-16-04

The guide shows how to setup two fresh Django sites, I just did the first one and that is now working here

http://128.199.120.162/

for the second site I want to deploy a Django blog I had built locally, put it on the same ip and serve it on port :8080 so then I followed this tutorial

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-local-django-app-to-a-vps

I have followed everything in the guide apart from the GUnicorn part as I believe I dont need that if I have Uwsgi.

Ive cloned my site there, setup psql, migrated the db and finally done collecstatic and they all seem to have worked without any problems, I can run the site and see it at :8080 if I do manage.py runserver 0.0.0.0:8080 but if I just go to that port normally then I get an 'internal server error'.

The following is my setup for uwsgi and Nginx, its almost exactly the same as what I did for 'first site' however I should add that the second tutorial had somethings a bit different for the Nginx server block setup which Im not sure if I need so I have commented those out (e.g. proxy_pass etc)

/etc/uwsgi/sites/codego.ini

[uwsgi]
project = codego
uid = david
base = /home/david

chdir = /home/david/codego/codego
home = /home/david/Env/codego
module = codego.wsgi:application

master = true
processes = 5

socket = /run/uwsgi/codego.sock
chown-socket = david:www-data
chmod-socket = 666
vacuum = true

/etc/systemd/system/uwsgi.service

[Unit]
Description=uWSGI Emperor service

[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown david:www-data /run/uwsgi'
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

/etc/nginx/sites-available/codego

server {
listen 8080;
server_name = 128.199.120.162;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
            root /home/david/codego;
    }

    location / {
            #proxy_pass http://127.0.0.1:8001;
            #proxy_set_header X-Forwarded-Host $server_name;
            #proxy_set_header X-Real-IP $remote_addr;
            #add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
            include         uwsgi_params;
            uwsgi_pass      unix:/run/uwsgi/codego.sock;
    }

}

Ive been banging my head against this for hrs now and still can't figure it out.

I should add btw that my wigs.py file was blank for some reason so I had to manually add that content there from local and also Nginx error logs dont seem to be logging anything for some reason.

Any advice would be appreciated, I think Im very close to finishing this setup!

6 Answers

Found the problem, my directory structure was causing this I think as I had too many folders within folders and not named correctly, after removing everything in the Env and starting again for that Django install but this time with a more simple folder structure I seem to have it right and can see the site at :8080.

@easycompany80

Have you checked the log files generated by NGINX?

tail -20 /var/log/nginx/error.log

The above will display the last 20 lines of the error.log and may have more information as to why you're receiving the ISE.

I already tried that but nothing is returned when I run it and also the log file itself seems to be empty when I open it, is that normal?

OK, I seem to be getting something back from Nginx error logs now:

2017/02/15 09:28:38 [error] 25498#25498: *97 connect() to unix:/run/uwsgi/codego.sock failed (111: Connection refused) while connecting to upstream, client: 23.251.55.38, server: =, request: "GET http://23.251.55.38/index.html?rands=_438580716224561904527728 HTTP/1.0", upstream: "uwsgi://unix:/run/uwsgi/codego.sock:", host: "23.251.55.38"
2017/02/15 09:38:29 [error] 25498#25498: *99 connect() to unix:/run/uwsgi/codego.sock failed (111: Connection refused) while connecting to upstream, client: 61.157.96.109, server: =, request: "GET http://www.so.com/?rands=_3444315521381502065840 HTTP/1.0", upstream: "uwsgi://unix:/run/uwsgi/codego.sock:", host: "www.so.com"
2017/02/15 09:38:31 [error] 25498#25498: *101 connect() to unix:/run/uwsgi/codego.sock failed (111: Connection refused) while connecting to upstream, client: 61.157.96.109, server: =, request: "GET http://m.baidu.com/?rands=_1905660072016447504031196 HTTP/1.0", upstream: "uwsgi://unix:/run/uwsgi/codego.sock:", host: "m.baidu.com"
2017/02/15 09:38:32 [error] 25498#25498: *103 connect() to unix:/run/uwsgi/codego.sock failed (111: Connection refused) while connecting to upstream, client: 61.157.96.109, server: =, request: "GET http://www.sogou.com/?rands=_18040125564024970503674080 HTTP/1.0", upstream: "uwsgi://unix:/run/uwsgi/codego.sock:", host: "www.sogou.com"
2017/02/15 09:45:35 [error] 25498#25498: *105 connect() to unix:/run/uwsgi/codego.sock failed (111: Connection refused) while connecting to upstream, client: 182.232.109.9, server: =, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/run/uwsgi/codego.sock:", host: "128.199.120.162:8080"
2017/02/15 09:45:42 [error] 25498#25498: *105 connect() to unix:/run/uwsgi/codego.sock failed (111: Connection refused) while connecting to upstream, client: 182.232.109.9, server: =, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/run/uwsgi/codego.sock:", host: "128.199.120.162:8080"
2017/02/15 09:51:16 [error] 25498#25498: *113 connect() to unix:/run/uwsgi/codego.sock failed (111: Connection refused) while connecting to upstream, client: 182.232.109.9, server: =, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/run/uwsgi/codego.sock:", host: "128.199.120.162:8080"

why is it showing things like this though? Are these bot hack attempts?

http://m.baidu.com/?rands=_1905660072016447504031196 HTTP/1.0", upstream: "uwsgi://unix:/run/uwsgi/codego.sock:", host: "m.baidu.com"

I should add that I made some changes to my uwsgi ini file as I think my project file structure may have been causing some problems, so its now like this

[uwsgi]
project = codego/codego
uid = david
base = /home/%(uid)

chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application

master = true
processes = 5

socket = /run/uwsgi/%(project).sock
chown-socket = %(uid):www-data
chmod-socket = 666
vacuum = true

the reason the project is codger/codego is that my directory structure for this Django setup is like this:

/home/david/codego/codego/

anyway, now instead of 'internal server error' Im getting a 502 bad gateway so not sure if Ive really made any progress lol

and could file ownership be the problem? This is what ls -ld gives me:

drwxr-xr-x 3 root root 4096 Feb 5 04:28 /home
drwxr-xr-x 9 david david 4096 Feb 12 04:54 /home/david
drwxr-xr-x 3 david root 4096 Feb 10 14:07 /home/david/codego
drwxr-xr-x 6 david root 4096 Feb 11 05:48 /home/david/codego/codego

I have setup uwsgi logging from the answer here:

https://www.digitalocean.com/community/questions/how-to-check-error-logs-for-flask-uwsgi-nginx-app

results are below, not sure what it means about 'No such file or directory [core/socket.c line 230]' though...

current working directory: /etc/uwsgi/sites
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/david/codego/codego
your processes number limit is 1882
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
bind(): No such file or directory [core/socket.c line 230]
*** Starting uWSGI 2.0.14 (64bit) on [Wed Feb 15 10:56:24 2017] ***
compiled with version: 5.4.0 20160609 on 05 February 2017 05:08:58
os: Linux-4.4.0-62-generic #83-Ubuntu SMP Wed Jan 18 14:10:15 UTC 2017
nodename: ubuntu-512mb-sgp1-01
machine: x86_64
clock source: unix
detected number of CPU cores: 1

Have another answer? Share your knowledge.