Configuring gunicorn for Django on Heroku - python

I'm trying to setup a test Django project on Heroku. Following the advice here and in the Heroku Getting Started I'm trying to use gunicorn instead of the Django dev server.
This was my first attempt at the Procfile:
web: gunicorn_django --workers=4 --bind=0.0.0.0:$PORT my_project/settings.py
worker: python my_project/manage.py celeryd -E -B --loglevel=INFO
This gave me this error:
ImportError: Could not import settings 'settings.py' (Is it on sys.path?): No module named py
I decided to take a different track and followed the advice here. Now my Procfile looked like this:
web: gunicorn_django -b 0.0.0.0:\$PORT -w 9 -k gevent --max-requests 250 --preload my_project.settings
(I also updated my requirements file to include gevent.) It gave me the same error:
ImportError: Could not import settings
Finally, I just set it to settings:
web: gunicorn_django -b 0.0.0.0:\$PORT -w 9 -k gevent --max-requests 250 --preload settings
But now I get this error:
Error: django project not found
The way my Django project is set up is that the settings.py file is in the parent directory of the repo -- I don't have the Django project under another directory. It's at the same level as the virtualenv and git files. Would that be a problem? I'm sure I'm doing something simple wrong -- any help would be much appreciated.
If I follow the instructions from Heroku here and change the Procfile to this:
web: gunicorn hellodjango.wsgi -b 0.0.0.0:$PORT
Nothing happens -- no errors in the logs, but no proceses run and the app just appears dead in the water.

I have just run into this same issue. In the procfile you copied from the Heroku guide, change hellodjango.wsgi to yourproject.wsgi
Looks like we all fall victim to blindly copy-pasting now and then, but in your (and my) defense, it looks like there's no *.wsgi file that's actually being opened, it's just how you signal to gunicorn that you want it to run your django project.

I had the same exact issue that you are having. The way I was able to finally get it working was to use the django app gunicorn.
I added gunicorn to the django settings.py
'gunicorn',
I then used this as my web entry in my Procfile.
web: python manage.py run_gunicorn -b 0.0.0.0:\$PORT -w 9 -k gevent --max-requests 250 --preload
You may have to alter you .manage.py if you use a different directory structure then I did. My app was in /app, and my python path was also /app.

I had this issue and landed up having to point directly to the python path and then set the settings reference.
In the end my Procfile looks like this:
web: gunicorn_django --pythonpath=/app/project --settings=settings
I had to run heroku run which showed the env variables and that's where I was able to find the /app which I prepended to my project name.

Do you have a requirements.txt in the root folder (containing the word django), as well as a settings.py? Those appear to the be the requirements for Django app detection, as documented here.

Related

How to add the heroku nginx buildpack to a python django app with gunicorn?

I am trying to deploy a python with django on heroku and configure an nginx reverse proxy to have like a filter which won't let the requests pass to my django backend until the token is checked to an third party IDP.
**
STEP 1
**
I am following this tutorial to add the nginx buildpack : https://elements.heroku.com/buildpacks/hq-mobile/v3-nginx-buildpack
After I've added the buildpack and start the app, I get the following message: bin/start-nginx: line 37: bundle: command not found
After some digging I noticed some paths need to be added to the config vars for the heroku app in order for bundler to get the needed dependencies :
So I've added this paths:
heroku config:add GEM_PATH=vendor/bundle/1.9.3
heroku config:set PATH=bin:vendor/bundle/ruby/1.9.3/bin:/usr/local/bin:/usr/bin:/bin
Then the config/unicorn.rb file:
require 'fileutils'
listen '/tmp/nginx.socket'
before_fork do |server,worker|
FileUtils.touch('/tmp/app-initialized')
end
Procfile:
web: bin/start-nginx bundle exec unicorn -c config/unicorn.rb gunicorn -c gunicorn.conf.py MyApp.wsgi
gunicorn.conf.py
# gunicorn.conf
def when_ready(server):
# touch app-initialized when ready
open('/tmp/app-initialized', 'w').close()
bind = 'unix:///tmp/nginx.socket'
workers = 4
Even after adding this, the error still persists.
**
STEP 2
**
Once the normal buildpack will be in place I want to follow this tutorial to configure nginx how I want:
https://www.nginx.com/blog/validating-oauth-2-0-access-tokens-nginx/
What configuration is needed to be able to have this buildpack working in my situation?
To fix this problem, you remove the bundle exec part from the Procfile. In other words, your Procfile becomes:
web: bin/start-nginx unicorn -c config/unicorn.rb gunicorn -c gunicorn.conf.py MyApp.wsgi
(sidenote: there might be an error with just using MyApp.wsgi since you might need to change it to something like MyApp.wsgi:application to expose the environment variable for your application)

Gunicorn ModuleNotFoundError

I'm reading a book about TDD and Django and there's a deployment part. I have a problem trying to run gunicorn with the following command:
/root/sites/django_blog/virtualenv/bin/gunicorn --bind unix:/tmp/django_blog.socket django_blog.wsgi:application
It fails with the following error:
ModuleNotFoundError: No module named 'django_blog'
But when I activate my virtualenv and instead of writing the full pass to gunicorn I just go with:
gunicorn --bind unix:/tmp/django_blog.socket django_blog.wsgi:application
And everything works perfectly! The problem is I still need to run it the first way, because I wil use it in the nginx service file. I wrote about this error and tried a couple of solutions but they didn't work for me. I guess I have to do something with environment variables but I don't know what exactly.
You can specify a directory to gunicorn to switch to before the apps are loaded.
Simply add --chdir /path/to/directory to the launch.
In your case this might look as follows:
/root/sites/django_blog/virtualenv/bin/gunicorn --chdir /root/sites/django_blog/source --bind unix:/tmp/django_blog.socket django_blog.wsgi:application
Here is the link to the specific gunicorn settings documentation.
Hope that helps and happy coding!

Django Heroku Profile

I am having trouble running my django app on Heroku. Following is my file structures:
---django_blog
---media_cdn
---static_cdn
---Procfile
---requirements.txt
---runtime.txt
---src
---blog
---...
---settings.py
---manage.py
---...
So 'src' is actually is my project root, and 'blog' is my app. I tried made the procfile to be
web: blog.wsgi --log-file -
and
web: src.blog.wsgi --log-file -
But none of them works. When I checked the heroku logs file, I found this error:
ImportError: No module named 'blog'
From Heroku documentation:
First, and most importantly, Heroku web applications require a
Procfile.
This file (named Procfile) is used to explicitly declare your
application’s process types and entry points. It is located in the
root of your repository.
You need to be more specific about how you declare your process types, if you are using gunicorn for this you will declare --chdir because you want to run it from different folder:
web: gunicorn --chdir src myproject.wsgi --log-file -
On the other hand I'm not using gunicorn rather I declare it like this:
web: python myproject/manage.py runserver 0.0.0.0:$PORT --noreload
FYI - Switch to gunicorn in production!

Newrelic error in Django : Configuration has already been done

I am getting this error when i am trying to run
python manage.py runserver for my Django Project,
newrelic.api.exceptions.ConfigurationError: Configuration has already been done against differing configuration file or environment. Prior configuration file used was "/home/project/newrelic.ini" and environment "staging".'
What does it mean ?? How to approach it?
Have you just setup New Relic?
I believe this is a problem with your procfile. Which you most likely just have edited.
Your procfile should look something like this, depending on your wsgi.
web: newrelic-admin run-program gunicorn hello.wsgi --log-file -

Django Heroku push failing

I'm running accross an error trying to push my Django project up to Heroku and I was looking to see if anyone had any insight.
! Heroku push rejected, no Cedar-supported app detected
I am guessing it is because of my folder structure in the git repo but I am not sure. My project is setup like this:
/subfolder/djangoproject/
/subfolder/requirements.txt
My Proc file content looks like this:
web: python manage.py runserver 0.0.0.0:$PORT --noreload --settings=djangoproject.settings.heroku
I have my project setting split and they work fine on my local. (In other words I having a setting directory with an init.py in it.)
I tried this:
/Procfile
and this:
/subfolder/Procfile
but neither worked.
Can this folder structure be the culprit? I was under the impression that the requirements.txt was how Heroku found where the project folder was.
Thanks
I wanted to add my solve for the first issue I had and then post the new issue I am having.
The solve I first did was to move the Procfile and requirements.txt files to the root level of the project. The second thing I did was edit the Procfile in so the path to the manage.py script was to the proper location
web: python subfolder/djangoproject/manage.py runserver 0.0.0.0:$PORT --noreload --settings=djangoproject.settings.heroku
I've hit a new error now when trying to run a syncdb. It seems to go through the process but nothing takes. In other words, if I run syncdb once and then run it a second time, it wants to set everything up all over again. Any thoughts on what I may have configures wrong to make the DB not save it's data? Should I use the setting for the DB from the dashboard or keep this line in the Django config:
import dj_database_url
DATABASES['default'] = dj_database_url.config()
Thanks
You need requirements.txt or setup.py in the root of the repo.
See https://github.com/heroku/heroku-buildpack-python/blob/master/bin/detect

Categories