RSS

Tag Archives: EC2

DANG on a Cloud

Thats Django, ArchLinux ,Nginx and Gunicorn on an Amazon EC2instance.Getting a server to play around with root access courtesy of Amazon AWS is pretty easy, just sign up provide your credit card details and you are almost ready to go.First things first setting up the server.Theer are plenty of ready-made AMI images floating out there so one is really spoilt for choice , personally my preference is for Archlinux having run the rolling release distro as a primary dev machine for the better part of a decade it seemed right to give it a run as a production server.Loading the instance was pretty straightforward (instructions available on AWS blogs and forums).As with all good Arch systems getting it up to the latest version is as easy as:

pacman -Syu
once the changes have been made to the system and confirmed it time to setup the environment.The deployed app will be deployed within a virtualenv , this has the advantage of keeping the python installation of the app sand-boxed from that running on the system with a simple:

pacman -S virtualenv
then executing
virtualenv /path/to/apps -p /usr/bin/python2
source /path/to/apps/bin/activate
once created install the source control system of choice (assuming the app is being managed by one) or good old sftp can work just as well with filezilla .Once unpacked into its deployment folder
installing the project requirements is a case of :
pip install requirements.txt
and
pip install gunicorn.
As the gunicorn server will be running embedded from within the django app its a matter of adding it to the settings.py file and running it will be:

INSTALLED_APPS = [
.
.
“south”,
“rosetta”,
“mptt”,
“gunicorn”,
#
.
.
]
python manage.py run_gunicorn
The final part of the puzzle is the nginx server, lighter than the more resource hungry Apache and way simpler to configure with the editing of one file the nginx.conf to server the static assets from the project .
   upstream app_server {
server localhost:8000 fail_timeout=0;
}

server {
listen 80 default;
client_max_body_size 4G;
server_name _;

keepalive_timeout 5;

location /static/ {
autoindex on;
alias   /home/path/to/site_media/static/;
}

location / {
# checks for static file, if not found proxy to app
try_files $uri @proxy_to_app;
}

location @proxy_to_app {
proxy_pass_header Server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

proxy_pass   http://app_server;
}

error_page 500 502 503 504 /500.html;

}

The gunicorn server will execute the python thread to a port that the nginx will listen to and server to the world your great new web-app hosted on amazon cloud.
 
Leave a comment

Posted by on February 11, 2012 in code, Internet & networks, Python

 

Tags: , , , , , ,