Friday, 17 October 2014

Set up Chef on Ubuntu Machine

Ubuntu + Chef + Errors

On  chef server(new ubuntu machine)

cd ~
Install the chef server or install from web site
=> wget

Install the package using terminal
=> sudo dpkg -i chef-server*

Run this command to confugre chef server on the machine
=> sudo chef-server-ctl reconfigure

Open browser (write https and ip of server)

Add default username and password.

On workstation(diiferent ubuntu machine)

update the system
=> sudo apt-get update

Install Git
=> sudo apt-get install git

Install Curl
=> sudo apt-get install curl

You can download the chef client from wesite and manually install this.
otherwise run
=> curl -L | sudo bash

Get the chef-repo from git
=> git clone

create .chef repository
=> mkdir ~/chef-repo/.chef

Copy the keys:

Now we will put the keys into .chef folder so open in browser put chef server url

Login and Tap on the Clients > edit(chef-validator) > chek the checkbox > save
It will generate a key so just copy the private key and paste in

create a chef-validator file in .chef folder
=> vim chef-validator.pem
paste the key

There should be no extra space
Now same for the Users > admin > edit > check checkbox  for regenerate the private key > save
It will generate a key so just copy the private key and paste in

create a chef-validator file in .chef folder
=> vim admin.pem
paste the key

run knife command
=> knife configure --initial

PASTE THE PATH ASKED BY OPETIONS LIKE FOR admin key path should be /home/vagrant/chef-repo/.chef/admnin.pem
same for the all

No need to commit the .chef folder so
=> vim ~/chef-repo/.gitignore

Add .chef in the end of the file


# Ignore Chef key files and secrets

run knife command to check if knife is configured or not
=> knife user list

if it gives error reconfigure the knife configuration

Client Node (another ubuntu instance) ip address(

now bootstrap to the clint using the following command
=> knife bootstrap -x username -P password -N name_for_node --sudo

If bootstraps success than you will find a node in chef-repo/nodes

Now chek client list
=> knife client list

Errors faced by me

While creating node
Error: RuntimeError: Please set EDITOR environment variable
export EDITOR=$(which vi)

ERROR: Failed to upload /home/vagrant/chef-repo/cookbooks/apt/recipes/default.rb
Chef-server redirects api clients between its components (in this case erchef and bookshelf). These redirects are based on the FQDN of the chef-server. If the server doesn't have a valid FQDN at install time, this can result in redirects to https://localhost:443.

One solution is to fix the FQDN on chef-server and run chef-server-ctl reconfigure.

Another solution is to set a few options in /etc/chef-server/chef-server.rb (I had to create this file) and then run chef-server-ctl reconfigure.

server_name = ""
api_fqdn server_name

nginx['url'] = "https://#{server_name}"
nginx['server_name'] = server_name
lb['fqdn'] = server_name
bookshelf['vip'] = server_name

Swap Memory in Ubuntu

Swap Memory + Ubuntu

I found these two blogs for swap memory which provide me a good understanding about swap memory and help me to increase swap memory of my Ubuntu system

Vagrant (Virtual Machine Manager on ubuntu)

Virtual Machine Manager + Vagrant + Generate Multiple VM's + Windows +
 Common Errors + VMware

Steps to starts

. Create a folder anywhere and give entry in the folder
   mkdir tarun

 . cd tarun

 . vagrant init   #it will generate a file named "vagrant" with cinfigurations.

 . cd vagrant

 . Add a box into the ".vagrant.d\boxes\" in C drive.

 . run command  :-
   vagrant box add precise64 "C:\Users\admin\.vagrant.d\boxes\"

 . When you will enter into the vagrant folder you will find a vagrant configuration file. In vagrant configuration file create a global configuration for virtual box. = "precise64"

 . Create the three layers.
   vagrant up

 . Define 3 enviornments with same virtual box.

    config.vm.define "web" do |web| = "web"
      #web.vm.hostname = "web" :private_network, ip: "" :forwarded_port, guest: 80, host: 8080

    config.vm.define "db" do |db| = "db"
      #db.vm.hostname = "mysql" "private_network", ip: "" :forwarded_port, guest: 3306, host: 8888

    config.vm.define "master" do |master| = "master"
      #master.vm.hostname = "master" "private_network", ip: "" :forwarded_port, guest: 80, host: 8080

  . Define networks to communicate. "private_network", ip: ""

  . Define port :forwarded_port, guest: 80, host: 8080

  . After changing run command
    vagrant reload   #helps to configure the changed configuration

  . After generate the three layers, check the virtual box.

  . To enter in the layers, use command
     vergant ssh web 


  Error: Failed to create the host-only adapter :-
  Sol:   Reinstall the virtualbox and run it as administration.
    Or update the virtual box

  Error: Vagrant cannot forward the specified ports on this VM, since they
      would collide with some other application that is already listening
      on these ports. The forwarded port to 8080 is already in use
      on the host machine.
  Sol: write    :-   , auto_correct: true
       else :- Change the host, if already defined.

Rails and Mysql Set up on ubuntu Server

Ruby + Rails + Mysql + RVM + Gemset + Apache

Steps to set up server

On Ubuntu Image

Reference Link

1. Install curl first

sudo apt-get install curl

2. Install rvm

\curl -L | bash -s stable

3. Path to load rvm (Given in comments after installation of )

source ~/.rvm/scripts/rvm
source ~/.profile

4. Requirements related to rvm

rvm requirements

5. Check version of rubies present on cloud

rvm list known

6. Install specific version

rvm install 1.9.3

7. Create gemset for particular rvm

rvm gemset create demo

8. Use gemset

rvm gemset use demo 

9. If you want to build a application from scratch than install gem rails.

gem install rails -v 3.2.14

10. Otherwise enter into the application and run

rvm use 1.9.3-p374@demo

Installing Mysql steps

Make sure your package management tools are up-to-date. Also make sure you install all the latest software available.

sudo apt-get update

sudo apt-get dist-upgrade

Install the MySQL server and client packages:

sudo apt-get install mysql-server mysql-client

You need to set a root password

sudo mysqladmin -u root -h localhost password 'mypassword'

sudo mysqladmin -u root -h myhostname password 'mypassword'

For Ruby dependencies install

sudo apt-get install libmysql-ruby

You can now access your MySQL server like this:

mysql -u root -p

Installing Apache has never been easier using apt-get.

First, I updated the repositories.

sudo apt-get update

Then fixed the locales.

sudo locale-gen en_US en_US.UTF-8 en_CA.UTF-8

sudo dpkg-reconfigure locales

And finally installed all the basic packages I thought I'd need.

sudo apt-get install apache2 curl git build-essential zlibc zlib1g-dev zlib1g libcurl4-openssl-dev libssl-dev libopenssl-ruby apache2-prefork-dev libapr1-dev libaprutil1-dev libreadline6 libreadline6-dev

Set Up PostgreSql On Ubuntu

sudo apt-get install postgresql postgresql-contrib
sudo apt-get install libpq-dev

sudo -u postgres createuser --superuser tarun
sudo -u postgres psql

postgres=# \password tarun
password: tarun
enter again password: tarun

Client programs, by default, connect to the local host using your Ubuntu login name and expect to find a database with that name too. So to make things REALLY easy, use your new superuser privileges granted above to create a database with the same name as your login name:

 sudo -u postgres createdb tarun

Connecting to your own database to try out some SQL should now be as easy as:


Creating additional database is just as easy, so for example, after running this:

 create database mmagap;

Wowza Server Setup with Ubuntu and Also Integration In Rails

Wowza + Flowplayer + Video Upload + Rails + Video Streaming + Stop video after some time + java setup on ubuntu

1. First set up java on ubuntu (should be 64 bit system) :- in browser

$ sudo sh -c 'echo "deb trusty main" >> /etc/apt/sources.list'
$ sudo sh -c 'echo "deb-src trusty main" >> /etc/apt/sources.list'
$ sudo apt-key adv --keyserver --recv-keys EEA14886
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer

2. Install wowza using (should have account on wowza) :- in browser

Copy the secret key of wowza

3. Let it download in directory /home/garg/Downloads/ :- in terminal
cd /home/garg/Downloads/

4. Install wowza
sudo chmod +x WowzaStreamingEngine-4.0.4.deb.bin
sudo ./WowzaStreamingEngine-4.0.4.deb.bin

5. start wowza service :- in terminal
sudo service WowzaStreamingEngine start
// sudo service WowzaStreamingEngine stop

6. On you machine, check wowza is running :- in browser
// http://[wowza-ip-address]:1935

7. Start engine manager :- in terminal
sudo service WowzaStreamingEngineManager start

8. In browser

Read and continue steps and run wowza
Login with user name and password you put in console on wowza installation

Keep flowplayer.content-3.2.9.swf' in assets

9. Set up amazon  S3 on wowza media server

- Click the "Server" tab at the top of the manager, and then select "Media Cache" in the left bar pane.

- Select "Sources" and "Add Media Cache Source"

- Selcect
Source Name = S3 Stream
Source Type = AmazonS3
Aws Key id = "amazon key"
AWS secret = "amazon secret"

Minimum Time To Live:
- select days to keep cache.

- Select "Applications" on top bar of the wowza server.

- Select "VOD Edge" named as "mediacache".

- select "Media Cache Sources", to your amazon "S3 Stream" => created media cache source name of amazon s3.

Add the following settings in code

<script type="text/javascript" src=""></script>

<%= form_for @taped, :url => upload_taped_video_videos_path, :html => { :multipart => true } do |form| %>
  <%= form.file_field :taped_video %>
  <%= form.submit "Upload" %>
<% end %><br/>

<%@taped_videos.each_with_index do |tape, i|%>
  <div id="videoSection_<%= i %>" style="width:300px;height:300px;"></div>
  <div class="cost"><%=tape.cost.to_f%></div>
  <div id="replaceLink_<%= i %>" style="display:none">
    <% if current_user %>
      <%= link_to "Purchase to watch", purchase_taped_video_video_path(>
    <% else %>
      <!-- <%#= link_to "Already Member?", new_user_session_path%> | <%#= link_to "Register", new_registration_path(resource_name) %> -->
      <%= link_to "Already Member?", purchase_taped_video_video_path(> | <%= link_to "Register", "/users/sign_up" %>
    <% end %>
  <script type="text/javascript">

    flowplayer("videoSection_<%= i %>", "",
        clip: {
          url: "mp4:amazons3/vmh(bucket name on amazon)/file_path/file.mp4",
          #url: "<%=File.extname(tape.taped_video_file_name).split('.')[1]%>:amazons3/vmh<%=tape.taped_video.path%>",
          autoPlay: false,
          autoBuffering: true,
          provider: 'rtmp',
          onCuepoint: [6000, function(clip, point) {
            var plugin = this.getPlugin("content");
  $("#replaceLink_<%= i %>").html());
        plugins: {
          content: {
            url: 'flowplayer.content-3.2.9.swf',
            display: 'none'
          rtmp: {
          url: '<%= asset_path "flowplayer/flowplayer.rtmp-3.2.13.swf"%>',
          netConnectionUrl: 'rtmp://'

    var player = $f()

    player.onLoad(function()  {
        // this- variable points to the player's API
        // this.getScreen().animate({width:300, height:200});

Tuesday, 27 May 2014

Configuration Unicorn over nginx

Nginx Using Rails
Nginx is an open source reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer, HTTP cache, and a web server.

How it works
Nginx works on event driven architecture. it means that notifications or signals are used to mark the initiation or completion of a process. Thus, the resources can be used by other process until a process initiation event is triggered and resource can be allocated and released dynamically. This leads to the optimized use of memory and CPU. Nginx doesn’t create a new process for a new request.
Nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate configuration, and maintain worker processes. Worker processes do actual processing of requests. nginx has focused on high performance, high concurrency and low memory usage.

Write the following code on your local machine config/unicorn.rb
listen ""
worker_processes 4
preload_app true
user 'vagrant'
root = "/home/vagrant/app/sample_app/current"
working_directory root
pid "#{root}/tmp/pids/"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

# listen "/tmp/unicorn.sample_app.sock"
timeout 30
user: set user name of the server.
working_directory: path to the application.
listen: Url which will communicate with nginx.

Nginx Configuration:
On server machine, open `vi /etc/nginx/nginx.conf` and paste the below code.
worker_processes 1;
user vagrant;
pid /tmp/;
error_log /home/vagrant/app/sample_app/shared/log/nginx.error.log;

events {
  worker_connections 1024; # increase if you have lots of clients
  accept_mutex off; # "on" if nginx worker_processes > 1

http {
  include mime.types;
  default_type application/octet-stream;
  access_log  /home/vagrant/app/sample_app/shared/log/nginx.access.log combined;
  sendfile on;
  tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
  tcp_nodelay off; # on may be better for some Comet/long-poll stuff

  upstream app_server {

  server {
    client_max_body_size 4G;
    server_name localhost;
    keepalive_timeout 600s;
    root /home/vagrant/app/sample_app/current;
    try_files $uri/index.html $uri.html $uri @app;

    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app_server;

    # Rails error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
      root /home/vagrant/app/sample_app/current;
   `upstream` should have the IP or path that is given in unicorn `listen`.