Jump to content
hades

[Tutorial] How to Deploy a Python/Django app

Recommended Posts

Tinand cont ca pe internet e plin de tutoriale de genu (o mare parte din ele nu prea sunt de actualitate) am zis sa scriu un mini-tutorial despre cum trebuie configurat si pregatit un server pentru deploy-ul unei aplicatii Django.

1. Pregatirea serverului & instalarea librariilor

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-dev python virtualenv

2. Instalam nginx:

sudo apt--get install nginx

3. Install mysql:

sudo apt-get install mysql-server
sudo apt-get install libmysqlclient-dev
sudo apt-get install mysql-python

4. Creem un user nou; User ce-l vom folosi pentru deploy-ul propriuzis.

adduser example

5. Creem directorul proiectului; Virtualenv pentru el. Instalam requirements-urile proiectului

mkdir app
cd app; virtualenv venv
cd venv; source bin/activate

Acum ca avem virtualenv-ul activat, facem un pip install -r requirements.txt (asta depinde de unde se afla requirements-urile voastre.

6. Instalam gunicorn (un python wsgi http server de unix)

pip install gunicorn

Pentru a testa daca totul functioneaza cum trebuie pana aici:

gunicorn -b 0.0.0.0:8080 project.wsgi

7. Creem un mic bash script pentru gunicorn, ce va fi folosit de supervisor.

Bash scriptul o sa arate cam asa:

#!/bin/bash

NAME="app" # Numele Aplicatiei
DJANGODIR=/home/example/app/project/ # Django project directory
SOCKFILE=/home/example/app/project/gunicorn.sock # we will communicte using this unix socket
USER=example # the user to run as
GROUP=www-data # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=app.settings # which settings file should Django use
DJANGO_WSGI_MODULE=app.wsgi # WSGI module name

echo "Starting $NAME as `whoami`"

# Activam virtualenv
cd $DJANGODIR
. ../venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start gunicorn
../venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--bind=unix:$SOCKFILE \
--user="$USER" \
--log-level=debug \
--log-file=-

8. Scriem configul pentru supervisor:

[program:app]
command = /home/example/app/venv/gunicorn_script.sh
user = example
group = www-data
stdout_logfile = /home/example/app/project/logs/supervisor.log
redirect_stderr = true
enviroment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8

Il pornim:

sudo supervisorctl start app

9. Ce mai ramane acum de facut e sa scriem configul de nginx in asa fel incat sa ne foloseasca drept proxy pentru servirea aplicatiei.

Un config foarte basic de nginx arata cam asa:

upstream app_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).

server unix:/home/example/app/project/gunicorn.sock fail_timeout=0;
}

server {

listen 80;
server_name example.com;

client_max_body_size 4G;

access_log /home/example/app/project/logs/nginx-access.log;
error_log /home/example/app/project/logs/nginx-error.log;


location / {
# an HTTP header important enough to have its own Wikipedia entry:
# http://en.wikipedia.org/wiki/X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

# enable this if and only if you use HTTPS, this helps Rack
# set the proper protocol for doing redirects:
# proxy_set_header X-Forwarded-Proto https;

# pass the Host: header from the client right along so redirects
# can be set properly within the Rack application
proxy_set_header Host $http_host;

# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;

# set "proxy_buffering off" *only* for Rainbows! when doing
# Comet/long-poll stuff. It's also safe to set if you're
# using only serving fast clients with Unicorn + nginx.
# Otherwise you _want_ nginx to buffer responses to slow
# clients, really.
# proxy_buffering off;

# Try to serve static files from nginx, no point in making an
# *application* server like Unicorn/Rainbows! serve static files.
if (!-f $request_filename) {
proxy_pass http://example_app_server;
break;
}
}
}

In linii mari (nu chiar mari, ca am cuprins toti pasii importanti) asa ar arata pregatirea unui server pentru un deploy Django/Python. Ramane de vazut acum cum va jucati cu permisiile. De-asemenea mai mult ca singur o sa vreti sa faceti niste forward la headere din configul de nginx.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...