Skip to main content

Getting Started with Gunicorn: A Powerful WSGI Server for Your Django Apps

65 views 2 min read read

Learn what Gunicorn is, how it works, and how to use it to serve your Django applications efficiently in production.

What is Gunicorn?

Gunicorn (Green Unicorn) is a WSGI HTTP server for running Python web applications. It's a lightweight and highly efficient way to serve Django applications in production.

Why Use Gunicorn?

  • Performance: Handles multiple requests efficiently.
  • Compatibility: Works with most Python web frameworks.
  • Simplicity: Easy to configure and deploy.

Installing Gunicorn

pip install gunicorn

Running Gunicorn

To serve a Django project, navigate to your project root and run:

gunicorn myproject.wsgi:application --bind 0.0.0.0:8000

Using Gunicorn with Systemd

For a more robust setup, create a systemd service:


[Unit]
Description=Gunicorn daemon for Django project
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/path/to/your/project
ExecStart=/path/to/venv/bin/gunicorn --workers 3 --bind unix:/run/gunicorn.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target

Then enable and start the service:


sudo systemctl start gunicorn
sudo systemctl enable gunicorn

Integrating Gunicorn with Nginx

Gunicorn works best when combined with Nginx for handling client requests efficiently.


server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://unix:/run/gunicorn.sock;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Final Thoughts

Gunicorn is a powerful and efficient WSGI server for serving Django applications in production. By combining it with Nginx and systemd, you can ensure your application runs smoothly and securely.

Related Posts