Sunday, 24 January 2016

How to setup Mongo db on ubuntu

Step 1: Import the public key used by the package management system.
The Ubuntu package management tools (i.e. dpkg and apt) ensure package consistency and authenticity by requiring that distributors sign packages with GPG keys.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
Step 2: Create a list file for MongoDB.
Create the /etc/apt/sources.list.d/mongodb-org-3.2.list list file using the command appropriate for your version of Ubuntu:
Ubuntu 12.04

echo "deb http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
Ubuntu 14.04

echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
Step 3: Reload local package database.
Issue the following command to reload the local package database:

sudo apt-get update
Step 4:  Install the MongoDB packages.
You can install either the latest stable version of MongoDB or a specific version of MongoDB.

Install the latest stable version of MongoDB.

Issue the following command:

sudo apt-get install -y mongodb-org

Step 5: Install PHP5

This is simple PHP5 installation on our system. You may also need to install other php modules as per your application requirements like php5-xml, php5-mbstring, php5-gettext etc.

sudo apt-get install php-pear php5-dev

Step 6: Install MongoDB PHP Module

Finally we need to Install Mongo PECL extension and add in php configuration file. which provides connectivity between PHP and MongoDB.

sudo pecl install mongo
Note: type no


What makes Ubuntu 12.04 a little different from other systems in how PHP extensions get loading when Apache/PHP starts. To make sure this extension gets loaded, we'll need to add a .ini file to "/etc/php5/conf.d". To do that, run the following command:

sudo vi /etc/php5/apache2/conf.d/mongo.ini
After creating the file, add this line of content:

extension=mongo.so
Also

sudo vi /etc/php5/apache2/php.ini

add this line of content:

extension=mongo.so



After installing the extension, lets restart apache service

sudo service apache2 restart

Step 7: Verify Setup

At this stage we have complete all installation. But make sure that we have properly integrated MongoDB with PHP5. To test this create an phpinfo.php file using following value.
<?php
phpinfo();
?>




Friday, 17 July 2015

cron job concept

Basic Linux Crontab Format

     MIN HOUR DOM MON DOW CMD


FieldDescriptionAllowed Value
MINMinute field0 to 59
HOURHour field0 to 23
DOMDay of Month1-31
MONMonth field1-12
DOWDay Of Week0-6
CMDCommandAny command to be executed.




Schedule cron job every five minute

*/5 * * * *  curl "CALLED_URL" 



Schedule cron job every week minute 


*/5 * * * *  curl "CALLED_URL"  




Saturday, 11 July 2015

setup logstash forwarder on client machine

A. Install Logstash Forwarder Package

echo 'deb http://packages.elasticsearch.org/logstashforwarder/debian stable main' | sudo tee /etc/apt/sources.list.d/logstashforwarder.list

wget -O - http://packages.elasticsearch.org/GPG-KEY-elasticsearch | sudo apt-key add -

sudo apt-get install logstash-forwarder

B. change config

sudo vi /etc/logstash-forwarder.conf

{ "network": { "servers": [ "logstash_server_private_IP:5000" ], "timeout": 15, "ssl ca": "/etc/pki/tls/certs/logstash-forwarder.crt" }, "files": [ { "paths": [ "/var/log/syslog", "/var/log/auth.log" ], "fields": { "type": "syslog" } } ] }

 C. First get file(logstash-forwarder.crt) from logstash server
 
sudo mkdir -p /etc/pki/tls/certs

and copy /etc/pki/tls/certs/

How to setup kibana,elasticsearch,logstash server,redis

A. Elasticsearch


sudo wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.5.2.deb
sudo dpkg -i elasticsearch-1.5.2.deb

sudo update-rc.d elasticsearch defaults 95 10

sudo /etc/init.d/elasticsearch restart

Elasticsearch is now installed. Let's edit the configuration:

sudo vi /etc/elasticsearch/elasticsearch.yml

network.host: localhost

sudo service elasticsearch restart

sudo update-rc.d elasticsearch defaults 95 10



B. Kibana

wget https://download.elasticsearch.org/kibana/kibana/kibana-4.0.1-linux-x64.tar.gz

tar xvf kibana-*.tar.gz

vi ~/kibana-4*/config/kibana.yml

host: "localhost"

sudo mkdir -p /opt/kibana

Now copy the Kibana files into your newly-created directory:

sudo cp -R ~/kibana-4/ /opt/kibana/

Kibana can be started by running /opt/kibana/bin/kibana, but we want it to run 
as a service. Download a Kibana init script with this command:

cd /etc/init.d && sudo wget https://gist.githubusercontent.com/thisismitch/8b15ac909aed214ad04a/raw/bce61d85643c2dcdfbc2728c55a41dab444dca20/kibana4

Now enable the Kibana service, and start it:

sudo chmod +x /etc/init.d/kibana4

sudo update-rc.d kibana4 defaults 96 9

sudo service kibana4 start



C. Setup Logstash

 sudo wget http://download.elastic.co/logstash/logstash/packages/debian/logstash_1.5.0-1_all.deb

sudo dpkg -i logstash_1.5.0-1_all.deb

sudo update-rc.d logstash defaults 95 10

sudo /etc/init.d/logstash restart


D. Create ssl key for clients


sudo mkdir -p /etc/pki/tls/certs sudo mkdir /etc/pki/tls/private

If you don't have a DNS setup—that would allow your servers, that you will gather logs from, to resolve the IP address of your Logstash Server—you will have to add your Logstash Server's private IP address to the subjectAltName (SAN) field of the SSL certificate that we are about to generate. To do so, open the OpenSSL configuration file:

sudo vi /etc/ssl/openssl.cnf

subjectAltName = IP: logstash_server_private_ip

cd /etc/pki/tls;

sudo openssl req -x509 -batch -nodes -days 365 -newkey rsa:2048 -keyout private/logstash-forwarder.key -out certs/logstash-forwarder.crt

E. Create logstash config files for getting data from client machine
  1. LogStash for LumberJack:
vi /etc/logstash/conf.d/01-lumberjack-input.conf

input { lumberjack { port => 5000 type => "logs" ssl_certificate => "/etc/pki/tls/certs/logstash-forwarder.crt" ssl_key => "/etc/pki/tls/private/logstash-forwarder.key" } }
  1. grok syslogs into the correct format:
vi /etc/logstash/conf.d/10-syslog.conf

filter { if [type] == "syslog" { grok { match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:[%{POSINT:syslog_pid}])?: %{GREEDYDATA:syslog_message}" } add_field => [ "received_at", "%{@timestamp}" ] add_field => [ "received_from", "%{host}" ] } syslog_pri { } date { match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ] } } }
  1. configure LogStash to store it’s logs in ElasticSearch
vi /etc/logstash/conf.d/30-lumberjack-output.conf

output { elasticsearch { host => localhost } stdout { codec => rubydebug } }

E. install redis server

sudo apt-get install redis-server

Search for the following line in /etc/redis/reis.conf and replace

bind 127.0.0.0

with

bind 0.0.0.0
 

How to resolve ajax cross domain error

If we request from sub domain to main domain from ajax, it will produce cors error. 


To allow cors request we need to set headers on server

header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');



Also in javascript we need to set domain name


document.domain='domain.com';

Tuesday, 25 November 2014

decode special character in php

many times html was unable to render properly. So we use

ex.  &nbsp;Which company in india

if we use htmlspecialchars_decode() then result

    Which company in india


Thursday, 20 November 2014

How to store audio file in browser local storage


Using below code we can store audio file in browser local storage

1. Create XHR and FileReader objects

var xhr = new XMLHttpRequest(),
fileReader = new FileReader();
xhr.open("GET", url_audio, true);


2. Set responseType to blob

xhr.responseType = "blob";


3. add addEventListener

xhr.addEventListener("load", function () 
{
if (xhr.status === 200)
{
fileReader.onload = function (evt) 
{
// Read out file contents as a Data URL
var result = evt.target.result;
var audioElement = document.createElement('audio');
audioElement.setAttribute('src',result);
audioElement.setAttribute('autoplay', 'autoplay');
try 
{
localStorage.setItem(key, result);
}
catch (e) 
{
console.log("Storage failed: " + e);
}
};
// Load blob as Data URL
fileReader.readAsDataURL(xhr.response);
}
}, false);
xhr.send();