Skip to content

Instantly share code, notes, and snippets.

@kmlxn
Last active June 9, 2016 12:16
Show Gist options
  • Select an option

  • Save kmlxn/29c93e921c8072ecf5a65ce9ac63e718 to your computer and use it in GitHub Desktop.

Select an option

Save kmlxn/29c93e921c8072ecf5a65ce9ac63e718 to your computer and use it in GitHub Desktop.
Bash script for settign up Django project on Ubuntu 14 with nginx, python 3, virtualenv and git installed
#!/bin/bash
set -e # exit script if any error occurs
# Bash script for settign up Django project on Ubuntu 14 with nginx, python 3,
# virtualenv, git installed
# Will create directory structure, clone source code, change Django secret key,
# set up Nginx conf, set up gunicorn upstart job.
# Upstart job will be named "site-<project_name>"
# HOW TO RUN
# sudo ./set_up_django_proj_ubuntu_14.sh <git_repo_url> <project_domain>
# Example:
# cd /home/someuser/sites
# sudo ./set_up_django_proj_ubuntu_14.sh https://github.com/somegithubuser/somedjangorepo.git somedomain.com
# DIRECTORY STRUCTURE
# <project_name>
# |-- <project_name> <-- django project source code
# |-- <project_name>
# |-- settings.py
# |-- ...
# |-- deploy_settings.py <-- will be created by the script
# |-- ...
# |-- requirements.txt <-- required to run the script
# |-- venv
# |-- database
# |-- static
# |-- media
# REQUIRED
# requirements.txt - file with list of required python packages
# To create it run 'pip freeze > requirements.txt'
# Also it is required to add this lines at the end of settings.py
# try:
# from .deploy_settings import *
# except ImportError:
# pass
# This lines tell Django to import deploy setting generated by the script
# STATIC_ROOT has to be set in settings.py
# ==============================================================================
if [ -z "$1" ]; then
echo "Error: supply git repo url"
exit 1
fi
if [ -z "$2" ]; then
echo "Error: supply project url domain"
exit 1
fi
git_repo_url=$1
project_domain=$2
project_name=$(echo $git_repo_url | sed -n 's#.*/\([^.]*\)\.git#\1#p')
upstart_job="site-$project_name"
project_container_dir="$(pwd)/$project_name"
project_source_dir="$project_container_dir/$project_name"
function create_folders() {
sudo -u ${SUDO_USER} mkdir $project_name
cd $project_name
sudo -u ${SUDO_USER} git clone $git_repo_url
sudo -u ${SUDO_USER} virtualenv -p python3 venv
sudo -u ${SUDO_USER} mkdir static
sudo -u ${SUDO_USER} mkdir media
sudo -u ${SUDO_USER} mkdir database
}
function create_secret_key() {
secret_key=$(python -c 'import random; import string; print "".join([random.SystemRandom().choice(string.digits + string.letters + "!@#$%^&*(-_=+)") for i in range(50)])')
sudo -u ${SUDO_USER} echo "SECRET_KEY = '$secret_key'" > "$project_name/$project_name/deploy_settings.py"
}
function install_required_packages() {
sudo -u ${SUDO_USER} venv/bin/pip install -r $project_name/requirements.txt
sudo -u ${SUDO_USER} venv/bin/pip install gunicorn
}
function migrate_db() {
sudo -u ${SUDO_USER} venv/bin/python $project_name/manage.py migrate
}
function collect_static_files() {
sudo -u ${SUDO_USER} venv/bin/python $project_name/manage.py collectstatic --noinput
}
function configure_nginx() {
tee /etc/nginx/sites-available/$project_name.conf <<-EOM
server {
listen 80;
server_name $project_domain;
location /static {
alias $project_container_dir/static;
}
location /media {
alias $project_container_dir/media;
}
location / {
proxy_set_header Host \$host;
proxy_pass http://unix:$project_container_dir/gunicorn.socket;
}
}
EOM
ln -s /etc/nginx/sites-available/$project_name.conf \
/etc/nginx/sites-enabled/$project_name.conf
service nginx restart
}
function configure_upstart() {
tee /etc/init/$upstart_job.conf <<-EOM
description "Gunicorn server for $project_name"
start on net-device-up
stop on shutdown
respawn
setuid ${SUDO_USER}
chdir $project_source_dir
exec ../venv/bin/gunicorn \
--bind unix:$project_source_dir/../gunicorn.socket \
--error-logfile ../gunicorn-error.log \
$project_name.wsgi:application
EOM
service $upstart_job start
}
create_folders
create_secret_key
install_required_packages
migrate_db
collect_static_files
configure_nginx
configure_upstart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment