Skip to content

Instantly share code, notes, and snippets.

@soncco
Last active September 16, 2017 22:08
Show Gist options
  • Save soncco/7034927 to your computer and use it in GitHub Desktop.
Save soncco/7034927 to your computer and use it in GitHub Desktop.
Manera fácil de hacer Deploy con Django en un VPS.
# Create sudo user
adduser <user>
usermod -a -G sudo <user>
logout
# Update and clean
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get autoremove
# Python and compile basics
sudo apt-get install build-essential python-dev
# easy_install and pip
curl -O http://python-distribute.org/distribute_setup.py
sudo python distribute_setup.py
rm distribute*
sudo easy_install pip
# virtualenv
# sudo pip install virtualenv, puede que instalando virtualenv con pip te de error de permisos.
sudo apt-get install python-virtualenv
# Create a virtualenv
mkdir public
cd public
virtualenv <vname>
cd <vname>
source bin/activate
# Installing Django
pip install django
# Installing MySQL and dependencies
sudo apt-get install mysql-server libmysqlclient-dev
pip install MySQL-Python
sudo apt-get install libcurl4-gnutls-dev
# Installing git and cloning project
sudo apt-get install git
git clone <github django project>
cd <django project>
# Installing requirements
pip install -r requirements.txt
# Creating database
mysql
create table <table>;
exit;
# Creating tables
./manage.py syncdb
# Installing gunicorn
pip install gunicorn
#test
gunicorn_django --bind 0.0.0.0:8001
cd ..
cd bin
vim gunicorn_start
<file gunicorn_start>
:wq
# Execution permissions
chmod u+x gunicorn_start
# testing
./gunicorn_start
pip install setproctitle
# Installing supervisor
sudo apt-get install supervisor
# Supervisor controller
sudo vim /etc/supervisor/conf.d/<django proj>.conf
<file <django proj>.conf>
:wq
cd ..
mkdir logs
touch logs/gunicorn_supervisor.log
# Checking supervisor
sudo supervisorctl reread
sudo supervisorctl update
# Utils commands for supervisor
sudo supervisorctl status <proj name>
sudo supervisorctl status <proj name>
sudo supervisorctl stop <proj name>
sudo supervisorctl start <proj name>
sudo supervisorctl restart <proj name>
sudo apt-get install nginx
sudo service nginx start
sudo vim /etc/nginx/sites-available/<proj name>
<file nginxhost.conf>
:wq
sudo ln -s /etc/nginx/sites-available/<proj name> /etc/nginx/sites-enabled/<proj name>
sudo service nginx restart
#!/bin/bash
NAME="hello_app" # Name of the application
DJANGODIR=/home/<me>/public/<vname>/<django proj> # Django project directory
SOCKFILE=/home/<me>/public/<vname>/run/gunicorn.sock # we will communicte using this unix socket
USER=<me> # the user to run as
GROUP=<me> # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=<django proj>.settings # which settings file should Django use
DJANGO_WSGI_MODULE=<django proj>.wsgi # WSGI module name
echo "Starting $NAME"
# Activate the virtual environment
cd $DJANGODIR
source ../bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--log-level=debug \
--bind=unix:$SOCKFILE
[program:<proj name>]
command = /home/<me>/public/<vname>/bin/gunicorn_start
user = <me>
stdout_logfile = /home/<me>/public/<vname>/logs/gunicorn_supervisor.log
redirect_stderr = true
upstream hello_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/<me>/public/<vname>/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name <domain>.com;
client_max_body_size 4G;
access_log /home/<me>/public/<vname>/logs/nginx-access.log;
error_log /home/<me>/public/<vname>/logs/nginx-error.log;
location /static/ {
alias /home/<me>/public/<vname>/static/;
}
location /media/ {
alias /home/<me>/public/<vname>/media/;
}
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://hello_app_server;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/<me>/public/<vname>/static/;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment