Sharing files and folders with Linux guest OS in VMWare player

One day, when preparing a virtual machine to study Hadoop, I have to find the way to share a files and folder between my Windows 8 machine with the Linux guest OS, more specific, I use LUbuntu as my guest OS. After a quick Googling and read the documentation, I was successful. Today, I want to share this experience with you.

Install the VMWare Tool

First of all, you have to install the VMWare Tool for the guest OS to enable the shared folder in this OS. Click in Player menu > Manage, > Install VMWare Tools…. You may have do download the tool before the installation start.

Start install the vmware tools
Start install the vmware tools

 

 

 

After the compressed installation file has been mounted to the guest OS, copy it to another writable location e.g. the Desktop folder and decompress it. Now, open the Terminal and change the working directory to the new created folder, usually named vmware-tools-distrib.

Change the working directory to the new installation folder
Change the working directory to the new installation folder

 

 

 

Execute the vmware-install.pl with super user privilege and accept all default value. It will automatically execute the vmware-config-tools.pl at the end of the installation. If your Linux does not have gcc and make, you have to install these package and re-run the wmware-config-tool.pl again by simply enter sudo vmware-config-tools.pl.

Config the Shared Folder in VMWare player.

Now, we have to VMWare tools in the guest Linux OS. We should open the virtual machine settings by click Player menu > Manage > Virtual Machine Settings. In the new dialog, choose the Options tab and enable the Shared Folder option.

Enable Share Folder in the virtual machine
Enable Share Folder in the virtual machine

 

 

 

Click the Add… button at the bottom, follow the wizard to choose a folder in the host OS and the name of shared folder in the guest OS. In the Linux guest OS, our shared folder will be mounted in the /mnt/hgfs/. You can make a soft link (shortcut) to the Desktop to used this folder more easily when working in the Linux OS by entering this command in the terminal:

$> ln -s /mnt/hgfs/shared-name ~/Desktop/shared-name

Now you can access the shared files and folder from you Desktop folder. The modifications in this shared folder is two-way affected by default. If you do not want the guest users write or change this shared folder content, in the previous wizard click the read-only option, you can change this option any time in the virtual machine settings screen, too.

Last, my demonstration of sharing folder with Linux guest OS

Now, you have read all my words. I also upload a video to my Youtube chanel, you can see it to get more specific detail

Setup a small local web server for your office with WampServer 2

WampServer is a useful and convenient tool to setup a web development environment on Windows. It’s so easy to use WampServer for a private purpose – the way that you only access your projects via localhost. However, if you have no experience, you will find it difficult to use WampServer for your local office. What below is how I setup the server for our web applications in my company and allow just my colleagues access the server, hope that it will help you in some way.

A first general look

First, let take a look at the general steps to know what you will do:

  • Give your server a static IP
  • Determine which computers can access your server

Step 1: A static IP for your server

In this post, I’ll setup a local web server, which means that there will be no internet connection from the server. If you want to setup a real web server which allows people connect from internet, consider looking at another article.

First, you have to know the Physical Address (or MAC Address) of the server. Open the Connection properties of the current network connection of the server, press details button and look for the Physical address line in the new opened window, you will find the Physical Address of the server as a string with 8 pair of characters separated by 5 hyphens. Copy or note this string in order to use it later.

Now, keep the Network Connection Details windows open and look at the line named as DHCP Enabled. If its value is No, write the IP of the server (which can be found at the line IPv4 Address) to another location to use in the next steps. Otherwise, you have to setup your router to get a static IP for the server.

To set the static IP for the server, open the browser and navigate to the address of the router (it may be 192.169.1.1 or 192.168.0.1). If you don’t know this, ask your network administrator to do this step. After logging in the router configuration page, find the page allow you to bind an IP to a client (computer) by the MAC address. Use the previous saved MAC address (Physical address) of the server to bind an static local IP. This step may vary in different routers.

In the end of this step, you have to know the IP address of the server. Review what I wrote before if you don’t.

Step 2: Allow computers allowed

In this step, you have to be sure all client computer can access to server. Open the Command Prompt and enter ipconfig /all command. If the result look like the follow figure, it is fine. Otherwise, consider another setting for your network.

C:\Users\TrungHieu>ping 192.168.0.101

Pinging 192.168.0.101 with 32 bytes of data:
Reply from 192.168.0.101: bytes=32 time<1ms TTL=64
Reply from 192.168.0.101: bytes=32 time<1ms TTL=64
Reply from 192.168.0.101: bytes=32 time<1ms TTL=64
Reply from 192.168.0.101: bytes=32 time<1ms TTL=64

Ping statistics for 192.168.0.101:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
The result of a successful “ping”

With WampServer running, click to its icon in the system tray, point to Apache, click httpd.conf to edit the httpd.conf file in your text editor. Find a line as # onlineoffline tag – don’t remove and be sure that the next lines are the same as below to allow access only from localhost:

#   onlineoffline tag - don't remove
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
    Allow from ::1
httpd.conf config to allow access only from localhost

To allow connections from more computer, insert Allow from <computer IP address> for each computer (with <computer IP address> is its address) after the Allow from ::1 line. You can also use IP address with subnet mask here for shorting your configuration.In some cases, you just want to allow your colleagues to access some web sites, not the whole server, do not insert more Allow statements. Go to the end of the httpd.conf file and insert this code:

<Directory "absolute/path/to/the/website/folder">

</Directory>
Insert Allow from <computer IP address> lines between the and tag.

Final step

Everything will work after you restart your Apache service by clicking to the Wamp system tray icon, point to Apache, Services and click Restart services. You can consider allowing Wamp to auto start and go online with the Windows or setup more secure method with the Basic Authentication. However, you will have a simple web server for your office with just above steps.

See my video to more specific

Solve the “Denied access” problem when using Wamp server 2 on Windows 8

After installing the Windows 8 Consumer Preview (I’ll post a article about its UX soon), I cannot access my localhost as well as all of my local web folders. For more specific, I met these error notice

Access denied on localhost when using Wamp server 2 on Windows 8
Access denied on localhost when using Wamp server 2 on Windows 8

Fortunately, I finally found the way to solve this problem (although I still don’t know why it happens now).

Quick fix on Apache httpd.conf and alias configuration files

First, open the httpd.conf file of your apache version by a text editor such as Notepad or Notepad++. It’s usually located at \wamp\bin\apache\<your apache version>\conf\httpd.conf or you can use the quick menu of Wamp manager to open it. Find these snippet before modify it:

# First, we configure the "default" to be a very restrictive set of 
# features.  
#
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>

Replace the Deny from all with Allow from all and save the file.We have not finished yet. Find another snippet:

#   onlineoffline tag - don't remove
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1

Replace Deny from all with Allow from all again. The localhost runs correctly now.After do all above thing, I believe that you can locate to localhost and your local web project with your browser. However, if you navigate to https://localhost/phpmyadmin, you will continue getting the Forbidden error. The reason is you did not modify the alias configuration file to allow access from your localhost. Let open this file by using Wamp manager or finding it in \wamp\alias\phpmyadmin.conf, find this snippet

<Directory "c:/wamp/apps/phpmyadmin3.4.10.1/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
</Directory>

Change Allow from 127.0.0.1 to Allow from ::1 or you can do as above, change Deny from all into Allow from all. Now, that’s all.

See how I did it with a video

You can see this video on youtube to see how I solved this problems more clearly.

Introduce to the version control system

Version control (aka Revision control or Source control) is the management of changes in computer files. It tracks your files over times and help you easily get back your work to a previous version. This is extremely important with a team, when many people can edit the same file.

You used it, but you don’t know …

It’s so interesting to know that you usually use the version control in your work. Perhaps there are some files named with numbers like these in your computer:

  • report-1.doc
  • report-2.doc
  • my_current_stuff_20110912.psd
  • my_current_stuff_20111011.psd

Why these file exist and have such names? You add a number or a date string to the end of the file name because you want to have a backup of your current work. If you go wrong in the future and want to have the latest good version, you can get it easily (may be not quickly). As you can see, version control plays an important roll in your work though you are not aware of it.

A version control system, huh, why do we need it?

A good version control system (VCS) will allow you to do the following stuff easily:

  • Backup and restore: files are saved when you need. You can jump to a previous version any time. You can throw many recent change and roll back to the last good version in the database. You can all so get the code from last year and what have changed when getting a bug.
  • Synchronization: all people can have a up-to-date version easily.
  • Track changes and ownership: the VCS can store the information about changes in you files and who the person who made it, which make you easily to see how a file is evolving over time.
  • Sandboxing: as its name, you can freely make and changes in you code and test it in your own computer before “checking in” what you have done.
  • Branching and merging: you can made your own copy of the code, save it in a separate location and develop it in isolation. Later, you can merge your work back into the common area.

The common words you have to know

There are many implementation of a VCS, but all of them should involve these concepts:

The basic:

  • Repository: the database storing files
  • Server: the computer has the repository
  • Client: the computer connect to the server to use the repository. The server and the client may be the same computer.
  • Trunk/Main: the primary location for your code. It is considered as the main branch of your work.

Basic actions:

  • Add: put a file (and/or a directory) into the repository in the first time.
  • Revision: the version of a file. It can be a number-liked string as 1, 2, 3, … or 1.2, 1.3, …
  • Head: the latest revision in the repository.
  • Checkout: download files from the repo to your local computer. Some VCS allow you edit the files when they were checked out, others require a explicit option when checking out, otherwise they only give you a read only version.
  • Check in: upload files to the repository. If the file exists and have changes, it will have a greater revision number. You can leave a message when checking in.
  • Changelog/History: a list of changes to a file from the time it’s added to the repository.
  • Update/Sync: ynchronize you files to the latest version (head version) of the repository.
  • Revert: throw away your local changes and roll back to the latest verion from the repo.

Advanced actions:

  • Branch: create a separate copy of your work. It is also a noun refers to the working copy which is branched.
  • Diff/Change/Delta: find the differences between two files.
  • Merge/Patch: apply changes from a file to another, for example, aplly what you did in you branch to the common work.
  • Conflict: when there are some changes which cannot be applied in a files in the same time, you know that there is a conflict.
  • Locking: make some files not allow other people to edit it. This is usually used to prevent conflict. You can unlock it later. Some person can unlock the files on purpose when needed, which is called breaking the lock.

Thanks to Kalid Azad at https://betterexplained.com for this section.

Back to the real world, which tools you can use

After reading all above, you can wonder how to use a VCS, implement your own? Feel free to try, but there are many free tool can help you. SVN (subversion https://subversion.tigris.org) and GIT (https://git-scm.com) are two of them. If you are working in Linux or some console system, just get these software to your computer and you the console! For windows users, TurtoiseSVN and TurtoiseGIT (found at https://tortoisesvn.tigris.org and https://code.google.com/p/tortoisegit/) can be a great choice.

Setup development evironment for SCALA 2.9 and Netbeans 7.0 in linux (Ubuntu 11.04)

Install scala

  • Go to https://www.scala-lang.org/downloads and get the tgz package for Unix, MacOS X, Cygwin. The current latest stable release is scala 2.9.1
  • Suppose that the downloaded package is in your home directory, open the terminal and type these lines:
    $> tar -xvf scala-2.9.1.final.tgz
    $> sudo mv scala-2.9.1.final/ /usr/share/scala/

    Remember to replace the 2.9.1 by your scala version

  • The next thing to do is register the SCALA_HOME and PATH environment variable. To do this, open the ~/.profile file and add these new lines to the end of the file
    export SCALA_HOME=/usr/share/scala
    export PATH=$PATH:$SCALA_HOME/bin
  • Log out and the log in again. Test you installation by typing this command in the terminal. If the terminal change to scala> then you have successfully installed scala.
    $> scala

Install the Netbeans plugin

I use the nbscala plugin to develop scala projects. To use this plugin in Netbeans 6, you can use the Netbeans plugin management in Tools > Plugins. For Netbeans 7.0, you can do as below:

The first scala application

Now, you can create your first scala project in Netbeans. Just create a new project and keep the default settings, you will have a Hellow world scala application. Click Run. If the the string Hello world is printed in the output then your Netbeans is ready.

How to use Codeigniter pagination

The pagination and the CodeIgniter framework

When you want to present a long list of data, the pagination becomes a useful way to give your users the best interactivity. This makes pagination supported as a class in CodeIgniter framework. Unfortunately, despite of what developers said on the user guide, using this class is not easily to understand for many people. In this post, I will introduce the basic of how to use CodeIgniter pagination class via an example. You can see my demo at the demo page

Our data source

Create a table in your database as below:

CREATE TABLE 'demo_page' (
'id' INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
'value_code' CHAR( 6 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
'quantity' INT NOT NULL
)

Our table has three field: id – the id of each record; value_code – the identifying string of each item, in this case, it contains six capital Latin letters, and quantity – the number of each item in the storehouse.

The Model

In this demo, our Model involves two method: getting the total number of record in the data source and getting an certain of number of records based on passed argument. Create a file called demo_page.php in your ./application/models/ folder and make its content as these line:

<?php
class Demo_page extends CI_Model {
    function __construct() {
        parent::__construct();
        $this->load->library('database');
    }

    function count_items(){
        return $this->db->count_all('demo_page');
    }

    function get_items($limit, $offset){
        $data = array();
        $this->db->limit($limit, $offset);
        $Q = $this->db->get('demo_page');
        if($Q->num_rows() > 0){
            foreach ($Q->result_array() as $row){
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }
}

The Controller

What we do in the Controller is loading libraries and models, configuring the pagination class, getting data from database and calling the view. In your ./application/controllers/ folder, create a new php file as below and name it demo.php

<?php
class demo extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        // return to previous page
        redirect(base_url());
    }

    function ci_pagination() {
        $this->load->library('pagination');
        $this->load->library('table');
        $this->load->model('demo/Demo_page');

        $result_per_page = 5;  // the number of result per page

        $config['base_url'] = base_url() . '/demo/ci_pagination/';
        $config['total_rows'] = $this->Demo_page->count_items();
        $config['per_page'] = $result_per_page;
        $this->pagination->initialize($config);

        $datatable = $this->Demo_page->get_items($result_per_page, $this->uri->segment(3));

        $this->load->view('demo/ci_pagination', array(
            'datatable' => $datatable,
            'result_per_page' => $result_per_page
        ));
    }
}

I will explain these lines after we finish our View

The View

I use this View to present our data in the tabular format:

<!doctype html>
<html>
    <head>
        <title>Demo for CodeIgniter Pagination :: Demo cho việc phân trang trong CodeIgniter</title>
        <meta charset="utf-8" />
        <style>
            td{
                text-align: center;
            }
            td{
                border-top: 1px solid #ccc;
            }
            table{
                margin: 1em;
            }
        </style>
    </head>
    <body>
        <h1>Demo for CodeIgniter Pagination</h1>
        <?php

        // generate the table
        $this->table->set_heading('ID', 'ITEM CODE', 'QUANTITY');
        echo $this->table->generate($datatable);

        // generate the page navigation

        echo $this->pagination->create_links();
        ?>
    </body>
</html>

Now, we can test our demo with by typing this URL into your browser https://Your_CI_App_folder_URL/demo/ci_pagination

CodeIgniter pagination configuration

Now, when our demo runs perfectly, let look at our Controller again to learn what those lines do.

The first three lines loads the necessary modules. In this demo, we needs the pagination library (of course) to generate the navigation links, the table library to quickly generate a table based on what we got from our source and the demo_page model to communicate with the database.

The, we define a variable contains how many item will be display in a page. After that, in the next four lines, we configure the pagination class before initiate it:

  • $config[‘base_url’]: the full URL to the controller class/function contains the pagination.
  • $config[‘total_rows’]: the total number of records you want to display. This our demo, we will display all record in the table and get it by count_items method of the Model.
  • $config[‘per_page’]: the number of records you want to display in each page. You had better use a constant or a variable to make it easily to be modified later.

After initiate the pagination, we just simply echo the $this->pagination->create_links(); result in our View.

Conclusion

After reading this post, I hope you can understand how to use the useful pagination class of CodeIgniter. Remember, this utility does not play a roll as a Model, its objective is generate the navigation links and write the URL. You can learn more about it in the CodeIgniter user guide.

Convert date time data between php servers and user browsers

You need this because:

  • Your web server is in a fixed location when your users can be in any country all over the world,
  • Our world doesn’t use the same clock but it’s divided in to 24 parts called timezones
  • You need to get, manipulate or display current date time between your web server and your user’s browser

Timezone, UTC time and local time

In general, we have this formula to calculate local time

local time = UTC time + your timezone (in hour)

For example, I’m in Vietnam, my timezone is UTC+7, so when the UTC time is 00:00, my clock will display current time as 07:00. It is so easily to reason the following lines out:

(A) local time = (B) local time – (B) timezone + (A) timezone

PHP function

To implement what we found in PHP, I use this code snippet:

function convert_from_another_time($source, $source_timezone, $dest_timezone){
    $offset = $dest_timezone - $source_timezone;
    if($offset == 0)
        return $source;
    $target = new DateTime($source->format(Y-m-d H:i:s));
    $target->modify($offset+' hours');
    return $target
}

In above function:

  • $source: a DateTime object contains our source data
  • $source_timezone: an integer refers to the timezone of our source data. For example, if the timezone of our source data is ‘Asia/Ho_Chi_Minh’ (UTC+7), $source_timezone is 7.
  • $dest_timezone: an integer refers to the timezone of our target data

As you can see, in my function, I create a new DateTime object and “clone” the data of $source to it. This ensure our old value will not be changed after executing.

Get the timezone of your php web server

Use this code:

$d = new DateTime();
$dtz = ($d->getOffset())/3600;
// we can also use the this procedure style
// $dtz = date_offset_get($d);

Notice that the getOffset method return the difference between your local web server time and UTC time in second, you have to change this value to hours before pass to our convert function.

Get the timezone of your user

The only way to do this is use a client script language like javascript. You can consider using my code:

var d = new Date();
var dtz = -(d.getTimezoneOffset())/60;

Why,i use the negative operation before the result? Unlike php, javascript getTimezoneOffset function return the difference between the UTC time and the client time in minute, therefore, you need to convert that value like above.

Let you server know the user timezone

This is what i used:

  • PHP: check if there is a timezone data in the cookie.
  • JS: if there is no timezone data, calculate like previous session and make an ajax call with post data contains the result.
  • PHP: set the client cookie.

The last thing for WAMP Server 2 users

If you use WAMP Server 2, It’s highly recommended to review your date.timezone value in the php.ini file. In my experience, WAMP makes your php timezone as UTC when keep the timezone of your MySQL the same as your local timezone by default. This may cause further troubles when working with DATETIME, DATE, TIME or TIMESTAMP fields.

Fix yahoo messenger and windows 7 gadget display error

The error manifestation

After a period of using Windows 7 or Yahoo! Messenger (Y!M), you can catch an error like this:

  • Your desktop gadgets doesn’t work and they display incorrectly
  • In the conversation window of Y!M, you cannot see either your or your partner ‘s words.

In this post, I will introduce you some ways to fixed this annoying error.

Cause of the error

There are many reasons why this evil thing happen to you. The corruption of windows registry, sidebar files or some .dll files as well as the existence of a virus can be the main factor. As a general rule of thumb, you should try from the easiest fix.

First thing to check: your windows files

Begin with your sidebar files. You can copy these file from another computer running windows 7 (of course, it current runs normally ) and paste them to the appropriate location in your one. Here, I suppose that you installed you windows in the C drive.

C:\Program Files\Windows Sidebar\sidebar.exe
C:\Program Files\Windows Sidebar\wlsrvc.dll
C:\Program Files\Windows Sidebar\Gadgets\*
C:\Windows\System32\jscript.dll
C:\Windows\System32\vbscript.dll

Restart your computer and hope this help.

The windows .dll files error

This is also a common error of windows, you can re-register your windows .dll files to solve this problem. Open your Start menu, click Run… (or use the shortcut key “Windows key” + “R”). In the new “Run” dialogue, enter the command regsvr32 jscript.dll and click OK button, if another dialogue appears, click OK to ignore this message. Do them same thing with the command regsvr32 vbscript.dll before restarting your computer.

Check the windows registry problem

In the case that this error haven’t been solved yet, you have to check you windows registry. First, open the Registry Editor by entering the command regedt32 to the Run windows as the previous section. After the Registry Editor windows appears, you can see that it looks like the Windows Explorer windows.

Point to the key HKEY_LOCAL_MACHINE\SOFTWARE\CLASSES\CLSID\{f414c260-6ac0-11cf-b6d1-00aa00bbbb558}\In procServer32. Look at the right panel, if the data of (Default) values is different from C:\WINDOWS\SYSTEM32\JSCRIPT.DLL, modify it to correct value.

Next, go to another key HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\. Look at its sub keys in the left panel. If you see a strange sub key whose name is a character like the “L” letter above the 0 sub key, delete the strange sub key. Restart your computer.

The last thing I can help

If things still go wrong, you can download my pre-built bat file and run it on your computer.