Replace Django's Development Web Server With Gunicorn
For more information on concepts covered in this lesson, you can check out:
Here are the command-line snippets used in this lesson:
$ tree portfolio/
$ vim portfolio/wsgi.py
$ source venv/bin/activate
$ python -m pip install gunicorn
$ python -m pip freeze > requirements.txt
$ vim Procfile
$ git status
$ git commit -am "Replace Django development web server with Gunicorn"
$ git push heroku
$ heroku open
$ heroku logs --tail
$ heroku config:set WEB_CONCURRENCY=4
asgiref==3.4.1
dj-database-url==0.5.0
Django==3.2.7
django-heroku==0.3.1
gunicorn==20.1.0
psycopg2==2.9.2
pytz==2021.3
sqlparse==0.4.2
whitenoise==5.3.0
web: gunicorn portfolio.wsgi
00:00 In this lesson, you’re going to replace the development web server built into Django with a proper HTTP server that can handle traffic concurrently and more securely.
00:10
One of the most popular choices for Django is a pure Python implementation called Gunicorn. When you started a new project, Django generated a management app along with a special module called wsgi
, which stands for the Web Server Gateway Interface.
00:26 WSGI is a Python specification that unifies the interface across different web frameworks, making it easier to interoperate with web servers and replace one framework with another.
00:37 The file generated by Django contains glue code that lets WSGI-compliant web servers such as Gunicorn communicate with the Django framework. You don’t need to know about the file’s contents, but if you’re curious, then there’s actually very little code in there.
00:52
Every time your web server receives a request, it will delegate it to a callable object named application
, which conforms to the specification.
01:04
To install Gunicorn, make sure you’ve activated the right virtual environment and use pip
.
01:15
Remember to freeze your dependencies and refresh the requirements file afterward. Next, modify the Procfile
to take advantage of Gunicorn.
01:30 You’ll need to invoke the Gunicorn command followed by a fully qualified name of the WSGI module in your portfolio management app that you just saw. Notice that you don’t need to provide the network interface nor the port number.
01:43 Gunicorn can pick those from the environment variables set by Heroku.
01:53
Finally, commit and push your changes to Heroku. They should include the updated requirements file and the Procfile
.
02:09
When the build is finished, check if you can still access your Django project on Heroku. Okay, perfect. Now, open the Heroku logs and look for the gunicorn
command.
02:27
You’ll notice that it runs two workers that can handle HTTP requests independently, increasing the overall throughput of your app. The number of worker processes is configurable through an optional WEB_CONCURRENCY
environment variable. Try increasing the number of workers as an exercise, redeploy your app, and verify the logs. In the next lesson, you’ll learn how to connect your app to a relational database in the Heroku cloud.
Become a Member to join the conversation.