Install ssl certificate on apache server in ubuntu 16.04

Install Apache2

sudo apt-get install apache2

Enable SSL Module

sudo a2enmod ssl

Apply SSL Module to Site

sudo a2ensite default-ssl
sudo /etc/init.d/apache2 restart

Add your certificates in default-ssl.conf in /etc/apache2/sites-available

SSLEngine on
SSLCertificateFile /path/to/your_domain_name.crt
SSLCertificateKeyFile /path/to/your_private.key
SSLCertificateChainFile /path/to/DigiCertCA.crt

 

Sometimes you need to create a .ca-bundle file. Use this command to create it

Note : the names of files should be something similar

cat ComodoRSAAddTrustCA.crt ComodoRSADomain/Organization/ExtendedvalidationSecureServerCA.crt AddTrustExternalCARoot.crt > yourDomain.ca-bundle

 

Fix splash screen black/white screen issue

The solution will work on cordova version > 4.00

Install splash screen plugin

cordova plugin add cordova-plugin-splashscreen

Add these lines in config.xml

<preference name="AutoHideSplashScreen" value="false" /> 

<preference name="ShowSplashScreenSpinner" value="false" />

From config.xml remove following lines

<preference name="SplashScreenDelay" value="2000"/>
 <preference name="FadeSplashScreenDuration" value="2000"/>

 

In app.js add following lines

angular.module('starter.controllers', []) 

.run(function($ionicPlatform) { 

$ionicPlatform.ready(function() {

 setTimeout(function() { 

navigator.splashscreen.hide(); 

}, 100); 

}); 

})

Install nginx and php7 on ubuntu 16.04

First of all get all updates.

sudo apt-get update

Install nginx server

sudo apt-get install nginx

Now open http://server_domain_or_IP You should see the default page of nginx.

The default directory is /var/www/html/

Now lets install php for processing

sudo apt-get install php-fpm php-mysql

Uncomment the follwoing lines in /etc/nginx/sites-available/default

 location ~ \.php$ {

                include snippets/fastcgi-php.conf;

        #

        #       # With php7.0-cgi alone:

        #       fastcgi_pass 127.0.0.1:9000;

        #       # With php7.0-fpm:

                fastcgi_pass unix:/run/php/php7.0-fpm.sock;

        }

Replace

index index.html index.htm index.nginx-debian.html;

with

index index.php index.html index.htm index.nginx-debian.html;

Restart nginx

service nginx restart

Now your nginx server is ready to execute php script.

Note : If you want to execute sql query through php script then use mysli_query() instead of mysql_auery().

First Application With Ionic Framework

In previous post, I had explained how you can get started with ionic framework. Now in this post I will walk you through the code and will explain the architecture of project.

In this post we will be making a simple calculator.

Make sure you have installed ionic.

In terminal run following command to check if ionic is installed or not.

ionic -v

If installed, it will return the version of ionic. If not installed then follow this article to install it.

Now run this command to create a blank application.

ionic start calculator blank

Your terminal should look like this
Screen Shot 2016-06-29 at 2.27.49 pm
Now navigate to calculator directory

cd calculator

Now run this command

ionic serve

It will start the node server (http://localhost:8100/)

Screen Shot 2016-06-29 at 2.33.45 pm

I am using safari browser and its responsive design mode.

I will strongly recommend webstorm editor. You can get it free if you have your college id.

Here is how the project structure looks like

Screen Shot 2016-06-29 at 2.40.51 pm

Open config.xml

Screen Shot 2016-06-29 at 2.43.13 pm

For us, this is the most important file for app configuration. You can change the name,id and other settings of the app in this file.

For now forget about other folders (I will explain later on) and open www folder. Now inside www, we have 4 folders and 1 index.html file.

  1. css – this folder has style.css, we will put all our css code here.
  2. img – folder will contain all the images we will be using in the app.
  3. js – this folder contain app.js (will explain later)
  4. lib – this folder has ionic libraries.
  5. index.html (this is the main file. When we open the app, this html file gets loaded first and then it loads all the dependencies)

Lets go through the code of index.html

Screen Shot 2016-06-29 at 3.58.26 pm

You can see index.html contains all the dependent css, and js files.

 

Lets change the title of header. Replace  Ionic Blank Starter with Calculator.

<ion-header-bar class=”bar-stable”>
<h1 class=”title”>Calculator</h1>
</ion-header-bar>

Above code is for header section

Below is the code of main content. <ion-content> section contains the code of main content.

<ion-content>
<h5 style=”text-align: center;”>A simple calculator which can perform addition, subtraction, multiplication, and division.</h5>
</ion-content>

This is how your app should look now.

Screen Shot 2016-06-29 at 4.06.31 pm

Now lets create the UI of calculator.

Replace your index.html code with following

https://github.com/neelkamal0666/Calculator/blob/master/www/index.html

In css/style.css, add css codes from here

https://github.com/neelkamal0666/Calculator/blob/master/www/css/style.css

Now you app should look like this

Screen Shot 2016-06-29 at 5.01.08 pm

Your calculator is ready now.

How to create apk file and check it in your android phone.

In terminal navigate to your project folder then run following commands

$ ionic platform add android

$ ionic build android

 

If you get permission issue (in mac/ubuntu), then add sudo before these commands.

Now check for apk file in

/calculator/platforms/android/build/outputs/apk/android-debug.apk

 

You can download the entire source code used in this tutorial from git hub repo.
https://github.com/neelkamal0666/Calculator

Getting started with ionic framework

Install Ionic

First, install Node.js. Then, install the latest Cordova and Ionic command-line tools. Follow the Android and iOS platform guides to install required platform dependencies.

Note: iOS development requires Mac OS X. iOS simulator through the Ionic CLI requires the ios-sim npm package, which can be installed with the command sudo npm -g install ios-sim.

After installing node.js run this command in your terminal (command prompt) to install ionic

$ npm install -g cordova ionic

Start a project

Ionic provide ready made templates to create basic app.

There are two types of templates available

  1. Tabs

Screen Shot 2016-06-29 at 1.54.49 pm

2. Side Menu

Screen Shot 2016-06-29 at 1.58.05 pm

To create app with default tabs run this command

$ ionic start myApp tabs

Here myApp is the name of the app.

To create app with side menu  run this command

$ ionic start myApp sidemenu

If you don’t want to use their default template then you can also create an empty app by using following command

$ ionic start myApp blank

Testing you app in browser

Navigate to your project folder

cd myApp

ionic serve

ionic serve will start the node server

Testing you app in emulator or device

$ cd myApp
$ ionic platform add android
$ ionic build android
$ ionic emulate android

Crawling

function curl_file_get_contents($url)
{
$curl = curl_init();
$userAgent = ‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)’;
try {
curl_setopt($curl,CURLOPT_URL,$url); //The URL to fetch. This can also be set when initializing a session with curl_init().
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5); //The number of seconds to wait while trying to connect.

curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //The contents of the “User-Agent: ” header to be used in a HTTP request.
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE); //To fail silently if the HTTP code returned is greater than or equal to 400.
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); //To follow any “Location: ” header that the server sends as part of the HTTP header.
curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect.
curl_setopt($curl, CURLOPT_TIMEOUT, 10); //The maximum number of seconds to allow cURL functions to execute.

$contents = curl_exec($curl);
curl_close($curl);
} catch(Exception $ex){
echo $ex->getMessage();
}
return $contents;
}

function getFlipkartProductTitle($content){
$reg_exUrl = “/<h1 class=\”title\” itemprop=\”name\”>.+?<\/h1>/is”;
preg_match_all($reg_exUrl, $content, $matches);
$usedPatterns = array();
foreach($matches[0] as $pattern){
if(!array_key_exists($pattern, $usedPatterns)){
$usedPatterns[$pattern]=true;
echo $pattern;
}
}
}
function getFlipkartProductSubtitle($content){
$reg_exUrl = “/<span class=\”subtitle\”>.+?<\/span>/is”;
preg_match_all($reg_exUrl, $content, $matches);
$usedPatterns = array();
foreach($matches[0] as $pattern){
if(!array_key_exists($pattern, $usedPatterns)){
$usedPatterns[$pattern]=true;
echo $pattern;
}
}
}
function getFlipkartProductPrice($content){
$reg_exUrl = “/<span class=\”selling-price.+?\”>.+?<\/span>/is”;
preg_match_all($reg_exUrl, $content, $matches);
$usedPatterns = array();
echo $matches[0][0];
}

function getSnapDealProductTitle($content){
$reg_exUrl = “/<h1 itemprop=\”name\”>.+?<\/h1>/is”;
preg_match_all($reg_exUrl, $content, $matches);
$usedPatterns = array();
foreach($matches[0] as $pattern){
if(!array_key_exists($pattern, $usedPatterns)){
$usedPatterns[$pattern]=true;
echo $pattern;
}
}
}
//$content = curl_file_get_contents(“http://www.flipkart.com/dimpy-stuff-bear-cuddles-16-53-inch/p/itme3ws6ueuhmzwe?pid=STFE3WS6Q8PYZZDF&#8221;);
//getFlipkartProductTitle($content);
//getFlipkartProductSubtitle($content);
//echo ‘<br />’;
//getFlipkartProductPrice($content);
//echo ‘<br />SnapDeal’;
function getSnapDealContent($url){
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HEADER, true);
curl_setopt($handle, CURLOPT_USERAGENT, ‘Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1’);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($handle);

return $result;

curl_close($handle);
}
$content1 = getSnapDealContent(“http://www.snapdeal.com/product/trident-hot-pink-cotton-towel/1759907333&#8221;);
getSnapDealProductTitle($content1);

Installing Apache,PHP,MySql,phpmyadmin on ubuntu server

Simply run these commands from terminal

Update the Package

sudo apt-get update

 

Install Apache2

sudo apt-get install apache2

 

Install PHP5

sudo apt-get install php5

sudo apt-get install libapache2-mod-php5

 

Install MySQL

sudo apt-get install mysql-server 

lib apache2-mod-auth-mysql php5-mysql

 

Install PhpMyAdmin

sudo apt-get install phpmyadmin

You just installed phpmyadmin, but you also need to tell apache where to find phpmyadmin. phpmyadmin already comes with an apache configuration and we just need to use it. This configuration is available in: /etc/phpmyadmin/apache.conf

Add the following line to the end of /etc/apache2/apache2.conf:

Include /etc/phpmyadmin/apache.conf

Restart Apache2

sudo /etc/init.d/apache2 restart
or execute service apache2 restart

Now your web services is ready. You can upload your files into /var/www/ directory.

 

To install php5 library for mysql run this command

sudo apt-get install php5-mysql

Create REST API Using Slim PHP Framework

I prefer slim framework for creating REST APIs because of two main reasons :

  1. It’s very lightweight and focused on REST and nothing else.
  2. It supports all the HTTP methods (GET, POST, PUT, DELETE).

I will create an API which will accept the JSON Data and store it into a file. Using GET Request you get these data in json format.

First you download the slim framework from here http://www.slimframework.com/install . Unzip the file and put it inside your server. Replace the contents of index.php .

<?php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim(array(
"MODE" => "development"
));
$app->post('/test', 'test');
$app->get('/test', 'get_data');
function test() {
 $request = file_get_contents('php://input');    //get the body of the http request
// $data_arr =json_decode($request,true);  //convert the json into array, you can manipulate the data the you you want
//store the json in a file
 $handle = fopen('data/test_data.json', 'w+');  // you must create a data folder in the same folder of index.php
 fwrite($handle, $request); 
echo "Success";
}
function get_data(){
$data=file_get_contents("data/test_data.json");  //read the json data from the file
echo $data; // return the data
}
?>

Now your api is  ready. You can test it using any rest client.
You can explore slim framework Documentation to create more complicated api.

You can also test it using curl -
curl -i -X POST -d '{"blog" : "codegem"}' localhost/test

Develop REST API Using Sinatra (A Ruby Framework)

This REST API accepts JSON  Data and stores it in a file. You can retrieve these data using GET Request.

require 'rubygems'
require 'sinatra'
require 'sinatra/json'
require 'json'
require 'sinatra/cross_origin'
configure do
 enable :cross_origin
end
set :environment, :production

post '/test' do
 cross_origin :allow_origin => '*',
 :allow_methods => [:post],
 :allow_credentials => true,
 :max_age => "60"
 data = JSON.parse(request.body.read)
 aFile = File.new("data/data.json", "w+")
if aFile
 aFile.syswrite(data)
end
 json data
end

get '/test' do
 cross_origin :allow_origin => '*',
 :allow_methods => [:get],
 :allow_credentials => true,
 :max_age => "60"
 aFile = File.new("data/data.json", "r+")
if aFile
 content = aFile.sysread(100)
end
 json content
end

You can test this api using REST Clients (Browser extensions).

You can also test it using curl -
 curl -i -X POST -d '{"blog" : "codegem"}' localhost:4567/test