Find us on facebook

Jan 31, 2018

Retrieve nearest drivers - query- laravel mysql

$results = \DB::select(DB::raw('SELECT id, ( 3959 * acos( cos( radians(' . $order->delivery_lattitude . ') ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(' . $order->delivery_logitude . ') ) + sin( radians(' . $order->delivery_lattitude . ') ) * sin( radians(lat) ) ) ) AS distance FROM rider_location HAVING distance < ' . $distance . ' ORDER BY distance'));

Laravel permissions for setup project ubuntu

sudo chgrp -R www-data /var/www/html/project

sudo chmod -R 775 /var/www/html/project/storage

serverless setup for cloned project

  • Install node
  • Install serverless (npm install -g serverless)
  • clone the repository
  • cd to repository folder
  • npm install
  • Login to AWS console → go to IAM→ security credentials→create access key
  • serverless config credentials --provider aws --key testkeyforaws --secret secretforaws
  • serverless deploy --aws-profile devProfile(didn't use - may be useful)
  • serverless login – (create github account to access code)
  • serverless invoke -f functionname

Restrict ssh to git

 cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
/usr/bin/tmux
/usr/bin/screen
 
$ which git-shell
/usr/bin/git-shell
 
$ sudo nano /etc/shells (add  /usr/bin/git-shell)
$ sudo chsh git -s /usr/bin/git-shell

setup git server

su -
sudo useradd git
  === Enter new UNIX password:
  === Retype new UNIX password:
  === passwd: password updated successfully

 sudo apt-get install git
 
 su git
 sudo mkdir git (create git folder on home)
 move id_rsa.pub to /home/git
 mkdir ~/.ssh && chmod 700 ~/.ssh
 touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys
 
 cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
 cat ~/.ssh/authorized_keys

 mkdir project.git
 cd project.git
 git init --bare

solve access denied issue to mysql server from remote machine

netstat -l --tcp -n -p


tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      -

sudo nano /etc/mysql/mysql.conf.d and comment "bind  127.0.0.1:3306"
 
netstat -l --tcp -n -p
 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17043/apache2

On git push , publish files to live

git init --bare
cd project.git
cd hooks

cat > post-receive

#!/bin/sh
git --work-tree=/var/www/html/project --git-dir=/home/ishara/git/project.git checkout -f

CTRL+D to exit

chmod +x post-receive
 

When deploying files, you need to give permission to /var/www/html/project 
add system user group permission to /var/www/html/project using following command.

chown -R user:group projectfolder
 

In local git repo add remote
 
local .git/config
[remote "live"]
        url = ssh://ishara@111.111.11.11/home/ishara/git/project.git
        fetch = +refs/heads/*:refs/remotes/live/*
[remote "repo"]
        url = git@222.222.22.222:project.git
        fetch = +refs/heads/*:refs/remotes/repo/*

 
push command from local machine
git push live master (deploy live)
git push repo master (deploy to git server repo)
 

LDAP Simple bind in PHP

<?php
$ldaphost = "ldap.xxx.yyy"; 
$ldapport = 389; 

$ds = ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost"); 
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
if ($ds) { 
 $username = "test"; 
 $upasswd = "password"; 
 $binddn = "cn=admin,dc=xxx,dc=yyy"; 
 $binddnt = "ou=mathematicians,dc=xxx,dc=yyy"; 
 $ldapbind = ldap_bind($ds,$binddn, $upasswd); 

 //check if ldap was sucessfull 
 if ($ldapbind) {
    $filter = "(uniqueMember=*)";
    $result = ldap_search($ds, $binddnt, $filter) or exit("Unable to search LDAP server");
 $entries = ldap_get_entries($ds, $result);
 var_dump($entries);
 } else {
    echo "LDAP bind failed...";
 }
}


?>

ldap anonymous bind with PHP - example

<?php
$ldaphost = "ldap.yyy.xx";
$ldapport = 389;

$ldapconn = ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost");
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
if ($ldapconn) {
 echo "LDAP Connect success...";
 $username = "aaaa";
 $password = "aaa@aa";
 $basedn = "dc=yyy,dc=xx";
 $ldapbind = ldap_bind($ldapconn);

 //check if ldap was sucessfull
 if ($ldapbind) {
    // Search for user
    //$result = ldap_search($ldapconn, $basedn, "uid=$username");
    //$entries = ldap_get_entries($ldapconn, $result);
    //var_dump($entries);exit;
    if(($res_id = ldap_search($ldapconn, $basedn, "uid=$username"))===false){
        var_dump('LDAP Auth: User '.$username.' not found in search');
    }
    if(ldap_count_entries($ldapconn, $res_id)!==1){
        var_dump('LDAP Auth: Failure, username '.$username.'found more than once');
    }
    if(($entry_id = ldap_first_entry($ldapconn, $res_id))===false){
        var_dump('LDAP Auth: Failure, entry of search result could not be fetched');
    }
    if(($user_dn = ldap_get_dn($ldapconn, $entry_id))===false){
        var_dump('LDAP Auth: Failure, user-dn could not be fetched');
    }
    if(($link_id = ldap_bind($ldapconn, $user_dn, $password))===false){
        var_dump('LDAP Auth: Failure, username/password did not match: ' . $user_dn);
    }
    var_dump('LDAP Auth: Success '.$user_dn.' authenticated successfully');
    ldap_close($ldapconn);
   
    exit;

 } else {
    echo "LDAP bind failed...";
 }
}


?>

Git - Slack integration

  • cd to your bare repo eg:project.git and cd to hooks and create post-receive hook
  • Add bash commands to push related information to slack. (You can find many bash scripts published in the internet or you can write your own script referring following parameters in the post-receive hook.
    oldrev
    newrev
    refname
  • Give permission 
chmod +x post-receive
  • cd to your bare repo eg:project.git and execute following commands
git config -f config hooks.slack.webhook-url 'https://hooks.slack.com/services/xxx.....'

git config hooks.slack.channel '#development-xxx'

Laravel 5.6 create project

Issue : laravel/framework v5.5.17 requires ext-mbstring * - missing mbstring
Fix : sudo apt install php7.2-mbstring

Issue :  phpunit/phpunit 6.5.5 requires ext-dom *
Fix : sudo apt-get install php7.2-xml

Both php 7.0 and 5.6 on Ubuntu

  • sudo add-apt-repository ppa:ondrej/php
  • sudo apt-get update
  • sudo apt-get install php5.6 php7.0
  • sudo a2dismod php5.6
  • sudo a2enmod php7.0
  • sudo service apache2 restart