skip to Main Content

I have a django project named MySite, and this project has some application inside as below:

- MySite
  -- app
  -- venv
  -- media
  -- django_project
     --- wsgi.py
     --- settings.py
     --- urls.py
     --- asgi.py

To deploy on aws, I am in the phase of gunicorn configuring. However I face with this error:

guni:gunicorn                    BACKOFF   Exited too quickly (process log may have details)

However, first time status is like:

gunicorn                         STARTING 

this is my gunicorn.conf:

[program:gunicorn]
directory=/home/ubuntu/MySite
command=/usr/bin/gunicorn  --workers 3 --bind unix:/home/ubuntu/MySite/app.sock django_project.wsgi.application
autostart=true
autorestart=true
stderr_logfile=/var/log/gunicorn/gunicorn.err.log
stdout_logfile=/var/log/gunicorn/gunicorn.out.log

[group:guni]
program:gunicorn

in gunicorn.err.log it says problem is in:

usage: gunicorn [OPTIONS] [APP_MODULE]
gunicorn: error: unrecognized arguments: django_project.wsgi.application

when I try this:

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

I get this error:

SyntaxError: invalid syntax
[2021-02-10 10:12:40 +0000] [6914] [INFO] Worker exiting (pid: 6914)
[2021-02-10 10:12:40 +0000] [6912] [INFO] Shutting down: Master
[2021-02-10 10:12:40 +0000] [6912] [INFO] Reason: Worker failed to boot.

The entire process to install and run gunicorn which I did:

**********************************START********************************
sudo apt-get upgrade -y
sudo apt-get update -y



1) Clone the git project
git clone https://github.com/XX/MyProj.git

2) cd /MySite ## there is a venv with django installed in

3) Activate venv
source venv/bin/activate


5. Instal NGINX and GUNICORN
pip3 install gunicorn ## install without sudo..
sudo apt-get install nginx -y
pip install psycopg2-binary

6. Connect gunicorn (#Error: Worker failed to boot.)
gunicorn --bind 0.0.0.0:8000 django_project.wsgi:application 

7. Install supervisor
sudo apt-get install -y supervisor ## This command holds the website after we logout

8. Config supervisor
cd /etc/supervisor/conf.d
sudo touch gunicorn.conf

9) ##In the file file do following###
[program:gunicorn]
directory=/home/ubuntu/MySite
command=/usr/bin/gunicorn --workers 3 --bind  unix:/home/ubuntu/MySite/app.sock django_project.wsgi:application
autostart=true
autorestart=true
stderr_logfile=/var/log/gunicorn/gunicorn.err.log
stdout_logfile=/var/log/gunicorn/gunicorn.out.log

[group:guni]
Program:gunicorn
####endfile####

10). Config supervisor
cd /etc/supervisor/conf.d
sudo touch gunicorn.conf

11). Connect file to supervisor
sudo mkdir -p /var/log/gunicorn

sudo supervisorctl reread
sudo supervisorctl reread
sudo supervisorctl update

12. Check if gunicorn is running in background
sudo supervisorctl status

**********************************END********************************

3

Answers


  1. The error tells you that the way you start gunicorn is wrong.

    It looks like you are using supervisor. As per the docs, the correct syntax is:

    [program:gunicorn]
    command=/path/to/gunicorn main:application -c /path/to/gunicorn.conf.py
    directory=/path/to/project
    user=nobody
    autostart=true
    autorestart=true
    redirect_stderr=true
    
    Login or Signup to reply.
  2. This guide should work just fine 🙂

    1. Install dependencies
    [user@machine]$ sudo apt update && sudo apt upgrade -y
    [user@machine]$ sudo apt install python3-dev python3-pip supervisor nginx -y
    [user@machine]$ sudo systemctl enable nginx.service
    [user@machine]$ sudo systemctl restart nginx.service
    [user@machine]$ sudo systemctl status nginx.service
    
    1. Create new site
      /etc/nginx/sites-available/<site-name>
    server{
        listen 80;
        server_name <ip or domain>;
    
        location = /favicon.ico {
            access_log off; 
            log_not_found off; 
        }
    
        location /static/ {
            root /path/to/static/root;
        }
    
        # main django application
        location / {
            include proxy_params;
            proxy_pass http://unix:/path/to/project/root/run.sock;
        }
    }
    
    1. Create symbolic link
    [user@machine]$ ln -s /etc/nginx/sites-available/<site-name> /etc/nginx/sites-enabled
    
    1. Setup supervisor
      /etc/supervisor/conf.d/<project-name>.conf
    [program:web]
    directory=/path/to/project/root
    user=<user>
    autostart=true
    autorestart=true
    redirect_stderr=true
    stdout_logfile=/path/to/logs/gunicorn-error.log
    command=/path/to/gunicorn --access-logfile - --workers 3 --bind unix:/path/to/project/root/run.sock <project-name>.wsgi:application
    
    
    1. Then
    [user@machine]$ sudo supervisorctl reread
    [user@machine]$ sudo supervisorctl update
    [user@machine]$ sudo supervisorctl status web
    [user@machine]$ sudo systemctl restart nginx.service
    
    Login or Signup to reply.
  3. Your issue could be a combination of a few things:

    1. Verify you django settings.py is set up properly for deployment. Pay very close attention to the STATIC_ROOT and STATICFILES_DIR variables as they are critical to serving your projects static files.

    2. Make sure your virtualenv is activated and you have ran pip install -r requirements.txt.

    Note: At this point try to run your project with your servers public ip python manage.py runserver server_public_ip:8000 a lot of people assume that, because their project ran locally, it will run on the server. Something always goes wrong.

    1. Make sure you run python manage.py collectstatic on your server. This collects your static files and makes a directory for it. Take note of the path it tells you it’s going to copy them to, you’re going to need it for your /static/ location block in your nginx sites-available configuration file.

    2. Make sure your gunicorn.conf command variable points to your virtualenv path, and that it points to a .sock file that gunicorn and nginx can access (sudo chown user:group). Here is an example:

       [program:gunicorn]
      
       command=/home/user/.virtualenvs/djangoproject/bin/gunicorn -w3 --bind unix:/etc/supervisor/socks/gunicorn.sock djangoproject.wsgi:application --log-level=info
       directory=/home/user/djangoproject
       numprocs=3
       process_name=%(program_name)s_%(process_num)d
       user=user
       environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8,HOME="/home/user/djangoproject", USER="user" 
      
       autostart=true
       autorestart=true
       stderr_logfile=/var/log/supervisor/gunicorn.err.log
       stdout_logfile=/var/log/supervisor/gunicorn.out.log
      
    3. There are a couple ways you can set up your nginx configuration files. Some docs have you do it all in the nginx.conf file, however, you should break it up into sites-available with a symbolic link to sites-enabled. Either way should get you the same result. Here is an example:

      upstream django {
          server unix:/etc/supervisor/socks/gunicorn.sock;
      }
      
      server {
          listen 80;
          server_name www.example.com;
          client_max_body_size 4G;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_headers_hash_max_size 1024;
          proxy_read_timeout 300s;
          proxy_connect_timeout 75s;
      
          access_log /var/log/nginx/access.log;
          error_log /var/log/nginx/error.log debug;
      
          location /media/ {
              autoindex off;
              alias /home/user/djangoproject/media/;
          }
      
          location /static/ {
              autoindex off;
              alias /home/user/djangoproject/static/;
          }
      
          location / {
              proxy_pass http://django;
              proxy_redirect off;
          }
      }
      
    4. Make sure that you run sudo service nginx restart after every nginx configuration change.

    5. Make sure you run sudo supervisorctl reread, sudo supervisorctl update all, and sudo supervisorctl restart all after every supervisor configuration file change. In your case every gunicorn.conf file change.

    6. Lastly, as a general rule, make sure all of your directory paths match to their respected processes. For example: Let’s say your gunicorn command variable points to a sock file that is in /path/to/project/gunicorn.sock, but your nginx configuration has a proxy_pass to /etc/supervisor/socks/gunicorn.sock, nginx doesn’t know where to pass your requests to, so gunicorn never sees the request. Also, you can add this to your nginx location blocks so you can see where each request gets to in your browser dev tools response header: add_header X-debug-message "The port 80, / location was served from django" always;

    Note: If you are getting the "Welcome to Nginx" page, it means nginx doesn’t know where to send the request. A lot of times you have a static root directory path problem. However, there are other issues, but situational to how things are set up. You’ll have to debug with some trial and error. Also, try adding a location block to a known url like http://example.com/login, if you get there, you know you have an nginx configuration issue. If you get 404 not found, then you most likely have a django project problem or a gunicorn problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search