Monday, November 24, 2014

CentOS 7 tips

Stop useing root ssh login

#/etc/ssh/sshd_config
PermitRootLogin no




VirtualBox guest addition


after install centos , update it first

yum update

reboot

then update kernel*

yum update kernel*

reboot

then install devel tool and kernel

yum install gcc kernel-devel kernel-headers dkms make bzip2 perl

then mount guestaddtions iso and install it

mkdir /media/cdrom
mount -r /dev/cdrom /media/cdrom
sh /media/cdrom/VBoxLinuxAdditions.run

done


//manual mount share folder

mount -t vboxsf share_name vm_folder

// mount on boot (remember do not checked auto mount on Virtual Box)
touch /etc/modules-load.d/vboxsf.conf

// append line "vboxsf" to vboxsf.conf
echo "vboxsf" >> /etc/modules-load.d/vboxsf.conf

vi /etc/fstab
share_name /path/to/vm defaults 0 0

Symlinks in VirtualBox

//after setting , restart Virtual machine and virtualbox

VBoxManage.exe setextradata VM_NAME VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARE_NAME 1


ifconfig


Centos default didn't have ifconfig command

we can use "ip addr" instend

or 

yum install net-tools

for install ifconfig


service

sudo systemctl start httpd.service
sudo systemctl status httpd.service
sudo systemctl enable httpd.service //on boot
systemctl list-unit-files --type=service


firewall

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=22/tcp --add-source=192.168.1.0/24 --permanent
firewall-cmd --reload

network static

#vi /etc/sysconfig/network-scripts/ifcfg-name

BOOTPROTO=static
IPADDR=192.168.x.x
NETMAST=255.255.255.0
GATEWAY=192.168.x.1

network dns

$ sudo vi /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

or

$ sudo vi /etc/sysconfig/network-scripts/ifcfg-name

DNS1=8.8.8.8

php-mcrypt


cd /tmp
wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
rpm -Uvh epel-release-7-5.noarch.rpm
yum install php-mcrypt*



alias su command

create a custom.sh shell script in  (rember file name have execute order problem)
# /etc/profile.d/custom.sh

alias ll='ls -al'



apache 2.4 Directory new option required

Require all granted






Friday, September 19, 2014

Add input auto suggest (autocomplete) with AJAX (XMLHttpRequest)

When we submit a form , browser will add autocomplete with input (if enabled)

But ajax post won't effect

This is a solution:

1. Add a hide iframe with empty page

<iframe name="hiddenIframe" class="hide" src="javascript:false"></iframe>

2. Let your form target with the iframe

<form target="hiddenIframe" id="myForm">
    <input name="some_input" />
    <button onclick="ajaxPost(this);">btn</button>
</form>

3. Trigger a submit event on this form when you send a AJAX post for it

<script>

function ajaxPost(obj){

     $("#myForm").submit();

    //do your ajax post

}

</script>



OK, now the input can catch user insert string for browser auto suggetst(autocomplete)

Saturday, June 28, 2014

angular vs ember performance


ember:  http://jsfiddle.net/274ed/1/

angular:  http://jsfiddle.net/hMpSS/1/

angular.js

//pratice angular version

//show what can angular do , and how

//require: underscore, angular, angular-route

// you can see the demo from below link


http://jsfiddle.net/34XS9/

//datas

var posts = [
    {
        id: 1,
        title: "first post",
        comments: [
            {
                content: "comment 1-1"
            },
            {
                content: "comment 1-2"
            }
        ]
    },
    {
        id: 2,
        title: "second post",
        comments: [
            {
                content: "comment 2-1"
            },
            {
                content: "comment 2-2"
            }
        ]
    },
    {
        id: 3,
        title: "third post",
        comments: [
            {
                content: "comment 3-1"
            },
            {
                content: "comment 3-2"
            }
        ]
    }
];

var topNews = [
    {
        title: "My life for aiur"
    },
    {
        title: "Show me the money"
    }
];


var messages = [
    {
        text:"message1"
    },
    {
        text:"message2"
    }
];




//app & controller & route

//here use ng-template tag for templateUrl

var praticeApp = angular.module('praticeApp', [
  'ngRoute'
]);

praticeApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/', {
        templateUrl: 'site-index',
        controller: 'siteIndexCtrl'
      }).
      when('/posts', {
        templateUrl: 'post-index',
        controller: 'postIndexCtrl'
      }).
      when('/post/:post_id', {
        templateUrl: 'post-view',
        controller: 'postViewCtrl'
      }).
      otherwise({
        redirectTo: '/'
      });
  }]);


praticeApp.controller('baseCtrl', function ($scope, $rootScope) {

    $scope.search = "test search";
    $rootScope.formatted = function(title){
        return title + " formatted";
    };
 
});

praticeApp.controller('siteIndexCtrl', function ($scope) {

    $scope.helloworld = "Hello World!";
    $scope.messages = messages;
 
});


praticeApp.controller('postIndexCtrl', function ($scope) {

    $scope.posts = posts;
    $scope.topNews = topNews;
 
    $scope.addNewsTitle = "";
 
    $scope.addNews = function(){
        $scope.topNews.push({
            title : $scope.addNewsTitle
        });
     
        $scope.addNewsTitle = "";
    };
 
    $scope.removeNews = function($index){
        $scope.topNews.splice($index,1);
    };
 
});

praticeApp.controller('postViewCtrl', function ($scope, $routeParams) {
 
    $scope.showComment = false;
    $scope.post = _.findWhere(posts, {id: parseInt($routeParams.post_id, 10)});
    $scope.toggleComment = function(){
        $scope.showComment = !$scope.showComment;
     
    };
});


<!--layout & templates-->

<body ng-app="praticeApp" style="margin: 50px;">

        <div ng-controller="baseCtrl">
             <input type="search"  placeholder="search text" /> <span>show the main layout</span>
        </div>

        <div ng-view ></div>
     
        <script type="text/ng-template" id="post-index">
            <div class="post-index">
             
                <p>
                    <a ng-href="#/">back</a>
                </p>
     
                <ul>
                    <li ng-repeat="post in posts">
                        <a ng-href="#/post/{{post.id}}">{{post.title}}</a>
                    </li>
                </ul>


                <ul>
                    <li ng-repeat="post in posts">
                        <a ng-href="#/post/{{post.id}}">{{formatted(post.title)}}</a>
                    </li>
                </ul>



                <ul>
                    <li ng-repeat="topNew in topNews">
                        {{topNew.title}}
                        <button ng-click="removeNews($index)">remove</button>
                    </li>
                </ul>

                <input type="text" ng-model="addNewsTitle"/>
                <button ng-click="addNews()">addnews</button>

             
            </div>
        </script>
     
     
        <script type="text/ng-template" id="site-index">
            <div class="site-index">
                <h1>{{helloworld}}</h1>
                <input type="text" ng-model="helloworld" /> try edit it!
                <ul>
                    <li ng-repeat="message in messages">
                        {{message.text}}
                    </li>
                </ul>

                <p>
                    <a ng-href="#/posts">link to posts</a>
                </p>
            </div>
        </script>
     
     
        <script type="text/ng-template" id="post-view">
            <div class="post-view">
                <h2>{{post.title}}</h2>
                <p>
                    <a ng-href="#/posts">back</a>
                </p>
                 
                <button ng-click="toggleComment()">
                    <span ng-if="showComment">hide comment</span>
                    <span ng-if="!showComment">show comment</span>
                </button>
             
                <ul ng-if="showComment">
                    <li ng-repeat="comment in post.comments">
                        {{comment.content}}
                    </li>
                </ul>
            </div>
        </script>
         

     

</body>

Wednesday, June 25, 2014

Ember.js

// you can see demo from below link

http://jsfiddle.net/PL2gf/1/

//some pratice , show what can ember do

//Datas

var posts = [
    {
        id: 1,
        title: "first post",
        comments: [
            {
                content: "comment 1-1"
            },
            {
                content: "comment 1-2"
            }
        ]
    },
    {
        id: 2,
        title: "second post",
        comments: [
            {
                content: "comment 2-1"
            },
            {
                content: "comment 2-2"
            }
        ]
    },
    {
        id: 3,
        title: "third post",
        comments: [
            {
                content: "comment 3-1"
            },
            {
                content: "comment 3-2"
            }
        ]
    }
];

var topNews = [
    {
        title: "My life for aiur"
    },
    {
        title: "Show me the money"
    }
];


var messages = [
    {
        text:"message1"
    },
    {
        text:"message2"
    }
];



//app


App = Ember.Application.create();


//router map

App.Router.map(function() {
 
    this.resource('site',  {path: "/"});
    this.resource('posts');
    this.resource('post',{path: '/post/:post_id'});
 
});


//router & controller


//main controller

App.ApplicationController = Ember.ArrayController.extend({
 
    searchPlaceholder: "layout search"
 
});


//site

App.SiteRoute = Ember.Route.extend({
 
    model: function(){
        return messages;
     
        //here allow use getJSON  (ember compute after async)
        //return $.getJSON('post/models');
    },
 
    setupController: function(controller, model){
        controller.set("model", model);
        controller.set("welcomeText", "Hellow world!");
     
        //here disallow use getJSON  (async true)
    }
 
});



//posts
App.PostsRoute = Ember.Route.extend({
    model:function(){
        return Ember.RSVP.hash({
            posts: posts,
            topNews: topNews
        });
     
    }
});


App.PostsController = Ember.ObjectController.extend({
    countNews: function(){
     
        return this.get("topNews.length");
     
    }.property("topNews.length"),
    new_news:"",
    addNews: function(){
     
        var topNews = this.get("topNews");
     
        topNews.addObject({
            title: this.get("new_news")
        });
     
        this.set("new_news", "");
    },
    removeNews: function(news){
        this.get("topNews").removeObject(news);
    }
 
});



//post
App.PostRoute = Ember.Route.extend({
    model:function(params){
        return posts.findBy("id", parseInt(params.post_id, 10));
    }
});

App.PostController = Ember.ObjectController.extend({
 
    commentsVisible: false,
    showComments: function(){
        this.set("commentsVisible", true);
    },
    hideComments: function(){
        this.set("commentsVisible", false);
    },
    formattedTitle: function(){
     
        return this.get("model").title + " formatted";
    }.property("formattedTitle")
 
});


templates

<!--layout-->

<script type="text/x-handlebars">
 
    <div style="padding: 50px;">
        {{input type="search"  placeholder=searchPlaceholder  }}
     
        no working,  just let you know where put the layout

        {{outlet}}
    </div>
 
</script>



<!--site-->

<script type="text/x-handlebars" id="site">

<h1>{{welcomeText}}</h1>

<section>
    {{input type="text" value=welcomeText class="edit-welcome" }}
    <span>try edit it!</span>  
</section>

<section>
    <ul class="messages">
        {{#each}}
            <li>{{text}}</li>
        {{/each}}      
    </ul>  
</section>

<section>
    <div>
        {{#link-to "posts"}}
            link to the posts
        {{/link-to}}
    </div>
</section>

</script>


<!--posts-->

<script type="text/x-handlebars" id="posts">

    <br/>
    {{#link-to "site"}}
        back
    {{/link-to}}
    <br/>
    <section>
        <ul>
            {{#each posts}}
                <li>
                    {{#link-to "post" this.id}}
                        {{title}}
                    {{/link-to}}
                </li>
            {{/each}}
        </ul>

    </section>

    <section>

        <ul>
            {{#each model.posts itemController="post"}}

                <li>
                    {{#link-to "post" this}}

                        {{formattedTitle}}

                    {{/link-to}}
                </li>


            {{/each}}
        </ul>

    </section>

    <section>

        <p>has: {{countNews}} news</p>

        <ul>
            {{#each news in model.topNews}}

            <li>
                <span>{{news.title}}</span>
                <button {{action "removeNews" news}}>
                    remove
                </button>

            </li>


            {{/each}}
        </ul>



        {{input type="text" value=new_news}}
        <button {{action "addNews"}}>
            add news
        </button>

    </section>

</script>

<!--post-->

<script type="text/x-handlebars" id="post">

        <br/>

    {{#link-to "posts"}}

    back

    {{/link-to}}

    <br/>


    <div class="post">

        <h2 class="title">{{title}}</h2>


        {{#if commentsVisible}}

            {{#each comments}}
                <p>{{content}}</p>
            {{/each}}

            <button {{action "hideComments"}}>
                hideComments
            </button>

        {{else}}

            <button {{action "showComments"}}>
                showComments
            </button>

        {{/if}}

    </div>

</script>




Wednesday, June 11, 2014

CentOs 6.5 minimal in virtualbox

CentOs 6.5 minimal in virtualbox


network

vi /etc/sysconfig/network-scripts/ifcfg-eth0   //onboot yes

change virtualbox network-adapter  to bridge

service network start

user

adduser someuser

passwd someuser

visudo  // someuser ALL=(ALL) ALL


iptables

sudo iptables -P INPUT   ACCEPT  //input default
sudo iptables -P OUTPUT  ACCEPT   //output default
sudo iptables -P FORWARD ACCEPT //forward default

sudo iptables -F  //remove chain rule
sudo iptables -X  // remove user-defined chain rule ( by iptables -N
sudo iptables -Z  // Zero counters in chain or all chains

 sudo iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
//accept had shack hand's reqeust


 sudo iptables -A INPUT -i lo -j ACCEPT

 sudo iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 22 -j ACCEPT

 sudo iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 80 -j ACCEPT

sudo iptables -D INPUT 3  // remove chain rule

sudo iptables -R INPUT 3 -p tcp -s 192.168.0.2 --dport 22 -j ACCEPT // replace chain rule

 sudo iptables -P INPUT DROP //policy default drop



sudo /etc/init.d/iptables save

sudo iptables -L -n


list mounted devices

df


VirtualBox Guest Additions on Fedora 20/19, CentOS/RHEL 6.5/5.10

Reference : http://www.if-not-true-then-false.com/2010/install-virtualbox-guest-additions-on-fedora-centos-red-hat-rhel/

Install VirtualBox Guest Additions on Fedora, CentOS and Red Hat (RHEL)

1. Change root user

su -
## OR ##
sudo -i

2. Mount VirtualBox Guest Additions

Click Devices > Install Guest Additions… on VirtualBox
VirtualBox Install Guest Additions
Mount VirtualBox Guest Additions device
mkdir /media/VirtualBoxGuestAdditions
mount -r /dev/cdrom /media/VirtualBoxGuestAdditions

3. Make sure that you are running latest kernel

Update virtual machine kernel and reboot
yum update kernel*
reboot

4. Install following packages

On CentOS/Red Hat (RHEL) 6/5, EPEL repo is needed
## CentOS 6 and Red Hat (RHEL) 6 ##
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
 
## CentOS 5 and Red Hat (RHEL) 5 ##
rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
Install following packages
yum install gcc kernel-devel kernel-headers dkms make bzip2 perl

5. Add KERN_DIR environment variable

## Current running kernel on Fedora, CentOS 6 and Red Hat (RHEL) 6 ##
KERN_DIR=/usr/src/kernels/`uname -r`
 
## Current running kernel on CentOS 5 and Red Hat (RHEL) 5 ##
KERN_DIR=/usr/src/kernels/`uname -r`-`uname -m`
 
## Fedora example ##
KERN_DIR=/usr/src/kernels/2.6.33.5-124.fc13.i686
 
## CentOS and Red Hat (RHEL) example ##
KERN_DIR=/usr/src/kernels/2.6.18-194.11.1.el5-x86_64
 
## Export KERN_DIR ##
export KERN_DIR

6. Install Guest Additions

cd /media/VirtualBoxGuestAdditions
 
# 32-bit and 64-bit systems run following
./VBoxLinuxAdditions.run
Output looks like following:
[root@fedora VBoxGuestAdditions]# ./VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.1.6 Guest Additions for Linux.........
VirtualBox Guest Additions installer
Removing installed version 4.1.6 of VirtualBox Guest Additions...
Removing existing VirtualBox DKMS kernel modules           [  OK  ]
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module                   [  OK  ]
Building the shared folder support module                  [  OK  ]
Building the OpenGL support module                         [  OK  ]
Doing non-kernel setup of the Guest Additions              [  OK  ]
You should restart your guest to make sure the new modules are actually used

Installing the Window System drivers
Installing X.Org Server 1.11 modules                       [  OK  ]
Setting up the Window System to use the Guest Additions    [  OK  ]
You may need to restart the hal service and the Window System (or just restart
the guest system) to enable the Guest Additions.

Installing graphics libraries and desktop services componen[  OK  ]

7. Reboot guest system

reboot
Then VirtualBox Guest Additions install is ready.




share virtual box folder

be sure you vm_folder exists & empty & auto_mount is disable

//manual

mount -t vboxsf share_name vm_folder

// auto

sudo vi /etc/fstab

add to last line:

share_name    /vm_folder   vboxsf    defaults 0 0


avoid shared file edit in host not change in VM (js,css,img)

edit apache config:

EnableSendfile Off


SELinux.....

virtualbox share folder for httpd with selinux ... i can't find solution , so i disable it

sudo vi /etc/selinux/config

change SELINUX=enforcing to SELINUX=permissive

reboot



mariadb

mysql_secure_installation // init root passwd


The following my.cnf example files are included with MariaDB. Examine them to see more complete examples of some of the many ways to configure MariaDB.
my-small.cnf
my-medium.cnf
my-large.cnf
my-huge.cnf
The above example files can usually be found in one of the following directories:
source-file-path/support-files
mysql-install-path/share/mysql (e.g. /usr/local/mysql/share/mysql)

so

sudo cp /usr/share/mysql/my-xxx.cnf /etc/my.cnf


//set default charactor to utf8 and utf8_unicode_ci

**edit my.cnf :


[client]

default-character-set=utf8


[mysqld]
character-set-server=utf8
collation-server=utf8_unicode_ci
#init_connect='SET collation_connection=utf8_unicode_ci'
#skip-character-set-client-handshake


[mysql]
default-character-set=utf8

end edit**

//even we set collation_connection=utf8_unicode_ci , mysql will reset it back to utf8_general_ci
// so need above two line :
// init_connect='SET collation_connection=utf8_unicode_ci'
// skip-character-set-client-handshake
// where [skip-character-set-client-handshake] can stop mysql handshake and reset it




MariaDB [(none)]> show variables like 'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |

+----------------------+-----------------+


mysql querys

CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';

GRANT ALL ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;


//replace user host

UPDATE mysql.user SET host = '10.0.0.%' WHERE host = 'internalfoo' AND user != 'root';
UPDATE mysql.db SET host = '10.0.0.%' WHERE host = 'internalfoo' AND user != 'root';

FLUSH PRIVILEGES;

// with grant option is mean this user can set grant options to others

DROP USER username@'123.123.123.%';
DROP USER username@localhost;
DROP USER username;

FLUSH PRIVILEGES;


select * from :table where :condition group by :column order by :column;

update :table set :column=:value;

insert into :table (:column1, :column2) value(:value1, :value2);

Centos6 with php-mcrypt



wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
sudo rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
2) Install it via:
yum update
yum install php-mcrypt*

Service on boot

chkconfig --list service_name

chkconfig service_name on

chkconfig service_name off