hades Posted July 28, 2015 Report Posted July 28, 2015 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 librariilorsudo apt-get updatesudo apt-get upgradesudo apt-get install python-dev python virtualenv2. Instalam nginx:sudo apt--get install nginx3. Install mysql:sudo apt-get install mysql-serversudo apt-get install libmysqlclient-devsudo apt-get install mysql-python4. Creem un user nou; User ce-l vom folosi pentru deploy-ul propriuzis.adduser example5. Creem directorul proiectului; Virtualenv pentru el. Instalam requirements-urile proiectuluimkdir appcd app; virtualenv venvcd venv; source bin/activateAcum 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 gunicornPentru a testa daca totul functioneaza cum trebuie pana aici:gunicorn -b 0.0.0.0:8080 project.wsgi7. Creem un mic bash script pentru gunicorn, ce va fi folosit de supervisor.Bash scriptul o sa arate cam asa:#!/bin/bashNAME="app" # Numele AplicatieiDJANGODIR=/home/example/app/project/ # Django project directorySOCKFILE=/home/example/app/project/gunicorn.sock # we will communicte using this unix socketUSER=example # the user to run asGROUP=www-data # the group to run asNUM_WORKERS=3 # how many worker processes should Gunicorn spawnDJANGO_SETTINGS_MODULE=app.settings # which settings file should Django useDJANGO_WSGI_MODULE=app.wsgi # WSGI module nameecho "Starting $NAME as `whoami`"# Activam virtualenvcd $DJANGODIR. ../venv/bin/activateexport DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULEexport PYTHONPATH=$DJANGODIR:$PYTHONPATHRUNDIR=$(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.shuser = examplegroup = www-datastdout_logfile = /home/example/app/project/logs/supervisor.logredirect_stderr = trueenviroment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8Il pornim:sudo supervisorctl start app9. 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. Quote