Using Apache to save data in redis

30 / Jul / 2012 by raj 1 comments

In one of my projects, I was using redis database to collect some statistics and I thought of saving data into it at apache level. This would considerably enhance the speed of saving data as it would not require the interception of grails to save data.

The first step for this was to install apache by firing the following command in terminal :

[bash]sudo apt-get install apache2[/bash]

After installing apache, it was required to set up a site. For that I created a file named in /etc/apache2/sites-available directory and configured it as follows:

<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /usr/lib/cgi-bin

<Directory />
Options FollowSymLinks
AllowOverride All
Order allow,deny

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride All
AddHandler cgi-script .cgi
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Allow from ::1/128



Here the ServerName is set to and DocumentRoot is set to /usr/lib/cgi-bin. It means that if I hit url, then i will see the contents of /usr/lib/cgi-bin directory (This will come into action after enabling the site).
AddHandler cgi-script .cgi means that we are adding a cgi-script handler for files whose extension is cgi.

Now my site is in the list of available sites, but it’s still not enabled. To enable it I fired the following command in terminal :


This command created a soft link of my site into /etc/apache2/sites-enabled directory and now my site was enabled.

The last step was to make an entry for my site in  /etc/hosts. I appended following line to /etc/hosts to do so :


Finally, i was required to reload apache using command

[bash]service apache2 reload [/bash]

And now my site was live. To save data into the redis database, i created a file named saveData.cgi in /usr/lib/cgi-bin folder, given execution permissions to it and entered the following script into it.


echo "Content-type: text/html\n\n"


date=`echo $QUERY_STRING | sed ‘s/.*date\=\([^&]\+\).*/\1/’`

partnerSite=`echo $QUERY_STRING | sed ‘s/.*partnerSite\=\([^&]\+\).*/\1/’`

videoId=`echo $QUERY_STRING | sed ‘s/.*videoId\=\([^&]\+\).*/\1/’`

`redis-cli hincrby "$videoId-$date-views" $partnerSite 1`


Here shebang is used to execute script in the bash shell and the content type is set to text/html.
In redis, HINCRBY command is used in relation with hashes, where a key has one or more fields each with a corresponding value.
If the key and field already exists in the redis database, HINCRBY command would increment the value of the field by specified number. If the key doesn’t exists, HINCRBY creates a key with specified filed and value.

Thus, if i hit the url in my browser, then date, partnerSite and videoId are extracted from the query string using sed command and in the redis database an entry would be saved whose key will be “wwYXWU-20July2012-views”, field will be “facebook” and value will be 1. If the same url is received again, the value of field “facebook” of key “wwYXWU-20July2012-views” will become 2 and so on.

In this way, we can easily use apache to save and update data in redis database.


comments (1 “Using Apache to save data in redis”)

Leave a Reply

Your email address will not be published. Required fields are marked *