Find us on facebook

May 31, 2015

Install Node.js on Ubuntu AND Linux

Ubuntu

sudo apt-get update

Then use apt-get to install Node.js build dependencies:
sudo apt-get install -y build-essential openssl libssl-dev pkg-config

Move to /usr/src directory - the usual place to hold software sources.
cd /usr/src

pick the latest compressed source archive from Node.js website at http://nodejs.org/download/.
sudo wget http://nodejs.org/dist/v0.12.4/node-v0.12.4.tar.gz

Uncompress the source files and move into that directory.
sudo tar zxf node-v0.12.4.tar.gz
cd node-v0.12.4

Prepare compiler commands, by executing the configure script:
sudo ./configure

It will read the properties of the system to prepare compiler flags. Ie. it could be a system architecture (32/64bit, CPU specific flags etc). With it, Now ready to actually compile the source now. To do that, just type:
sudo make

To make this available system-wide:
sudo make install

Linux

Update software repository to the latest versions:
sudo yum -y update

Install "Development Tools". It's a group of tools for compiling software from sources.
sudo yum -y groupinstall "Development Tools"

Move to /usr/src directory - the usual place to hold software sources.
cd /usr/src

pick the latest compressed source archive from Node.js website at http://nodejs.org/download/.
sudo wget http://nodejs.org/dist/v0.12.4/node-v0.12.4.tar.gz

Uncompress the source files and move into that directory.
sudo tar zxf node-v0.12.4.tar.gz
cd node-v0.12.4

Prepare compiler commands, by executing the configure script:
sudo ./configure

It will read the properties of the system to prepare compiler flags. Ie. it could be a system architecture (32/64bit, CPU specific flags etc). With it, Now ready to actually compile the source now. To do that, just type:
sudo make

To make this available system-wide:
sudo make install

Connecting to EC2 Instance with Filezilla

Go to Edit->Settings

Go to SFTP and Add key file
Click OK. An go to File ->Site manager

Give Host. Select protocol as SFTP. If Ubuntu then user ubuntu. If linux then user ec2-user.

Then click connect.

Done! :)

Install and start the LAMP web server on Amazon Linux And Ubuntu

Linux

Install updates
[ec2-user ~]$ sudo yum update -y

you can install the Apache web server, MySQL, and PHP software packages. Use the yum install command to install multiple software packages and all related dependencies at the same time.
[ec2-user ~]$ sudo yum install -y httpd24 php56 mysql55-server php56-mysqlnd

Start Apache
[ec2-user ~]$ sudo service httpd start

Start apache at each system boot
[ec2-user ~]$ sudo chkconfig httpd on

Check httpd is on
[ec2-user ~]$ chkconfig --list httpd

Modify ownership and permissions for document root

Add a www group to your instance, and you give that group ownership of the /var/www directory and add write permissions for the group. Any members of that group will then be able to add, delete, and modify files for the web server.

[ec2-user ~]$ sudo groupadd www

[ec2-user ~]$ sudo usermod -a -G www ec2-user (Add user to www group)

[ec2-user ~]$ exit (logout)

[ec2-user ~]$ groups (Verify membership in www group)

[ec2-user ~]$ sudo chown -R root:www /var/www (Change the group ownership of /var/www and its contents to the www group.)

Change the directory permissions of /var/www and its subdirectories to add group write permissions and to set the group ID on future subdirectories.
[ec2-user ~]$ sudo chmod 2775 /var/www
[ec2-user ~]$ find /var/www -type d -exec sudo chmod 2775 {} +

Recursively change the file permissions of /var/www and its subdirectories to add group write permissions.
[ec2-user ~]$ find /var/www -type f -exec sudo chmod 0664 {} +

[ec2-user ~]$ sudo service mysqld start (Start mysql server)

[ec2-user ~]$ sudo mysql_secure_installation (Run mysql_secure_installation, here u can set password for mysql etc...)

[ec2-user ~]$ sudo chkconfig mysqld on (Start mysql at every boot)


Ubuntu

Install updates
$ sudo apt-get update

Install the LAMP stack:
$ sudo apt-get install lamp-server^

During installation this asks for mysql root password. Type it and press enter button. Then retype password will be asked. Type and hit enter.


Start Apache server
$ sudo service apache2 start

Go to your Domain name for your instance. You will see Apache2 Ubuntu default page.

Modify ownership and permissions for document root (Same steps should apply as linux)

That's all.

Connect to your EC2 instance with Putty.

Open puttygen (PuTTy Key Gen)

Go to conversions->Import key->Select the key pair  .pem file you downloaded while launching instance.


Click on Save private key. It will save a .ppk file

Then open pageant click on Add key and select saved .ppk file It will ask for passphrase. Enter it and click OK.


Then close it.


Then open Putty 

Add your instance's EIP, Port 22,


Go To Data. For Ubuntu username is ubuntu. For Linux username is ec2-user


Go to auth under SSH and browse your .ppk file for key pair


Finally Go to session and click Save. And Click Open to connect to the ubuntu instance with SSH.



That's All. Now you have connected to your instance with SSH.





Amazon Web Services EC2 Set Up for Ubuntu Server

Go to AWS Console, Select EC2, On EC2 Dashboard click on Launch Instance


You will get following screen


Choose AMI(Amazon Machine Image)

Here I'm selecting Ubuntu. After click on select button you will get following screen.



Then click on Right bottom Next Configuration... Button. You will get following screen.
Click on Next: Add Storage Button. You will get following screen.


Click on Next: Tag Instance Button. You will get following


Click on Configure security group button

Then click on review and launch button. You will see all the details regarding your instance. Then click on launch button.

It will ask you to create or select key pair.

After giving key pair name, Download your key pair clicking on Download key pair button. It will download bainstance002.pem file.
Save it somewhere you can access it again in future. Then click on launch instance button.

In instance tab you will see your new instance is being created.

Then you can create EIP (An Elastic IP address (EIP) is a static IP address designed for dynamic cloud computing. With an EIP, you can mask the failure of an instance or software by rapidly remapping the address to another instance in your account. Your EIP is associated with your AWS account, not a particular instance, and it remains associated with your account until you choose to explicitly release it.)

Click on Allocate new address to create new EIP. Then Click on Associate Address to associate it with your instance.

Then click on Associate button. That's All. Now your instance is ready to use.



May 30, 2015

How To Set Up a Node.js Application for Production on CentOS

"Node.js is a platform built on Chrome's JavaScript runtime(V8 JavaScript Engine.V8 is Google's open source JavaScript engine.) for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices." - nodejs.org

After installing node.js as mentioned in early post, create file app.js

http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(2000,'88.208.221.72');
console.log('Server running at port 2000');

and save and exit.

Then run the following command

node app.js

And you can see the output through http://yourip:2000

Running a Node.js application this wayr will block additional commands until the application is killed by pressing CTRL+C.

Then install PM2, which is a process manager for Node.js applications. PM2 provides an easy way to manage and daemonize applications (run them as a service).

Use Node Packaged Modules (NPM), which is basically a package manager for Node modules that installs with Node.js, to install PM2. Use following command to install PM2:

npm install pm2 -g


Then use pm2 start command to run the application, app.js, in the background:
pm2 start app.js

This adds application to PM2's process list, which is outputted every time start an application:


PM2 assigns an App name (based on the filename) and a PM2 id. PM2 also maintains other information, such as the PID of the process, its current status, and memory usage.

Applications that are running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot).

The startup subcommand generates and configures a startup script to launch PM2 and its managed processes on server boots. Specify the platform you are running on
pm2 startup centos

To find your os version following command can be used
cat /etc/redhat-release




May 29, 2015

Backups directory - Plesk 12/11

Plesk 12
# Backups directory
/var/lib/psa/dumps

Exit from vim

In vim there are 3 different modes:

Insert - allows typing and editing as normal
Visual - used for selecting copy/paste etc.
Normal - used for commands
To get back to Normal mode, you can always press esc.

Once at Normal mode Press : to begin your command (you'll see it appear in the bottom left). The following commands are related to quiting vim:

:q - quit if no changes were made
:q! - quit and destroy any changes made
:wq - write changes (save) and quit
:x - similar to :wq, only write the file if changes were made, then quit

Create App with node.js


After installed Node.js, "npm" command will be available, this is short for the Node Package Manager, this allows to keep track of any dependencies that the app uses. To allow to easily setup a lightweight web application, install Express using the "npm" command. 

npm install express --save

This command will instruct NPM to install express onto the machine and also save a reference to it into a file called package.json. package.json should include all the dependencies required to run the app wherever it may be, on whatever machine using the npm install command. Another package require is "ejs", a templating engine that will help to bring together HTML and Node.js.

npm install ejs --save



Install node.js on CentOS 6

Update software repository to the latest versions:
yum -y update

Install "Development Tools". It's a group of tools for compiling software from sources.
yum -y groupinstall "Development Tools"



Move to /usr/src directory - the usual place to hold software sources.
cd /usr/src

pick the latest compressed source archive from Node.js website at http://nodejs.org/download/.
wget http://nodejs.org/dist/v0.12.4/node-v0.12.4.tar.gz



Uncompress the source files and move into that directory.
tar zxf node-v0.12.4.tar.gz
cd node-v0.12.4

Prepare compiler commands, by executing the configure script:
./configure


It will read the properties of the system to prepare compiler flags. Ie. it could be a system architecture (32/64bit, CPU specific flags etc). With it, Now ready to actually compile the source now. To do that, just type:
make


To make this available system-wide:
make install



Display file contents in putty

displays only last 50 lines of file
tail -n 50 filename

shortcut/command to exit/close the file when viewing in putty
Ctrl+C

Delete Files/Folders

To delete all files/folders in the current directory, without deleting the directory itself
rm -rf *

To delete a whole folder and its content recursively
rm -rf foldername/

To remove all files starting with ‘myFile’ and ending in ‘.txt’
rm myFile*.txt

To remove files
rm myFile.txt myFile1.txt myFile2.txt …etc…

filemng failed: filemng: opendir failed: Permission denied System error 13: Permission denied in plesk 12

Use following command with superuser privileges

/usr/local/psa/bin/repair --restore-vhosts-permissions


After running this command, FTP access also available.

May 27, 2015

Simple Login Form - Using web services - Android/PHP




Step 01 : AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.firstapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 02 : activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:hint="@string/Email"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="67dp"
        android:ems="10"
        android:hint="@string/Password"
        android:inputType="textPassword" />

      <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="22dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/progressBar1"
        android:layout_marginLeft="22dp"
        android:text="@string/Login" />

</RelativeLayout>

Step 03 : MainActivity.java

package com.example.firstapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;


public class MainActivity extends Activity {
    EditText password,email;
    Button login;
    ProgressBar progressBar;
   
   
       
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        password=(EditText) findViewById(R.id.editText2);
        email=(EditText) findViewById(R.id.editText1);
        login=(Button) findViewById(R.id.button1);
               
        progressBar=(ProgressBar) findViewById(R.id.progressBar1);
        progressBar.setVisibility(View.GONE);
       
        login.setOnClickListener(new OnClickListener() {
       
            public void onClick(View v) {
                progressBar.setVisibility(View.VISIBLE);
                new ExecuteTask().execute(email.getText().toString(),password.getText().toString());
               
            }
        });
       

    }
   
     class ExecuteTask extends AsyncTask<String, Integer, String>
        {

            @Override
            protected String doInBackground(String... params) {
               
                String res=PostData(params);
               
                return res;
            }
           
            @Override
            protected void onPostExecute(String result) {
            progressBar.setVisibility(View.GONE);
           }
           
        }
   
    public String PostData(String[] valuse) {
        String s="";
        try
        {
        HttpClient httpClient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost("http://batransfer.cf/mobile/member-login");
       
        JSONObject loginDetails = new JSONObject();
       try {
        loginDetails.put("email", valuse[0]);
        loginDetails.put("password", valuse[1]);
         

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        StringEntity se = new StringEntity(loginDetails.toString());
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httpPost.setEntity(se);
        HttpResponse httpResponse=  httpClient.execute(httpPost);
   
        //HttpEntity httpEntity=httpResponse.getEntity();
        result= readResponse(httpResponse);
        }
        catch(Exception exception)  {}
        return result;
   
       
    }
    public String readResponse(HttpResponse res) {
        InputStream is=null;
        String return_text="";
        try {
            is=res.getEntity().getContent();
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is));
            String line="";
            StringBuffer sb=new StringBuffer();
            while ((line=bufferedReader.readLine())!=null)
            {
            sb.append(line);
            }
            return_text=sb.toString();
        } catch (Exception e)
        {
           
        }
        return return_text;
       
    }
   

}  

JSONObject Vs JSONArray

We can use JSONArray to parse JSON which starts with the array brackets. A collection of related items can be included in JSONArray. (Number of JSON objects can be included within JSONArray).
EG:
 [
       {"label":"HA0-Brent","id":"1085","table":"plot_dis"},
       {"label":"HA1-Harrow","id":"1086","table":"plot_dis"},
       {"label":"HA2-Harrow","id":"1087","table":"plot_dis"},
       {"label":"HA3-Harrow","id":"1088","table":"plot_dis"},
       {"label":"HA4-Hillingdon","id":"1089","table":"plot_dis"}
  ]

We can  use JSONObject when dealing with JSON that begins with curly braces. A JSON object contains key/value pairs related to item.
EG: {"email":"testbawebservice@gmail.com","password":"testba123"}

JSON arrays and objects can be nested in one another.
EG:{
"code":"0",
"message":"success",
 "locations":[
       {"label":"HA0-Brent","id":"1085","table":"plot_dis"},
       {"label":"HA1-Harrow","id":"1086","table":"plot_dis"},
       {"label":"HA2-Harrow","id":"1087","table":"plot_dis"},
       {"label":"HA3-Harrow","id":"1088","table":"plot_dis"},
       {"label":"HA4-Hillingdon","id":"1089","table":"plot_dis"}
  ]}

Using web services with Android App - Provide Internet permission

First,to use web services with android,

We need to provide INTERNET permission in AndroidManifest.xml file.

 <uses-permission android:name="android.permission.INTERNET" />


Android R.java File

aapt (Android Asset Packaging Tool) auto-generates R.java file. This file contains resource IDs for all the resources of res/ directory.

activity_main.xml file contains many components. For these each specific component, id is automatically created in this file. This id can be used in the activity source file to perform any action on the component.

If R.jar file is deleted, automatically it will be created by android.

strings.xml

In Eclipse the strings.xml file is used to declare strings instead of hard-coding them into other project files.

Place your strings in strings.xml file like shown in this example, and reference those strings in your other project files.

FirstApp/res/layout/activity_main.xml

 <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="67dp"
        android:ems="10"
        android:hint="@string/Password"
        android:inputType="textPassword" />

FirstApp/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="Password">Password</string>
</resources>

Failed to install FirstApp.apk on device emulator-5554

When this error appears with the red font, don't close anything - leave it there and then press the run button again. 

May 26, 2015

Fix Error Code 13 For Eclipse



Open eclipse.ini file

-vm
C:/Program Files/Java/jre6/bin/javaw.exe (Path to javaw.exe file)

Put above part before -vmargs

May 13, 2015

Commiting and Pushing in Git

git add <path/filename> OR git add *
git commit -m "Worldpay changed view"
git push origin master


Install Git in CentOS server

yum install gettext-devel expat-devel curl-devel zlib-devel openssl-devel
cd /usr/local/src
wget https://www.kernel.org/pub/software/scm/git/git-1.8.2.3.tar.gz
tar xzvf git-1.8.2.3.tar.gz
cd git-1.8.2.3
make prefix=/usr/local all
make prefix=/usr/local install


May 2, 2015

No User logged in//Current user authenticated?/Get current user ID

use Illuminate\Support\Facades\Auth;

Auth::guest()

Auth::check()

\Auth::user()->name;

Move all model classes under models folder -laravel 5

If you moved User model to app/models folder then you have to change app/auth.php to
'model' => 'App\Models\User', instead  'model' => 'App\User',