Run Django application using Gunicorn

To run a Django application using Gunicorn, you can follow the steps below:

  1. Install Gunicorn: Start by installing Gunicorn using pip, which is the package installer for Python. Open your command prompt or terminal and enter the following command:

pip install gunicorn

  1. Navigate to your Django project directory: Use the cd command to navigate to the directory where your Django project is located. For example:

cd /path/to/your/django/project

  1. Activate your virtual environment (if applicable): If you are using a virtual environment for your Django project, activate it using the appropriate command for your operating system.

  2. Start Gunicorn: To start Gunicorn, use the following command:

gunicorn your_project_name.wsgi:application

Replace your_project_name with the actual name of your Django project. The wsgi:application part refers to the WSGI application object that Gunicorn needs to start the Django application.

  1. Specify the number of workers (optional): By default, Gunicorn will run one worker process. You can specify the number of worker processes using the -w flag. For example, to run 4 worker processes, use the following command:

gunicorn -w 4 your_project_name.wsgi:application

Adjust the number of workers based on the resources available on your server.

  1. Specify the address and port (optional): By default, Gunicorn will serve your Django application on localhost:8000. You can specify a different address and port using the -b flag. For example, to serve on 0.0.0.0:8000, use the following command:

gunicorn -b 0.0.0.0:8000 your_project_name.wsgi:application

Replace 0.0.0.0:8000 with your desired address and port.

  1. Test your Django application: Open your web browser and visit the address and port where your Django application is being served. You should see your Django application running successfully.

That's it! Your Django application is now running using Gunicorn. Gunicorn is a powerful tool for serving Django applications, providing improved performance and scalability. It's commonly used in production environments to handle incoming web requests efficiently.