Switch to HTTPS, Install free SSL Certificates using Let’s Encrypt

SSH, also known as Secure Socket Shell, is a network protocol that provides administrators with a secure way to access a remote computer. SSH also refers to the suite of utilities that implement the protocol. Secure Shell provides strong authentication and secure encrypted data communications between two computers connecting over an insecure network such as the Internet.

Without SSH, browser marked as “Not Secure”  on the address line in Chrome.  This is essential to have secure browsing.

If you have SSH access, use command line.

1. get cerbot

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-apache

2. Then install cerbot

$ sudo certbot --apache

3. Automating renewal for testing

sudo certbot renew --dry-run

4. Add below to crontab for auto renewal

43 6 * * * certbot renew --post-hook "systemctl reload nginx"

If you don’t have access to SSH then check control panel if there’s Let’s Encrypt option.

In my case,
1. After login plesk control panel, go to Let’s Encrypt.

Let's Encrypt

2. Then enable it.

Let's Encrypt

3. If your website is not redirecting to https automatically, go to host settings and setup redirect from HTTP to HTTPS.

Let's Encrypt

Real time user count from Google analytics API

I posted how to say hello to Google Analytics API.

Continuing the post, I would like to get Real-time user number using Google Analytics API.

1. Initialize google analytics API.

require '../composer/vendor/autoload.php';

function initializeAnalytics(){
  // Creates and returns the Analytics Reporting service object.

  // Use the developers console and download your service account
  // credentials in JSON format. Place them in this directory or
  // change the key file location if necessary.
  $KEY_FILE_LOCATION = __DIR__ . '/credentials.json';

  // Create and configure a new client object.
  $client = new Google_Client();
  $client->setApplicationName("Hello Analytics Reporting");
  $client->setAuthConfig($KEY_FILE_LOCATION);
  $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  $analytics = new Google_Service_Analytics($client);

  return $analytics;
}

$analytics = initializeAnalytics();

2. Function for Real-time user number.

function get_realtime_active_user($analytics, $ga_internal_id){
  $optParams = array(
      'dimensions' => 'rt:medium');
  try {
    $results = $analytics->data_realtime->get(
        'ga:'.$ga_internal_id,
        'rt:activeUsers',
        $optParams);
    // Success. 
    $return = $results->totalsForAllResults['rt:activeUsers'];
    return $return;
  } catch (apiServiceException $e) {
    // Handle API service exceptions.
    $error = $e->getMessage();
  }
  
}

3. Get multiple site’s data, change $ga_id_array variable according to your GA account number.
You can get GA account number just like the below.

ga account number
ga account number copy from here
$data = array();
$ga_id_array = array('site1'=>'1234567','site2'=>'12345678','site3'=>'123456789');
foreach($ga_id_array as $name => $ga_id){
  $each_data = array();
  $each_data['name'] = $name;
  $each_data['num'] = get_realtime_active_user($analytics, $ga_id);
  $data[] = $each_data;
}
echo json_encode($data);

4. All together.

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
  
require '../composer/vendor/autoload.php';

function initializeAnalytics(){
  // Creates and returns the Analytics Reporting service object.

  // Use the developers console and download your service account
  // credentials in JSON format. Place them in this directory or
  // change the key file location if necessary.
  $KEY_FILE_LOCATION = __DIR__ . '/credentials.json';

  // Create and configure a new client object.
  $client = new Google_Client();
  $client->setApplicationName("Hello Analytics Reporting");
  $client->setAuthConfig($KEY_FILE_LOCATION);
  $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  $analytics = new Google_Service_Analytics($client);

  return $analytics;
}

$analytics = initializeAnalytics();

function get_realtime_active_user($analytics, $ga_internal_id){
  $optParams = array(
      'dimensions' => 'rt:medium');
  try {
    $results = $analytics->data_realtime->get(
        'ga:'.$ga_internal_id,
        'rt:activeUsers',
        $optParams);
    // Success. 
    $return = $results->totalsForAllResults['rt:activeUsers'];
    return $return;
  } catch (apiServiceException $e) {
    // Handle API service exceptions.
    $error = $e->getMessage();
  }
  
}

$data = array();
$ga_id_array = array('site1'=>'1234567','site2'=>'12345678','site3'=>'123456789');
foreach($ga_id_array as $name => $ga_id){
  $each_data = array();
  $each_data['name'] = $name;
  $each_data['num'] = get_realtime_active_user($analytics, $ga_id);
  $data[] = $each_data;
}
echo json_encode($data);

That’s all, this will return with JSON format like this.

[{"name":"site1","num":"722"},{"name":"site2","num":"100"},{"name":"site3","num":"20"}]

 

Get started with Google analytics API

Making something new is always challenging.
Google API is well made, but documentation is always confusing me.
So let’s say “Hello” to Google Analytics. It’s good start for this kind of complicate API work.

I have checked this Hello analytics page.

1. Download composer

$ curl -sS https://getcomposer.org/installer | php

2. creat composer.json

{
  "require": {
    "google/apiclient": "^2.0"
  }
}

3. Initialize composer with google analytics. This will create vendor folder with google folder inside

$ php composer.phar install

4. Get credential JSON file from Google credential page.
Go to Credentials > Create credentials > Service account key > Click key type as JSON
Download and save it in to proper folder.

5. Enable Google Analytics API
Go to Library on Google API page. And click Analytics API under Other popular APIs

6. Open Json credential file and copy client email and Add user profile in google analytics ( admin > user management > Add permissions for:)
e.g. some-username@my-api-123456.iam.gserviceaccount.com

7. Write this code and run it from browser.

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require '../composer/vendor/autoload.php';

function initializeAnalytics(){
  // Creates and returns the Analytics Reporting service object.
  // Use the developers console and download your service account
  // credentials in JSON format. Place them in this directory or
  // change the key file location if necessary.
  $KEY_FILE_LOCATION = __DIR__ . '/credentials.json';

  // Create and configure a new client object.
  $client = new Google_Client();
  $client->setApplicationName("Hello Analytics Reporting");
  $client->setAuthConfig($KEY_FILE_LOCATION);
  $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  $analytics = new Google_Service_Analytics($client);

  return $analytics;
}

function getFirstProfileId($analytics) {
  // Get the user's first view (profile) ID.

  // Get the list of accounts for the authorized user.
  $accounts = $analytics->management_accounts->listManagementAccounts();

  if (count($accounts->getItems()) > 0) {
    $items = $accounts->getItems();
    $firstAccountId = $items[0]->getId();

    // Get the list of properties for the authorized user.
    $properties = $analytics->management_webproperties
        ->listManagementWebproperties($firstAccountId);

    if (count($properties->getItems()) > 0) {
      $items = $properties->getItems();
      $firstPropertyId = $items[0]->getId();

      // Get the list of views (profiles) for the authorized user.
      $profiles = $analytics->management_profiles
          ->listManagementProfiles($firstAccountId, $firstPropertyId);

      if (count($profiles->getItems()) > 0) {
        $items = $profiles->getItems();

        // Return the first view (profile) ID.
        return $items[0]->getId();

      } else {
        throw new Exception('No views (profiles) found for this user.');
      }
    } else {
      throw new Exception('No properties found for this user.');
    }
  } else {
    throw new Exception('No accounts found for this user.');
  }
}

function getResults($analytics, $profileId) {
  // Calls the Core Reporting API and queries for the number of sessions
  // for the last seven days.
   return $analytics->data_ga->get(
       'ga:' . $profileId,
       '7daysAgo',
       'today',
       'ga:sessions');
}

function printResults($results) {
  // Parses the response from the Core Reporting API and prints
  // the profile name and total sessions.
  if (count($results->getRows()) > 0) {

    // Get the profile name.
    $profileName = $results->getProfileInfo()->getProfileName();

    // Get the entry for the first entry in the first row.
    $rows = $results->getRows();
    $sessions = $rows[0][0];

    // Print the results.
    print "First view (profile) found: $profileName\n";
    print "Total sessions: $sessions\n";
  } else {
    print "No results found.\n";
  }
}

$analytics = initializeAnalytics();
$profile = getFirstProfileId($analytics);
$results = getResults($analytics, $profile);
printResults($results);

This looks easy but it need some effort to make it work.
Once you get Hello from analytics, next step is a lot easier.

 

Amazon S3 folder open to public with bucket policy

When upload file to S3, files are not allowed to open to public.
We need to make policy to open public.

There’s Bucket Policy on permission tab.
Add policy with Json format like following:

{
    "Version": "2012-10-17",
    "Id": "AWS-some-name-of-id",
    "Statement": [
        {
            "Sid": "AWS-some-name-of-id",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::arn-url-get-from-above/*"
        }
    ]
}

We need to keep Version date, it’s not today’s date, but it is Amazon policy version date.
Also Resource ARN code could get from above the policy editor.

Varnish command

Varnish is web application to increase web contents delivery time by cache. It can be installed in front of any servers with HTTP. Varnish is very fast, so it can speed up 300 to 1000 times faster.

Using Ubuntu, here’s varnish config file located:

vi /etc/varnish/default.vcl

After config varnish vcl file, you can test it using this command:

varnishd -C -f /etc/varnish/default.vcl

To varnish start / stop / restart:

service varnish start
service varnish stop 
service varnish restart

To view varnish status:

service varnish status

In my case, varnish stopped working with no reason.
I found out that varnish disk was full, so I had to find out which file has the most big size.

du -h / | grep '[0-9\.]\+G'

And I narrow down and found varnish log file was too big.
After delete the file and start, it worked as normally.

Happy caching!
Bravo Varnish!

Delete git repository histories

One of my site git file became too big, and took long time to clone.
So I had to clean it.

1. Checkout

git checkout --orphan latest_branch

2. Add all the files

git add -A

3. Commit the changes

git commit -am "commit message"

4. Delete the branch

git branch -D master

5. Rename the current branch to master

git branch -m master

6. Finally, force update your repository

git push -f origin master

To be a good house keeper.

Host a website on Raspberry Pi in 30 mins

I have old Raspberry Pi and I want to use it for personal server to running my task manager software.

Install raspbian on SD card

sudo dd bs=1m if=path_of_your_image.img of=/dev/diskn

And turn on Raspberry and check ip address.

ifconfig

And I get 10.50.101.62

Connect using terminal.

ssh pi@10.50.101.62

Make apt-get up to date.

apt-get update

Install apache2

sudo apt-get install apache2 -y

Install php

sudo apt-get install php5 libapache2-mod-php5 -y

Install mysql

sudo apt-get install mysql-server

Install phpmyadmin

sudo apt-get install phpmyadmin
sudo nano /etc/apache2/apache2.conf
Include /etc/phpmyadmin/apache.conf
sudo /etc/init.d/apache2 restart
Access to http://10.50.101.62/phpmyadmin/

I have downloaded task manager from mytinytodo.net and install on the server.

Boom! Here’s my website.

It took about 40 mins to install and host a website and hosting is free, isn’t it cool?

How to use Selenium webdriver with Javascript on Mac

I am managing many accounts CMS, Google account and ETC. My new project is making automated account creation system so that I can spend less time to do boring works.

You can imagine that type just name and email address in the form, it will open browser and open chrome browser and go google admin account page and click create new and adding the typed information accordingly. Then go to CMS account page and create an account in the same way. Wonderful, I can save couple minutes or even an hour DAILY!

Selenium is browser automation solution.

To install this it need some preparation

First install Xcode. Open App store and search for Xcode, download and install.

apple download xcode

Another one is Homebrew. This makes installing software a lot easier. Open terminal and type this.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install Homebrew, type this in terminal

Now, install node.

brew install node

install node using brew

Then Install chrome driver.

brew install chromedriver

install chrome driver

Make webdriver home folder and install selenium-webdriver.

mkdir webdriver
cd webdriver
npm init -y
npm install -S selenium-webdriver

Install selenium-webdriver

Note that there were error, without init npm. I got an error like ‘missing package.json warnings and errors off’. So I ran “npm init -y”. This will create package.json file.

Now everything is prepared.

Are you ready for launching new browser? Go!

Type node. And command like this order.

$ node
var webdriver = require('selenium-webdriver');

// Open Chrome Browser
var driver = new webdriver.Builder().forBrowser('chrome').build();
OR
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();

// Open google.com
driver.get('http://www.google.com');

Then browser opened and open google home page.
Launching selenium webdriver

Now create js file which includes all the commands like this. I have created open_browser.js file using below commands.
Open browser command selenium

And Run js file.

node open_browser.js

open chrome browser using selenium

Working good. I will post how to login to google account for next step.

Will this makes me lazy? Not sure!

WWW not working on domain, then add CNAME

When I purchased domain and website from whois.com, they linked  the domain to the site automatically. This is easy to use, many of hosting service provide like this way. Anyone who doesn’t know about DNS, Apache or programming can build website easily. What a good service it is!

But wait, something is missed, www is not automatically recognized. When I type www.tituschoi.com on the browser, it goes to yahoo home page! What a crap! So I decide to fix this.

Here’s how to.

  1. Go to your domain setup page. In my case, I logged in whois.com > DNS management
    whois.com dis management
  2. When opened DNS information page, click CNAME. You need to add CNAME. This is also used for adding subdomain. input Value as www. TTL is not much important, just leave it default value.
    domain CNAME setup
  3. After add CNAME, you can see it like this.
    DNS CNAME add
  4. When you ping it from terminal, it doesn’t work. But this is normal. It takes up to 3 days to be applied to DNS servers. In my case, it just took overnight.
    cname ping subdomain

Relax, time heals all wounds!