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
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:
This guide should work just fine 🙂
/etc/nginx/sites-available/<site-name>
/etc/supervisor/conf.d/<project-name>.conf
Your issue could be a combination of a few things:
Verify you django
settings.py
is set up properly for deployment. Pay very close attention to theSTATIC_ROOT
andSTATICFILES_DIR
variables as they are critical to serving your projects static files.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.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.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: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:
Make sure that you run
sudo service nginx restart
after every nginx configuration change.Make sure you run
sudo supervisorctl reread
,sudo supervisorctl update all
, andsudo supervisorctl restart all
after every supervisor configuration file change. In your case every gunicorn.conf file change.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.