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
Advertisements

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

Getting Started With Sinatra (Ruby Framework) on Windows

Sinatra is a free and open source software web application library and domain-specific language written in Ruby. It is an alternative to other Ruby web application frameworks such as Ruby on Rails, Merb, Nitro and Camping.
Getting Started With Sinatra (For Development)
1. Install latest version of Ruby : You can download the windows installer from http://rubyinstaller.org/.
2. Set path : C:\(ruby install directory)\bin;
3. Go to command promt. Type this command to install Sinatra : gem install Sinatra (if path is not set then go to C:\Ruby193\bin and then run the command from cmd).
4. Install Sinatra_contrib : gem install sinatra-contrib (after installing this you need not to restart the server with every change in your file)

Now you are ready to build your first app with Sinatra.
Step 1: create your project directory “myproject” anywhere in your computer.
Step 2: create a new file test.rb and add these lines

 require 'rubygems'
 require 'sinatra'
get '/firstapp' do
 "Welcome to my first Sinatra App !"
 End

Step 3 : In command Prompt, traverse to you project directory and run this command
ruby test.rb
If your path is not set then you will have to run it from C:\Ruby193\bin directory.
Step 4 : Now go to http://localhost:4567/firstapp

You will see the webpage with message “Welcome to my first Sinatra App !”

So you have Created your First App In Sinatra

Client side IP geolocation using JavaScript

<h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3>
<hr/>
<div id="ip"></div>
<div id="address"></div>
<hr/>Full response: <pre id="details"></pre>

<script type="text/javascript">
$.get("http://ipinfo.io", function (response) {
 $("#ip").html("IP: " + response.ip);
 $("#address").html("Location: " + response.city + ", " + response.region);
 $("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");
</script>

OS Detection in JavaScript

You can easily get the operating System of your visitor in javascript. Here is the javascript code which will give you the operating system of your visitor.

<script type="text/javascript">
var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
alert(OSName);
</script>

Getting Visitor’s Country in JavaScript

In some of the applications we may need to find the visitor’s location like City, Country,etc. Thereb is a very easy to use service provided by ws.geonames.org . The GeoNames geographical database covers all countries and contains over eight million placenames that are available for download free of charge.

Here is a Jquery Code which will give you the visitor’s Country and Country Code. You will need the updated version of jquery as well.

<script type="text/javascript">
if (navigator.geolocation) {
 navigator.geolocation.getCurrentPosition(function(position) {
 $.getJSON('http://ws.geonames.org/countryCode', {
 lat: position.coords.latitude,
 lng: position.coords.longitude,
 type: 'JSON'
 }, function(result) {
 alert('Country: ' + result.countryName + '\n' + 'Code: ' + result.countryCode);
 });
 });
 }
</script>