Dec 24, 2015

How to Install Redis on CentOs 6

BY Altaf Hussain IN , 1 comment


Redis is an open source (BSD licensed), in-memory data structure used as database and cache. Its a very powerful and advanced key-value store. It supports data structures such as strings, hashes, lists, sets, sorted sets and much more.



We will discuss more on it. Let us start with the Installation of Redis. As said in couple of previous article you have to login on the terminal with root privileges to install it. or use sudo. I am asuming you are on root privilege.

Before installing Redis you need some tools installed like gcc, gcc-c++, make and tcl. So install it first.

    yum -y install gcc gcc-c++ make tcl

Tools are now installed. Now navigate to /usr/local/src and download redis

Download and Install Redis


    cd /usr/local/src
    wget http://download.redis.io/releases/redis-3.0.6.tar.gz
    tar xzf redis-3.0.6.tar.gz
    cd redis-3.0.6

After downloading untar the downloaded file with above command and navigate to redis directory
Now build It and Install

    make distclean
    make
        
You can now test installation (Optional) If everything is ok it will display All tests passed without errors! in the end

    make test

Now copy the binary to bin directory

    cp src/redis-server src/redis-cli /usr/local/bin

Now create a directory and copy Redis config file to it.

    mkdir /etc/redis
    cp redis.conf /etc/redis

Now create a working directory for this Redis instance. -p option is for creating recursive directory structure under /var i.e. redis/6379

    mkdir -p /var/redis/6379

Edit Redis config file with your favourite text editor to make some necessery changes. Make sure you make change to following lines and set its value as given
 
    vi /etc/redis/redis.conf
    
    #Change Following
    
    daemonize yes
    pidfile /var/run/redis.pid
    loglevel notice (choose any one from verbose/debug/notice/warning)
    logfile /var/log/redis_6379.log
    dir /var/redis/6379

Now download Redis init script (redis-server) and move it to /etc/init.d and make it execeutable

    wget https://raw.githubusercontent.com/saxenap/install-redis-amazon-linux-centos/master/redis-server
    mv redis-server /etc/init.d
    chmod 755 /etc/init.d/redis-server

Make sure Redis restarts on server reboot. So add it and start Redis

    chkconfig --add redis-server
    chkconfig redis-server on
    service redis-server start

Now open /etc/sysctl.conf in any editor and add following line in the bottom of file (Optional)
 
    vi /etc/sysctl.conf
    
    #Add following line on bottom
    
    sysctl vm.overcommit_memory=1

And thats it. We are done with Redis Installation. Open Redis CLI terminal with following command

    /usr/local/bin/redis-cli

You are now on Redis command line. Try executing Redis commands here. Type exit or press ctrl+c to close session.

Dec 23, 2015

Create a new user and grant permission in MySQL

BY Altaf Hussain IN , No comments


In previous article I have explained How to Install Apache, MySQL, PHP on CentOs 6. Let us proceed one step further and add a MySQL user and grant privileges. So to add user you need to login on the terminal with root privileges.

Login to MySQL with following command

    mysql -u root -pmypwd@123

mypwd@123 is root password. Change it with your password.
Note: There is no space between -p and password. In following commands I have used john as user and john@123 as password. You are advised to change this with a secure password.

You are now logged in to MySQL. You can see MySQL command prompt as well.
Create user with this command

    CREATE USER 'john'@'localhost' IDENTIFIED BY 'john@123';

The User john is created with password john@123. But the user can not do anything until we grant privileges to him.

Now grant him all privileges by

    GRANT ALL PRIVILEGES ON *.* TO 'john'@'localhost' WITH GRANT OPTION;

So we have now given ALL privileges to user john on all database and all tables. The first asterisk refers the database and the second one refers tables.

Now to make the changes to take effect we need to reload all the privileges with following command

    FLUSH PRIVILEGES;

Here is the list of different user permission
ALL PRIVILEGES – To grant all privileges
CREATE – To allow user to create databases and tables
DROP - To allow user to drop databases and tables
DELETE - To allow user to delete rows
INSERT - To allow user to insert rows
SELECT – To allow user to read the database
UPDATE - To allow user to update rows

And thats it. A new user has been created in MySQL. You can see all this action in following screen



This user can now login to the MySQL on the same host i.e. localhost. What if user wants to access it from outside say from some MySQL query browsers. Let us create a user to access it from outside. We need to follow the same three commands what we used above just changing the localhost to some IP address (to access from this IP) or with wildcard to allow from everywhere. See

    CREATE USER 'john'@'%' IDENTIFIED BY 'john@123';

    GRANT ALL PRIVILEGES ON *.* TO 'john'@'%' WITH GRANT OPTION;

    FLUSH PRIVILEGES;

Above three commands will create a user who can access this database from anywhere with help of any query browser.

Dec 22, 2015

Install Apache, MySQL, PHP on CentOs 6

BY Altaf Hussain IN , , No comments


LAMP stack is the most powerful open source solution on the web. LAMP stands for Linux, Apache, MySQL and PHP. In this article we are going to install these on CentOs 6. So We already have Linux running centOs. We will work on the next three in this article.

For installation you have to login on the terminal with root privileges. Otherwise you will need to use sudo with every command.

INSTALL APACHE

To install Apache type this command in terminal

    sudo yum install httpd

You can skip sudo if you are logged in as root. Above command will install apache and will create a directory /var/www/html to keep files. Now change the directory permission to 777 to make it writable so that the files can be transfered to it.

    sudo chmod 777 /var/www/html

Next make Apache to restart on server Reboot

    sudo chkconfig httpd on

Now start Apache with following command

    sudo service httpd start

You can now navigate on the browser with your IP or Domain (If it is mapped to the IP).

You will now see a page running Apache like below. This page will be removed when you upload your index file in /var/www/html



INSTALL PHP

Now Install PHP with following command

    sudo yum install php php-mysql

Above command will install php and php-mysql module. You can check its version with php -v command. You can install multiple modules in one command separated by space like we did in the above command. You will need some dependencies to work with php. So install them as well.

    sudo yum install gd gd-devel php-gd php-xml

Now restart Apache with following command

    sudo service httpd restart

So as of now we have installed Apache and PHP. Let us proceed further and install MySQL.

INSTALL MYSQL

Install MYSQL with following command

    sudo yum install mysql-server

Next make Apache to restart on server Reboot

    sudo chkconfig mysqld on

Now start MySQL with following command

    sudo service mysqld start

This will display info something like this

    Initializing MySQL database:  Installing MySQL system tables...
    OK
    Filling help tables...
    OK
    
    To start mysqld at boot time you have to copy
    support-files/mysql.server to the right place for your system
    
    PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
    To do so, start the server, then issue the following commands:
    
    /usr/bin/mysqladmin -u root password 'new-password'
    /usr/bin/mysqladmin -u root -h li1256-197 password 'new-password'
    
    Alternatively you can run:
    /usr/bin/mysql_secure_installation
    
    which will also give you the option of removing the test
    databases and anonymous user created by default.  This is
    strongly recommended for production servers.
    
    See the manual for more instructions.
    
    You can start the MySQL daemon with:
    cd /usr ; /usr/bin/mysqld_safe &
    
    You can test the MySQL daemon with mysql-test-run.pl
    cd /usr/mysql-test ; perl mysql-test-run.pl
    
    Please report any problems with the /usr/bin/mysqlbug script!
    
                                                               [  OK  ]
    Starting mysqld:                                           [  OK  ]


As it says above let us now set a root MySQL password. Type the following command in terminal.

    sudo /usr/bin/mysql_secure_installation

Above command will display something like this in terminal and will ask some questions to fill in. Fill in what it asks including root password. mypwd@123 is the sample password used below. Choose a secure password for root.

    Enter current password for root (enter for none):
    OK, successfully used password, moving on...
 
    .........
    .........   
    
    Set root password? [Y/n] Y
    New password: mypwd@123
    Re-enter new password: mypwd@123
    Remove anonymous users? [Y/n] y
    Disallow root login remotely? [Y/n] y
    Remove test database and access to it? [Y/n] y
    Reload privilege tables now? [Y/n] y
     ... Success!

    Cleaning up...

    All done!  If you've completed all of the above steps, your MySQL
    installation should now be secure.
    
    Thanks for using MySQL!

And now we are done with MyQL installation too. So our LAMP stack stack is ready to use. In next article I will explain how do we connect to this MySQL Server, and Create user and grant privileges.

Dec 11, 2015

Track email open event with google analytics

BY Altaf Hussain IN No comments


Google Analytics has a great feature which allows us to track email open stats. It provide us a single url which is to be embed as an image with img tag. The url of the image contains all necessary parameters to track the event. The full image url sample would be something like this

http://www.google-analytics.com/collect?v=1&tid=UA-48261097-1&cid=2b884d0f825b&t=event&ec=email&ea=open&el=John+Doe&cs=productPromotion&cm=email&cn=businessEmail

URL ParamDescriptionSample Value
vGoogle Analytics (GA) Version Number1
tidGoogle Analytics Tracking IDUA-48261097-1
cidCLIENT ID (Random alpha numeric characters)2b884d0f825b
tEvent Hit Type event
ecEvent Categoryemail
eaEvent Actionopen
elEvent Label (Unique identification for this recipient)John Doe
csCampaign SourceproductPromotion
cmCampaign Mediumemail
cnCampaign Name IdentifierbusinessEmail
Make sure all values of the url parameters should be url encoded.

You can add this url in image tag like this in the bottom of your email content.

<img src="http://www.google-analytics.com/collect?v=1&tid=UA-48261097-1&cid=2b884d0f825b&t=event&ec=email&ea=open&el=John+Doe&cs=productPromotion&cm=email&cn=businessEmail"/>

You can track events in Google Analytics and you will see something like this

Google Analytics Preview
Google Anlytics Dashboard Preview

Jun 7, 2015

Filter list items with jQuery

BY Altaf Hussain IN No comments


Sometimes while working with list items or contents, we need to filter out the list items or contents for some matching records. We can implement this using jQuery with ease. Here is a little jQuery code snippet to implement local search.


  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script>
  <script type="text/javascript">
  $(document).ready(function(e) {
    $('#search').keyup(function(){
    var val = $(this).val();
    $(".list").find('li').each(function(){  
      ($(this).text().search(new RegExp(val, "i")) === -1) 
             ? $(this).hide() : $(this).show();
      });  
    });
  });
  </script>


  <input type="text" id="search" />
  <ul class="list">
    <li>Australia</li>
    <li>Bangladesh</li>
    <li>India</li>
    <li>New Zealand</li>
    <li>Pakistan>
    <li>South Africa</li>
    <li>Sri Lanka</li>
    <li>West Indies</li>
  </ul>

Here is a working demo

  • Australia
  • Bangladesh
  • India
  • New Zealand
  • Pakistan
  • South Africa
  • Sri Lanka
  • West Indies

Jun 6, 2015

Check uncheck all checkboxes with jQuery

BY Altaf Hussain IN No comments

Use jQuery prop function to check or uncheck form check boxes.
Its javascript version is available in another article named Check uncheck all checkboxes with javascript

The Html Form

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">  </script>
  <script type="text/javascript">
  $(document).ready(function(e) {
    $('#checkAll').click(function(){
       $('input:checkbox').prop('checked', this.checked);  
    });
  });
  </script>
  
  <form name="checkForm">
  <input type="checkbox" id="checkAll">Check/Uncheck All
  <input type="checkbox" name="one" value ="one">One
  <input type="checkbox" name="two" value ="two">Two
  <input type="checkbox" name="three" value ="three">Three
  <input type="checkbox" name="four" value ="four">Four
  <input type="checkbox" name="five" value ="five">Five
  </form>

The Example

Check/Uncheck All
One
Two
Three
Four
Five