Dev

nginx / nodejs stack setup

Mon Feb 17, 2014

So when you have an nginx + nodejs combo, you want to use nginx to port foward to nodejs

my nodejs uses forever to run on port 3000.

here is the nginx config setup


sudo vi /etc/nginx/site-enabled/default


#add this line before server { ... }
[...]
upstream app_nodejs {
        server localhost:3000;
}
[...]

#now in server {...}, assuming you want to map your url root to point to nodejs
location / {
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header Host $http_host;
               proxy_set_header X-NginX-Proxy true;

               proxy_pass http://app_nodejs;
               proxy_redirect off;
           }


Cheers

nginx with changed phpmyadmin directory setup

Mon Feb 17, 2014

Recently decided to setup a nginx / python / mysql lightweight backend to interact with a nodejs services. So decided to change phpmyadmin directory as well - i did run into 502 / 403 nginx errors, hope this post will help anyone who had the same issues too.

Setup process - assuming you are using ubuntu 12.04LTS


#install mysql & nginx
sudo apt-get install mysql-server mysql-client
sudo apt-get install nginx

#start nginx
sudo service nginx start

#install php5 & phpmyadmin
sudo apt-get install php5-fpm php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-xcache phpmyadmin

#reload php5
sudo service php5-fpm reload


#modify php5-fpm
sudo vi /etc/php5/fpm/pool.d/www.conf


#look for listen and modify as follow
[...]
;listen = 127.0.0.1:9000
listen = /tmp/php5-fpm.sock
[...]


#setup nginx config
sudo vi /etc/nginx/site-enabled/default


#add this after location /
[...]
location /pma {
    alias /usr/share/phpmyadmin/;
}

location ~ ^/pma/(.*\.(js|css|gif|jpg|png))$ {
    alias /usr/share/phpmyadmin/$1;
}

location ~ ^/pma(.+\.php)$ {
    alias /usr/share/phpmyadmin$1;
    fastcgi_pass unix:/tpm/php5-fpm.sock;
    fastcgi_index index.php;

    charset utf8;

    include fastcgi_params;
    fastcgi_param DOCUMENT_ROOT /usr/share/phpmyadmin;
}

#if you want to enable php site wide
location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/tmp/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
}
[...]


sudo service nginx reload

FYI: The most common 502 / 403 errors are due to this line in the nginx config file.


fastcgi_pass unix:/tmp/php5-fpm.sock; #example using unix:/var/run/php-fpm.sock;

Hopefully this post will help some travellers here.

Source
stackoverflow
howtoforge

JavaScript Function Crib Sheet

Wed May 8, 2013

Getting Lazy! So created a javascript function cheat sheet to share with the travellers here. Hope it will be help many of you.

The cryptsheet includes these functions and can be found here : https://github.com/alfredkam/jsFunctions

General Laziness::
- jquery toggle string
- jquery enter keyup event

Mathematical Functions::
- n choose r
- Binomial Distribution
- Probability Mass Function (Useful when wanted to create a bell curve)
- Summation

If the function your missing is not listed, comment below and i may be able to write one.

CSS Cheat Sheet

Wed Mar 6, 2013

css wildcards

css goodies from the net

CheckBox
Dust Particles
Loading Effect
Pure css3 dial clock

Allow password authentications on Amazon ec2 instance

Sat Feb 9, 2013

Step 1 - ssh into server


ssh -i ssh_key.pem my_amazon_ec2_instance.url

*This is really not a recommended way to login

Searchable Hash Bang (SEO)

Fri Feb 1, 2013

Having a dynamic website, AJAX is often used and rely on hash bang to act as a http GET to let the site be interactive. Inorder to have your application searchable / crawable, there are couple of rules to follow as suggested by the google specs.

Assuming your too lazy to checkout the specs. In summary the hash bang should be setup this way:

In a typical site, you will notice the url with http GET are written in this format

http://yoursite.com?key1=value1&key2=value2

Now in your hashbang, your will want to transform

http://yoursite.com#value1&value2

into beautiful url

http://yoursite.com#!key1=value1&key2=value2

Best way to develop a dynamic website

Thu Dec 20, 2012

The develop a dynamic website, with all those smooth page transitions similar to gmail. As per design we will use Bootstrap, Bootstrap provides all the css tools you need to setup a quick website.

Frontend skills you will need
- javascript
- html

Frameworks
First of all we will start of with a model view controller + observer design patterns to achieve a maintainable site.
The web frameworks that we will use are
- backbone js , giving a "structure" to the code
- require js , setting up javascript dependencies, works like php include
- mustache , page templates
- jquery , dom manipulations

as font icons we can get them for free at fontello, their package includes font-awesome

Folder Structure

  
        index.html
        css/
        templates/      <=== contains the html templates
        js/views/   <=== responsible for inital html materials and listenes to model changes / actions / events
        js/models/  <=== responsible for data models
        js/controller/  <=== defines the site routes
        blueprints/     <=== contains all the 3rd party library

To setup hashbang (similar to gmail) for page manipulations, there is a backbone module - backbone router. Using backbone router we can set up page to change dynamically depending on the hashbang setup. To let your hash bang searchable / crawable, checkout this post.

You can checkout the Boiler Plate i've setup using this structure for off the shelve deployment.

Further Readings
Backbone Router
- http://backbonetutorials.com/what-is-a-router/

Backbone + MVC
- http://backbonetutorials.com/organizing-backbone-using-modules/
- http://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/
- http://blog.andyet.com/2010/oct/29/building-a-single-page-app-with-backbonejs-undersc/
- http://backbonetutorials.com/what-is-a-model/

Git Cheat Sheet

Mon Dec 17, 2012


#Setting up git from scratch
git init                                             #initialize git
git remote add < remote_label > < destination >      #adds a new remote destination with a label
git fetch < remote_label >                           #fetches the head of the repo
git checkout master                                  #checkouts the master branch

#Setting up git from another repo git clone < remote_label > < optional_dir_location > #clone a repo with git config ready

#Branching git branch #list all branches git branch < branch_label > #creates a new local branch with name < branch_label > git branch -d < branch_label > #deletes the local branch with name < branch_label > git push < remote_label> :< branch_label > #deletes remote branch

#Merging git checkout < branch to be merge >
git merge < branch_name > #merge recursively and auto commit

additional info - git merge man page

#fetch & pulling git pull < remote_label > < branch_label> #it will recursively merge with your repo git fetch < remote_label >

#Commit git commit -a -m < message > #local commit git commit -amend -m < message > #edit last commit message git push < remote_label > < branch_label > #commits to remote branch

#Tagging git tag -a < comment > #to remember a specific point git push –tag #pushes tag

#To delete a tag git tag -d < tag_name > git push origin :< tag_name >

#if tag name is same as one of your branch name git tag -d < tag_name > git push origin :refs/tag/< tag_name >

#Logging git log #shows the commit log

#Untrack a file git rm –cache < file_name > #untracks a file, thus not included in repo

#Stashing git stash

#cloning a bare repo for deployment purpose git clone –bare

additional info git stash man page


#reverting to previous commits  [Advance]
git checkout < commit_hash >                     #checkout the indicated commit hash
git checkout -b < branch_label > < commit_hash>  #branches with < branch_label > and checkout to the commit hash
git reset –hard < commit_hash >                 #destroys the local modification, you will loose the uncommit work
git revert < commit_hash >                       #reverts to the previous commit

additional info - git revert man page & stack over flow git revert qa


#splitting a subpath out into a new repo
git filter-branch –prune-empty –subdirectory-filter lib master

additional info - github:help