Installing Node.js on Ubuntu

I would continue explaining more about Node.js, but for all those folks who do things and understand them, here’s the way to install Node.js on Ubuntu machines..

You need to first install necessary packages like git-core and libssl-dev

sudo apt-get update
sudo apt-get install g++ curl libssl-dev apache2-utils
sudo apt-get install git-core

Download the latest source and build it

wget http://nodejs.org/dist/v0.8.2/node-v0.8.2.tar.gz
gunzip node-v0.8.2.tar.gz
tar xvf node-v0.8.2.tar.gz
cd node-v0.8.2
./configure
sudo make install

Check the version after install

node -v

 

Advertisements

Django setup on Ubuntu

 

Setting up a django website calls for (though not always):

  • django installation
  • configuring Apache
  • mod_wsgi
  • others like database servers, static file server etc

Now if you are developing a small scale website, you may not want to go the Apache, mod_wsgi way.. Django helps here by providing a development web server, so that you can get your website up and running rapidly.

This blog talks about setting django website on Ubuntu 10.04:

Step1: Get python-pip

buntu@ubuntu:~$ sudo apt-get install python-pip 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libtext-glob-perl libcompress-bzip2-perl libparams-util-perl libfile-chmod-perl libdata-compare-perl libfile-pushd-perl libfile-which-perl
  libcpan-inject-perl libfile-find-rule-perl libcpan-checksums-perl libnumber-compare-perl
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  python-setuptools
The following NEW packages will be installed:
  python-pip python-setuptools
0 upgraded, 2 newly installed, 0 to remove and 186 not upgraded.
Need to get 262kB of archives.
After this operation, 1,192kB of additional disk space will be used.
Do you want to continue [Y/n]? 
Get:1 http://us.archive.ubuntu.com/ubuntu/ lucid/main python-setuptools 0.6.10-4ubuntu1 [213kB]
Get:2 http://us.archive.ubuntu.com/ubuntu/ lucid-updates/universe python-pip 0.3.1-1ubuntu2.1 [49.8kB]
Fetched 262kB in 5s (50.6kB/s)      
Selecting previously deselected package python-setuptools.
(Reading database ... 124327 files and directories currently installed.)
Unpacking python-setuptools (from .../python-setuptools_0.6.10-4ubuntu1_all.deb) ...
Selecting previously deselected package python-pip.
Unpacking python-pip (from .../python-pip_0.3.1-1ubuntu2.1_all.deb) ...
Processing triggers for man-db ...
Setting up python-setuptools (0.6.10-4ubuntu1) ...

Processing triggers for python-central ...
Setting up python-pip (0.3.1-1ubuntu2.1) ...

Step 2: Install django

buntu@ubuntu:~$ sudo pip install django
Downloading/unpacking django
  Downloading Django-1.4.1.tar.gz (7.7Mb): 7.7Mb downloaded
  Running setup.py egg_info for package django
Installing collected packages: django
  Running setup.py install for django
    changing mode of build/scripts-2.6/django-admin.py from 644 to 755
    changing mode of /usr/local/bin/django-admin.py to 755
Successfully installed django

Step 3: Check for django installation

buntu@ubuntu:~$ python -c "import django; print(django.get_version())"
1.4.1

Step 4: Create a project site

buntu@ubuntu:~$ django-admin.py startproject mysite
buntu@ubuntu:~$ tree mysite
mysite
|-- manage.py
`-- mysite
    |-- __init__.py
    |-- settings.py
    |-- urls.py
    `-- wsgi.py

1 directory, 5 files

Step 5: Start django development server

buntu@ubuntu:~$ cd mysite
buntu@ubuntu:~/mysite$ python manage.py runserver
Validating models...

0 errors found
Django version 1.4.1, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Step 6: Browse to http://127.0.0.1:8000/ home page

[09/Oct/2012 01:42:52] "GET / HTTP/1.1" 200 1957

 

 

Tornado – Database MySQL Client Wrapper

Tornado provides a simple MySQL wrapper for performing database operations. Class tornado.databse.Connection acts as a wrapper over MySQLdb DB-API connection.

Consider you have a MySQL installed on your system and you create a DB ‘mydb’ with table ‘post’ and records as below:

mysql> use mydb
Database changed

mysql> create table post (Id int, Title char(50), Author char(50));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into post values (1, 'Tornado Database', 'TechnoBeans');
Query OK, 1 row affected (0.01 sec)

mysql> insert into post values (2, 'Tornado Authentication', 'TechnoBeans');
Query OK, 1 row affected (0.00 sec)

mysql> insert into post values (3, 'Tornado Locale', 'TechnoBeans');
Query OK, 1 row affected (0.00 sec)

mysql> select * from post;
+------+------------------------+-------------+
| Id   | Title                  | Author      |
+------+------------------------+-------------+
|    1 | Tornado Database       | TechnoBeans |
|    2 | Tornado Authentication | TechnoBeans |
|    3 | Tornado Locale         | TechnoBeans |
+------+------------------------+-------------+
3 rows in set (0.02 sec)

Tornado helps you to connect, retrieve and display records from ‘mydb’ MySQL DB as shown in the example below

Output

 

In this example:

1. tornado.database.Connection connects to MySQL DB instance with appropriate server, database name, username and passwd

2. Object of class tornado.database.Connection ‘db’ is then used to query the records of table ‘post’

3. DB connection is then closed with db.close()

4. Records are then rendered by the webserver on accessing http://localhost/posts URL

Note:

While trying out this example, you may bump into this error

ubuntu@ubuntu:~/tornado-2.2$ python tornadodatabase.py 
Traceback (most recent call last):
  File "tornadodatabase.py", line 4, in 
    import tornado.database
  File "/home/ubuntu/tornado-2.2/tornado/database.py", line 20, in 
    import MySQLdb.constants
ImportError: No module named MySQLdb.constants

This is because python-mysqldb is not installed on your system. On ubuntu systems, you can get it with

sudo apt-get install python-mysqldb