Technical Blog Of JackCHAN

June 21, 2010

Synchronize data between load balance servers

Filed under: linux — Tags: , , — kaisechen @ 7:14 am

It always faces similiar business requirment when developing web application using load balance server, e.g upload image to one server and need to synchronize images to another server.

One solution is building a shell script  and put it into crontab , every five or ten minutes run the script.

In the script , use rsync or scp linux command.

Such as :

myaccount@myserver1.com~>rsync ./ -avz myaccount@myserver2.com:/home/myaccount/html/ -e ssh –safe-links

Of course, it should configue public/private key in myserver1.com.

1) produce public/private key pair

myaccount@myserver1.com~>ssh-keygen -b 2048 -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/home/myaccount/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/myaccount/.ssh/id_rsa.
Your public key has been saved in /home/myaccount/.ssh/id_rsa.pub.
The key fingerprint is:
74:78:91:ca:c7:20:14:08:9b:68:cf:15:32:18:7d:21 myaccount@myserver1.com

Notice: don’t input passphrase, because it is the password for private key.

2)copy the public key file to remote server

myaccount@myserver1.com~>scp .ssh/id_rsa.pub myaccount@myserver2.com:/home/myaccount

3)add the content of public key file into authorized_keys in remote server

myaccount@myserver2.com:~> mv id_rsa.pub .ssh/authorized_keys

or

myaccount@myserver2.com:~>cat id_rsa.pub >> .ssh/authorized_keys

4)copy data from local server to remote server now

myaccount@myserver1.com:~>rsync ./ -avz myaccount@myserver2.com:/home/myaccount/html/ -e ssh –safe-links                      building file list … done
./
test.txt

sent 182 bytes  received 48 bytes  460.00 bytes/sec
total size is 60  speedup is 0.26

June 7, 2010

PHP Zend Framework Application Example

Filed under: php, Technology — Tags: , — kaisechen @ 6:33 am

1. Apache configue

<VirtualHost *>
ServerName handbook-admin.myuniversity.com.au
ServerAlias http://www.handbook-admin
.myuniversity.com.au
DocumentRoot /hosts/handbook-admin/htdocs

CustomLog /hosts/handbook-admin/logs/access_log combined
ErrorLog  /hosts/handbook-admin/logs/error_log

CheckSpelling On

<Directory /hosts/handbook-admin/htdocs>
AllowOverride All
</Directory>


<Files ~ “\.(inc|ini)$”>
Order allow,deny
Deny from all
</Files>


</VirtualHost>

2.index.php

<?
require_once(“modules/trob.base/includes/includes.inc”);
require_once(“modules/trob.base/includes/admin-includes.inc”);
require_once(“modules/myuniversity.handbook/includes/includes.inc”);
Main::start(“myuniversity.handbook”);

3.project init file

modules/myuniversity.handbook/project.ini

4.TemplateEngine

Using Smarty as template engine.

The Apache URL Rewriting Example

Filed under: apache, Technology — Tags: — kaisechen @ 2:33 am

This article describes one example to use Apache’s mod_rewrite to solve typical URL-based problems webmasters might be usually confronted with in practice.

Firstly,  load ‘mod_rewrite’.

Open conf/httpd.conf, find ‘Dynamic Shared Object (DSO) Support’.

input :

LoadModule rewrite_module modules/mod_rewrite.so

mod_rewrite is sophisticated and powerful module.

With it webmasters can nearly do all types of URL manipulations ever dreamed about. The price  have to pay is to accept complexity.

Secondly,configue URL Layout.

Canonical Hostnames

RewriteEngine on
RewriteCond   %{HTTP_HOST} ^www\.[^.]+
RewriteRule   ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule   ^www\.([^.]+)(.*) http://$1$2 [L,R]

Thirdly,Configure how the server determines its own name and port.

With UseCanonicalName Off Apache will form self-referential URLs using the hostname and port supplied by the client if any are supplied (otherwise it will use the canonical name, as defined above). These values are the same that are used to implement name based virtual hosts, and are available with the same clients. The CGI variables SERVER_NAME and SERVER_PORT will be constructed from the client supplied values as well.

UseCanonicalName Off

VirtualDocumentRoot “C:\www\%0\html”

<Directory “C:\www\*\html”>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

VirtualScriptAlias “C:\www\%0\cgi”
<Directory “C:\www\*\cgi”>
Options None
Order allow,deny
Allow from all
</Directory>

Fourthly,modify the hosts file in desk machine(winxp).

windows/system32/drivers/etc/hosts.

Add:

127.0.0.1       localhost
127.0.0.1       test.com.au
127.0.0.1       my.company.com.au

Finally, build application directories.

C:\www\test.com.au\html

C:\www\my.company.com.au\html

when your type test.com.au, you can visit the test.com.au application hosting in C:\www\test.com.au\html.

June 4, 2010

Configue svn plugin in Eclipse/MyEclipse (3)

Filed under: eclipse, java, linux, myeclipse, svn, Technology — Tags: , , , , — kaisechen @ 5:44 am

Next, we need to set the ‘Enviroment variables’  ‘SVN_SSH’ in your desk machine.

1) Right click ‘mycomputer’, choice ‘Properties’, in ‘System Properties’, choice ‘Advanced’, click ‘Enviroment variables’.

2) in ‘System variables’, input ‘SVN_SSH’ value, then click ‘ok’

Now, we can begin to use svn plugin in eclipse/myeclipse

You can check in a new project into svn server or check out a existed project from svn server.

If check in a new project into svn server.

1) Chose the project, right click, and in popuped list, chose ‘Team’, then chose ‘Share Project….”

2) chose ‘SVN’

3)create a new repository location

4)Input the url:svn+ssh://jackchan@mycompany.com/svn/myproject/trunk/

5)Enter fold name

6)click Finish

If check out a existed project from svn server.

click File–>’Import…’ , chose ‘SVN’ , ‘checkout projects from SVN’, create a new repository location or use existing repository location, click ‘next’, select the folder to be checked out from SVN, then click ‘Finish’ button.

Because we have finished the ssh automation connection configure, the process of check out or check in will be finished automatically.

If the configue is fine, we should see svn plugin work normally.

Configue svn plugin in Eclipse/MyEclipse (2)

Filed under: eclipse, java, linux, myeclipse, svn, Technology — Tags: , , , , — kaisechen @ 5:12 am

After installing svn plugin in eclipse/myeclipse, we need to configue how to connect svn server.

The svn repository location is:

svn+ssh://jackchan@mycompany.com/svn/myproject/trunk/

Before connecting svn repository in eclipse/myeclipse, we need to configue Putty client to connect the svn server automatically.

How to configue Putty client to connect the svn server automatically? The important thing is producing a private key file.

1) login the svn server using your name (here  use jackchan),

then input below command in server:

ssh-keygen -b 2048 -t rsa -N happy1HAPPY1 -f jack.ppk
Generating public/private rsa key pair.

2) mv jack.ppk.pub .ssh/authorized_keys , the authorized_keys will be used as public rsa key file in server.

3) scp jack.ppk to your client machine, it will be used as private rsa key file in client.

4)open putty client,select ‘session’, input the hostname and click ‘save’

5)click ‘Data’ in putty,in ‘Auto-login username’ input your name(here is jack)

6)click ‘Auth’ in putty, in ‘Private key file for authentication’, load your private rsa key file

7)return session in putty, saves the session again.

8)in putty installation directory, run ‘pageant’

9)right click pageant icon in right-bottom at your desk, and choice ‘add key ‘

10) load your private rsa key file, and input your password, then ‘close’

11) close putty client, reopen it, and you will find you can login automatically the svn server now.


Configue svn plugin in Eclipse/MyEclipse (1)

Filed under: eclipse, java, linux, myeclipse, svn, Technology — Tags: , , , , , — kaisechen @ 2:25 am

How to connect svn Server?

I tried sliksvn, tortoisesvn.

Sliksvn don’t support too much protocal, such as svn+ssh.

Tortoisesvn is good as a svn client.

Both of them support command line. But my favorite IDE is eclipse/myeclipse, so i decide to install a svn client plugin.

There are two kind svn plugins .

(1) Eclipse Subversive – SVN Team Provider Project, Visit http://www.eclipse.org/subversive

(2)Subversion Native Library Adapter (JavaHL), Subclipse is an Eclipse team provider for the Subversion version
control system.  Visit http://subclipse.tigris.org/
This product includes software developed by CollabNet (http://www.Collab.Net/), includes SVNKit(http://svnkit.com/).

I installed both of them,Eclipse Subversive installs and uses easily, so i  just introduce how to install subversion.

Install subversion steps:

1) In myeclipse, click help–>Software Update –>Add/Remove Software …

2)click “Add”. Then in the opened window, click “Add Site..”.

input ‘http://subclipse.tigris.org/update_1.6.x&#8217;

3) click “OK”, click “next”, click “Apply”, the myeclipse will begin to install the plugin, and after finishing the install, it will remind to restart myeclipse.

June 2, 2010

Using Maven In Eclipse/Myeclipse

Filed under: eclipse, hibernate, java, myeclipse, Spring, Technology — Tags: , , , , , , , — kaisechen @ 5:18 am

How to use maven to manage a web application in eclipse/myeclipse?

Firstly,we  need to install one kind of  maven plugin in eclipse/myeclipse.

There are three options which provide Maven integration for Eclipse/MyEclipse.

1)m2eclipse

http://m2eclipse.sonatype.org/, it is the most matured maven plugin for eclipse at the moment

2)Eclipse IAM

3)Maven4MyEclipse, which is base on myeclipse

Maven4MyEclipse and Maven-enabled MyEclipse projects co-exists with command line Maven use perfectly,Maven4MyEclipse was designed, however, to make command line Maven un-necessary in most instances, allowing you to stay inside the IDE and execute all Maven tasks from the UI.

The vice of maven4myeclipse is :

Maven4MyEclipse brings Maven support to MyEclipse Web Projects, MyEclipse EJB Projects, and conventional Java projects,MyEclipse Enterprise Application (EAR) Projects are not supported at this time, but should be in a future release.

Because i am using myclipse, i decided to use Maven4MyEclipse plugin,it is very easy to install it, let’s jump the step.

Secondly, we create a web project ‘Add Maven Support’ in myclipse.’

Thirdly, find the ‘pom.xml’ under the project, open it with ‘Maven POM Editor”

A POM(Project Object Model) is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples for this is the build directory, which is target; the source directory, which is src/main/java; the test source directory, which is src/main/test; and so on.

There are a lot of configues including Overview,Dependencies,Repositories,Build and so on.

Fourthly, we need install maven  and configue a local repository.

Download the maven from http://maven.apache.org, unzip it to any disk directory(e.g. D:\apache-maven-2.2.1). Modify settings file under conf(D:\apache-maven-2.2.1\conf).

Add one line:

<localRepository>D:/apache-maven-2.2.1/.m2/repo</localRepository>

Fifthly, configue maven4myeclipse in myclipse.

Open MyEclipse –>Preferences –>Maven4Myeclipse–>Maven–>Installations,

In User Settings,load the settings.xml in fouth step.

Sixthly,configue ‘Dependencies’ and ‘Repositories’  using ‘Maven Pom Editor’.

Add jboss repository if can’t find library in default maven repositories.

ID:org.jboss.repository.maven, URL:http://repository.jboss.org/maven2

After adding ‘dependencies’, the tool can produce a dependency graph.

In my project, i use spring mvc,hibernate etc. So the dependency graph looks like:

Seventhly, Deploy the web application into Tomcat/Jboss container.

Right click the web application, in popuped list, chose ‘Debug As’,chose ‘MyEclipse Server Application’, chose Tomcat or Jboss.

It is hot deployment, it is convenient to debug application.

Blog at WordPress.com.