Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
723 views
in Technique[技术] by (71.8m points)

Django Deployment with nginx - gunicorn from another server

I'm trying to deploy my Django Project through different application server : Apache2, Nginx/gunicorn, ...

It works fine with Apache2 or Nginx, but I would like to dissociate these application server with my Django Projet. I would like to get an environment like this :

  • Server 1 (172.30.10.92) : Django Project & wsgi
  • Server 2 (172.30.10.93) : Nginx/gunicorn

Why ? Because later I will have some Django applications, but I would like to use just one application server.

I think I'm making a mistake with my configuration files syntax.

I have in /etc/nginx/sites-available/DatasystemsCORE.conf :

server {
    listen 8000;
    server_name 172.30.10.92; 

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
            root http://172.30.10.92:/var/www/html/;
    }

    location / {
            include proxy_params;
            proxy_pass http://172.30.10.92/unix:/var/www/html/DatasystemsCORE/dscore.sock;
    }
}

server_name : Is it IP adress from Django server or nginx server ? I think it's the first one but I'm not sure.

proxy_pass : I think there is an issue in my path

Then, I'm executing this command :

gunicorn --daemon --workers 3 --bind 172.30.10.92/unix:/var/www/html/DatasystemsCORE/dscore.sock 172.30.10.92:/var/www/html/DatasystemsCORE/DatasystemsCORE.wsgi

One more time, I thing there is a syntax problem because I'm getting 502 Bad Request

How I can add IP address from a distant Django server there ?

Thank you !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found How to solve my issue and I made a tutorial (based on my case) in order to help everyone who would like to make the same thing.

My file is there : Download tutorial file

But this is the same tutorial written in English.

My Django IP server : 172.30.10.92

My Nginx IP server : 172.30.10.93

1- Install and Configure wsgi (located on Django server)

WSGI is a file created with your Django project.

The file is located in /path/to/your/project/Myproject/wsgi.py

We have to edit this file like this :

import os
from django.core.wsgi import get_wsgi_application

import sys sys.path.append('/var/www/html/Myproject')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Myproject.settings")
application = get_wsgi_application()

2- Install and Configure gunicorn/supervisor (located on Django server)

In order to install gunicorn/supervisor, you have to execute in your shell :

pip install gunicorn
pip install supervisor

Then, you have to create a new file in /etc/supervisor/conf.d/Myproject.conf which looks like this :

[program:Myproject]
command = /home/valentin/.virtualenvs/MyprojectEnv/bin/gunicorn Myproject.wsgi:application --name "Myproject" --workers=4 --bind=0.0.0.0:8080 -- user="valentin" --group="valentin" ; Command to start app
user = username #You have to replace by your username
stdout_logfile = /var/log/supervisor/supervisor.log
redirect_stderr = true
log
environment=LANG=fr_FR.UTF-8,LC_ALL=fr_FR.UTF-8

I specified port 8080 which is the communication port between my application server and my web server.

3- Edit hosts file on nginx server (located on nginx server)

You have to edit your hosts file located to /etc/hosts and add a new entry to your Django server :

127.0.0.1 localhost 
127.0.1.1 valentin 
172.30.10.92 Myproject

# The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

4- New config file in nginx repository (located on nginx server)

This new file should be placed in /etc/nginx/sites-available/Myproject.conf

 server {
    listen 8080;
    server_name Myproject;

    root /var/www/html/Myproject/;

    location /static/ {
        root /var/www/html/;
    }

    location / {
        include proxy_params;
        proxy_pass http://172.30.10.92:8080;
        }       
}

The IP address corresponds to my Django server address. I specified the listen port (8080), the path to my Django project & static directory.

Then, you have to create a symbolic link to sites-enabled.

After ths operation, restart nginx service :

sudo service nginx restart

5- Allow nginx IP address in Django (located on Django server)

You have to edit your settings.py file in order to allow nginx IP address in ALLOWED_HOSTS :

ALLOWED_HOSTS = ['localhost', '172.30.10.93', '127.0.0.1', '[::1]']

6- Finally execute gunicorn (located on Django server)

Finally, you have to start gunicorn. You should be inside your Django root project and execute :

gunicorn Myproject.wsgi:application --bind 172.30.10.92:8080

Now, in your browser, try to connect to your nginx server with the port :

http://172.30.10.93:8080

It works !

PS : This tutorial works for me, if it doesn't work for you, maybe I missed something, or maybe you didn't make exactly like me ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...