Find us on facebook

Jun 30, 2015

Setup Chef Server


View Ubuntu version details - cat /etc/lsb-release

chef-solo -v
Install chef - curl -L https://www.opscode.com/chef/install.sh | sudo bash

check version - chef-solo -v

make configuration file 
cd /etc/
sudo mkdir chef
In etc/chef create file solo.rb
sudo vi solo.rb

repo = '/etc/chef/src'

cookbook_path repo + '/cookbooks'
data_bag_path repo + '/data_bags'
role_path repo + '/roles'

log_level :info
log_location STDOUT


Press esc to go to normal mode and :x or :wq to save and exit
sudo mkdir src
go to src
cd src
cat ../solo.rb


sudo mkdir cookbooks data_bags roles
cookbooks - contain bunch of recipes
within cookbooks create directory called common and go inside it
create recipes directory and create sudo vi default.rb
directory '/root' do
 user 'root'
 group 'root'
 mode '0700'
end
Save and exit


Create run list to run the default recipes with command - sudo chef-solo -o 'recipe[common]'
change the mode - sudo chmod 0770 /root
Again run the default recipes with command - sudo chef-solo -o 'recipe[common]'


Jun 29, 2015

Setting up a cron in AWS Linux/Ubuntu


  1. Login to server using SSH (Windows users PuTTY)
  2. sudo crontab -l
  3. sudo crontab -e
  4. Linux : Open vim editor and allow to enter commands
  5. Ubuntu : For the first time it asks which editor to use (nano is easy)
  6. Enter following commands and save the file

MAILTO=yyy@gmail.com
*/5 * * * * /usr/bin/wget http://yyy.com/mail201506/index.php >/dev/null

>/dev/null
Send all the normal output to a place called "/dev/null" which is basically like a black hole. It accepts anything you dump there, but you will never see it again.

Jun 28, 2015

CentOS6.6 MySql Login with admin password


  1. cat /etc/psa/.psa.shadow
  2. Display Eg : "$AES-128-CBC$33yZTimLYQFax/iFgiQGkQ==$gqGlImLkTalp26cDeS3C9g=="
  3. mysql -u admin -p
  4. When say Enter Password: Just enter above displayed password. And hit Enter.

Plesk 12 Login error after changing MySql Password -Resolved


  1. old-passwords = 1 should be disabled in the /etc/my.cnf file.
  2. Obtain the correct Plesk password: cat /etc/psa/.psa.shadow ($AES-128-CBC$33yZTimLYQFax/iFgiQGkQ==$gqGlImLkTalp26cDeS3C9g==)
  3. sudo /etc/init.d/mysqld stop
  4. sudo mysqld_safe --skip-grant-tables &
  5. mysql -u admin
  6. update mysql.user set password=PASSWORD("$AES-128-CBC$33yZTimLYQFax/iFgiQGkQ==$gqGlImLkTalp26cDeS3C9g==") where User='admin';
  7. flush privileges;
  8. sudo /etc/init.d/mysqld stop
  9. sudo /etc/init.d/mysqld start
Update the password for Plesk using the ch_admin_passwd utility:
  1. /usr/local/psa/bin/admin --show-password
  2. export PSA_PASSWORD=Abe3s3
  3. /usr/local/psa/admin/bin/ch_admin_passwd

Jun 21, 2015

server API calls with AngularJS - yii2 (Part 2)

assets/AppAsset.php


web/js/services.js


web/js/controllers.js


Angular JS with Yii2

assets/AppAsset.php


assets/AngularAsset.php


web/js/app.js


web/js/controllers.js

view file


Jun 17, 2015

Send emails using SMTP Yii 2.0

In your controller
Yii::$app->mail->compose()->setFrom('yyy@gmail.com')->setTo('sss@gmail.com')->setSubject('Email sent from Yii2-Swiftmailer')->send();

In common/config/main-local.php
 In components array
 'mail' => [
        'class' => 'yii\swiftmailer\Mailer',
        'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.gmail.com',  // e.g. smtp.mandrillapp.com or smtp.gmail.com
        'username' => 'ttt@gmail.com',
        'password' => 'ggg',
        'port' => '587', // Port 25 is a very common port too
        'encryption' => 'tls', // It is often used, check your provider or mail server specs
        ],
        ],

Jun 16, 2015

Selecting data using JDatabase

Select query
Method 1 : 
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('fname', 'lname')));
$query->from($db->quoteName('#__students'));
$db->setQuery($query);
$row = $db->loadAssocList();

Method 2 : 
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('fname', 'lname')));
$query->from($db->quoteName('#__students'));
$db->setQuery($query);
$row = $db->loadRowList();
return $row[0][0];

Access individual values:

Method 1 
echo  $row[1]['lname'];

Method 2 : 
echo  $row[1][1];


Creating a table on module install

Step 1 : mod_students.php

<?php
/**
 * Students Name Module Entry Point
 *
 * @package    Joomla.Tutorials
 * @subpackage Modules
 * @link http://docs.joomla.org/J2.5:Creating_a_simple_module/Developing_a_Basic_Module
 * @license        GNU/GPL, see LICENSE.php
 * mod_students is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */

// No direct access
defined('_JEXEC' ) or die;

// Include the syndicate functions only once
require_once dirname(__FILE__) . '/helper.php';

$studentName = modStudentsHelper::getStudentNames($params);
require(JModuleHelper::getLayoutPath('mod_students'));
?>

Step 2 : helper.php

<?php
/**
 * Helper class for Students module
 *
 * @package    Joomla.Tutorials
 * @subpackage Modules
 * @link docs.joomla.org/J2.5:Creating_a_simple_module/Developing_a_Basic_Module
 * @license        GNU/GPL, see LICENSE.php
 * mod_students is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */
class ModStudentsHelper
{
    /**
     * Retrieves the hello message
     *
     * @param array $params An object containing the module parameters
     * @access public
     */  
    public static function getStudentNames( $params )
    {
        // Obtain a database connection
$db = JFactory::getDbo();
// Retrieve the shout
$query = $db->getQuery(true)
->select($db->quoteName('fname'))
->from($db->quoteName('#__students'))
->where('lang = ' . $db->Quote('en-GB'));
// Prepare the query
$db->setQuery($query);
// Load the row.
$result = $db->loadResult();
// Return the Hello
return $result;
    }
}
?>

Step 3 : mod_students.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="2.5.0" client="site" method="upgrade">
    <name>Student Names</name>
    <author>Ishara</author>
    <version>1.0.0</version>
    <description>Display Student Names module.</description>
    <files>
        <filename>mod_students.xml</filename>
        <filename module="mod_students">mod_students.php</filename>
        <filename>index.html</filename>
        <filename>helper.php</filename>
        <filename>tmpl/default.php</filename>
        <filename>tmpl/index.html</filename>
<filename>sql/install.mysql.utf8.sql</filename>
<filename>sql/uninstall.mysql.utf8.sql</filename>
    </files>
<install>
<sql>
<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
</sql>
</install>
<uninstall>
<sql>
<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
</sql>
</uninstall>

<config>
    </config>
</extension>

Step 4: sql/install.mysql.utf8.sql

CREATE TABLE IF NOT EXISTS `#__students` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`fname` varchar(25) NOT NULL,
`lname` varchar(25) NOT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

INSERT INTO `#__students` (`fname`, `lname`) VALUES ('Derick', 'Cristo');
INSERT INTO `#__students` (`fname`, `lname`) VALUES ('Ruchi', 'Doe');
INSERT INTO `#__students` (`fname`, `lname`) VALUES ('Brunila', 'Cristine');

Step 5: sql/uninstall.mysql.utf8.sql

DROP TABLE IF EXISTS `#__students`

Step 6 : tmpl/default.php

<?php
// No direct access
defined('_JEXEC') or die; ?>
<?php echo $studentName; ?>

Create simple module with Joomla 2.5

Step 1: Create folder and name it as mod_helloworld

Step 2: Create folder tmpl under mod_helloworld

Step 3: Create mod_helloworld.php under  mod_helloworld

<?php
/**
 * Hello World! Module Entry Point
 *
 * @package    Joomla.Tutorials
 * @subpackage Modules
 * @link http://docs.joomla.org/J2.5:Creating_a_simple_module/Developing_a_Basic_Module
 * @license        GNU/GPL, see LICENSE.php
 * mod_helloworld is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */

// No direct access
defined('_JEXEC' ) or die;

// Include the syndicate functions only once
require_once dirname(__FILE__) . '/helper.php';

$hello = modHelloWorldHelper::getHello($params);
require(JModuleHelper::getLayoutPath('mod_helloworld'));
?>

Step 4: Create helper.php under  mod_helloworld
<?php
/**
 * Helper class for Hello World! module
 *
 * @package    Joomla.Tutorials
 * @subpackage Modules
 * @link docs.joomla.org/J2.5:Creating_a_simple_module/Developing_a_Basic_Module
 * @license        GNU/GPL, see LICENSE.php
 * mod_helloworld is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */
class ModHelloWorldHelper
{
    /**
     * Retrieves the hello message
     *
     * @param array $params An object containing the module parameters
     * @access public
     */  
    public static function getHello( $params )
    {
        return 'Hello, World!';
    }
}
?>

Step 5: Create mod_helloworld.xml under  mod_helloworld
<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="2.5.0" client="site" method="upgrade">
    <name>Hello, World!</name>
    <author>John Doe</author>
    <version>1.0.0</version>
    <description>A simple Hello, World! module.</description>
    <files>
        <filename>mod_helloworld.xml</filename>
        <filename module="mod_helloworld">mod_helloworld.php</filename>
        <filename>index.html</filename>
        <filename>helper.php</filename>
        <filename>tmpl/default.php</filename>
        <filename>tmpl/index.html</filename>
    </files>
    <config>
    </config>
</extension>

Step 6: Create default.php under  mod_helloworld/tmpl
<?php
// No direct access
defined('_JEXEC') or die; ?>
<?php echo $hello; ?>

Step 7: Put  mod_helloworld under joomla/tmp (To install the module)

Step 8 : Go to Extensions->Extension manager

Step 8 : Go to Extensions->Module Manager
Configure module position


Jun 13, 2015

Enable Pretty URL - Yii2 Advanced template

Step 1 : edit common/config/main.php as below
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
Step 2 : Add following .htaccess file into backend/web folder

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Step 3 :Add following .htaccess file into frontend/web folder

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Step 4: Add following to apache\apache2.4.9\conf\extra\httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "D:\Projects\new\yii-application"
ServerName yii2_test.ml
<Directory "D:\Projects\new\yii-application">
        AllowOverride All
    </Directory>
</VirtualHost>

Step 5: Enable mod_rewrite on apache\apache2.4.9\conf\httpd.conf


Configuring RBAC Manager Yii2-Advanced Template

Step 1 : edit common/config/main.php And console/config/main.php  as below
'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],


Step 2: use the migration stored in @yii/rbac/migrations:

[yii migrate --migrationPath=@yii/rbac/migrations]

Step 3: Install yii2-admin via composer [composer.phar should be within yii-application folder]

1) php composer.phar require mdmsoft/yii2-admin "~2.0" OR
2) add "mdmsoft/yii2-admin": "~2.0" to the require section of your composer.json file[and run composer.phar update].


 Step 4: edit common/config/main.php as below

'modules' => [
        'admin' => [
            'class' => 'mdm\admin\Module',
        ]
],
'as access' => [
        'class' => 'mdm\admin\components\AccessControl',
        'allowActions' => [
            'admin/*', // add or remove allowed actions to this list
        ]
    ],

Now you can access admin panel for creating/editing Roles/Permissions , Assign users to roles etc...
http://localhost/path/to/index.php?r=admin
http://localhost/path/to/index.php?r=admin/route
http://localhost/path/to/index.php?r=admin/permission
http://localhost/path/to/index.php?r=admin/menu
http://localhost/path/to/index.php?r=admin/role
http://localhost/path/to/index.php?r=admin/assignment

Install Yii2 Advanced Application Template via Composer

Step 1 : Create a folder under your web root (Let's say yii2)
Step 2 : Copy the composer.phar file into to yii2
Step 3 : Go to that folder in cmd
Step 4 : Type in cmd [php composer.phar global require "fxp/composer-asset-plugin:1.0.0"]

Step 5 : Type in cmd [php composer.phar create-project --prefer-dist yiisoft/yii2-app-advanced yii-application]
Step 6 : In middle of the process this would ask for a token.

(To get a token, login to your github profile and generate a token. Steps are as below.
Step A : In the top right corner of any page, click your profile photo, then click Settings.

Step B : Personal access tokensIn the user settings sidebar, click Personal access tokens.


Step C : Generate new token buttonClick Generate new token.


Step D : Token description field Give your token a descriptive name.
Step E:  Selecting token scopesSelect the scopes you wish to grant to this token. The default scopes allow you to interact with public and private repositories, user data.
Step F: Generate token buttonClick Generate token.


[Newly created tokenCopy the token to your clipboard. For security reasons, after you navigate off this page, no one will be able to see the token again.]
)
Step 6 :  Copy and paste this token into cmd and hit enter.

Step 7 :  It says token stored successfully. And install all the packages.


Step 8 : Move to the folder that is project created and Run in cmd [php init]

Step 9 : Create a new database in mysql and adjust the components['db'] configuration in common/config/main-local.php accordingly.


Step 10 : Apply migrations with console command yii migrate.

Step 11 : Open yii2-advanced web on browser





Jun 8, 2015

you should configure authmanager option error (When using yii migrate --migrationPath=@yii/rbac/migrations)

yii2-base template: change config/console.php configuration file where the authManager needs to be declared. And change the config/web.php too.
'authManager' => [
    'class' => 'yii\rbac\DbManager',
],

yii2-advanced template: Change console/config/main.php And common/config/main.php

Then run yii migrate --migrationPath=@yii/rbac/migrations command

Jun 6, 2015

Yii2 - Basic - Create new project

php composer.phar global require "fxp/composer-asset-plugin:1.0.0"


php composer.phar create-project --prefer-dist yiisoft/yii2-app-basic D:/wamp/www/angular/skwirk


Jun 2, 2015

Change MYSQL Root Password

Do not confuse server root user with the MySQL root user.

Server's main user is server root. The MySQL root(admin) user has complete control over MySQL only.
This two 'root' users are not connected.

Stop MySQL

Ubuntu or Debian:
sudo /etc/init.d/mysql stop

For CentOS, Fedora, and RHEL:
sudo /etc/init.d/mysqld stop

Safe mode
Next we need to start MySQL in safe mode - start MySQL but skip the user privileges table.

sudo mysqld_safe --skip-grant-tables &
The ampersand (&) at the end of the command is required.

Login
Log into MySQL and set the password.

mysql -u root
when we started MySQL we skipped the user privileges table.So no password needed.

Next, instruct MySQL which database to use:

use mysql;(Where all details kept)(in mysql db there is user table. All users stored there with passwords.
We are going to change That user's table's root user's password. If there is no user called root in this table,
We need to get main user from this table. once my main user was admin. So instead "root" I had to use "admin")

Reset Password

Enter the new password for the root user as follows:
update user set password=PASSWORD("mynewpassword") where User='root'; //where User='admin'

Flush the privileges:
flush privileges;

Restart
Now the password has been reset, we need to restart MySQL by logging out:
quit

Stop and Star MySQL.

Ubuntu and Debian:
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start

CentOS and Fedora and RHEL:
sudo /etc/init.d/mysqld stop
sudo /etc/init.d/mysqld start

Login
Again login and test password:
mysql -u root -p // mysql -u admin -p

Get latest from Git Master to other branch

git fetch origin
git checkout master
git merge origin/master

To switch to your branch, just do
git checkout branch1

Merge a git branch into master

git checkout master
git pull origin master
git merge test(branch name)
git push origin master

Adding an existing project to GitHub

Initialize the local directory as a Git repository.
git init

Add the files in your new local repository. This stages them for the first commit.
git add .

Commit the files that you've staged in your local repository.
git commit -m 'First commit'

In the Command prompt, add the URL for the remote repository where your local repository will be pushed.
git remote add origin https://ishara@bitbucket.org/ishara/weatherapp.git
# Sets the new remote
git remote -v
# Verifies the new remote URL

GitHub for Windows users should use the command git remote set-url origin instead of git remote add origin here.

git push origin master

Install Git on Ubuntu

sudo apt-get update
sudo apt-get install build-essential libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip
wget https://github.com/git/git/archive/v1.9.2.zip -O git.zip (You can get latest version from git)
unzip git.zip
cd git-*
make prefix=/usr/local all
sudo make prefix=/usr/local install

if you want to upgrade to a later version, you can simply clone the repository and then build and install:
git clone https://github.com/git/git.git

Following commands will rebuild and reinstall the package
make prefix=/usr/local all
sudo make prefix=/usr/local install

git config --global user.name "Your Name"
git config --global user.email "youremail@domain.com"

Jun 1, 2015

Set Up MySQL Master-Master Replication

Server A - 54.154.46.139 (Ubuntu)
Server B - 52.17.26.158 (Linux)

Server A Config
sudo apt-get install mysql-server mysql-client

sudo nano /etc/mysql/my.cnf

Change following lines
#server-id              = 1
#log_bin                = /var/log/mysql/mysql-bin.log
#binlog_do_db           = include_database_name
bind-address            = 127.0.0.1

To
server-id               = 1
log_bin                 = /var/log/mysql/mysql-bin.log
binlog_do_db            = example1
# bind-address            = 127.0.0.1

To save Ctrl+O Then press Enter.
To exit use Ctrl+X

sudo service mysql restart

mysql -u root -p

Create a pseudo-user that will be used for replicating data between our two VPS. Assume that you name this user "replicator". And password is abc123
create user 'replicator'@'%' identified by 'abc123';

Give this user permissions to replicate our mysql data:
grant replication slave on *.* to 'replicator'@'%';

Permissions for replication cannot, be given on a per-database basis. Our user will only replicate the database(s) that we instruct it to in our config file.

show master status;


Server B Config

sudo apt-get install mysql-server mysql-client

sudo nano /etc/my.cnf

server-id = 2
binlog-do-db=example1
relay-log = /var/lib/mysql/mysql-relay-bin
relay-log-index = /var/lib/mysql/mysql-relay-bin.index
log-error = /var/lib/mysql/mysql.err
master-info-file = /var/lib/mysql/mysql-master.info
relay-log-info-file = /var/lib/mysql/mysql-relay-log.info
log-bin = /var/lib/mysql/mysql-bin

To save Ctrl+O Then press Enter.
To exit use Ctrl+X

sudo /etc/init.d/mysqld restart

mysql -u root -p

Create a pseudo-user . Assume that you name this user "replicator". And password is abc123
create user 'replicator'@'%' identified by 'abc123';

Give newly created 'replication' user permissions to replicate it.

grant replication slave on *.* to 'replicator'@'%';

Use information that we took a note of earlier and applying it to our mysql instance. This will allow replication to begin. The following should be typed at the mysql shell:

slave stop;
CHANGE MASTER TO MASTER_HOST = '54.154.46.139', MASTER_USER = 'replicator', MASTER_PASSWORD = 'abc123', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 346;
slave start;

Now Get master status of this server
SHOW MASTER STATUS;


Go back to server A

mysql -u root -p

slave stop;
CHANGE MASTER TO MASTER_HOST = '52.17.26.158', MASTER_USER = 'replicator', MASTER_PASSWORD = 'abc123', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 437;
slave start;

That's all

Testing (Both servers should have example1db)

Create table on Server A
create table example.abc(`id` varchar(10));

Go to server B and check
show tables in example1;

DROP TABLE abc;

Go back to Server A and check
No tables will display