Skip to content

Instantly share code, notes, and snippets.

@llazzaro
Forked from panuta/gist:3075882
Created March 1, 2013 14:22
Show Gist options
  • Save llazzaro/5064963 to your computer and use it in GitHub Desktop.
Save llazzaro/5064963 to your computer and use it in GitHub Desktop.
#!/bin/bash
### BEGIN INIT INFO
# Provides: Django EC2 server
# Required-Start: networking
# Required-Stop: networking
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Django server management script
# Description: Django server management script
### END INIT INFO
########## Nginx Config ##########
NGINX_BIN=/usr/local/nginx/sbin/nginx
NGINX_CONFIGFILE=/web/conf/nginx/nginx.conf
########## UWSGI Config ##########
UWSGI_BIN=uwsgi
UWSGI_EMPEROR_FOLDER=/web/conf/uwsgi
UWSGI_LOGFILE=/web/logs/uwsgi.log
############### No additional configuration below ###############
start_nginx() {
if [ "`pidof nginx`" = "" ]; then
echo "Starting nginx server"
$NGINX_BIN -c $NGINX_CONFIGFILE
else
echo "nginx server is already running"
fi
}
reload_nginx() {
if [ "`pidof nginx`" = "" ]; then
echo "Nginx server is not running"
else
kill -HUP `pidof nginx`
echo "Nginx Configuration reloaded"
fi
}
stop_nginx() {
if [ "`pidof nginx`" = "" ]; then
echo "nginx server is already stopped"
else
echo "Stoping nginx server"
kill -INT `pidof nginx`
fi
}
start_uwsgi() {
if [ "`pidof uwsgi`" = "" ]; then
echo "Starting uwsgi server"
$UWSGI_BIN --emperor $UWSGI_EMPEROR_FOLDER --daemonize $UWSGI_LOGFILE
else
echo "uwsgi server is already running"
fi
}
reload_uwsgi() {
if [ "`pidof uwsgi`" = "" ]; then
echo "uwsgi server is not running"
else
echo "Sending signal to uwsgi server to reload"
kill -HUP `pidof uwsgi`
fi
}
stop_uwsgi() {
if [ "`pidof uwsgi`" = "" ]; then
echo "uwsgi server is already stopped"
else
echo "Stoping uwsgi server"
kill -INT `pidof uwsgi`
fi
}
# See how we were called.
case "$1" in
start_nginx)
start_nginx
;;
reload_nginx)
reload_nginx
;;
stop_nginx)
stop_nginx
;;
start_uwsgi)
start_uwsgi
;;
reload_uwsgi)
reload_uwsgi
;;
stop_uwsgi)
stop_uwsgi
;;
*)
echo $"Usage: $prog {start_nginx|reload_nginx|stop_nginx|start_uwsgi|reload_uwsgi|stop_uwsgi}"
exit 1
esac
exit 0

Install necessary packages

Update aptitude

$ sudo apt-get update
$ sudo apt-get upgrade

Install necessary packages

$ sudo apt-get install build-essential

Install Python

$ sudo apt-get install python2.7-dev python-setuptools
$ sudo easy_install pip

Setup Git

Install git

$ sudo apt-get install git-core

Generate a new SSH key

$ ssh-keygen -t rsa -C "your_email@youremail.com"

Then add the key from ~/.ssh/id_rsa.pub to GitHub

Test the key by

$ ssh -T git@github.com

Setup folders

$ cd /
$ sudo mkdir web
$ cd web
$ sudo mkdir conf run logs

Install Virtualenv

Install virtualenv and virtualenvwrapper

$ sudo pip install virtualenv virtualenvwrapper

Add the following to .bashrc

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
export PIP_VIRTUALENV_BASE=$WORKON_HOME

Create a new folder to keep all virtual environments

$ mkdir $HOME/.virtualenvs

Start a new terminal then create a new virtualenv by

$ mkvirtualenv [project name]

Install Supervisor

Install Supervisor

$ sudo easy_install supervisor

Create a new config file

$ echo_supervisord_conf > $HOME/supervisord.conf
$ sudo mv $HOME/supervisord.conf /web/conf/

Install Postgresql Server

Install Postgresql Server and Python library

$ sudo apt-get install postgresql python-psycopg2 libpq-dev

Create a new database and database's user

$ sudo -u postgres createdb [database_name]
$ sudo -u postgres createuser -SDRP [database_user_name]

Config pg_hba.conf file

$ sudo vim /etc/postgresql/9.1/main/pg_hba.conf

Then add the following to pg_hba.conf file let database user authenticated using password

local [database_name] [database_user_name]  md5

Reload Postgresql server

$ sudo /etc/init.d/postgresql reload

Install Web server (nginx)

Download nginx from its website

http://nginx.org/en/download.html

Install PCRE library (necessary for rewrite module)

$ sudo apt-get install libpcre3-dev

Extract nginx tar file then configure use the following commands

$ sudo ./configure --conf-path=/web/conf/nginx/nginx.conf
$ sudo make
$ sudo make install

The following is the default settings for nginx

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/web/conf/nginx"
nginx configuration file: "/web/conf/nginx/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"

Run nginx by

$ /usr/local/nginx/sbin/nginx

Install WSGI server (uWSGI)

Download uWSGI from

http://projects.unbit.it/uwsgi/wiki/WikiStart#Getit

Build uWSGI and copy to bin folder

$ sudo python uwsgiconfig.py --build
$ sudo cp uwsgi /usr/local/bin/

Run uWSGI in Emperor mode by

$ sudo uwsgi --emperor /web/conf/uwsgi

Install PIL

Install dependencies

$ sudo apt-get build-dep python-imaging

Symlink the libraries

$ sudo ln -s /usr/lib/`uname -i`-linux-gnu/libfreetype.so /usr/lib/
$ sudo ln -s /usr/lib/`uname -i`-linux-gnu/libjpeg.so /usr/lib/
$ sudo ln -s /usr/lib/`uname -i`-linux-gnu/libz.so /usr/lib/

Install PIL

$ pip install PIL

Setup project

Setup folders

$ cd /web
$ sudo mkdir [project_name]
$ cd [project_name]
$ sudo mkdir source logs www

Create a new SSH key for GitHub

$ sudo ssh-keygen -t rsa -C "your@email.com"
  • Note: You should generate a new key as root.

Add content from $HOME/.ssh/id_rsa.pub to GitHub and clone the project

$ git@github.com:[github_username]/[repo_name].git
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
root /web/[project_name]/www;
#charset koi8-r;
#access_log logs/host.access.log main;
location /static/ {
alias /web/[project_name]/source/[project_name]/static/;
expires 30d;
}
location /media/ {
alias /web/[project_name]/source/[project_name]/media/;
expires 30d;
}
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3031;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
[uwsgi]
virtualenv=/root/.virtualenvs/[project_name]
chdir = /web/[project_name]/source/[project_name]
pp = /web/[project_name]/source/[project_name]/
socket = 127.0.0.1:3031
master = 1
processes = 4
env = DJANGO_SETTINGS_MODULE=[project_name].settings
module = django.core.handlers.wsgi:WSGIHandler()
daemonize = /web/[project_name]/logs/uwsgi.log
disable-logging = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment