Try Us Free for 30 Days – Your Wallet Can Thank Us Later!

Try Us Free for 30 Days – Your Wallet Can Thank Us Later!

How to easily setup Gunicorn Django app?

How to easily setup Gunicorn Django app

Django is a popular way to build Python applications. Although it comes with a built-in server, it may create problems in production environment.

That’s where Gunicorn server helps. Gunicorn server can run python applications in multiple platforms.

But, to run Python apps, we need to configure Gunicorn to work with Django.

At 1 onlyhost , we often get requests from customers to deploy Gunicorn Django server as part of our Technical Support Services.

Today, we’ll see how our Dedicated Engineers setup Gunicorn server for hosting Python apps.

 

 

How Gunicorn helps?

Firstly, let’s see more details about Gunicorn.

Gunicorn aka ‘Green Unicorn’ is a pure-Python server for UNIX. The major benefits of Gunicorn is that it has no dependencies and is easy to install and use.

As we already saw, the built-in server of Django has problems when scaling up. That is, it will work fine on your personal computer. You can also share it with your friends too. But, when you port the application to real production servers, things may not work. Additionally, Python apps can show dependency errors with the platform used in live servers too.

Luckily, Gunicorn server is broadly compatible with a number of web frameworks. It is rather simple to implement and light on server resources usage. Therefore, it can speed up the apps.

That’s why, our Dedicated Engineers often recommend Gunicorn as a popular way to run Python applications.

 

Steps to setup Gunicorn Django server

Now, let’s see the steps to setup Gunicorn server and make the Django app working on it.

 

1. Install Gunicorn

As the first step to deploy Django application, we need to set up the Gunicorn server. Here, we first setup the server with the operating system of customer’s choice. To install the Gunicorn server, our Dedicated Engineers first activate the virtualenv in Django application. Then, proceed with the Gunicorn installation using the command:

pip install gunicorn

Prior to the installation, we always ensure that the server has supported Python versions. For example, if it is using Python 2, it should have Python version greater than 2.6, whereas in Python 3, it should be at least Python3.4.

 

2. Integrate Gunicorn with Django

Now, it’s the time to integrate Gunicorn with Django. Basically, there are 2 ways to use Gunicorn with Django. We’ll check each method in detail.

 

a) Django in Gunicorn as WSGI application

In the first method, Django app works in Gunicorn as a Web Server Gateway Interface (WSGI) application. Here, Gunicorn server implements the web server side for running Python web applications.

After installing Gunicorn in the server, a gunicorn command is available which starts the Gunicorn server process. So, to make the application work, we just need to call gunicorn with the location of the WSGI application object.

Thus, for a typical Django project mywork.wsgi , our Support Engineers invoke gunicorn using:

gunicorn mywork.wsgi

This will start one process running one thread listening on 127.0.0.1:8000. Here, we always ensure that the port 8000 is open in the server firewall. Additionally, we see that the Django project is correctly set in the Python path.

 

b) Gunicorn’s Django integration

Similarly, it is possible to use Gunicorn’s built-in Django integration.

For this to work, our Support Engineers first add “gunicorn” to INSTALLED_APPS. We add a run_gunicorn command to the ./manage.py by adding gunicorn to the INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    "gunicorn",
)

Then we run:

python manage.py run_gunicorn

Fortunately, this provide many advantages like validating installed models, allowing an option –adminmedia  for passing in the location of the admin media files, etc.

 

Common usability issues with Gunicorn and their fixes

Although Gunicorn works great with Django apps, we often see customers experiencing usability problems with it. Let’s have a look at the top problems and how our Dedicated Engineers fix them.

 

1. Django – Gunicorn bind problem

Recently, one of our customers reported problems with gunicorn – django bind. When he was executing the bind command, it showed the error:

Import error: no module named django.conf

Here, we began troubleshooting by tracing the path of the application files. They were all intact. We further checked the log files at /var/log/gunicorn.log. And, the problem appeared as the bind was missing the OS path. To fix the problem, we had to make customer execute the bind command in the Virtual environment. Also, we added OS path to the bind command too.

 

2. 502 Bad Gateway Error

Similarly, 502 errors can pop up when using Gunicorn Django applications with Nginx web server. This happens when Nginx is unable to find the gunicorn.sock file at the given location. Additionally, errors show up when Nginx is unable to connect to the Gunicorn socket because of permissions problems too.

In either case, the fix is to make the connection to the Gunicorn socket working. Therefore, our Support Engineers first ensure that Gunicorn is running on the server and the socket is available to listen. Further, we correct the permissions on the directories leading up to the socket.

[Having trouble with Gunicorn setup on the server? We can fix it right away.]

 

Conclusion

In short, deploying Gunicorn Django servers make Python hosting easier. Today, we saw how our Support Engineers setup Gunicorn to work with Django and fix common problems with it.

Picture of Ramandeep Singh Sethi

Ramandeep Singh Sethi

Hello, I’m Ramandeep Singh Sethi a Public Speaker, Entrepreneur, Philanthropist, and the CEO & Founder of three pioneering technology companies: Hydizo Global Solutions, 1Only Host, and Leadtuts.
Share the Post:

Related Posts

Scroll to Top