Personal Project

Friday, February 26, 2016

How to sync time with NTP on Ubuntu ?

sudo apt-get install ntp

vim /etc/ntp.conf
server ntp.nict.jp
server ntp1.jst.mfeed.ad.jp
server ntp2.jst.mfeed.ad.jp
server ntp3.jst.mfeed.ad.jp

service ntp restart
ntpdate ntp.nict.jp


sudo dpkg-reconfigure tzdata

Monday, February 8, 2016

How to rsync local folder to remote folder ?



Rsync local folder to remote folder 


Command

rsync -avz --delete -e 'ssh -i key' localfolder username@remote ip:remote folder

Ex: 
rsync -avz --delete -e 'ssh -i server.pem' test ubuntu@192.168.0.1:/var/www/html/test/



Sunday, February 7, 2016

Install Apache, PHP, and Memcached on CentOS

Install Apache 

# yum install update 

Setup Apache


# vim /etc/httpd/conf/httpd.conf

<Directory "/var/www/html">
   AllowOverride all
#
# Controls who can get stuff from this server.
#
    Order allow,deny
    Allow from all

</Directory>

Install PHP


# yum install php

# yum install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel
curl-d

Check PHP

# vim /var/www/html/info.php
<?php
phpinfo();
?>

// Test Link
http://Server`s IP/info.php

Install Memcached

#yum install memcached

Start Memcached


# chkconfig --level 235 memcached on
# /etc/init.d/memcaached start
# /etc/init.d/memcached sratus

Install Memcached Perl Library

Install perl library for Memcached.
# yum install perl-Cache-Memcached

Install Memcached Python Library

Install python library for Memcached.
# yum install python-memcached

Restart Apache

Restart the Apache service to reflect changes.
# /etc/init.d/httpd restart
OR
# service httpd restart

Check Memcache


# vim /var/www/html/memtest.php

<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
var_dump($get_result);
?>

// Test Link
http://Server`s IP/memtest.php


Restrat Apache

# /etc/init.d/httpd restart


Thursday, February 4, 2016

How to optimize Apache, setup Memcached and PHP on Ubuntu ?

Install and setup Apache

// Install Apache2  
sudo apt-get install apache2

// Edit Config
vim /etc/apache2/apache2.conf

<IfModule mpm_prefork_module>
StartServers       10
MinSpareServers    10
MaxSpareServers   10
ServerLimit      256
MaxClients       256
MaxRequestsPerChild 10000
</IfModule>


// check module

apachectl -V | grep -i mpm


// disable event

a2dismod mpm_prefork


// Enable Prefork

a2enmod mpm_prefork

// Restart Apache

service apache restart


Install Memcahced

// Install memcached

sudo apt-get update
sudo apt-get install php5 php5-memcache
sudo apt-get install memcached
sudo apt-get install php-pear
sudo apt-get install build-essential
sudo pecl install memcache

// restart memcached

sudo service memcached restart 

You should add "extension=memcache.so" to php.ini  and restart Apache 
 

// Check Memcache
ps aux | grep memcache 

Install PHP

sudo apt-get install php5 php5-mysql
sudo apt-get install --purge mysql-client mysql-common 

// Remove MYSQL
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean

Solutions to common problems

Problem1:  
The mcrypt extension is missing. Please check your PHP configuration.

Solution:
sudo apt-get install php5-mcrypt
sudo apt-get install mcrypt

Problem2:  
Need CURL PHP extension to connect facebook

Solution:
sudo apt-get install curl libcurl3 libcurl3-dev php5-curl

Problem3:
Echo PHP Not Support at HTML

Solution:
vim /etc/php5/apache2/php.ini
short_open_tag = On  
service apache2 stop

service apache2 start

   
chown -R www-data /var/www/html/
chmod -R u+w /var/www/html/


Tuesday, February 2, 2016

Build Quick-Cocos2dx-Lua environment on Windows and Mac

I have used Corona SDK developing games for months.  Its not free and flexible to add some native features.  Quick Cocos2ds-Lua is open source and you have more freedom to add any features.
If you want to develop a 2D game and make it time to market, Quick Cocos2ds-Lua would be the best tool to boost your development speed.

Install Quick-Cocos2dx-Community_3.6 on Windows



uzip Quick-Cocos2dx-Community_3.6_release_190dccc.zip


cd C:\Cocos2d-x\Quick-Cocos2d-x\Quick-Cocos2dx-Community


execute setup_win.bat


Execute Simulator

C:\Cocos2d-x\Quick-Cocos2d-x\Quick-Cocos2dx-Community\quick\player\proj.win32/player3.exe



Install Quick-Cocos2dx-Community_3.6 on Mac

cd /Quick-Cocos2dx-Community

execute setup_mac.sh

Execute Simulator

/Applications/player3.app


Monday, February 1, 2016

Using Amazon route 53 to create DNS failover for MYSQL

AWS Route 53 allows you to set a timer to periodically check the connection status of MYSQL via TCP or HTTP protocol. There are many approaches to achieve this, such as LAMP (PHP + Apache), Java + Tomcat, and C/C++ Sock Programming.
I used LAMP method to save time and efforts in terms of development and testing.

Create a health check  trigger by the following settings. 
IP Address : 192.168.0.1
Port  :  80
Site :  testdb.php
domain name: test.com

Create a testdb.php to check mysql connection.
If the connection is fine, then respond 200 OK.
Otherwise respond 500 error code to AWS Route 53. 


testdb.php 
<?php
/**
 * Description of Connection
 */
class Connection {
/** Instance */
private  static $_singleton = null;
// DB Settings
    private $db;
private $dbHost;
private $dbName;
private $dbUserName;
private $dbPassword;
private $dbPort = "3306";
/**
* DB Instance
*/
public static function getInstance() {
if(self::$_singleton == null) {
self::$_singleton = new Connection();
}
return self::$_singleton;
}

private function __construct(){
}
  
public function createConnection($host, $dbname, $username, $password, $port = '3306')
{
$this->dbHost = $host;
$this->dbName = $dbname;
$this->dbUserName = $username;
$this->dbPassword = $password;
try
        {
            $this->db = new PDO('mysql:host='.$this->dbHost.';port='.$this->dbPort.';dbname='.$this->dbName.'', $this->dbUserName, $this->dbPassword);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$sql = "SELECT host from user LIMIT 1";
$result = $this->db->query($sql);
foreach ($result as $row) {
echo "SUCC";
break;
}
 
$this->db = null; // close the database connection
        }
        catch (PDOException $e)
        {
            throw new Exception("Connection to database  failed. (".$e->getMessage().")");
die();
        }
    
}

}

/**
 * Initial Database Connection
 *DbManager::getInstance()->dbConnection($DBi, $db1, $DBu, $DBp)
* @param $DBi  Database server IP
* @param $db1  Database name
* @param $DBu  user name
* @param $DBp  user passwoard
*/

// DEV 
$DBi   = '127.0.0.1';
$db1   = 'mysql';
$DBu   = 'account';
$DBp   = 'password';


// Initial DB Connection
Connection::getInstance()->createConnection($DBi, $db1, $DBu, $DBp);

?>

Create a Primary hosted zone for test.com.

Now, in the Edit Record Set panel on the right side of the page, do the following:
  1. Set the TTL to 60 seconds. This limits the amount of time this DNS record will be cached within the Internets DNS system, which means that there will be a shorter delay between the time failover occurs and the time that end users begin to be routed to your backup site.
  2. Set the Routing Policy to Failover.
  3. Select Primary as the Failover Record Type.
  4. Select Yes for Associate Record Set with Health Check.
  5. Select the health check to associate with this record. In the drop-down that appears, you should see the health check we just created. Select this health check.
  6. Click Save Record Set.

Create a Slave Record Set

  1. Set the TTL to 60 seconds. This limits the amount of time this DNS record will be cached within the Internets DNS system, which means that there will be a shorter delay between the time failover occurs and the time that end users begin to be routed to your backup site.
  2. Set the Routing Policy to Failover.
  3. Select Slave as the Failover Record Type.
  4. Select NO for Associate Record Set with Health Check.
  5. Select NO for health check to associate with this record. 
  6. Click Save Record Set.

The switching time between MYSQL Master DB and Slave DB details as below.
test.com -> DB Master IP Address
DB Master  or Appache is down
test.com  --> DB Slave  IP Address
time : 30 sec 
DB Master is UP.
test.com  --> DB Master  IP Address
time : 60 sec    

DNS failover approach is not a perfect solution for MYSQL HA, because it will take at
least 60 sec to switch database. It means the system will have 60 sec of down time.
An architecture of  a load balancer and a MYSQL cluster in sync mode might be superior to this.