0
0
Djangoframework~5 mins

Gunicorn as WSGI server in Django

Choose your learning style9 modes available
Introduction

Gunicorn helps your Django app talk to the web by running it safely and efficiently. It acts like a waiter taking web requests and giving back responses.

You want to run your Django app on a real web server for users to access.
You need a simple way to serve your app in production without complex setup.
You want to handle many users at once by running multiple worker processes.
You want to connect your Django app to a web server like Nginx for better performance.
Syntax
Django
gunicorn myproject.wsgi:application
Replace myproject with your Django project name.
This command starts Gunicorn using the WSGI application from your Django project.
Examples
Starts Gunicorn with default settings to serve your Django app.
Django
gunicorn myproject.wsgi:application
Runs Gunicorn with 3 worker processes to handle multiple requests at the same time.
Django
gunicorn --workers 3 myproject.wsgi:application
Binds Gunicorn to all network interfaces on port 8000, making your app accessible on your local network.
Django
gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
Sample Program

This command starts Gunicorn with 2 workers, listening only on your computer's local address at port 8000. You can open http://127.0.0.1:8000 in your browser to see your Django app.

Django
# Run this command in your terminal inside your Django project folder

gunicorn --workers 2 --bind 127.0.0.1:8000 myproject.wsgi:application
OutputSuccess
Important Notes

Gunicorn works only with WSGI-compatible apps like Django.

Use multiple workers to improve handling many users but don't use too many to avoid slowing your server.

For public access, combine Gunicorn with a web server like Nginx for security and speed.

Summary

Gunicorn runs your Django app by acting as a bridge between the web and your code.

You start Gunicorn with your Django project's WSGI application to serve requests.

Adjust workers and binding options to control performance and accessibility.