Nginx Count Traffic

# /etc/nginx/nginx.conf
log_format traffic
'$http_host|$bytes_sent|$time_local|$remote_addr|$request_uri';
access_log /var/log/nginx/traffic traffic;
# example php cron job to parse traffic log
# need to be modified to fit your situation
<?php

$servers = Array('web01', 'web02', 'web03');

$daysback = 1;
for($x=$daysback; $x>0; $x--) {
        $day = date('Ymd', time()-(60*60*24*$x));
        echo "$day\n";
        runstats($day);
}
system("rm -f /tmp/log??????");

function runstats($day) {
        global $servers;

        $tmpfile = tempnam('/tmp', 'log');
        $logfile = '/var/log/nginx/traffic.'.$day;

        $hosts = array();
        $bytes = array();
        $transfer = array();

        foreach($servers as $server) {
                echo "\t{$server}\n";
                system("/usr/bin/scp {$server}:{$logfile} {$tmpfile}
2>/dev/null");
                if(file_exists($tmpfile)) {
                        $fp = fopen($tmpfile, 'r');
                        while(!feof($fp)) {
                                list($host, $bytes, $date, $ip, $uri)
= fgetcsv($fp, 1024, '|');
                                $host = preg_replace('/^www\./', '', $host);
                                $host = preg_replace('/\.$/', '', $host);
                                if(strstr($host, ':')) {
                                        $host = rtrim($host, ':80');
                                        $host = rtrim($host, ':443');
                                }
                                $host = strtolower($host);
                                if(substr($uri, 0, 21) ==
'/rating/video/get.php') {
                                        if(strstr($host, 'foo3')) {
                                                $host = 'foo.com';
                                        } elseif(strstr($host, 'foo2')) {
                                                $host = 'foo.com';
                                        }
                                }
                                if(!isset($requests[$host])) {
                                        $requests[$host] = 1;
                                } else {
                                        $requests[$host]++;
                                }
                                if(!isset($transfer[$host])) {
                                        $transfer[$host] = $bytes;
                                } else {
                                        $transfer[$host] =
$transfer[$host] + $bytes;
                                }
                                $hosts[$host] = 1;

                        }
                        fclose($fp);
                        unlink($tmpfile);
                }
        }

        $day = date('Y-m-d', strtotime($day));
        db_query("DELETE FROM stats WHERE day='{$day}'");
        foreach(array_keys($hosts) as $host) {
                if(!empty($host)) {
                        $vhost = db_escape($host);
                        $t = isset($transfer[$host]) ? $transfer[$host] : 0;
                        $r = isset($requests[$host]) ? $requests[$host] : 0;
                        db_query("REPLACE INTO stats
(vhost,day,bytes,requests) VALUES('$vhost', '$day', $t, $r)");
                }
        }
}
?>

Setting up dev environment on OSX Mountain Lion

Databases: MYSQL and POSTGRES

brew install mysql

PHP 5.5 FPM

brew tap josegonzalez/php
brew tap homebrew/dupes
brew install php55 --with-fpm --with-homebrew-openssl
brew install php55-xdebug php55-imagick php55-mcrypt

Nginx

brew install nginx

# add alias to your rc
alias restart-nginx='sudo nginx -s stop; sudo nginx'

Now DNSMASQ

brew install dnsmasq
echo "address=/.dev.com/127.0.0.1" > $(brew --prefix)/etc/dnsmasq.conf

sudo cp -fv /usr/local/opt/dnsmasq/*.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

sudo mkdir -v /etc/resolver
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/dev.com'

# From now on anything at *.dev.com will be pointed at 127.0.0.1

Based on:

http://vanderveer.be/blog/2012/10/12/setting-up-my-perfect-developer-environment-on-osx-10-dot-8-mountain-lion-10-dot-8-2-final-edition/
http://echodittolabs.org/blog/2012/04/os-x-107-lion-development-nginx-php-mariadb-homebrew

Single site Varnish

This settings only work for single site per server because nginx would otherwise get confused about which site to serve.

# /etc/default/varnish
# ...
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"
# ...
# /etc/varnish/default.vcl
# ...
backend default {
    .host = "localhost";
    .port = "8080";
}
# ...
# /etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
    multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    client_max_body_size 50M;

    server_name_in_redirect on;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_disable "msie6";

    server {
        listen 8080;
        root /mnt/projects/the_sands;
        index index.php;

        location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
        }

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass backend;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;
        }
    }

    upstream backend {
        server unix:/var/run/php5-fpm.sock;
    }
}

Spree Deployment Service

http://spreecasts.org/03-deploy-spree-shop-to-vps-with-deployment-s

# on mac, preparation
brew install mysql
brew info mysql
# follow the prompt to set root user and start mysql

rails new spree-shop -d mysql
cd spree-shop
vim config/database.yml
# update database password
bundle
bundle exec rake db:create
bundle exec rake db:create:all
spree install --edge

# go to spree website and add deployment
# once deployment is set up, update Gemfile

# on local
bundle
bundle exec capify .

# on remote run the initialization configuration script from spree, and wait

# on remote
cd /home/spree
mkdir .ssh
vim .ssh/authorized_keys
# paste your local id_rsa.pub into authorized_keys
chown spree:spree .ssh
chmod 700 .ssh
chmod 600 .ssh/authorized_keys

# replace the content of config/deploy.rb with capistrano recipe from spree

# on local
git add .
git commit -am "capified"
git push

# on local
bundle exec cap deploy:check
bundle exec cap deploy:cold

# shop is up now, but no ssl
# on local
vim config/initializers/spree.rb
# in the Spree.config, add:
config.allow_ssl_in_production = false
bundle exec cap deploy

# now you can visit admin without ssl
# however admin user is not present on remote
# on remote
cd /data/spree/current
rake db:seed RAILS_ENV=production