Saturday, March 19, 2011

Cheat Script - So I Won't Forget

function TranslateAnswer(s, answerIndex)

{

var value;

var newString;

var code;

var i;



value = (answerIndex % 31) + 1;

newString = "";

for (i=0; i <>

code = parseInt(s.substring(i, i + 2), 16);

newString += String.fromCharCode(code ^ value);

}



return(newString);

}


for(i=0;i<=50;i++){
alert(i+1 +" = "+TranslateAnswer(ansMap[i],i));
}

//# http://www.squarefree.com/shell/shell.html

Monday, February 28, 2011

Installing django, nginx and uwsgi on Ubuntu

Installing nginx and uwsgi

sudo su -
nginx=stable # use nginx=development for latest development version
apt-get install python-software-properties
add-apt-repository ppa:nginx/$nginx
add-apt-repository ppa:uwsgi/release
apt-get update
apt-get install nginx uwsgi python-virtualenv

Make a virtual environment. Ex.
virtualenv /home/jason/virtualenv

Activate the virtualenv ex.
source /home/jason/virtualenv/bin/activate

Install django
pip install django

Create django project ex.
django-admin.py startproject /home/jason/virtualenv/djangoprojecthome

Deploy nginx
Edit /etc/nginx/sites-available/default
ex.
upstream django {
# server unix:/var/run/uwsgi/uwsgi-python2.6/pinax/socket;
server 127.0.0.1:4000;
}

server {
listen 80;
server_name django.jasonwang.us;

location / {
uwsgi_pass django;
include uwsgi_params;
uwsgi_param UWSGI_PYHOME /home/jason/virtualenv;
uwsgi_param UWSGI_SCRIPT deploy; #the name of deploy.py
# uwsgi_param SCRIPT_NAME django;
uwsgi_param UWSGI_CHDIR /home/jason/virtualenv/djangoprojecthome;
}
location ^~ /media/ {
root /home/jason/virtualenv/djangoprojecthome/static;

}
}

ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default

Edit /etc/uwsgi/uwsgi-python2.6/default.ini
I use vhost in my xml for virtual hosting to run multiple sites.

[uwsgi]
socket = 127.0.0.1:4000
master = true
processes = 4
vhost = true
no-site = true


Create a django wsgi file in your django project file. ex. /home/jason/virtualenv/djangoprojecthome/deploy.py

# pinax.wsgi is configured to live in projects/pine/deploy.
# from pinax
import os
import sys

from os.path import abspath, dirname, join
from site import addsitedir
addsitedir('/home/jason/virtualenv/lib/python2.6/site-packages')

sys.path.insert(0, abspath(join(dirname(__file__), "../")))

from django.conf import settings
os.environ["DJANGO_SETTINGS_MODULE"] = "djangoprojecthome.settings"

# sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))

from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

Restart nginx and uwsgi

sudo /etc/init.d/uwsgi-python2.6 restart
sudo /etc/init.d/nginx restart




SETTING UP DJANGO FROM SCRATCH ON AMAZON EC2


  • Download putty with keygen
  • download the pem key
  • put pem through the putty keygen and save it as ppk
  • find ami micro instances from here http://uec-images.ubuntu.com/releases/10.10/release/
  • login to amazon ec2 interface and run that instance
  • setup security group and open up port 22, 80
  • login to server with putty and the key file
  • "sudo aptitude update && sudo aptitude upgrade"
  • run "sudo aptitude install python-virtualenv"
  • "sudo aptitude install libc6 libgeoip1 libpcre3 libssl0.9.8 zlib1g lsb-base"
  • "sudo aptitude install ec2-api-tools"
$ virtualenv examplesite $ cd examplesite/ $ . bin/activate
$ mkdir pkg && cd pkg/ $ wget 'http://projects.unbit.it/downloads/uwsgi-0.9.4.2.tar.gz' $ wget 'http://nginx.org/download/nginx-0.7.65.tar.gz'
sudo su - nginx=stable # use nginx=development for latest development version add-apt-repository ppa:nginx/$nginx apt-get update  apt-get install nginx

sudo add-apt-repository ppa:uwsgi/release

sudo aptitude install git

sudo apt-get build-dep python-psycopg2
easy_install -i http://downloads.egenix.com/python/index/ucs4/ egenix-mx-base



#pip_requirement file pip install -r pip_requirement

BeautifulSoup==3.2.0Django==1.2.4PIL==1.1.7Pygments==1.4South==0.7.3akismet==0.2.0cmsplugin-gallery==0.1.7cmsplugin-lastfm==0.1.0cmsplugin-pygments==0.7.3cmsplugin-youtube==0.1.5-alpha-1distribute==0.6.10django-appmedia==1.0.1-e git://github.com/Fantomas42/django-blog-zinnia.git@2931d7f540f883b60a49842f84af099dc1d2c618#egg=django_blog_zinnia-0.7-py2.6-devdjango-classy-tags==0.3.0django-cms==2.1.0django-cms-jplayer==0.1.10django-grappelli==2.3.1django-inline-ordering==0.1.3django-tagging==0.3.1django-tinymce==1.5.1a1django-xmlrpc==0.1.3easy-thumbnails==1.0-alpha-15egenix-mx-base==3.1.2feedparser==5.0psycopg2==2.3.2pyparsing==1.5.5wsgiref==0.1.2

PIL

psycopg2

pygments

pyparsing

south

sudo aptitude install postgresql-8.4

# adduser databaseuser
# passwd databaseuser

sudo su postgres -c psql template1
template1=# ALTER USER postgres WITH PASSWORD 'password';
template1=# \q

sudo passwd postgres


sudo nano /etc/postgresql/8.3/main/postgresql.conf

Change the line containing

data_directory = '/var/lib/postgresql/8.3/main'

to

data_directory = '/data/main'

Now change

#listen_addresses = 'localhost'

to

listen_addresses = '*'

and also change

#password_encryption = on

to

password_encryption = on

Save the file, then open the pg_hba.conf file so we can control who can access the server:

# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database
# super user can access the database using some other method.
# Noninteractive
# access to all databases is required during automatic maintenance
# (autovacuum, daily cronjob, replication, and similar tasks).
#
# Database administrative login by UNIX sockets
local all postgres ident sameuser
# TYPE DATABASE USER CIDR-ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5

# Connections for all PCs on the subnet
#
# TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
host all all 0.0.0.0/0 md5 # wide-open, you may want to make this more specific to your database


start sql server

#create sql user

sudo su postgres

psql

CREATE USER 'user';

CREATE DATABASE 'database';


template1=# CREATE USER tom WITH PASSWORD 'myPassword';

Step #5: Add a database called jerry

Type the following command (you need to type command highlighted with red color):
template1=# CREATE DATABASE jerry;
Now grant all privileges on database
template1=# GRANT ALL PRIVILEGES ON DATABASE jerry to tom;
Type \q to quit:
template1=# \q


$ psql template1
OR
$ psql -d template1 -U postgres


#fixing the mptt problem with django-cms

remove ../bin/lib/python2.6/site-.../mptt

pip install -U django-cms


change db settings in settings.py

python manage.py syncdb --all

python manage.py migrate --fake


#test uwsgi process

sudo /usr/bin/uwsgi-python2.6 --processes 2 --home /home/ubuntu/%site --socket 127.0.0.1:4000 --chmod-socket --env DJANGO_SETTINGS_MODULE=%site.settings -w "django.core.handlers.wsgi:WSGIHandler()" --pythonpath /home/ubuntu/%virutalenvpath --daemonize /var/log/uwsgi.log


edit the /etc/nginx/sites-available with


location / {

uwsgi_pass django;

include uwsgi_params;

}


upstream django {

server unix:/var/run/uwsgi/uwsgi-python2.6/%site/socket;

}

replace %site with your site folder

the nginx site config files are in /etc/nginx/sites-available and ln -s to /etc/nginx/sites-enabled


In /etc/uwsgi/uwsgi-python2.6/ place your %site.ini file

home=/home/ubuntu/%virtualenvpath

processes=2

module=django.core.handlers.wsgi:WSGIHandler()

env=DJANGO_SETTINGS_MODULE=%sitepath.settings

pythonpath=/home/ubuntu/%virtualenvpath


#My path goes like /home/ubuntu/%virtualenvpath/%sitepath


sudo /etc/init.d/uwsgi-python2.6 restart

sudo /etc/init.d/nginx restart