Rodando Projeto Django no NGINX com Gunicorn e Supervisor

Sejam, bem vindos ao terceiro post do blog, tendo em vista que já temos um Nginx configurado e um ambiente com virtualenv que foi mostrado nos dois posts anteriores , iremos faze-lo rodar no NGINX.

confira os posts anteriores para que seja dada acontinuação (apenas instalação do Nginx)

Configurando  NGINX, PHP-FPM e Mysql no Fedora


Preparando Virtualenv para projeto Django

Vamos aos passos:

instalação da lib Gunicorn
Web Server Gateway Interface (WSGI)  python

pip install gunicorn
retorno

 

Collecting gunicorn

Downloading gunicorn-19.4.1-py2.py3-none-any.whl (112kB)

 

 

Caso queira escolher outro WSGI
seguem alguns candidatos

https://www.digitalocean.com/community/tutorials/a-comparison-of-web-servers-for-python-based-web-applications

projeto utilizado
http://gunicorn.org/

 

acesse

cd /var/www/python/projetos/teste_projeto

 

 

validando servidor

gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application

 

 

que habilitara o servidor novamente sem a necessidade do manage.py

runserver_Django_puro

 

 

 

 

edite o arquivo

/var/www/python/projetos/teste_projeto/teste_projeto/settings.py

e insira a seguinte linha

STATIC_ROOT = "teste_projeto/static"

 

 

criando pasta para static files

python manage.py collectstatic

retornando

 


You have requested to collect static files at the destination
location as specified in your settings:

/var/www/python/projetos/teste_projeto/teste_projeto/static

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes

Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/timeparse.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/collapse.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/inlines.min.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/urlify.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/collapse.min.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/actions.min.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/inlines.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/jquery.init.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/SelectBox.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/actions.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/calendar.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/prepopulate.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/core.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/SelectFilter2.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/prepopulate.min.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/vendor/jquery/jquery.min.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/vendor/jquery/LICENSE-JQUERY.txt'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/vendor/jquery/jquery.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/vendor/xregexp/LICENSE-XREGEXP.txt'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/vendor/xregexp/xregexp.min.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/icon-calendar.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/icon-changelink.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/LICENSE'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/tooltag-arrowright.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/README.txt'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/search.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/selector-icons.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/icon-unknown-alt.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/tooltag-add.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/inline-delete.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/icon-alert.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/calendar-icons.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/icon-deletelink.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/icon-no.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/icon-yes.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/sorting-icons.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/icon-unknown.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/icon-clock.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/icon-addlink.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/gis/move_vertex_on.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/img/gis/move_vertex_off.svg'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/fonts/Roboto-Light-webfont.woff'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/fonts/Roboto-Regular-webfont.woff'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/fonts/README.txt'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/fonts/LICENSE.txt'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/fonts/Roboto-Bold-webfont.woff'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/css/base.css'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/css/forms.css'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/css/widgets.css'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/css/changelists.css'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/css/dashboard.css'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/css/rtl.css'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/css/login.css'
Copying '/usr/lib/python2.7/site-packages/django/contrib/admin/static/admin/css/fonts.css'

56 static files copied to '/var/www/python/projetos/teste_projeto/teste_projeto/static'

 

coloque o dominio testeprojeto.local

no arquivo /etc/hosts , grave
criando arquivo de configuraçao para host testeprojeto.local

vim /et c/nginx/conf.d/testeprojeto.conf

e insira o seguinte conteudo

 upstream gunicorn_testeprojeto {

 server 127.0.0.1:8009 fail_timeout=0;
 # server 127.0.0.1:8081;
 # ..
 # .

 }

 # Configuration for Nginx
 server {

 # Running port
 listen 80;
 server_name testeprojeto.local;
 # Settings to serve static files
 location ^~ /static/ {

 # Example:
 # root /full/path/to/application/static/file/dir;
 root /var/python/projetos/teste_projeto/teste_projeto/static/;
 #try_files $uri @proxy_to_app;
 }

 # Serve a static file (ex. favico)
 # outside /static directory
 location = /favico.ico {

 root /teste_projeto/favico.ico;

 }

 # Proxy connections to the application servers
 # app_servers
 location / {

 proxy_pass http://gunicorn_testeprojeto;
 proxy_redirect off;
 proxy_set_header Host $host;
 #proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_set_header X-Forwarded-Host $server_name;

 }
 }

importante: apelidar o upstream , no caso gunicorn_testeprojeto

e no proxy_pass deve ser passado este atributo como visto acima
http://gunicorn_testeprojeto

Valide o arquivo de configuração

nginx -t -c /etc/nginx/nginx.conf

e reinicie o servidor

systemctl restart nginx

com o seguinte resultado

gunicorn_ligado

 

mantendo a aplicacao de pe com supervisor

dnf install supervisor

que retornara

 


==================================================================================================
Package Arq. Versão Repo Tam.
==================================================================================================
Instalando:
python-meld3 x86_64 0.6.7-10.fc22 fedora 76 k
supervisor noarch 3.0-2.fc21 fedora 560 k

Resumo da transação
==================================================================================================
Instalar 2 Packages

Tamanho total do download: 637 k
Tamanho depois de instalado: 2.8 M
Correto? [s/N]:s
Baixando pacotes:
(1/2): python-meld3-0.6.7-10.fc22.x86_64.rpm 145 kB/s | 76 kB 00:00
(2/2): supervisor-3.0-2.fc21.noarch.rpm 685 kB/s | 560 kB 00:00
--------------------------------------------------------------------------------------------------
Total 233 kB/s | 637 kB 00:02
Executando verificação da transação
Verificação de transação completa.
Executando teste de transação
Teste de transação completo
Executando a transação
Instalando : python-meld3-0.6.7-10.fc22.x86_64 1/2
Instalando : supervisor-3.0-2.fc21.noarch 2/2
Verificando : supervisor-3.0-2.fc21.noarch 1/2
Verificando : python-meld3-0.6.7-10.fc22.x86_64 2/2

Instalados:
python-meld3.x86_64 0.6.7-10.fc22 supervisor.noarch 3.0-2.fc21
vim /etc/supervisord.d/testeprojetopy.conf
[program:testeprojetopy]
command=/var/www/python/virtual_envs/projeto_teste_um_django-1.9/bin/gunicorn --bind 127.0.0.1:8009 teste_projeto.wsgi:application w9 
directory=/var/www/python/projetos/teste_projeto
logfile=/tmp/testeprojetopy.log
stdout_logfil=/tmp/testeprojetopy.log
autostart=true
autorestart=true

ativando supervisor

systemctl enable supervisord ; systemctl restart supervisord

agora e possivel invocar a aplicacao por

supervisorctl start testeprojetopy

resulta em

testeprojetopy: started

e

supervisorctl stop testeprojetopy

resulta em

testeprojetopy: stopped

gunicorn_desligado

e reativando

supervisorctl start testeprojetopy
testeprojetopy: started

 

gunicorn_ligado

 

É isso aí espero ter sido claro na explicação. Valeu e até a próxima!

 

brunocaramelo

 

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *