What is the propper way to debug gunicorn? - django

I am trying to deploy my very first Django applicatin on heroku. Gunicorn gives me this error: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
In this answer https://stackoverflow.com/a/55623889 command gunicorn app:application --preload -b 0.0.0.0:5000 is said to give detailed error message, but as its comments state it is not clear what words to replace with what. For instance somewhere I saw something similar called with <name of my app>.wsgi. To make matter worse name of my Django app in local is different from the one on Heroku.
Also it is not clear to me should this be called strait from terminal (within virtual env) or should I be using some sort of gunicorn CLI.
So what is the actual propper way to figure out what is failing gunicorn?

I've faced similar issues in the past. I hope this might help:
Make sure gunicorn is installed in your activated virtual environment project by running pip freeze in the terminal to verify it's installation. If it is not listed, in the activated virtual environment, run pip install gunicorn or else if you are using pipenv, run pipenv install gunicorn. Don't forget to update the requirements.txt file by running pip freeze > requirements.txt
In the Procfile which is within the project_directory type the syntax:
web: gunicorn your_django_project_name.wsgi --log-file -
N.B: There should be a space between;
The --log-file and the - next to it.
web: and the gunicorn part of the code.
Finally add, commit and push the changes to the heroku server.

Related

Gunicorn not working on Amazon ec2 server

I am trying to deploy a django website on an aws ec2 instance (ubuntu 18.04) following this tutorial that is consistent with other online resources. Everything is working fine but there is some issue with gunicorn. The worker doesn't seem to boot.
I figured there was something wrong with my installation and I tried uninstalling and installing with different commands-
inisde my virtualenv with
pip install gunicorn
and
sudo -H pip install gunicorn
I even physically deleted all gunicorn files and reinstalled gunicorn but its always the same. Where have I gone wrong?
p.s: I had initially done sudo apt-get
From the screenshot you attached, it seems that gunicorn is installed correctly, but perhaps you have not passed a configuration file. The command to run gunicorn with a configuration is
gunicorn -c /path/to/gunicorn.conf.py myapp.wsgi:application

Django Manage.py Migrate from Google Managed VM Dockerfile - How?

I'm working on a simple implementation of Django hosted on Google's Managed VM service, backed by Google Cloud SQL. I'm able to deploy my application just fine, but when I try to issue some Django manage.py commands within the Dockerfile, I get errors.
Here's my Dockerfile:
FROM gcr.io/google_appengine/python
RUN virtualenv /venv -p python3.4
ENV VIRTUAL_ENV /venv
ENV PATH /venv/bin:$PATH
# Install dependencies.
ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
# Add application code.
ADD . /app
# Overwrite the settings file with the PROD variant.
ADD my_app/settings_prod.py /app/my_app/settings.py
WORKDIR /app
RUN python manage.py migrate --noinput
# Use Gunicorn to serve the application.
CMD gunicorn --pythonpath ./my_app -b :$PORT --env DJANGO_SETTINGS_MODULE=my_app.settings my_app.wsgi
# [END docker]
Pretty basic. If I exclude the RUN python manage.py migrate --noinput line, and deploy using the GCloud tool, everything works fine. If I then log onto the VM, I can issue the manage.py migrate command without issue.
However, in the interest of simplifying deployment, I'd really like to be able to issue Django manage.py commands from the Dockerfile. At present, I get the following error if the manage.py statement is included:
django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/cloudsql/my_app:us-central1:my_app_prod_00' (2)")
Seems like a simple enough error, but it has me stumped, because the connection is certainly valid. As I said, if I deploy without issuing the manage.py command, everything works fine. Django can connect to the database, and I can issue the command manually on the VM.
I wondering if the reason for my problem is that the sql proxy (cloudsql/) doesn't exist when the Dockerfile is being deployed. If so, how do I get around this?
I'm new to Docker (this being my first attempt) and newish to Django, so I'm unsure of what the correct approach is for handling a deployment of this nature. Should I instead be positioning this command elsewhere?
There are two steps involved in deploying the application.
In the first step, the Dockerfile is used to build the image, which can happen on your machine or on another machine.
In the second step, the created docker image is executed on the Managed VM.
The RUN instruction is executed when the image is being built, not when it's being run.
You should move manage.py to the CMD command, which is run when the image is being run.
CMD python manage.py migrate --noinput && gunicorn --pythonpath ./my_app -b :$PORT --env DJANGO_SETTINGS_MODULE=my_app.settings my_app.wsgi

gunicorn not found on heroku

I want to deploy a very basic Django app on Heroku. I followed the instructions, but the browser shows me the Heroku error page and the heroku logs say
bash: gunicorn: command not found
I have gunicorn in my requirements.txt and configured the wsgi.py and Procfile as instructed by the documentation. What else can I try?
Edit: I also tried to install gunicorn manually on Heroku (heroku run pip install gunicorn), which worked fine, so I am pretty sure that gunicorn is installed. Why doesn't Heroku find it?
Edit 2: It seems that I can install gunicorn manually (heroku run bash and then pip install gunicorn:
~ $ gunicorn
usage: gunicorn [OPTIONS] [APP_MODULE]
gunicorn: error: No application module specified.
but when i log out and log in to the bash again, i get:
~ $ gunicorn
bash: gunicorn: command not found
Heroku seems to install but then discard gunicorn. How can that be?
Hi got the same problem but this might help:
Go to this:
https://github.com/heroku/heroku-buildpack-python
And try to run this command in project's root terminal:
heroku buildpacks:set git://github.com/heroku/heroku-buildpack-python.git
and then update heroku with running below command:
git push heroku master

Trying to test django app locally with foreman

I'm working on a django app and trying to use foreman to test my app locally before pushing to heroku. I can successfully run it using python manage.py server. However when running it using forman it fails - Whenever I do a foreman start inside of the directory it would return me this:
09:21:09 web.1 | started with pid 9956
09:21:09 web.1 | /usr/local/foreman/bin/foreman-runner: line 41: exec: gunicorn: not found
09:21:09 web.1 | exited with code 127
09:21:09 system | sending SIGTERM to all processes
SIGTERM received
What does this mean?
Below is my Procfile:
web: gunicorn myapp.wsgi
Below is my requirements.txt
Django==1.4.3
distribute==0.6.31
dj-database-url==0.2.1
psycopg2==2.4.6
#wsgiref==0.1.2
gunicorn==0.16.1
Thanks for the help in advance!
It looks like gunicorn isn't installed properly on your system.
Run pip install -r requirements.txt and then manually run the gunicorn command to check it works.
I came to this question with the same problem finding gunicorn when running 'foreman start', but eventually dug up at other sources that I was not recreating the virtualenv in a new bash session. I had originally followed the instructions from Heroku, but days later with new sessions, needed to remember to run
source venv/bin/activate
Had the issue - installing separately gunicorn did the trick
pip install gunicorn
I had the same issue:
sudo apt-get install libpq-dev python-dev
and then reinstalling the heroku-toolbelt solved it!

Cannot install gunicorn on Ubuntu

I'm trying to install gunicorn in my virtual env, but get the following:
$ pip install gunicorn
Downloading/unpacking gunicorn
Downloading gunicorn-0.14.2.tar.gz (203Kb): 203Kb downloaded
Running setup.py egg_info for package gunicorn
warning: no files found matching '*' under directory 'debian'
Installing collected packages: gunicorn Found existing installation: gunicorn 0.14.2
Uninstalling gunicorn:
Successfully uninstalled gunicorn
Running setup.py install for gunicorn
warning: no files found matching '*' under directory 'debian'
Installing gunicorn_paster script to /home/aemdy/Documents/projects/reborn/env/bin
Installing gunicorn script to /home/aemdy/Documents/projects/reborn/env/bin
Installing gunicorn_django script to /home/aemdy/Documents/projects/reborn/env/bin
Successfully installed gunicorn
Cleaning up...
And when I use python manage.py run_gunicorn for django it says that this is unknown command. I have added gunicorn to INSTALLED_APPS.
Warnings like that appear sometimes when installing apps. I believe it's related to cleanup pip tries to do, but it doesn't matter regardless. As the console output says "Successfully installed gunicorn". So no problems there.
With gunicorn installed, the only other requirement is adding gunicorn to INSTALLED_APPS. If you've done that as well, you're done. run_gunicorn will be available.
So, if it's not working, one of the following is in play:
Gunicorn isn't actually installed. However, you should get an error trying to reference in in INSTALLED_APPS in that scenario. Check your virtualenv's site-packages directory to ensure there's gunicorn folder there.
You installed Gunicorn in a different virtualenv. Again, you should be getting an error just as in #1. And, just as in #1, check to make sure it's actually in the proper virtualenv's site-packages directory
You don't have the virtualenv activated. However, same error as in #1 and #2 applies here.
You really don't have gunicorn in INSTALLED_APPS or a compiled version of settings.py is being used that doesn't have it in INSTALLED_APPS. Delete settings.pyc if it exists.
I just ran into this issue. What I did was try to run the app using runserver which led to me seeing the hostname on the server was not set(and thus can't get an IP address). Once we fixed that issue the command worked again.
It is a little misleading to see:
Unknown command: 'run_gunicorn'
Type 'manage.py help' for usage.
when the hostname is not set...confusing I know but I hope this helps someone in the future.
Did you remember to add gunicorn to your INSTALLED_APPS?