How to create mysite.wsgi file? - python

I am following this tutorial http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html to setup django with nginx and uwsgi. But I am confused about this line:
uwsgi --http :8000 --module mysite.wsgi
In the tutorial there's nothing about mysite.wsgi file. What should be the content of this file?

Actually, that's not a filename.
That's a python module path.
The relevant file is actually mysite/wsgi.py (but to import it in a python interpreter, you'd have to import mysite.wsgi, hence the name used in command line).

Related

What is the command for starting a WSGI server for Django?

I've developed an application in Django that I usually run in development mode:
python manage.py runserver
I do the same for my deployed instances - obviously a security issue that I now want to resolve.
From the Django docs, its not clear to me how to:
For simplicity sake, I picked wsgi (over asgi): https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/ . From this page, its not clear to me how my 'runserver' command changes to run the wsgi server over the development server. Should I run the wsgi.py file? That doesn't seem to do anything.
From the page above, its not clear whether wsgi is actually a server, or more a platform/type of servers. Do I need to use uwsgi/uvicorn/etc. instead?
I'm testing on windows - uvicorn is unix only, so I tried uwsgi, but thats giving me this error message: AttributeError: module 'os' has no attribute 'uname' - I guess its Unix only as well
So I'm using the docker image I was already building for deployment - uwsgi is giving me issues again because my docker image has no compiler, so I try now with gunicorn.
that should be easy: gunicorn project.wsgi, which gives me:
ModuleNotFoundError: No module named 'project/wsgi'
my folder structure looks like:
root-folder
project
wsgi.py
settings.py
django_app_1
django_app_2
manage.py
As the manual says, the gunicorn command should work as long as you run the gunicorn command from the same location as manage.py - which is what I'm doing.
I guess I'm missing something very obvious - who knows what?
The wsgi.py file just gives you a WSGI compatible application that a WSGI HTTP server (such as Gunicorn) can run.
I guess you have to run gunicorn project.wsgi from the root folder (that one containing the project module).
Typically, the directory containing manage.py and the module in which wsgi.py resides are one and the same. But not in your case.

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!

How can I deploy Django Gunicorn under Apache proxy?

I've run through the documentation and am hitting the same pages over and over again. At present I've found documentation to run off an existing myapp.wsgi file, but documentation for how to make an appropriate wsgi file is a little harder to find.
If I want to make, proxied by Apache, the equivalent of, on an older version of Gunicorn etc.:
python manage.py run_gunicorn 0.0.0.0:8888
what should I be doing to supply a WSGI file for:
gunicorn project.wsgi:application
Thanks,
Why do you think you need a special wsgi file? You just use exactly the same one you would use for any other deployment.
I assume you're not asking about the content of the wsgi file (which is often pretty standard) but how to assemble a gunicorn command line that allows gunicorn to find/use the wsgi file for your project?
When deploying to a staging server, I recall having a problem getting the Python path set so that gunicorn could find the proper wsgi file for my app. Here is a trimmed version of the gunicorn command that I ended up using:
gunicorn --pythonpath /some_path/my_app/my_app my_app.wsgi:application
In plain English, starting from the right and working backward:
There is a function named application inside a the wsgi.py file
The wsgi.py file is inside a module named my_app (which in this case is a directory containing a __init__.py file)
The my_app module is located at /some_path/my_app/my_app, so this needs to be on the PYTHONPATH.
So the full path to your wsgi.py is:
/some_path/my_app/my_app/my_app/wsgi.py.
In the same directory as wsgi.py is a __init__.py file, which causes Python to recognize that directory as a module.

Deploying a Pyramid application using uWSGI and Cherokee

I'm attempting to setup a generic Pyramid project to work with uWSGI through Cherokee, but constantly get a "no app loaded" error. All the research I've done doesn't really give me much to go on. Anyone have any ideas? Please note that I 'am' using a virtualenv via virtualenvwrapper.
This is from my development.ini
[uwsgi]
socket = 127.0.0.1:2626
master = true
processes = 1
virtualenv = /home/user/.virtualenvs/pyramid/
pythonpath = /home/user/Projects/ConventionMeStatic
And this is the command I've been trying to use to launch it: /usr/bin/uwsgi --ini development.ini --plugin python.
I can post any further details but there have been no other changes made to the project itself.
You have specified a virtualenv and a pytonpath, but you have not specified which app to load.
If you have a single-file app you can load that file with the --wsgi-file option, if you have a deployment.ini file you can use the --paste option as described here
http://projects.unbit.it/uwsgi/wiki/UsePaste
or the --ini-paste shortcuts described in examples section of the uwsgi wiki

Configuring gunicorn for Django on Heroku

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.

Categories