Install and Configure – Django Virtualenv, Gunicorn, Nginx and Supervisor


Hi, in this article we will create a Virtualenv, then create a django project in Virtualenv, how to run this project on gunicorn. We will talk about how to talk about this project with Nginx and finally how to transfer Gunicorn to Supervisor.
Before going to our post, let’s update our system by running the following commands in order.

Python Virtualenv

Python Virtualenv creates a special Python workspace for us. And the libraries we install do not affect the overall system by loading only in this area. We did ensure that the installation is valid only here. What advantages do you have? You can easily publish projects developed like 1.6 and 1.7 with different Django versions under a single system. First, we set Virtualenv to our system by issuing the following command.

Let’s create a special Virtualenv for ourselves after the installation.

After the creation process, you can examine the index contents. Let’s go into the directory we created and activate it.

After this you will see that the command line has changed. The libraries we will install now will be installed in this virtualenv.

How to Install Django

by running the command, we install the Django into our virtual workspace.

How to Install Nginx

Let’s install nginx on our system. To do this, first run the following command from virtualenv. Then let’s give the command to install nginx.

How to Install Gunicorn

Gunicorn is a very powerful Python WSGI HTTP server. Now we’re going to go back into virtualenv and build Gunicorn.

Creating A Django Project

After successfully installing gunicorn, we can build a django project.

Note : the reason I type the entire command line here is to make it easy for you to follow the current directory and workspace.
Our project has successfully stood up. Now let’s get the project up with Gunicorn.

If the Django project you created is 1.7 and above, you will get an error if you run this command.

This error is caused by the removal or replacement of some classes starting with Django 1.7. It’s the same problem here. All you have to do to get rid of this error is to set up virtualenv in gunicorn edit the file. Open this file from here

Under this directory open the file and restore it from its old state.

After you perform this correction, we run the same command again.

Now it worked flawlessly. From the browser you can see that it works by typing localhost:8000. You can change the value of workers here. Now let’s create a config file for gunicorn. Then let’s run our project through this file by typing our settings into this file. To the home directory of virtualenv we create a file named, and add the following lines to it.

We then run our project through this file as follows.

and its done.

How to Configure NGINX

We’ve set up nginx before. Now we will make adjustments to run our project on Nginx. Let’s start nginx first.

Then we create a conf file by going into /etc/nginx/sites-available directory.

You’ve got your attention here. Nginx is listening to the project we got up on the 8000 port with Gunicorn. If you have just finished executing the project from the command line, press CTRL+C, and so on. You can’t run the project through nginx. So do this by opening another terminal without closing it. So here, nginx acts as a kind of tunneling. He’s listening to the project we’ve got up with Gunicorn, showing it to the user. Now, let’s move on.
We created the config file in /etc/nginx/sites-available directory. However, when transferring files from /etc/nginx/sites-enabled to the main config file, nginx transfers files from /etc/nginx/sites-enabled to the main config file. It is /etc/nginx/ can see the conf file by examining it. That’s why we created the file ..we will link to the /sites-enabled directory. To do this, run the following command.

If you type localhost from the browser, you can see that the project is running. If an error occurs, check your transactions.

How to Install Supervisor

So far, we ran our project through gunicorn on nginx. But you may have noticed that there’s something missing here. We need to run our project on the terminal gunicorn with the following command so that it can be accessed via nginx.

So we have to keep track of this. If we turn the computer off and on, we have to do this again. That’s what Supervisor will do for us. Let’s set it up first. After you exit Virtaulenv, run the following command.

How to Configure Supervisor

After installing /etc/supervisor/conf.d open the D directory and create the config file.

Run the following commands in order to show the config file to Supervisor.

Our transactions were successful. We don’t have to run gunicorn from the scanner anymore. The Supervisor will take care of this for us. Even if you restart your computer now you should have an application running on Nginx. From the browser http://localhost you can reach by typing.

  • Design

Leave A Reply

Your email address will not be published.