Friday, December 27, 2013

How to remove old lock file terminating mongodb

dileep@dileep-VirtualBox:~$ sudo mongod --dbpath /var/lib/mongodb/
Sat Dec 28 01:36:55.801
Sat Dec 28 01:36:55.803 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Sat Dec 28 01:36:55.804
Sat Dec 28 01:36:55.933 [initandlisten] MongoDB starting : pid=2656 port=27017 dbpath=/var/lib/mongodb/ 32-bit host=dileep-VirtualBox
Sat Dec 28 01:36:55.936 [initandlisten]
Sat Dec 28 01:36:55.937 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Sat Dec 28 01:36:55.937 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Sat Dec 28 01:36:55.938 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Sat Dec 28 01:36:55.939 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Sat Dec 28 01:36:55.939 [initandlisten]
Sat Dec 28 01:36:55.940 [initandlisten] db version v2.4.8
Sat Dec 28 01:36:55.941 [initandlisten] git version: a350fc38922fbda2cec8d5dd842237b904eafc14
Sat Dec 28 01:36:55.941 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Sat Dec 28 01:36:55.942 [initandlisten] allocator: system
Sat Dec 28 01:36:55.942 [initandlisten] options: { dbpath: "/var/lib/mongodb/" }
**************
Unclean shutdown detected.
Please visit http://dochub.mongodb.org/core/repair for recovery instructions.
*************
Sat Dec 28 01:36:55.951 [initandlisten] exception in initAndListen: 12596 old lock file, terminating
Sat Dec 28 01:36:55.952 dbexit:
Sat Dec 28 01:36:55.952 [initandlisten] shutdown: going to close listening sockets...
Sat Dec 28 01:36:55.953 [initandlisten] shutdown: going to flush diaglog...
Sat Dec 28 01:36:55.955 [initandlisten] shutdown: going to close sockets...
Sat Dec 28 01:36:55.956 [initandlisten] shutdown: waiting for fs preallocator...
Sat Dec 28 01:36:55.957 [initandlisten] shutdown: closing all files...
Sat Dec 28 01:36:55.959 [initandlisten] closeAllFiles() finished
Sat Dec 28 01:36:55.959 dbexit: really exiting now

To remove the Error we only do ,

dileep@dileep-VirtualBox:/$ cd var/lib/mongodb
dileep@dileep-VirtualBox:/var/lib/mongodb$ sudo rm mongod.lock
dileep@dileep-VirtualBox:/var/lib/mongodb$ ls
local.0  local.ns  _tmp

Then have to put the mongod --repair comand for repairing the mongodb. or else try to do the

 sudo mongod --dbpath /var/lib/mongodb/      command in the terminal.

Then the mongodb will work again.




Thursday, December 26, 2013

HTTPS server (SSL) Implementation in node.js

Node.js provides HTTP server as one of the core libraries. So there is no need for a separate HTTP server. But if you are developing any application in node js which needs secure transactions then HTTPS server is necessary for it. So it can allow private information to be transmitted without the problems of eavesdropping, data tampering, or message forgery between your node.js  server and your visitor’s browser.
Developer should add SSL certificate in code to implement HTTPS server in node.js. In my last post I have explained, how to create self-signed SSL certificate in Ubuntu. It has commands and detail explanation for generating self-signed SSL in any Unix OS.  So for testing purpose you do not need to buy SSL certificate.
You can implement HTTPS server using simple steps:
Prerequisites:
  • node.js
Before start to implement HTTPS server please make sure that you have installed node.js in your system.
Commands to Create self-signed SSL certificate 
If you need a detail explanation and process, you can get it here. Following are commands to generate self-signed SSL in any Unix OS :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Become root first

$ apt-get install openssl

$ mkdir /etc/ssl/self-signed && cd /etc/ssl/self-signed

$ openssl genrsa -des3 -out server.key 2048

$ openssl rsa -in server.key -out server.key.insecure

$ mv server.key server.key.secure && mv server.key.insecure server.key

$ openssl req -new -key server.key -out server.csr

$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Above commands will generate two files server.key(private key to SSL certificate) and server.crt(signed certificate) in /etc/ssl/self-signed folder.
Node.js Code to implement HTTPS server
Now create a file server.js in your system. And add following code in it :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Include the https and ,file system modules
var https = require('https');
var fs = require('fs');

//Create the server options object, specifying the SSL key & cert
var options = {
key: fs.readFileSync('/etc/ssl/self-signed/server.key'),
cert: fs.readFileSync('/etc/ssl/self-signed/server.crt')
};

//Create the HTTPS enabled server - listening on port 443
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(443);
Code Explanation :
Following is line by line explanation of above code:
1
2
var https = require('https');
var fs = require('fs');
In above two lines we are adding HTTPS and FS modules which are required for SSL implementation. After including require modules we need to add SSL files (key and cert files) path. We have already generated self-signed SSL and its files are stored in /etc/ssl/self-signed folder.
1
2
3
4
var options = {
key: fs.readFileSync('/etc/ssl/self-signed/server.key'),
cert: fs.readFileSync('/etc/ssl/self-signed/server.crt')
};
In above code we are adding server.key and server.crt files path in options array variable. Now we need to create HTTPS server and pass this files through options variable to it.
1
2
3
4
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(443);
In above code lines, we have created HTTPS server by using createServer method. In it we have passed options variable as a parameter which contains SSL files path. And this server is listening on port no 443 which is a HTTPS port.
If you buy authenticated SSL certificate from any trusted service provider then you will get .key and .crt files. Just save this files in your system and replace self-signed SSL files path with this new authenticated files in above code.
Now open terminal and goto path where you have saved server.js file in your system. And run following command:
1
node server.js
Now open your favourite browser and enter https://localhost in address bar and press enter key. You will get hello world text in browser. That means you have successfully implemented HTTPS server in node.js. Please note that if you are using any other port than 443 then please mention that port number in url. For example, if you are using port number 8443 then enter https://localhost: 8443 url in browser.


Monday, December 23, 2013

Create self-signed SSL certificate in UbuntuinShare
What is SSL Certificate?
SSL is an acronym for Secure Sockets Layer. SSL Certificates are small data files that digitally bind a cryptographic key to an organisation’s details. It creates an encrypted connection between your web server and your visitor’s browser, allowing for private information to be transmitted without the problems of eavesdropping, data tampering, or message forgery. When SSL gets install on web server,  it activates the padlock and the https protocol (over port 443).
What is Self-signed SSL Certificate?
Organisation needs to buy SSL certificate from trusted hosting companies and its prices are very high. So, for development and testing purpose organisation can use self-signed SSL certificates. This self-signed SSL are certificates which get locally create on web server where it needs for testing of new SSL implementation. It is an identity certificate signed by its own creator; however, they are considered to be less trustworthy.
This temporary certificate generates an error in the client browser to the effect that the signing certificate authority is unknown and not trusted because it’s not signed by any known trusted CA authority.
SSL Certificate Files:
SSL certificate needs .key and .crt files. This files represent both parts of a certificate, .key is a private and .crt is a public part of certificate that means key being the private key to the certificate and crt being the signed certificate.
How to create Self-Signed SSL certificate in Ubuntu?
The openssl library is required to generate self-signed SSL certificate in Ubuntu. Open terminal and become root first . Now check if you already have openssl installed. Use following command for it:
1
$ which openssl
If above command returns path like /usr/bin/openssl , that means your system has openssl. But if it does not return any path then you will need to install openssl yourself. Use following command to install openssl:
1
$ apt-get install openssl
After installing openssl, private key (.key file) and signed certificate (.crt file) is required for SSL certificate. We need to store .key and .crt files in a single folder.
1
$ mkdir /etc/ssl/self-signed && cd /etc/ssl/self-signed
Above commands create self-signed folder on /etc/ssl path. Following are simple four steps which will guide you to create self-signed SSL certificate for your web server.
Step 1 :  Create a Private Key
The first step is to create your RSA Private Key. This key will be 1024 bit RSA key which will be encrypted using Triple-DES and will be store in a PEM format so that it will be readable as ASCII text. Command to create RSA key:
1
$ openssl genrsa -des3 -out server.key 1024
Edited:
Due to the increasing computing power available to decrypt SSL certificates, the Certificate Authority Browser (CAB) Forum (the entity that establishes SSL industry standards) requires that all SSL certificates issued after Jan. 1, 2014, use at least 2048-bit keys. SSL certificates that use 1024-bit keys are no longer secure. Command to create 2048 bit RSA key:
1
$ openssl genrsa -des3 -out server.key 2048
Step 2 : Remove pass-phrase from key
One side-effect of private key is that, Apache always ask for pass-phrase when web server gets start. But we can remove this pass-phrase restriction using following command:
1
$ openssl rsa -in server.key -out server.key.insecure
Now rename files:
1
$ mv server.key server.key.secure && mv server.key.insecure server.key
Step 3 : Generate a CSR (Certificate Signing Request)
Once private key is generated and pass-phrase restriction is removed, you should generate certificate signing request(CSR). During CSR generation, you will get prompted for different information which are the certificates attribute. Please note when you will get prompt for “Common Name”, you should enter complete name of your domain. For example if request is getting generate for shivalibari.com then please enter shivalibari.com as a common name. So this domain name will get protected with SSL like : https://shivalibari.com. Use following command for CSR generation:
1
$ openssl req -new -key server.key -out server.csr
Step 4 : Generate Self-Signed SSL Certificate
Use following command to generate self-signed certificate:
1
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
This certificate can get use for 365 days.
Using above simple four steps you can successfully create self-signed SSL certificate. You can use the filesserver.key and server.crt which are located in /etc/ssl/self-signed into your code or in any programming language to implement SSL or HTTPS connection with your web application.