bluesummers
bluesummers

Reputation: 12607

Django-Gunicorn-Nginx deployement doesn't get past Nginx

I'm trying to deploy a project on an EC2 instance.

When I go to my EC2 instance URL, I can see Nginx welcome message, but I can't seem to roll it into django.

I have a Gunicorn service up and Nginx service up, these are the outputs of their service status calls:

Gunicorn:

● gunicorn_mynew_website.service - Gunicorn daemon for mynew website
Loaded: loaded (/etc/systemd/system/gunicorn_mynew_website.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2017-08-19 12:29:05 UTC; 2s ago Main PID: 1760 (gunicorn) Tasks: 2 Memory: 33.6M CPU: 368ms CGroup: /system.slice/gunicorn_mynew_website.service ├─1760 /opt/mynew/venv/website-venv/bin/python3.5 /opt/mynew/venv/website-venv/bin/gunicorn website.wsgi:application --name website --workers 1 --user ubuntu --bind=unix:/opt/mynew/run/gunicor └─1769 /opt/mynew/venv/website-venv/bin/python3.5 /opt/mynew/venv/website-venv/bin/gunicorn website.wsgi:application --name website --workers 1 --user ubuntu --bind=unix:/opt/mynew/run/gunicor

Aug 19 12:29:05 ip-172-31-26-24 systemd[1]: Started Gunicorn daemon for mynew website. Aug 19 12:29:05 ip-172-31-26-24 gunicorn_start.sh[1760]: Starting website as ubuntu Aug 19 12:29:05 ip-172-31-26-24 gunicorn_start.sh[1760]: [2017-08-19 12:29:05 +0000] [1760] [INFO] Starting gunicorn 19.7.1 Aug 19 12:29:05 ip-172-31-26-24 gunicorn_start.sh[1760]: [2017-08-19 12:29:05 +0000] [1760] [INFO] Listening at: unix:/opt/mynew/run/gunicorn.sock (1760) Aug 19 12:29:05 ip-172-31-26-24 gunicorn_start.sh[1760]: [2017-08-19 12:29:05 +0000] [1760] [INFO] Using worker: sync Aug 19 12:29:05 ip-172-31-26-24 gunicorn_start.sh[1760]: [2017-08-19 12:29:05 +0000] [1769] [INFO] Booting worker with pid: 1769

Nginx

● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2017-08-19 11:34:14 UTC; 56min ago Process: 1151 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 1090 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 1165 (nginx) Tasks: 2 Memory: 9.6M CPU: 24ms CGroup: /system.slice/nginx.service ├─1165 nginx: master process /usr/sbin/nginx -g daemon on; master_process on └─1166 nginx: worker process

Aug 19 11:34:14 ip-172-31-26-24 systemd[1]: Starting A high performance web server and a reverse proxy server... Aug 19 11:34:14 ip-172-31-26-24 systemd[1]: Started A high performance web server and a reverse proxy server.

And my `/etc/nginx/nginx.conf (Http part) is:

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;



        upstream test_server {
          server unix:/opt/mynew/run/gunicorn.sock fail_timeout=10s;
        }

        server {
            listen   80;

            client_max_body_size 4G;

            access_log /opt/mynew/logs/nginx-access.log;
            error_log /opt/mynew/logs/nginx-error.log warn;

            location /static/ {
                autoindex on;
                alias   /opt/mynew/website/static/;
            }

            location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;

                if (!-f $request_filename) {
                    proxy_pass http://test_server;
                    break;
                }
            }

}

My gunicorn_start.sh file (service):

#!/bin/bash

NAME="website"                              
DJANGODIR=/opt/mynew/website             
SOCKFILE=/opt/mynew/run/gunicorn.sock        
USER=ubuntu
GROUP=www-data
NUM_WORKERS=1
DJANGO_SETTINGS_MODULE=website.settings
DJANGO_WSGI_MODULE=website.wsgi
echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source /opt/mynew/venv/website-venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /opt/mynew/venv/website-venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user $USER \
  --bind=unix:$SOCKFILE

My django folder is at /opt/mynew/website/

To understand what's going on I intentionally added exception to /website/website/settings.py just to see if gunicorn is connected to it, and when I started the gunicorn service it indeed failed

So I'm guessing the fault is between Nginx and the gunicorn...

Any suggestions?

Upvotes: 0

Views: 669

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146490

Remove these 2

include /etc/nginx/conf.d/*.conf; 
include /etc/nginx/sites-enabled/*; 

and see if it works. By default nginx comes with a default.conf which will listen on port 80 and serve the /var/www/html directory with the static nginx page. So either know every file you are including or don't include them

Upvotes: 1

Related Questions