Django WSGI and Gunicorn - django

This might be really stupid question; I'm trying to deploy Django application using Gunicorn. However, I just created wsgi.py which looks like below (wsgi.py is in my root project folder):
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)
and now I ran:
python manage.py run_gunicorn
Will Gunicorn automatically picks up this wsgi.py? How does this work? (not sure what wsgi is doing). Or do i need to specify something?

If you have gunicorn listed in INSTALLED_APPS of Django settings module, the command is:
python manage.py run_gunicorn
Not the command you give.

I have Gunicorn to host my django site and this is the config details hope this is helpfull

Related

No module named 'mysite.settings'

I know this question has been asked quite a lot but all the solutions given do not seem to fit for me.
I run this in a venv with python 3.6.8 and django 2.2.10
When I run django from cli it works and all functionality is working great so I know it is not django itself that is failing me.
Path to wsgi script is "/opt/sites/aws/okta/wsgi.py"
Actual wsgi.py:
import os, sys
sys.path.append('/opt/sites/aws')
#path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#sys.path.append(path)
print(sys.path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "okta.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
When running python to see what errors it gives I get
ModuleNotFoundError: No module named 'okta.settings'
I get the same error when running apache with debug logging.
I have quadruple checked the path and the environment variables and they are set right in the wsgi.py script.
Asked for the INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'okta_oauth2.apps.OktaOauth2Config',
'ops.apps.OpsConfig'
]
aws/
bin/
lib/
lib64/
logs/
okta/
__init__.py
urls.py
settings.py
wsgi.py
okta_oauth2/
ops/
share/
static/
manage.py
pyvenv.cfg
Although I did not figure out what was wrong. This is what I did to resolve.
Note for anyone reading this in the future, please DO NOT do this first. I tried other more common results like ImportError: No module named mysite.settings (Django) to fix the issue first and when none worked I tried this.
cd /opt/sites #Just outside the venv
sudo service httpd stop # stop web server
mv aws aws_broken_20201223 # move your broken env to a new name but do not delete
python3 -m venv aws #create new venv
cp -R aws_broken_20201223/(django dirs) aws/ #copy all the custom apps and project you have to the new folder
cd aws #go to the new install
bin/activate #activate your env
pip 3 install -r requirements.txt # I had a predefined requirements.txt for all mods I needed. I would suggest you do the same before doing this.
python3 okta/wsgi.py # Test to see if you are still getting the not found error
sudo service httpd start # start web server back up
voila it works without issue, no config change at all. Very strange but I'll take a working production site then a broken one.

Adding whitenoise to django gives an "ImproperlyConfigured: WSGI application" error

My application works if I were not to add
"whitenoise.middleware.WhiteNoiseMiddleware"
into the MIDDLEWARE in settings.py
but if I were to add it back, then it would not work and would give this error
django.core.exceptions.ImproperlyConfigured: WSGI application 'story_4.wsgi.application' could not be loaded; Error importing module.
This is what is inside my wsgi.py file
#wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'story_4.settings')
application = get_wsgi_application()
Any answer is appreciated.
Problem solved, Please remember to update your Whitenoise to the latest version with pip install --upgrade whitenoise
Mine was still at 3.3.0 and apparently django didn't like it.

how should i add python path in wsgi.py when using virtualenv

I am trying to deploy a django website using apache2.4, mod_wsgi on ubuntu 14.04.
The problem is that my wsgi.py file is unable to import django. this obviously means that i have not set python path for the virtualenv. But i am a little confused as to how to add python path of virtuaenv's site-packages.
my wsgi.py is:
import os
import sys
sys.path.append('/home/sp/webapps')
sys.path.append('/home/sp/webapps/ilog_dev')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ilog_dev.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
so the question is how should i add the virtualenv's site-package to python path
Have you tried path/to/virtualenv/activate before doing anything? Or maybe you didn't use virtualenv's python instead of global python.

ImportError: cannot import name get_path_info

My question is why foreman start not serving css file in dev environment ...? but my app running perfectly fine in heroku production server. Correct me if i am wrong, i thought dj-static is the only option for serving static in foreman env, so in order to work dj-static in wsgi.. follows...
wsgi.py
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
if i am changing above code to
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "readtamil.settings")
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
then foreman start gives me this error
ImportError: cannot import name get_path_info
Try setting dj-static==0.0.6 in your requirements.txt file for Virtualenv. There might be a versioning issue on PyPI or a Django1.7 issue or the Heroku tutorial shows to use 0.0.5.
I've reported this issue to Heroku and their tutorial is now updated to reflect newer versions in the requirements.txt.

Django and gunicorn: Specify path to django.wsgi file in environment specific folders for Heroku

I have my project based on the django startproject: https://github.com/lincolnloop/django-startproject/
Here is my procfile:
web: gunicorn <myproject>.wsgi:application
Right now I have my set up like this:
[myproject]
---[conf]
------[local]
---------settings.py
------[qa]
---------settings.py
---[server_configs]
------[local]
---------django.wsgi
------[qa]
---------django.wsgi
My django.wsgi looks like this:
import os
import sys
import django.core.handlers.wsgi
sys.path.append('/app')
sys.path.append('/app/myproject')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.conf.qa.settings'
application = django.core.handlers.wsgi.WSGIHandler()
I want to use the qa/django.wsgi for Heroku, but I am getting an error saying:
ImportError: No module named [myproject].wsgi
I have already gone through and tried solutions in this post: Configuring gunicorn for Django on Heroku but with no luck on my end.
Right now my PYTHONPATH is /app and have already tried Gunicorn+Django+Heroku. Python Path Issue with no luck either.
I had a similar problem once. Basically, you're running gunicorn from your root directory, but your wsgi is in [my_project]/[server_configs]/[qa]/.
There are two ways to go about it. If each of those directories has an init.py file, you can call it like a normal python module. Use:
web: gunicorn [myproject].[server_configs].[qa].django.wsgi:application
If they don't have init.py files (I didn't), you'll need your Procfile to switch into the proper directory. Try:
web: sh -c 'cd [server_configs]/[qa] && gunicorn django.wsgi:application'
Basically this is running a command in your shell to (1) change directories and (2) run gunicorn.
Heroku allows you to use environment variables in your Procfile. If you define an environment variable on Heroku, like so: heroku config:set WSGI_PATH=path/to/my/file/wsgi.py, then in your Procfile do: gunicorn $WSGI_PATH:application you should be good to go!