> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Run Flask or Django in Production with Gunicorn

> Gunicorn for Flask and Django on Square Cloud: bind to 0.0.0.0:80, size workers to your RAM with gunicorn.conf.py and set the START command in your config.

Gunicorn is a Python WSGI HTTP server that runs Flask, Django and other WSGI applications in production. This guide shows how to start your application with Gunicorn, tune it for the RAM of your plan and run it on Square Cloud.

## Install Gunicorn

Install Gunicorn on your machine:

```bash theme={"system"}
pip install gunicorn
```

Add `gunicorn` to your `requirements.txt` too, so Square Cloud installs it with your other dependencies:

```txt requirements.txt theme={"system"}
flask
gunicorn
```

## Basic usage

To run an application with Gunicorn, you need a WSGI application, such as one built with Flask or Django, and you need to know the module name and the variable name that holds the WSGI application object.

For example, if you have a Flask application in a file named `app.py` and the Flask instance is named `app`, you can run it with Gunicorn using the following command:

```bash theme={"system"}
python -m gunicorn --bind 0.0.0.0:80 app:app
```

In this command:

* `python -m gunicorn` runs Gunicorn as a Python module.
* `--bind 0.0.0.0:80` tells Gunicorn to listen on all network interfaces on port 80.
* `app:app` specifies the module name (`app`) and the variable name (`app`) that contains the WSGI application object.

For Django, point Gunicorn to the `wsgi.py` module Django creates inside your project package (replace `myproject` with your project's name):

```bash theme={"system"}
python -m gunicorn --bind 0.0.0.0:80 myproject.wsgi:application
```

## Additional options

Gunicorn provides several options to customize its behavior. These are the most common ones; the [official Gunicorn settings reference](https://gunicorn.org/reference/settings/) lists them all.

### Workers

* `--workers <number>`

This option sets the number of worker processes for handling requests. Each worker is a separate process with its own copy of your application in memory, so on Square Cloud the limit is usually RAM, not CPU. Start with 2 workers and add more only after raising `MEMORY`. If your application stops with `LACK_OF_RAM`, lower the number of workers.

<Warning>Don't size workers with `multiprocessing.cpu_count()` or the `(2 x $num_cores) + 1` formula: inside a container, the core count can be the host machine's, which starts dozens of workers and exhausts your RAM.</Warning>

### Name

* `--name <name>`
* `-n <name>`

This option sets the process name for Gunicorn.

### Worker class

* `--worker-class <class>`
* `-k <class>`

This option sets the type of worker to use. The default is `sync`, but you can also use `gevent`, `eventlet`, `tornado` or other worker type.

## Config file

You can also set Gunicorn options in a Python configuration file. Gunicorn automatically loads a file named `gunicorn.conf.py` from the directory where it runs, which is your project root on Square Cloud. For any other file name, pass it with `-c <file>`.

```python gunicorn.conf.py theme={"system"}
import os

# Square Cloud sets PORT=80; the fallback keeps the file working anywhere
bind = f"0.0.0.0:{os.getenv('PORT', '80')}"
# Each worker uses its own share of RAM: raise WEB_CONCURRENCY only together with MEMORY
workers = int(os.getenv("WEB_CONCURRENCY", 2))
```

With this file in your project, the start command no longer needs the `--bind` option.

## Run it on Square Cloud

Create a `squarecloud.app` file at the root of your project and put the Gunicorn command in its `START` field, so Square Cloud starts your application with Gunicorn instead of the default `python app.py`:

<CodeGroup>
  ```systemd Command line theme={"system"}
  DISPLAY_NAME=My Flask App
  MAIN=app.py
  MEMORY=512
  VERSION=recommended
  SUBDOMAIN=my-flask-app
  START=python -m gunicorn --bind 0.0.0.0:80 app:app
  ```

  ```systemd With gunicorn.conf.py theme={"system"}
  DISPLAY_NAME=My Flask App
  MAIN=app.py
  MEMORY=512
  VERSION=recommended
  SUBDOMAIN=my-flask-app
  START=python -m gunicorn app:app
  ```
</CodeGroup>

<Note>Gunicorn must bind to `0.0.0.0:80`: Square Cloud delivers your subdomain's traffic to port `80` of your application. Square Cloud also sets the `PORT` environment variable to `80`, and Gunicorn reads it: without a `--bind` option, it listens on `0.0.0.0:$PORT` when `PORT` is set.</Note>

For Django, use `myproject.wsgi:application` in `START` and your project's `manage.py` as `MAIN`. The [Flask](/en/tutorials/api/flask) and [Django](/en/tutorials/api/django) tutorials cover the full deploy.

## Next steps

<CardGroup cols={3}>
  <Card title="Flask" icon="flask" href="/en/tutorials/api/flask">
    Deploy a Flask application behind Gunicorn.
  </Card>

  <Card title="Django" icon="python" href="/en/tutorials/api/django">
    Deploy a Django project behind Gunicorn.
  </Card>

  <Card title="App won't start" icon="triangle-exclamation" href="/en/platform/troubleshooting/app-wont-start">
    Fix EADDRINUSE, LACK\_OF\_RAM and other startup failures.
  </Card>
</CardGroup>

## Did you like this article?

* We created this content with great care to offer the best help possible.
  If this article helped you in any way, support our work! Developers have already rated Square Cloud **4.9/5 across 402 reviews** on Google and Trustpilot: leave yours too! It helps us understand what matters most to you.

<CardGroup cols={2}>
  <Card title="Google Reviews" icon="google" href="https://g.page/r/CYZenpQcDgTzEAI/review">
    Leave your review in Google Reviews.
  </Card>

  <Card title="Trustpilot" icon="star" href="https://www.trustpilot.com/review/squarecloud.app">
    Leave your review in Trustpilot.
  </Card>
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.