Skip to content

Instantly share code, notes, and snippets.

@aderowbotham
Created June 7, 2012 14:19
Show Gist options
  • Save aderowbotham/2889048 to your computer and use it in GitHub Desktop.
Save aderowbotham/2889048 to your computer and use it in GitHub Desktop.
Django stack setup on EC2 AMI

Django Stack

Overview

This is a set of instructions to setup a Django Nginx Gunicorn MySQL/Postgres stack on a single Amazon EC2 instance.

Server - AWS

  • Create an EC2 instance, choose the Amazon AMI type - it's a CentOS flavour.

  • Download the .pem file

  • To connect to the server over SSH:

      ssh -i PEMSFILE  ec2-user@domain
    

    e.g.

      ssh -i ~/pems_files/myfile.pem [email protected]
    

Open ports in ec2 security group:

22 (SSH)
80 (HTTP)
3306 (MYSQL) -- TESTING DB -- DANGER
8000 (Django/Gunicorn) -- TESTING

make sure everything is up to date:

sudo yum update

nginx

Install:

sudo yum install nginx

sudo vim /etc/nginx/conf.d/MY-PROJECT.conf

conf file:

server {
	listen   80;
	server_name staging.project_name.co.pt;
	# no security problem here, since / is alway passed to upstream
	root /srv/www/project_name/checkout/website;
	# serve directly - analogous for static/staticfiles

	location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8000/;
	}
	# what to serve if upstream is not available or crashes
    error_page 500 502 503 504 /media/50x.html;
}

init script:

sudo  /sbin/chkconfig nginx on

start:

sudo service nginx start

MySQL

sudo yum -y install mysql mysql-server 

sudo  /sbin/chkconfig mysqld on

sudo service mysqld start

sudo mysqladmin -u root password 'new-password'

mysql -u root -p

mysql> create database MY-PROJECT;

[local access - safer]
mysql> GRANT ALL ON project_name.* TO user@'localhost' IDENTIFIED BY 'sga 	rcez';

[remote access]
mysql> GRANT ALL ON project_name.* TO user@'%' IDENTIFIED BY 'user';

mysql> FLUSH PRIVILEGES;

control:

security:

[removes the test database]
mysql> DROP DATABASE test;

[Removes anonymous access]
mysql> DELETE FROM mysql.user WHERE user = ;

mysql> FLUSH PRIVILEGES;

Project

mkdir /srv/www/project_name

sudo chown ec2-user project_name

git clone [email protected]:user/project_name.co.pt.git checkout

install virtualenv:

sudo easy_install virtualenv

make a virtualenv:

cd /srv/www/project_name

virtualenv --no-site-packages env

start it:

source env/bin/activate

install package manager:

sudo easy_install pip

probably not needed:

sudo yum install python-imaging
sudo yum install freetype-devel
sudo yum install gcc
sudo yum install python-devel
sudo yum install python26* mysql-devel

example requirements.txt in project folder:

    Django==1.3.1
    gunicorn==0.13.4
    MySQL-python==1.2.3

install:

pip install -r requirements.txt

test Django

#to test the base setup - open port 8000 in sec group
python manage.py runserver 0.0.0.0:8000

#test gunicorn
gunicorn_django -b 0.0.0.0:8000

make gunicorn script in project folder (http://senko.net/en/django-nginx-gunicorn/)

#!/bin/bash
set -e
LOGFILE=/var/log/gunicorn/project_name.log
LOGDIR=$(dirname $LOGFILE)
NUM_WORKERS=3
# user/group to run as
USER=ec2-user
GROUP=ec2-user
cd /srv/www/project_name/checkout/website
source ../../env/bin/activate
test -d $LOGDIR || mkdir -p $LOGDIR
exec gunicorn_django -w $NUM_WORKERS \
--user=$USER --group=$GROUP --log-level=debug \
--log-file=$LOGFILE 2>>$LOGFILE

# for local add to the above: --bind 0.0.0.0:8000

chmod ug+x run_gunicorn.sh

sudo chown ec2-user /var/log/gunicorn/

Supervisor

This controls Gunicorn

sudo easy_install supervisor

sudo vim /etc/supervisord.conf

    echo_supervisord_conf > /etc/supervisord.conf
    
# note: this doesn't work for some reason; echo_supervisord_conf then copy the output into a new file at /etc/supervisord.conf

#append :

[program:project_name]
directory = /srv/www/project_name/checkout/website/
user = ec2-user
command = /srv/www/project_name/checkout/website/run_gunicorn.sh

stdout_logfile = /var/log/supervisor/stout.log
stderr_logfile = /var/log/supervisor/error.log

create new file /etc/rc.d/init.d/supervisord

create empty files at /var/log/supervisor/stout.log /var/log/supervisor/error.log

tell linux to start supervisor on reboot chkconfig --add supervisord

sudo /sbin/chkconfig supervisord on

add script to /ect/init.d/supervisord: http://serverfault.com/questions/96499/how-to-automatically-start-supervisord-on-linux-ubuntu/259230#259230

sudo chmod +x /etc/init.d/supervisord

sudo chkconfig --add supervisord

sudo /etc/init.d/supervisord status

# stop nginx
sudo service nginx stop

sudo /etc/init.d/supervisord start
or
sudo service supervisord start

restart nginx:

sudo service nginx start

References:

http://senko.net/en/django-nginx-gunicorn/ http://davidpoblador.com/run-django-apps-using-gunicorn-and-nginx/

@fabre-thibaud
Copy link

I'm using your Gist (2+ years later :D) as I was looking for a way to install supervisor as a service on Amazon AMI. The command that works for echo_supervisord_conf is:

sudo bash -c 'echo_supervisord_conf > /etc/supervisord.conf'

Thanks for the guide anyways

@vadorvatsal
Copy link

Really helpful. I scratched my whole head before landing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment