Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amitasthana/4f47af5a2c91a50d4b39230408ae294c to your computer and use it in GitHub Desktop.
Save amitasthana/4f47af5a2c91a50d4b39230408ae294c to your computer and use it in GitHub Desktop.
Steps to configure a django app on digitalocean.com
deploy django app on digitalocean
=================================
# Packages
sudo apt-get install python-pip python-dev python-virtualenv supervisor nginx
# Create dirs
mkdir -p /webapps/my-project/{logs,run}
> /webapps/my-project/logs/gunicorn_supervisor.log
# Virtualenv
cd /webapps/my-project
virtualenv .
# Gunicorn
pip install gunicorn
> /webapps/my-project/bin/start.sh
#!/bin/bash
NAME="my-project"
DJANGODIR=/webapps/my-project/conf # Django project directory
SOCKFILE=/webapps/my-project/run/gunicorn.sock # we will communicte using this unix socket
USER=myproject_user # the user to run as
GROUP=webapps # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=conf.settings # which settings file should Django use
DJANGO_WSGI_MODULE=conf.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source ../bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# 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 \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-
# Supervisor
> /etc/supervisor/conf.d/my-project.conf
[program:myproject]
command = /webapps/my-project/bin/start.sh
user = carolcycles
stdout_logfile = /webapps/my-project/logs/gunicorn_supervisor.log
redirect_stderr = true
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8
autostart=true
autorestart=true
redirect_stderr=true
sudo supervisorctl reread
sudo supervisorctl update
# Nginx for static
edit /etc/nginx/nginx.conf
server_names_hash_bucket_size 64;
> /etc/nginx/sites-available/my-project
ln -s /etc/nginx/sites-available/my-project /etc/nginx/sites-enabled/my-project
upstream myproject_app {
server unix:/webapps/my-project/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name example.com;
client_max_body_size 4G;
access_log /webapps/my-project/logs/nginx-access.log;
error_log /webapps/my-project/logs/nginx-error.log;
location /static/ {
alias /webapps/my-project/static/;
}
location /media/ {
alias /webapps/my-project/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://myproject_app;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapps/my-project/static/;
}
}
# Configure settings.py
STATIC_DIR = '/webapps/my-project/static/'
python manage.py collectstatic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment