1 | initial version |
Setting up Gunicorn to work with your Django project is relatively straightforward. Follow these steps to set up Gunicorn:
Install Gunicorn using pip:
pip install gunicorn
To run Gunicorn, navigate to the root directory of your Django project (where the manage.py file is located) and execute the following command:
gunicorn your_project_name.wsgi:application
Replace yourprojectname with the actual name of your Django project. This command will start Gunicorn on port 8000 by default.
To run Gunicorn on a different port, you can use the --bind option followed by the IP address and port number:
gunicorn --bind 0.0.0.0:8000 your_project_name.wsgi:application
This command binds Gunicorn to all available IP addresses (0.0.0.0) and port 8000.
To run Gunicorn as a background process, you can use the --daemon option:
gunicorn --bind 0.0.0.0:8000 --daemon your_project_name.wsgi:application
This command runs Gunicorn as a daemon, so it will continue running in the background even after you close the terminal.
To run Gunicorn with multiple worker processes, use the --workers option followed by the number of worker processes you want to run:
gunicorn --bind 0.0.0.0:8000 --workers 4 your_project_name.wsgi:application
This command starts Gunicorn with four worker processes, which can help improve performance for concurrent requests.
Once you have Gunicorn running, you can configure your web server (e.g., Nginx or Apache) to proxy requests to Gunicorn. In the previous response, I provided an example of configuring Nginx to work with Gunicorn. Please refer to that example if you're using Nginx. If you're using Apache, the configuration will be different; you'll need to use the mod_proxy module to proxy requests to Gunicorn.
For more information about Gunicorn and its various options, you can visit the official documentation: https://docs.gunicorn.org/en/stable/index.html