Git tip: Beautiful colored and readable output

Git is a powerful tool that is very user friendly when used from the Command Line Interface (CLI). There is no need to add Graphical User Interface (GUI) tools to simplify or visualize the output. What can help to make the CLI output more usable is to enable colors and to allow for some command abbreviations (aliases). Below you see the output we get when we run our commands.

Git colored output


As you can see, we configured the Git alias for “status”, so that it can be abbreviated with “st”. You can also see that the colored output has been enabled.


In Ubuntu colored diffs can be achieved be installed by using the “colordiff” tool. The “colordiff” tool is a replacement for the normal diff tool and can be used with the same arguments. It can be installed using “apt-get”, like this:

sudo apt-get install colordiff

However, Git does not require this because it has support for the colored diff built in.


This is the most useful output. It is an enhanced Git “log” output that we alias as “lg”. By adding color it becomes dramatically more usable.


We assume you have enabled the colored prompt in “.bashrc” by uncommenting the “force_color_prompt=yes” line as described in the previous Git post. Next you should add the following in your “~/.gitconfig” file:

		lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
		st = status
		branch = auto
		diff = auto
		status = auto
	[color "branch"]
		current = yellow reverse
		local = yellow
		remote = green
	[color "diff"]
		meta = yellow bold
		frag = magenta bold
		old = red bold
		new = green bold
	[color "status"]
		added = yellow
		changed = green
		untracked = cyan

Save the file and try the commands. Enjoy!


Git tip: Show your branch name on the Linux prompt


Git is one of the most popular Source Control Management (SCM) software packages to provide revision control. Subversion (aka SVN) and Mercurial are other popular systems. These systems organize source code in branches and revisions that can be named by means of tagging. Branches can be used to allow you to work on different versions of the software concurrently. A popular way of organizing is to create a branch for every major feature you are building. Another organization can be used to distinguish between a “development” and “production” version, so that you can easily apply a small code change in case of any bugs.

In any of the above schemes it is important to know which branch you are working on, when committing code to the repository. This post explains how you can add the branch name in red to the prompt.

Firstly, we have to turn on the colored prompt in “.bashrc”:

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt

Now add the following code to “.bashrc” for Git branch information:

# Add git branch if its present to PS1
parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
if [ "$color_prompt" = yes ]; then
 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
unset color_prompt force_color_prompt

With this change you will be less likely to commit your code to the wrong branch. Enjoy!

PS: You can also show the branch name on the Linux prompt when using Mercurial (tip by Răzvan Tudorică).


Creating a custom Debian package

You’ve created your own application, it works like a charm and you’re very proud of it. Yet installing this application on a different machine requires you to manually copy over files, a VCS checkout, manual configuration or other operations that are still open human to error.

This post describes a method of creating a simple Debian package using dpkg. The focus will be on the creation of a .deb package containing files (pre-compiled), post-installation and removal script, and dpkg-reconfigure support in combination with debconf. At the end of this brief guide you will have a .deb package that can be installed and reconfigured using ‘dpkg -i mycustompackage.deb’ and ‘dpkg-reconfigure mycustompackage’.

This post does not cover how to build a package using the official Debian way, including sources, documentation and package checking using lintian or piuparts. Please refer to the full how to package for Debian guide if you are looking for that specific information.


Creating packages requires the following packages to be installed:

  • build-essential
  • devscripts
  • debhelper

Preparing your package

The basic idea is that a given directory is built into a .deb package. This directory contains all the files required for the installation, as well as a separate ‘DEBIAN’ folder that contains any scripts or files required for dpkg. Let’s start out with the directory itself before diving in deeper.

The directory needs to follow a certain naming convention. The directory name should start with the package name, followed by a dash and then the version number. For example, “lswhelloworld-1.0”.

This directory should contain the ‘DEBIAN’ folder for dpkg and any other files that you wish the package should contain. Note that all the files will be installed at the same given location. For example, if you create a script at “lswhelloworld-1.0/usr/bin/helloworld”, the file will be located at “/usr/bin/”  after installation. Dpkg will also take care of removing the files when you uninstall the package.

After you have placed all the required files at the correct location, lets move on to the files within the ‘DEBIAN’ folder.


The most important file is the control file and should be located at “lswhelloworld-1.0/DEBIAN/control”.  This file contains all the information that dpkg needs to install your package correctly. This is the place where you should define your package name, version (followed by packaging number), architecture, dependencies and basic description of your package.

Lets look at a small example, which includes the minimum required fields to get it to work. There is an extensive explanation about the control file for binary packages available for people that want to learn more about the control file.

Package: lswhelloworld
Version: 1.0-1
Section: base
Priority: optional
Architecture: all
Depends: debconf (>= 0.2.26)
Maintainer: Rene van Aerle <>
Description: Hello World!
Say hello to the user.

The main thing to look for is the Depends field, this field lets you setup the dependencies for your package. In our case we will depend on debconf version 0.2.26 or higher.

The config file, located at “lswhelloworld-1.0/DEBIAN/config” will be run right after the package has been installed or when it is being pre- or re-configured. In the config script we can ask the user questions that may be used during installation. We could use bash to read user input as described on, but in general it is advised to use debconf for asking the user questions.

Debconf offers graphical package configuration and stores all the answers within a database. This database of answers can then be used for future reference and thus the values can be re-used in the pre-installation, post-installation, pre-removal and post-removal scripts without bothering the user with the same questions again. Only the dpkg-reconfigure command will re-ask the user questions.

Everytime you want to ask the user a question, you will need to add a template to the templates file, located at “lswhelloworld-1.0/DEBIAN/templates”.

Template: packagename/question1
Type: [select,multiselect,string,boolean,note,text,password]
Default: [an optional default value]
Description: Blah blah blah?
 Blah blah blah. Blah blah. Blah blah blah. Blah blah? Blah
 blah blah blah. Blah blah blah. Blah blah.
 Blah blah blah. Blah blah. Blah blah blah. Blah blah. Blah blah blah.

Template: packagename/question2

All your questions should be included, each identified with a unique template name (in example above: “packagename/questionX”). The type obviously depends on your question. The available data types include:

  • string  holds any arbitrary string of data
  • select  holds one of a finite number of possible values. These values must be specified in a field named Choices. Separate the possible values with commas and spaces, like this, Choices: yes, no, maybe
  • multiselect  just like the select data type, except the user can choose any number of items from the list. This means that the Default field and the actual value of the question may be a comma and space delimited list of values, just like the Choices field.
  • boolean  holds “true” or “false”
  • note this template is a note that can be displayed to the user. As opposed to text, it is something important that the user really should see. If debconf is not running interactively, it might be saved to a log file or mailbox for them to see later.
  • text  this template is a scrap of text that can be displayed to the user. It’s intended to be used for mostly cosmetic reasons, touching up around other questions that are asked at the same time. Unlike a note, it isn’t treated as something the user should definitely see. Less complex front-ends may refuse to ever display this type of element.
  • password  holds a password. Use with caution. Be aware that the password the user enters will be written to debconf’s database. You should consider clearing that value out of the database as soon as is possible.

After you’ve created your templates, you can use debconf to ask the questions in the config file. Note that it is advised to only ask questions within the config file and to do any other operation within the post/pre-installation or post/pre-removal scripts.

The example config script below asks the user 2 questions using debconf.


# Exit on error
set -e

# Source debconf library.
. /usr/share/debconf/confmodule

# Ask questions
db_input low packagename/question1 || true
db_input low packagename/question2 || true

# Show interface
db_go || true

First we include the debconf library and then prepare the questions using ‘db_input’. The priority is set so that the user can skip some questions according to the priority used with the dpkg command and fallback to the default values. (see: Ubuntu Manpage on dpkg-reconfigure)

Debian preinst, postinst, prerm, postrm scripts

The debian scripts are automatically run before or after a package is installed or removed. Along with the file named control, all these files are part of the control section of a debian archive and should be located in the ‘DEBIAN’ folder. For example, the post-installation script will be located at “lswhelloworld-1.0/DEBIAN/postinst”. The following scripts can be created (make sure the files are executeable, ‘chmod 0755’):

  • preinst – this script executes before that package will be unpacked from its Debian archive (“.deb”) file. Many ‘preinst’ scripts stop services for packages which are being upgraded until their installation or upgrade is completed (following the successful execution of the ‘postinst’ script).
  • postinst this script typically completes any required configuration of the package foo once it has been unpacked from its Debian archive (“.deb”) file. Often ‘postinst’ scripts ask the user for input, and/or warn the user that if they accept the default values, they should remember to go back and re-configure that package as the situation warrants. Many ‘postinst’ scripts then execute any commands necessary to start or restart a service once a new package has been installed or upgraded.
  • prerm this script typically stops any daemons which are associated with a package. It is executed before the removal of files associated with the package.
  • postrm – this script typically modifies links or other files associated with foo, and/or removes files created by the package.

Any of the above scripts can query the debconf database and take the appropriate action. To read the answers from the debconf database, you can use the following example code:


# Source debconf library.
. /usr/share/debconf/confmodule

# Fetching configuration from debconf
db_get packagename/question1

db_get packagename/question2

Again we include the debconf library and then use ‘db_get’ to fetch the answer from the debconf database. The value is then stored with the $RET variable. After creating your scripts, it is possible to build the package.

Building the package

To build the package, make sure the debian scripts (config, preinst, postinst, prerm, postrm) have the correct permissions (0755) and make sure the control and templates files are present within the ‘DEBIAN’ folder.


    • control (required)
    • templates (optional)
    • preinst (optional, chmod 0755)
    • postinst (optional, chmod 0755)
    • prerm (optional, chmod 0755)
    • postrm (optional, chmod 0755)
  • (files to be installed at specified location)

If you have all the files in place, you can build the package using ‘dpkg-deb –build’.

rene@ubuntu:~# dpkg-deb --build lswhelloworld-1.0/
dpkg-deb: building package `lswhelloworld' in `./lswhelloworld-1.0.deb'.

This will generate a .deb package that can be installed using ‘dpkg -i lswhelloworld-1.0.deb’ and will run the config and scripts accordingly. You can re-run the config by using ‘dpkg-reconfigure lswhelloworld’ or completely remove it ‘dpkg -r lswhelloworld’.


If you need help in building any of these packages, please leave your comment below.


Symfony2 under Nginx on Ubuntu 12.04

Here at LeaseWeb we use the Symfony2 framework a lot. While many people run Apache2 as their web server, some prefer running Symfony2 under Nginx. At this moment Nginx is very popular among high traffic websites. It is actually the second most popular web server on the internet. Unfortunately the default Nginx configuration will not work with Symfony2 since it does not support the .htaccess format. However, with some small tweaks to the configuration you can make  your server run Symfony2 under Nginx.

For this guide, we used a fresh Ubuntu 12.04 installation, downloaded Symfony Standard 2.2.1 (latest) and installed Nginx 1.1.19 and PHP-FPM 5.3.10 from the standard repositories.

Install Nginx

Firstly, you must install Nginx and start the service.

sudo apt-get install nginx
sudo service nginx start

Install PHP5

Since Symfony2 is a PHP framework, you need to install that as well. To make PHP work with Nginx you must install the PHP-FPM package.

sudo apt-get install php5-fpm

Configure Nginx virtual host

Create a new file at /etc/nginx/sites-available with the name of your domain or project, this will be the Nginx configuration for your Symfony2 project. Edit the file as required.


server {

    # Listen on port 80
    listen 80;

    # Server name being used (exact name, wildcards or regular expression)
    server_name *;

    # Document root, make sure this points to your Symfony2 /web directory
    root /var/www/;

    # Logging
    error_log /var/log/nginx/;
    access_log /var/log/nginx/;

    # Handle static files, redirect to app.php if not found
    location / {
        try_files $uri /app.php;

    # Pass the PHP scripts to FastCGI server
    location ~ ^/(app|app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  HTTPS off;

Now test the configuration.

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Add to your hosts file

If you are following this guide for production you can skip this step. For development you need to add the domain to the hosts file to make sure the current machine reacts when you type in the address. You can do that by running this command:

echo "" | sudo tee -a /etc/hosts

Enable site and reload the Nginx configuration

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
sudo service nginx reload

That should do the trick! Check your, and locations to verify that everything works correctly.

Using TCP/IP for FPM (optional)

If you prefer using TCP/IP for FPM, open the PHP-FPM pool config file: /etc/php5/fpm/pool.d/www.conf


listen = /var/run/php5-fpm.sock

By line:

listen =

Then change your Nginx site configuration.


fastcgi_pass unix:/var/run/php5-fpm.sock;

By line:


You must reload php-fpm and nginx for the changes to take effect. Note that with TCP/IP you can build a more scalable setup where you have multiple FPM machines and one Nginx as the load balancer in front of them.