Deploy/Run a Django app with Gunicorn

Here is my directory structure of Django app:

myproject
├── myproject
│   ├── wsgi.py
|   ├── settings.py 
├── app_folder
├── manage.py
├── virtualenv

Install Guinicorn using pip:

pip install gunicorn

Now you might be wondering what is that wsgi thing is. Well, it stands for Web Server Gateway Interface and basically is a way how apps/frameworks and servers talk to each other. If the server(like Gunicorn) has wsgi implemented and so has your framework(Django), it means that you can run your app with that server. And the entry point of communication for these two is the variable application, which is located in myproject/wsgi.py in our case.

Let’s play with it a little bit to see what it got.

We can bind it to a specific port:

gunicorn --bind 0.0.0.0:8030 myproject.wsgi

You can increase the number of workers to serve requests, which you probable will in real life as your users increase:

gunicorn --workers 3 myproject.wsgi

Run it in a daemon mode:

gunicorn --daemon myproject.wsgi

Or all of them altogether(a shorter version):

gunicorn --daemon -b 0.0.0.0:8030 -w 3 myproject.wsgi

After running your app with gunicorn, go to the django admin panel at localhost:8000/admin. You will see that all styles are gone. The reason is that gunicorn is an application server and it does not serve static files. In order to solve this problem, we will take a look at Nginx next and use it as a reverse proxy for gunicorn. We will talk about what reverse proxy is as well so don’t think about it for now.

Source: http://rahmonov.me/posts/run-a-django-app-with-gunicorn-in-ubuntu-16-04/

FollowFollow on FacebookFollow on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *