Categories
Career Code Area Tips and Tricks Tools and Technologies

Install Elasticsearch in Ubuntu 16.04

Elasticsearch is a distributed, real-time, full-text search engine. Elasticsearch developer by JAVA (based on Lucene). It has an HTTP web interface and schema-free JSON documents. In this article, I will describe how to install and then configure elasticsearch in ubuntu 16.04. Let’s see

Before you install elasticsearch, please ensure java is installed on your machine. If not then install java first before doing anything.

Install Elasticsearch in Ubuntu 16.04
Install Elasticsearch in Ubuntu 16.04

Installing Java 8

 First Add the Java PPA to apt package database.

sudo add-apt-repository -y ppa:webupd8team/java

Then run update commands to update your apt package database.

sudo apt-get update
Then install Java 8 and verify it is installed, run following command.

sudo apt-get -y install oracle-java8-installer
java -version
After successfully install Java, now you can install  Elasticsearch on your machine.

Installing and Configuring Elasticsearch

You can download elasticsearch from their official website elastic.co. It would be always good to download deb (Debian) version for the ubuntu, cause it has everything integrated to successfully run elasticsearch. You can also download it through the command line:

sudo wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.deb
Then install this as usual ubuntu way with the dpkg command:

sudo dpkg -i elasticsearch-1.7.2.deb

After execution this command your installation will be completed, now you have to configure elasticsearch. By default all configuration files of elasticsearch stored into the /etc/elasticsearch directory. Here you can get two files elasticsearch.yml and logging.yml. To configure the elasticsearch you have to open elasticsearch.yml file in your text editor.

sudo nano /etc/elasticsearch/elasticsearch.yml

Now find the line node.name and cluster.name in this file and uncomment it (remove # before them) and change the value of these two.

Find the line, network.bind_host uncomment it by removing the # character at the beginning of the line, and change the value to 0.0.0.0 so it looks like this:

network.bind_host: 0.0.0.0

After all done, elasticsearch will be installed on your computer, now just run this command to start elasticsearch.

sudo service elasticsearch start

For testing elasticsearch output. just type http://localhost:9200/ in your browser and see the possible response.

{
  "status" : 200,
  "name" : "sohel-rana-pc",
  "cluster_name" : "sohel-rana-elasticsearch",
  "version" : {
    "number" : "1.7.2",
    "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
    "build_timestamp" : "2015-09-14T09:49:53Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}
Categories
CakePHP Career Code Area PHP Tips and Tricks Tools and Technologies

Internationalization & Localization Application with CakePHP 3

CakePHP has one of the coolest features to make application Internationalization & Localization. It’s really easy to translate your application or website into multiple languages with CakePHP 3.  Let’s see it.

First of all, create a directory Locale inside PROJECT_ROOT/src

Create a subdirectory of each language (which language you want to translate your application) inside PROJECT_ROOT/src/Locale/. Subdirectory name can be anything (ex. english, french, etc), but prefer ISO code of the language (ex. en_US, fr_FR, bn_BN etc). So follow this tutorial and make your subdirectory name en_USfr_FR, and bn_BN

Create default.po file inside all subdirectories (en_USfr_FR, and bn_BN). And copy the following code in the respective file.

src/Locale/en_US/default.po

msgid "message"
msgstr "Hello World"

 

src/Locale/fr_FR/default.po

msgid "message"
msgstr "Bonjour le monde"

 

src/Locale/bn_BN/default.po

msgid "message"
msgstr "ওহে বিশ্ব"

Here msgid key of which will use inside the template (view files) and msgstr is the values which will gonna translate.

So now you have three different languages (en_USfr_FR, and bn_BN). and one key (message) which can be translated into these three languages.

Now use <?php echo __(‘message’); ?> in your view (any ctp file of your application).

Then open PROJECT_ROOT/config/app.php file and find

 'defaultLocale' => env('APP_DEFAULT_LOCALE', 'fr_FR')

and replace with your language.

But if you want to translate language in runtime then you can use it

use Cake\I18n\I18n;

I18n::locale('fr_FR');

I hope this article will help you to translate your application in multiple language.

Categories
Career Code Area Google Cloud Tips and Tricks

Host Static Website in Google App Engine

This article is for hosting your static website into Google App Engine. This article provides an app.yaml file so you can host your static website into Google App Engine. Before do anything make your website’s folder structure like that (see below image). So put all the static files inside assets folder (css, js, images, fonts) and all HTML files in project’s root directory. Inside assets folder css, js, img, and fonts are sperate folders for holding separate files (ex. css file stylesheets, img for images)

Host Static Website in Google App Engine

Then copy-paste this snipped in your YAML file. And then deploy (gcloud app deploy) your application.

You can also make your project structure your own way, then you have to change YAML content based on your changes. So that means need to change YAML information according to your project’s structure.

Categories
Career Nginx Ubuntu

Create NGINX Virtual Host with PHP 7.0

Here I have shared a script for creating NGINX virtual host with PHP 7.0. NGINX virtual host with  5.x and 7.0 is very similar but only different is php5-fpm.sock location. To create a NGINX virtual host open the configuration files by running this command.

sudo gedit /etc/nginx/sites-available/default

And then copy-pastes this code snippet and modify it by your configuration (information). Then restart your server. And you are ready to go.

See it for creating virtual host with PHP 5.x

Categories
CakePHP PHP Tips and Tricks Tools and Technologies

CakePHP 3 – DebugKit Toolbar Not Visible

Are you looking for the solution of DebugKit Toolbar not visible in CakePHP 3? These small tips will help you do that. Ensure you have pdo-sqlite installed & enabled on your machine. Cause debugKit by default uses a sqlite db. If not then follow this instruction.

At first install pdo-sqlite in your machine.

sudo apt-get install php7.0-sqlite

Then restart your server (apache server)

sudo service apache2 restart

Now that will help you to visible the DebugKit Toolbar

 

Categories
Career Code Area PHP

Change an Array Value if Duplicate

I have an array which holds the multiple same values in same categories. And I want to If the data is duplicate and the category is ‘Sales’ I want 1 of them to change to ‘Service’ and vice versa.

From above snippet, you can see ‘Lane Assist’ is duplicate in ‘Sales‘ category and ‘Bandenafdichtset’ is duplicate in ‘Service‘ category and I changed them as vice versa.

Categories
CakePHP Code Area PHP

Configure Email Transporter in CakePHP Runtime

If you are looking for a solution to configure email transporter from CakePHP controller, components in run-time. This script will help you lot. This script is working for CakePHP 3.1.X – 3.3.9.

Email::configTransport('transporter_name', [
            'className' => 'Smtp',
            'host' => 'SMTP_HOST',
            'port' => 'SMTP_PORT',
            'timeout' => 30,
            'username' => 'SMTP_USERNAME',
            'password' => 'SMTP_PASSWORD',
        ]);
        
        $email = new Email();
        $email->transport('transporter_name');
        
        $email->from('no-replay@example.com')
            ->to('to@example.com')
            ->subject('Dummy Subject')
            ->message('This is a test message')
            ->send();

Simply copy paste this snippet and modified with real information and test it.

Categories
AngularJS Career Code Area Java Script Tips and Tricks

Getting Query String Values Without a Hash in the URL

$location.search() returns empty? Angular does not remove the query before the hash bang in URL. According to AngularJS documentation,  hash bang URL format is:

http://example.com/#!/bar?baz=23

So if you have no hash bang in your URL (e.g. http://example.com/bar?baz=23), and try to get query string value, then you can use this snippet.

function getQueryParams(queryParam, url) 
{
        var pageUrl = url || window.location.search.substring(1);
        var urlVariables = pageUrl.split(/[&||?]/);
        var response = null;

        for (var i = 0; i < urlVariables.length; i += 1) {
            var param = urlVariables[i];
            var paramName = (param || '').split('=');

            if (paramName[0] === queryParam) {
                response = paramName[1];
            }
        }
        return response;
}
    
console.log(getQueryParams('baz', 'http://example.com/bar?baz=23'));

This code is also with pure JavaScript (withour AngularJS).

Categories
Career Tips and Tricks

Change Bower Default Directory And Folder

Bower is the client-side dependencies manager. The developer can easily manage their client-side dependencies with the Bower. By default when you install any package (e.g jQyery, Bootstrap etc) through Bower, then a bower_components folder will create in the project’s root directory and store all the files inside it. So the default directory of bower installation is ROOT and folder is bower_components
But if you want to change the Bower default directory and folder then you have to follow these following steps.

Remove bower_components (if exist) from your project root directory

 

Create .bowerrc file in your project’s root directory

 

Paste these code in .bowerrc file and modify DIRECTORY_NAME & FOLDER_NAME

{
  "directory": "DIRECTORY_NAME/FOLDER_NAME"
}

Then finally run bower install command.

Hope these will helpful to configure your Bower directory and folder name.

Categories
Career Code Area PHP

Avoid Too Much Complexity to the Code, Instead of Returning Often and Early.

Code readability is really important part of programming. The readable and cleaner code is easily manageable and customizable. The programmers don’t take it seriously but it’s important.  Actually, there is always an ongoing debate, but returning often, and returning early will make your code much cleaner looking and readable.

public function saveUser($data = [], $sendEmail)
    {
        // Checking data is exiest or not
        if ($data) {
            $isSaved = $this->UserModel->save($data);

            // Checking if the data is successfully stored or not.
            if ($isSaved) {
                //  If stored then doing some other work

                // send the email to the user
                if ($sendEmail) {
                    // send the email
                }

            } else {
                // Otherwise return false
                return false;
            }
        } else {
            // Otherwise return false
            return false;
        }
    }

If you look over the above code snippet, then you can see there is some nested if-else. And code quickly becomes unreadable and complex. You have to have a deep look to understand this sort of code.

public function saveUser($data = [], $sendEmail)
    {
        if(!$data)
        {
            // If data not exiest then return.
            return false;
        }

        $isSaved = $this->UserModel->save($data);
        if (!$isSaved) {
            // If data not saved then return.
            return false;
        }

        if (!$sendEmail) {
            // send the email
        }
        
        // If everything good, then return true.
        return true;
    }

Now see this code snippet, this code is much easier to read and very cleaner look. Both snippets are doing same work but the second one is much more readable and clearer than first one. And this can be easily manageable & customizable. so that avoid too much complexity to the code, instead of returning often and early.