afurlan's blog

/^random (nerd|geek)? posts$/

tag: debian

Nginx and PhpLdapAdmin

Monday, May 24, 2010 - Nine comments

I usually run Apache in my web servers, it works pretty well but spends too much memory for that. So I decided to give a try to the Nginx and for now, I'm really impressed about its preformance.

I don't intend to present Nginx at this post, but if you want to know more about it (and IMHO, you should) go to its official documentation. I'm going to show how to configure the PhpLdapAdmin and Nginx but not how to install the PHP itself. So I'll presume you currently have PHP working on Nginx but if you don't, take a look at here to know how to do that using the spawn-fcgi.

Let's start installing the PhpLdapAdmin and the PHP5 module to connect to the LDAP server. If you're using Debian/Ubuntu, all new software are just an "aptitude install" of distance from you, so let's install them:

$ aptitude install phpldapadmin php5-ldap

Now we can start the PhpLdapAdmin on Nginx configuration itself... You can use the PhpLdapAdmin under a specific domain or use it as a standalone application. If you want to install the PhpLdapAdmin under a specific domain, you should create a new virtual host named example.com as follows:

$ vim /etc/nginx/sites-available/example.com
server {
    server_name example.com;
    listen 80;

    # document root
    root /usr/share/phpldapadmin/htdocs;
    index index.php index.html index.htm;

    # application: phpldapadmin
    location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_param HTTPS on;
    }

    # logging
    error_log /srv/example.com/nginx/log/error.log;
    access_log /srv/example.com/nginx/log/access.log;
}

Or if you want to install the PhpLdapAdmin as a standalone application, you should create a new virtual host as follow:

$ vim /etc/nginx/sites-available/example.com
server {
    server_name example.com;
    listen 80;

    # document root
    root /srv/example.com/nginx/www;
    index index.php index.html index.htm;

    # application: phpldapadmin
    location /phpldapadmin {
            alias /usr/share/phpldapadmin/htdocs;
            index index.php index.html index.htm;
    }
    location ~ ^/phpldapadmin/.*\.php$ {
            root /usr/share;
            if ($request_filename !~* htdocs) {
                    rewrite ^/phpldapadmin(/.*)?$ /phpldapadmin/htdocs$1;
            }
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            include fastcgi_params;
    }

    # logging
    error_log /srv/example.com/nginx/log/error.log;
    access_log /srv/example.com/nginx/log/access.log;
}

And now, we can just enable the new virtual host and restart Nginx:

$ cd /etc/nginx/sites-enabled
$ ln -s ../ldap.example.com
$ /etc/init.d/nginx restart

No, this blog isn't running over nginx (yet). It's a Python (Django) application, so I want to migrate it ASAP and test a new setup based on Nginx + Gunicorn following a recomendation of Osvaldo Santana... but for now, that's all. :)

As always: if you found some english bug, warn me and I'll be glad to fix it. :)

Using IPython as your default shell

Thursday, October 29, 2009 - 46 comments

If you are a shell addicted person and like Python, then the IPython was made just for you! The IPython provides a very usefull feature (that may looks a bit strange at first) of mixing Python and Shell commands in a easy and intuitive way. I don't pretend to say why you should use IPython instead of your current shell but I think that, if you know Python, you should prefer to write Python code instead of Bash code.

I'll assume that you already have the IPython installed but if you don't, take a look at IPython's documentation to see how to install it. I'm running Debian and my setup is focused in my machine, so it should work for the most of the others linux distros but if you are running a different system, I'm sorry for you you may have to adapt some commands and/or paths. :)

Configuring the IPython's prompt

The first time you run IPython, it displays a warning about the creation of your personal configuration directory. One of the most important parts of this warning is about the new configuration file: ~/.ipython/ipy_user_conf.py. So let's configure this file and the IPython's prompt to make it looks exactly like the Debian's prompt. Edit your configuration file and change the following settings as below:

...
import ipy_profile_sh
...
o.prompt_in1 = r'\C_Normal\u@\H:\Y2$ '
o.prompt_in2 = r'\C_Normal... '
...

The first option, import ipy_profile_sh, avoid you to have to escape all the Bash commands. The second and third options, o.prompt_in1 and o.prompt_in2, change the IPython's prompt. And now start the IPython as follow:

afurlan@merlin:~$ ipython -nosep -nobanner -noconfirm_exit
afurlan@merlin:~$
afurlan@merlin:~$ 
afurlan@merlin:~$ print 'hello python!'
hello python!
afurlan@merlin:~$ echo 'hello bash!'
hello bash!

Now you're able to run Python commands:

afurlan@merlin:~$ for line in open('domains.txt'):
              ...     print line
              ...    
              ...    
afurlan.org

blog.afurlan.org

And a mixed of Python and Bash commands:

afurlan@merlin:~$ for line in open('domains.txt'):
              ...     echo "$line"
              ...    
              ...    
afurlan.org

blog.afurlan.org

afurlan@merlin:~$ for line in open('domains.txt'):
              ...     echo "$line.strip()"
              ...    
              ...    
afurlan.org
blog.afurlan.org

Setting the IPython as your default shell

Actually I don't like to set the IPython as my default shell... I use to run screen so I configure it to open five IPython sessions by default but if you really like to set it as your default shell, you can add the following line at the end of your ~/.bashrc file:

...
exec ipython -nosep -nobanner -noconfirm_exit

If you're a screen user too, you can add the following lines at your ~/.screenrc:

...
# open 5 ipython sessions by default
screen -t p 1 ipython -nosep -nobanner -noconfirm_exit
screen -t p 2 ipython -nosep -nobanner -noconfirm_exit
screen -t p 3 ipython -nosep -nobanner -noconfirm_exit
screen -t p 4 ipython -nosep -nobanner -noconfirm_exit
screen -t p 5 ipython -nosep -nobanner -noconfirm_exit
...

Or, if you like, you can also get my current .screenrc file.

And, as always: if you found some english bug, warn me and I'll be glad to fix it. :)

Playing with Python and GeoIP in Debian

Wednesday, October 14, 2009 - 63 comments

Yep, I know, in the previous post I said that I'd try to write more often and in small chunks but looks like it's not working...

By the way, today I needed to find out the origin country of an hostname (actually a list of hostnames). As Debian has a GeoIP database available via aptitude, I could accomplish that using this database and the Python's GeoIP module.

First of all I installed the necessary packages

merlin:~# aptitude install geoip-database python-geoip

And then I wrote the following small script that configure the Python's GeoIP module to use the Debian's GeoIP database:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import sys
import GeoIP

# oops, check the usage! :)
if len(sys.argv) != 2:
        print 'Usage: %s ADDRESS' % sys.argv[0]
        sys.exit(1)

# geoip database from "geoip-database" debian package
GEOIP_DATABASE = '/usr/share/GeoIP/GeoIP.dat'
geoip = GeoIP.open(GEOIP_DATABASE, GeoIP.GEOIP_STANDARD)

# get the country name based in the given input
if re.match('^[0-9]{1,3}(\.[0-9]{1,3}){3}$', sys.argv[1]):
        response = geoip.country_name_by_addr(sys.argv[1])
else:
        response = geoip.country_name_by_name(sys.argv[1])

# print the response if there is any response
if response:
        print response

Some tests to check if it is really working... and it looks like it is. :)

afurlan@merlin:~$ ./hostname_country.py afurlan.org
United States
afurlan@merlin:~$ ./hostname_country.py mandriva.com
France
afurlan@merlin:~$ ./hostname_country.py debian.com
Netherlands
afurlan@merlin:~$ ./hostname_country.py mps.com.br
Brazil
afurlan@merlin:~$ ./hostname_country.py some-nonexistent-domain.org
afurlan@merlin:~$

I think that's all...

Update: I changed the code based on the Bogdano's comment, thanks Bogdano. :)

And, please, never forget: if you found some english bug, warn me and I'll be glad to fix it. :)

Hello world, again

Monday, May 25, 2009 - 193 comments

merlin Yep, that's the second time I post a "Hello world" entry here. The reason is that I recently moved my domain from an account at Bluehost to a VPS at Linode. So, first of all, I would like to introduce you to merlin, my new VPS. :)

As I'm the sysadmin behind my server now, I decided to not install mod-php here and then I had to change my blog engine. I used to run Wordpress for years and now I'm running django-diario, a wonderful blog engine for django made by Brazilian djanglers.

About this new version of my old blog, you may have noticed that it has only this post... That's right, I removed the old stuff (in fact I just didn't migrate them from Wordpress) and I intend to start translating and re-posting a small part of them as soon as possible. If you want to see here any specific post from my old blog, please, let me know.

As you can see now I'm writing in English and I used to write in Portuguese... I'm not a professional English writer, so you won't be a lucky guy if you find some bugs around here and if you do find something, please, let me know about it and I will gladly fix it.

You'll probably see at here lots of posts about Python and Debian. This time I'll also try to write more often and in small chunks. :)

I think that's all...