Monthly Archives: November 2011

Picking out a part of a string

A limited use-case, but in case you get to the situation where you need the following bold part of a string:


(The asterisk matching any word)

And here’s the slightly messy solution I used; in this case, I needed the first three directories of the path the script was running in:

pubpath=`echo $fullpath | rev | cut -c13- | rev`

So I’ve reversed it so I know the exact number of characters from the start of the string that I need, used cut to select part of the string; from character 13 to the end (reversed form of /home/*/public_html), then reserved it back again.

Adding php support to nginx (notes)

Already got nginx running with ruby, now I want a php site:

apt-get install php-cgi

configure nginx (mine is in a weird location, /opt/nginx/conf/nginx.conf:

server {
    listen 80;
    server_name my.hostname.local;
    root /path/to/site/public/;
    access_log /path/to/site/log/access.log;
    error_log /path/to/site/log/error.log;
    index index.php;
    location ~ \.php$ {
       try_files $uri =404;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       include fastcgi_params;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME /path/to/site/public$fastcgi_script_name;
       fastcgi_pass localhost:8020;

then start php

php-cgi -b

and restart nginx