• Blocking time consuming sites on Linux

    Have you noticed that you spend quite a bit of time on time consuming sites, such as youtube, reddit, Hacker news, CNN.com and etc.? I certainly did. When it comes to productivity, such websites don’t help at all. But now that you think of it, maybe it’s worth to block them completely?

    Blocking websites with hosts files

    Linux has a host file at /etc/hosts, Windows have one as well (but it’s hidden somewhere in Windows folder).

    You simply need to add entries in such manner:

    127.0.0.1 example.com
    

    127.0.0.1 is your internal localhost IP address and instead of example.com you enter your time consuming site. Add as many entries as you need.

    Just as an example, here’s my /etc/hosts file:

    127.0.0.1 localhost
    127.0.0.1 reddit.com
    127.0.0.1 twitter.com
    127.0.0.1 news.ycombinator.com
    

    And when you’re done, you won’t be able to access these sites that easily (but you will still be able to do that by using certain methods).

    And that’s it! This change is easily to apply and easy to revert. It blocks access to your most time consuming websites and reminds you your decision not to visit them.


  • Setting up PHP5 with Nginx through FastCGI

    I need to run a LNMP (Linux Nginx Mysql PHP) stack for development. Here’s an easy way how you can run PHP5 through FastCGI on Nginx as well.

    Install required packages

    You need to install these packages: php5-common, php5-cgi and nginx: sudo apt-get update sudo apt-get install php5-common php5-cgi nginx

    Start/stop PHP-FastCGI

    Create a start script in your /etc/init.d directory:

    sudo gedit /etc/init.d/php-fastcgi
    

    And add this code in it:

    #!/bin/bash
    BIND=127.0.0.1:9000
    USER=www-data
    PHP_FCGI_CHILDREN=15
    PHP_FCGI_MAX_REQUESTS=1000
    
    PHP_CGI=/usr/bin/php-cgi
    PHP_CGI_NAME=`basename $PHP_CGI`
    PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
    RETVAL=0
    
    start() {
        echo -n "Starting PHP FastCGI: "
        start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
        RETVAL=$?
        echo "$PHP_CGI_NAME."
    }
    stop() {
        echo -n "Stopping PHP FastCGI: "
        killall -q -w -u $USER $PHP_FASTCGI
        RETVAL=$?
        echo "$PHP_CGI_NAME."
    }
    
    case "$1" in
            start)
                start
        ;;
            stop)
                stop
        ;;
            restart)
                stop
                start
        ;;
            *)
                echo "Usage: php-fastcgi {start|stop|restart}"
                exit 1
        ;;
    esac
    exit $RETVAL
    

    Make it executable:

    sudo chmod +x /etc/init.d/php-fastcgi
    

    Make it launch at startup (optional):

    sudo update-rc.d php-fastcgi defaults
    

    Launch it manually:

    sudo /etc/init.d/php-fastcgi start
    

    That’s it, now you should be able to use PHP on Nginx through PHP-FastCGI

    Nginx configuration example

    Example server config:

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;
        include fastcgi_params;
    }
    

    Restart NginX:

    sudo /etc/init.d/nginx restart
    

    Set up a phpinfo() sample page:

    echo "<?php phpinfo();" > /var/www/index.php
    

    Visit the page as you normally would. You should see a PHP information page.


  • Remapping Unity hotkeys for Ubuntu 12.04 LTS

    Upon installing the new Ubuntu 12.04 release I noticed terrible inconsistencies in window management. E.g. to snap window to left half (or right half for that matter) the default hotkey is Ctrl+Alt+Left, however snapping window to the upper half is Ctrl+Alt+Kp8 (Keypad 8). Let’s fix that.

    If you don’t have Compiz Settings Manager installed (it is not installed by default), type this command into Terminal to install it:

    sudo apt-get install compizconfig-settings-manager
    

    Once installed, launch the Compiz Settings Manager. Under “Window Management” click “Grid”, then select “Bindings” tab. Now you can set up your own Window Management hotkeys by clicking on each already defined binding and modifying it to your own desire. If you have a keypad, I suggest you to use these key bindings:

    • Put Center: <Control><Alt>KP5
    • Put Left: <Control><Alt>KP4
    • Put Right: <Control><Alt>KP6
    • Put Top: <Control><Alt>KP8
    • Put Bottom: <Control><Alt>KP2
    • Put Top Left: <Control><Alt>KP7
    • Put Top Right: <Control><Alt>KP9
    • Put Bottom Left: <Control><Alt>KP1
    • Put Bottom Right: <Control><Alt>KP3
    • Maximize: <Control><Alt>KP0

    If I remember this correctly, these key bindings were default on Ubuntu 11.10


  • Disabling Nvidia Optimus enabled card on Ubuntu 12.04

    A new Ubuntu 12.04 version has been released just a few days ago. You grab an ISO image of the OS from Ubuntu.com, burn it to a CD and do a fresh install of Ubuntu 12.04. Everything seems fine, it is working nice and quick on your laptop. But once you unplug it, your Nvidia GPU keeps on working. Well, at least the fan is running on full speed and your battery is getting drained faster than you expected. What do you do? Well, the first thing you may try is to install Bumblebee:

    Installing Bumblebee

    Bumblebee is an opensource program that manages switching for your Optimus graphics card. Official guide

    Run these commands on your Terminal:

    • Add Bumblebee repository: sudo add-apt-repository ppa:bumblebee/stable
    • Update cache from repositories: sudo apt-get update
    • Install Bumblebee: sudo apt-get install bumblebee bumblebee-nvidia
    • Configure Bumblebee for your user account (replace $USER with your username): sudo usermod -a -G bumblebee $USER
    • Restart and relogin

    Now you should have Bumblebee installed and that noisy fan silent again!


  • Switching from Wordpress to Octopress

    It has been over a year since I published my first post on this blog. It all started with a simple Wordpress installation and now I decided to switch to a different blogging system.

    What is Octopress?

    Octopress is a blogging framework, which is primarily built for hackers, as it requires extensive use of shell, Git and other scary things. It certainly is not for everyone, but those, who use it, are happy with it.

    Octopress uses Jekyll (written in Ruby) to generate static HTML pages of content, so it is really good for performance, as everything is already generated. However the need of regenerating whole site each time you post something is a bit of a drawback, as it can take up to few minutes if you have lots of posts. Actually I’ve found some people saying that it takes one to two minutes to regenerate whole blog consisting of ~1000 posts on an i7 machine.

    Why Octopress?

    Personally, it’s just a matter of performance (although it’s not that this blog had trouble with it before) and convenience. I can write posts in Markdown and get Octopress to turn it into regular HTML in a way I want it to be. No more crappy WYSIWYG, where you often get frustrated with not being able to get it to work properly fast enough. But maybe it’s just me. Also having the ability to churn out a blog post without the need of leaving the Terminal is a huge upside. I can just write it with VIM, run rake generate and it’s published.

    Also it has a nice HTML5 theme out of the box, so that’s on the upside as well.

    Time to switch!

    The switch was much smoother than I anticipated actually, although not without any problems.

    Wordpress has a neat Export function (Admin->Tools->Export), which exports your whole blog to XML. Then I used a Python app ExitWp, which converted all of my Wordpress posts to Octopress pages in seconds.

    NOTE: At this point I’ve experienced my first problem. For some strange reason conversion of one of the posts always failed. It turns out that < (&lt;) was the problem. I suspect that it attempted to use it as a HTML tag. Having that fixed it worked great!

    Installing Octopress was easy as well. The process is well documented on Octopress setup documentation. All it needs is the latest Ruby version (1.9.2 at the time of writing, installed with RVM), Gem Bundler and rake install command.

    Also make sure that your shell locale settings are right. I had to install and set en_US.UTF-8 for my shell (How to fix Ubuntu locale settings). Otherwise rake generate just crashes with a Ruby error.

    Afterwards it’s just a matter of configuration (and following the documentation).

    Closing thoughts

    Switching to Octopress was quite easy and took about two hours, but it easily could be done much quicker if you know what you’re doing. Other than that it is stable (static content), server load has dropped down even further. And it is much more comfortable to write my first Octopress blog post in Markdown.