What is the difference in the way manage.py runserver serves up a django project compared to gunicorn.wsgi?
I am using nginx + gunicorn to serve my django project. Everything seems to work fine, but when users try to register, they get a 502 Bad Gateway. However, when I run the server off of the development manage.py runserver, the registration goes through fine.
So, what is the difference between manage.py runserver and gunicorn.wsgi and what can I do to make sure gunicorn is configured properly?
Please let me know if you need more details. Thanks
Related
I am learning Django so I've created many Django webapps under one directory. For example,
\webapps
\polls
\polls
\api
\manage.py
...
\ponynote
\ponynote
\frontend
\manage.py
...
I didn't use a virtualenv for developing django apps. I don't know whether it's the reason that causes the problem as below.
App 1
python manage.py runserver works all fine. (default port 8000)
App 2
python manage.py runserver still shows the App 1 page.
Method I tried:
change the port python manage.py runserver 8001, it shows App 2 page.
try to find the process ID PID, and kill it. No sign of port 8000.
However, this isn't the best solution since I can't change the port everytime when developing a new django app. Does anyone have a clue why this happens? Kudos.
Problem solved:
remove web browser cache. In my case, it's Chrome.
One effective solution would be to create a bash script for your use. Create 2 separate bash scripts for your projects (The same dir where your project's manage.py can be found).
For App 1:
# script- App 1
python manage.py runserver 0.0.0.0:8000
For App 2:
# script- App 2
python manage.py runserver 0.0.0.0:8080
And for running:
./yourbashscriptfile
I made some updates to a project: add 1 admin model, add 1 template
I'm using wagtail. I pulled the updates to my server, ran migrations, got success. I restart nginx and gunicorn, I even rebooted the server.
When I go to the wagtail admin, my adminmodel is missing (it exists locally). when I go to create a new page, my template is available, but when I select it I get taken to a wagtail 404 page.
Ubuntu 20.04
ngnix
gunicorn
django/wagtail
digital ocean vpc
digital ocean postgres database cluster
The site works like normal, only a template is available I can't select, and the Model, that migrated, isn't available and isn't showing up in admin. my local version is working perfectly, with no differences. it seems like the server is both updating and not updating. I don't get it. running makemigrations or migrate returns no changes. even when running on the specific app. Do I need to do something to rebot the database?
Listen it looks like caching issue with Nginx. Try clearing the cache.
I have 2 settings files: dev.py, production.py
dev.py is connected to a sqlite3 db and production is connected to the digitalocean postgres cluster.
python manage.py automatically uses dev.py (at least in my setup) so my migrations were working, but they were targeting an old copy of the sqlite.db I had on the server, or perhaps created one as it does when one doesn't exist. Either way, the live site runs on the production.py settings and this is why the changes were being made but not reflecting.
The correct way to run migrations when you have a production.py file is similar to:
python manage.py migrate --settings=<settings app>.<settings folder>.production
I'm also going to add that I found that you need to collectstatic each time you update your css stylesheet. that is a little tedious, and maybe I am missing an automation step, but considering that is the case, just to be safe, I run like this:
python manage.py collectstatic --settings=<settings app>.<settings folder>.production
It doesn't cause any issues and it works, so I use the --settings flag to be safe.
git pull
python manage.py migrate --settings=app.settings.production
python manage.py collectstatic --settings=app.settings.production
sudo systemctl restart ngix
sudo systemctl restart gunicorn
I don't know that restarting ngix is necessary except it solved something with my static files updating I believe.
I developed a django application to handle requests from angular application. Now, should I collect static files for django application while deploying it?
python manage.py collectstatic
Will my django respond to the requests without staticfiles? Because, I found in many documentations that collectstatic is being performed in the steps to deploy django application.
It depends on how you are going to run your Django application. If you are putting it anywhere public, you should have Debug set to False in your settings.py and as such you will need to do the collectstatic step, but not necessary every time you make a change; only if you've added more static files.
The 'Serving the files' section of the documentation (https://docs.djangoproject.com/en/3.1/howto/static-files/) is clear on using runserver in production:
This method is grossly inefficient and probably insecure, so it is
unsuitable for production.
Determine what sort of volume your site is going to have to decide if you want something like nginx being your webserver and proxying requests to your Django app being run by Daphne, Gunicorn or uvicon while nginx (or other fav web service) serves up your static content. If it is too much for the one connection or the server it may make sense to host your static files elsewhere - it really all depends on your use case.
I set up my first django server using python manage.py runserver 0.0.0.0:8000 command. Why is google chrome not loading localhost:8000/ for me even though Microsoft edge is?
I found a solution to this problem.
in your local settings file add the following line:
ALLOWED_HOSTS = ['0.0.0.0', ]
Then you can access it by going to
0.0.0.0:8000
I deployed a django project and everything works great on manage.py, but when I run the default gunicorn server via gunicorn projectname.wsgi, the registration form cannot be submitted and there is a 502 Bad Request Error.
Any ideas?
manage.py runserver seems to boot the celery workers automatically while gunicorn.wsgi does not. Booting all the workers fixed the problem.
Thanks for the comments!