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.

sudo apt-get update sudo apt-get upgrade 1 2 sudo apt - get update sudo apt - get upgrade

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.

sudo apt-get install python-virtualenv 1 sudo apt - get install python - virtualenv

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

virtualenv django-virt 1 virtualenv django - virt

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

cd django-virt/ source bin/activate 1 2 cd django - virt / source bin / activate

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

(django-virt)massumo@ubuntu:~/django/django-virt$ 1 ( django - virt ) massumo @ ubuntu : ~ / django / django - virt $

How to Install Django

(django-virt)massumo@ubuntu:~/django/django-virt$ pip install django 1 ( django - virt ) massumo @ ubuntu : ~ / django / django - virt $ pip 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.

(django-virt)massumo@ubuntu:~/django/django-virt$ deactivate sudo apt-get install nginx 1 2 ( django - virt ) massumo @ ubuntu : ~ / django / django - virt $ deactivate sudo apt - get 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.

$ source bin/activate (django-virt)massumo@ubuntu:~/django/django-virt$ pip install gunicorn 1 2 $ source bin / activate ( django - virt ) massumo @ ubuntu : ~ / django / django - virt $ pip install gunicorn

Creating A Django Project

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

(django-virt)massumo@ubuntu:~/django/django-virt$ django-admin.py startproject django_project (django-virt)massumo@ubuntu:~/django/django-virt$ cd django_project (django-virt)massumo@ubuntu:~/django/django-virt/django_project$ python manage.py runserver 1 2 3 ( django - virt ) massumo @ ubuntu : ~ / django / django - virt $ django - admin . py startproject django_project ( django - virt ) massumo @ ubuntu : ~ / django / django - virt $ cd django_project ( django - virt ) massumo @ ubuntu : ~ / django / django - virt / django_project $ python manage . py runserver

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.

(django-virt)massumo@ubuntu:~/django/django-virt/django_project$ gunicorn_django --workers=3 --bind localhost:8000 1 ( django - virt ) massumo @ ubuntu : ~ / django / django - virt / django_project $ gunicorn_django -- workers = 3 -- bind localhost : 8000

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

… from django.core.management.validation import get_validation_errors ImportError: No module named validation 1 2 3 . . . from django . core . management . validation import get_validation_errors ImportError : No module named validation

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 django_wsgi.py edit the file. Open this file from here

/django-virt/lib/python2.7/site-packages/gunicorn/app/ 1 / django - virt / lib / python2 . 7 / site - packages / gunicorn / app /

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

##Old … ##delete this line from django.core.management.validation import get_validation_errors … def make_wsgi_application(): # validate models s = StringIO() if get_validation_errors(s): s.seek(0) error = s.read() msg = "One or more models did not validate:

%s" % error print(msg, file=sys.stderr) sys.stderr.flush() sys.exit(1) translation.activate(settings.LANGUAGE_CODE) if django14: return get_internal_wsgi_application() return WSGIHandler() … ##Yeni Hali … def make_wsgi_application(): # validate models s = StringIO() import django from django.core.management.base import BaseCommand django.setup() cmd = BaseCommand() import sys cmd.stdout, cmd.stderr = sys.stdout, sys.stderr cmd.check() translation.activate(settings.LANGUAGE_CODE) if django14: return get_internal_wsgi_application() return WSGIHandler() … 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 ##Old . . . ##delete this line from django . core . management . validation import get_validation _ errors . . . def make_wsgi_application ( ) : # validate models s = StringIO ( ) if get_validation_errors ( s ) : s . seek ( 0 ) error = s . read ( ) msg = "One or more models did not validate:

%s" % error print ( msg , file = sys . stderr ) sys . stderr . flush ( ) sys . exit ( 1 ) translation . activate ( settings . LANGUAGE_CODE ) if django14 : return get_internal_wsgi_application ( ) return WSGIHandler ( ) . . . ##Yeni Hali . . . def make_wsgi_application ( ) : # validate models s = StringIO ( ) import django from django . core . management . base import BaseCommand django . setup ( ) cmd = BaseCommand ( ) import sys cmd . stdout , cmd . stderr = sys . stdout , sys . stderr cmd . check ( ) translation . activate ( settings . LANGUAGE_CODE ) if django14 : return get_internal_wsgi_application ( ) return WSGIHandler ( ) . . .

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

(django-virt)massumo@ubuntu:~/django/django-virt/django_project$ gunicorn_django --workers=3 --bind localhost:8000 1 ( django - virt ) massumo @ ubuntu : ~ / django / django - virt / django_project $ gunicorn_django -- workers = 3 -- bind localhost : 8000

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 gunicorn_config.py we create a file named, and add the following lines to it.

command = '/home/massumo/django/django-virt/bin/gunicorn' pythonpath = '/home/massumo/django/django-virt/django_project' bind = '127.0.0.1:8000' workers = 3 1 2 3 4 command = '/home/massumo/django/django-virt/bin/gunicorn' pythonpath = '/home/massumo/django/django-virt/django_project' bind = '127.0.0.1:8000' workers = 3

We then run our project through this file as follows.

(django-virt)massumo@ubuntu:~/django/django-virt$ gunicorn -c gunicorn_config.py django_project.wsgi 1 ( django - virt ) massumo @ ubuntu : ~ / django / django - virt $ gunicorn - c gunicorn_config . py django_project . wsgi

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.

#service nginx start 1 #service nginx start

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

#####file name 'Open Source Projects' server { server_name localhost; access_log off; listen 80; location /static/ { alias /home/massumo/django-virt/django_project/static/; } location / { proxy_pass http://127.0.0.1:8000; proxy_set_header X-Forwarded-Host $server_name; proxy_set_header X-Real-IP $remote_addr; add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"'; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #####file name 'Open Source Projects' server { server_name localhost ; access_log off ; listen 80 ; location / static / { alias / home / massumo / django - virt / django_project / static / ; } location / { proxy_pass http : //127.0.0.1:8000; proxy_set _ header X - Forwarded - Host $ server_name ; proxy_set _ header X - Real - IP $ remote_addr ; add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"' ; } }

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/nginx.you 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.

massumo@ubuntu:/etc/nginx/sites-available$ sudo ln myproject ../sites-enabled/myproject $sudo service nginx restart 1 2 massumo @ ubuntu : / etc / nginx / sites - available $ sudo ln myproject . . / sites - enabled / myproject $ sudo service nginx restart

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.

/django-virt$ gunicorn -c gunicorn_config.py django_project.wsgi 1 / django - virt $ gunicorn - c gunicorn_config . py django_project . wsgi

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.

sudo apt-get install supervisor 1 sudo apt - get install supervisor

How to Configure Supervisor

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

##file name 'django_project.conf' [program:project] command = /home/massumo/django/django-virt/bin/gunicorn -c /home/massumo/django/django-virt/gunicorn_config.py django_project.wsgi 1 2 3 4 ##file name 'django_project.conf' [ program : project ] command = / home / massumo / django / django - virt / bin / gunicorn - c / home / massumo / django / django - virt / gunicorn_config . py django_project . wsgi

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

sudo supervisorctl reread sudo supervisorctl update 1 2 sudo supervisorctl reread sudo supervisorctl update

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.