Django Deployment with nginx - gunicorn from another server

喜你入骨 提交于 2019-12-02 12:31:11

What you're trying to do doesn't make any sense. You can certainly use a single nginx on a separate server, but gunicorn is the application server and belongs with the application.

There is no "Django server" so your first question is irrelevant. gunicorn will need to serve on a port, not a socket, and the nginx proxy_pass will point to that port.

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 ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!