Tuesday, December 17, 2013

Install varnish on centos 6

Varnish is a web application accelerator. You install it in front of your web application and it will speed it up significantly.

To install varnish on CentOS 6, Install the Varnish Yum repository

rpm -Uvh http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release/varnish-release-3.0-1.noarch.rpm

Now Install varnish

yum install varnish

Enable varnish at startup

chkconfig varnish on

Now start varnish

service varnish start

Thursday, September 26, 2013

MySQL distance calculation in kilometers or miles

To calculate distance in MySQL, use the following query

The distance is in kilometers.

SELECT *,
6371 * acos( cos( radians($user_latitude) ) * cos( radians( latitude ) ) * cos( radians ( longitude ) - radians($user_longitude) ) + sin( radians($user_latitude) ) * sin( radians ( latitude ) ) )  as 'distance'
FROM locations
HAVING distance < 10;

To calculate distance in miles instead of kilometers

replace 6371 woth 3959

SELECT *,
6371 * acos( cos( radians($user_latitude) ) * cos( radians( latitude ) ) * cos( radians ( longitude ) - radians($user_longitude) ) + sin( radians($user_latitude) ) * sin( radians ( latitude ) ) )  as 'distance'
FROM locations
HAVING distance < 10;

Note: $user_latitude and $user_longitude is PHP variables containing user lat and lon, change them as you wish.

Sunday, September 15, 2013

Installing webmin on centOS using yum

create a file /etc/yum.repos.d/webmin.repo

[Webmin]

name=Webmin Distribution Neutral

#baseurl=http://download.webmin.com/download/yum

mirrorlist=http://download.webmin.com/download/yum/mirrorlist
enabled=1

paste the above content into the new file.
save the file.

Now enter "yum install webmin"

You are done

How to see disk space usage in linux

Estimating file space usage, we use "du" command.
du summarize disk usage of each file, recursively for directories.
It can be used with folders to get the total disk usage.

To see usage of /var/ we can run the following command

du /var/

The above command will show you the usage of each file in /var/ directory

If you add -c argument, it will include total size at the end.

du /var/ -c

If you want to see the summary only, use -s argument

du /var/ -s

If you want to see the size in human readable format, include -h argment

du /var/ -sh

du /var/ -h

du /var/ -ch

By default du dont include files which is not consuming any space.
If you want to include all files, use -a argument

Monday, August 5, 2013

Improve MySQL performance with Query Cache

If you want to optimize and speed up responses from MySQL, The MySQL query cache can help.

To enable query cache set the follwoing global variable

mysql> SET GLOBAL query_cache_size = 16777216;

The query_cache_size is defined as Byte, the above command will setup cache size 16Mb:

Now set the cache option

mysql> query_cache_type=OPTION

Set the query cache type. Possible options are as follows:
0 : Don't cache results in or retrieve results from the query cache.
1 : Cache all query results except for those that begin with SELECT S_NO_CACHE.
2 : Cache results only for queries that begin with SELECT SQL_CACHE

You can see all query cache related variable

mysql> SHOW VARIABLES LIKE '%query_cache%';

If you want to enable query cache permanently, open the my.cnf
/etc/my.cnf (Red Hat) or /etc/mysql/my.cnf (Debian) file

Append the following config

query_cache_size = 268435456
query_cache_type=1
query_cache_limit=1048576

Now restart mysql

service mysqld restart

Monday, July 29, 2013

How to see disk space usage in linux

Sometimes we need to know the hard drive usage statistics.
In Linux we can use df command to find out how much disk space is used.

df command will show you Used and available space, File system mount point, File systen capacity, The number of inodes available,

The basic usage is follow:

df [options]

df [options] /path_to_dev

Example

df

df -h

The following command will show information only for the /home directory partiton

df /home

df -h /home

use Option -T to see file system type

df -T


Below is the list for df command options

  -a, --all             include dummy file systems
  -B, --block-size=SIZE  use SIZE-byte blocks
      --total           produce a grand total
  -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G)
  -H, --si              likewise, but use powers of 1000 not 1024
  -i, --inodes          list inode information instead of block usage
  -k                    like --block-size=1K
  -l, --local           limit listing to local file systems
      --no-sync         do not invoke sync before getting usage info (default)
  -P, --portability     use the POSIX output format
      --sync            invoke sync before getting usage info
  -t, --type=TYPE       limit listing to file systems of type TYPE
  -T, --print-type      print file system type
  -x, --exclude-type=TYPE   limit listing to file systems not of type TYPE

Sunday, July 28, 2013

Enable mod_rewrite for apache in Ubuntu

For clean URL we need to use .htaccess and mod rewrite.
In ubuntu we can apply the following command to enable the rewrite modules.

sudo a2enmod rewrite

Change all occurrence of "AllowOverRide None" to "AllowOverRide All".

sudo vi /etc/apache2/sites-available/default

Now restart apache

/etc/init.d/apache2 restart

You are done.

Tuesday, July 23, 2013

CentOS Sync time from NTP server

First we need to install NTP service.

To install NTP

yum install ntp

Now Synchronize the system clock with pool.ntp.org server

ntpdate pool.ntp.org

You can auto start NTP service during system startup

chkconfig ntpd on

Finally Start the NTP service

service ntpd start

Monday, July 22, 2013

Delete files older than x days/minutes in Linux

Sometimes we need to delete older than x-days. In Linux we can use following command

find /path/ -mtime +5 -exec rm {} \;

The above command will delete all file older than 5 days.

If we need to remove all files older than 10 minutes, apply the following command

find /path/ -mmin +10 -exec rm {} \;

The above command will delete all files older than 5 minutes.

If you don't want to delete files but see the list, use ls instead of rm.

find /path/ -mtime +5 -exec ls {} \;

The above command will list all files older than 5 days.

Note that there are spaces between rm, {}, and \; 

Wednesday, July 17, 2013

Linux Basic - Part 2


If you want to see path settings in linux, apply the following command

echo $PATH

it will show current path settings.

if you run echo * command, it will show a list of files/folder in current directory.

Now if you want to see a list of files/folders which are starting with D, apply echo D*

If you need to see a list of files/folders which are ending with s, apply echo *s

If you need to know your home directory location, use echo ~

If you apply echo /usr/*/share it will show you a list which location match /usr/ then anything then /share

If you want to do match in linux terminal, use as echo $((2+2)) Here You must start with $(( and end with ))

You can also do a long match like echo $(( ((2+2)-(1+1))*2 )) it will output 4

Tuesday, July 16, 2013

Using alias in Linux - Temporary and Permanent

Sometimes we need to make alias of command or group of command including of any options, arguments and redirection.
We can pre-set a string to run any command using alias.

alias command allows a user to create simple name for commands including arguments or options.

The format is alias name='command'

For example:

alias install="sudo apt-get install"
alias poweroff="sudo shutdown -h now"

After making alias of above command, you can install package with install command instead of sudo apt-get install and you can shutdown the PC with simple command poweroff.

we can also use alias ls="ls -al" to create a alias of ls command with -al option.
now if we run ls command it will execute ls -al command.
Now if we need to run original ls command we can by adding a backslash before the command
example \ls it will execute the original ls command.

If we need to create permanent alias we have to put alias in .bashrc file under user home directory.

The alias should be typed in below the line that says # User specific aliases and functions.

System-wide aliases can be put in the /etc/bashrc file.

System must be restarted after creating permanent alias.

Monday, July 8, 2013

Changing Hostname in Linux

To see current hostname

# hostname

change hostname temporary

# hostname myhostname.com

Now see new hostname

# hostname

To change the hostname permanently

# vi /etc/sysconfig/network

change the hostname variable then save and exist

Now you have add your new hostname into /etc/hosts file

# vi /etc/hosts

Add your new hostname and If you want you can remove the old hostname from the file

Then save and exist

Now its time to restart your server.

You can also try restarting network service

In Ubuntu/Debian

# service network restart

In CentOS/Red Hat/Fedora

# service networking restart


If you want you can use GUI tool too

# system-config-network

And now follow the wizard

Finally Restart the server

Thursday, July 4, 2013

Install Apache MySQL PHP (LAMP stack) in ubuntu

LAMP stack is a group of Apache, MySQL, PHP for Linux.

To install LAMP stack you will need to have root privileges.
To get root privileges

sudo su

OR use sudo before every command

First we are updating apt-get databases

sudo apt-get update

Now we are installing apache 2 package

sudo apt-get install apache2

Apache is installed now. Try to access your website using localhost or server IP address.

To get the server IP

ifconfig eth0 | grep inet | awk '{ print $2 }'

Now we can proceed to install PHP 5

sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

It may ask you some question, choose yes and continue

You can install other PHP externsions/modules which you need.
To see a list of PHP modules

apt-cache search php5-

It will show you a list of php5 moudules

Then you can install php5 modules as follow

sudo apt-get install name_of_the_module name_of_the_module_2 ....

You can install multiple modules by separating the name of each module with space

Now It's time to install MySQL

sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

You will be asked to give a mysql root password. You can also set mysql root password later.

Once MySQL has been installed, we should activate it with this command

sudo mysql_install_db

Now Finish up it by running MySQL set up script

sudo /usr/bin/mysql_secure_installation

You will be asked to enter current mysql root password.
Then the prompt will ask you if you want to change the root password.
Choose N if you dont want to change current password and proceed to next step.
Complete all the steps and now you are done.

Start MySQL and Apache

sudo service mysql start
sudo service apache2 start

If its already started, restart them

sudo service mysql restart
sudo service apache2 restart

Wednesday, July 3, 2013

Resetting MySQL root Password

If you forget MySQL Root Password, you can reset it.
You need to have root access on the system OS (CentOS, Ubuntu etc..)

First Stop the current MySQL process.

service mysqld stop

OR

/etc/init.d/mysql stop

After stopping the current MySQL process, Start MySQL in safe mode with --skip-grant-tables option

mysqld_safe --skip-grant-tables &

After running MySQL in safe mode, you can run mysql update statement to change the root password.

Connect to MySQL server with root user

mysql -u root

Select/Connect to mysql database

use mysql;

Update root password

update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';

Flush MySQL privileges

flush privileges;

Exit MySQL

quit

You are all done now.

Stop MySQL safe mode instance

/etc/init.d/mysql stop

OR

service mysqld stop

Now It's time to start MySQL in normal mode.

service mysqld start

OR

/etc/init.d/mysql start

Sunday, June 30, 2013

Controlling Linux Processes

In my last post, I discussed about viewing linux processes.
Today you will learn how to controll linux processes.

If you run command from terminal, It is attached process OR foreground process.

for exiting from current process, Press Control+C (Ctrl+C). The process will be terminated.

For example, run gedit command. it will open gedit program and this program is attached with your current terminal.
You can not do anything in this terminal without closing this program. So, for closing this program Press Ctrl+C into your terminal.
The running gedit program is closed now.

If you need to run any program in background or detached from the terminal, you can add "&" at the end of the command.
For Example: run "gedit &". it will open gedit and it is detached from the terminal. The terminal should show you it's pid number which you need to close this program.


if you have run multiple background program using "&" and if you need to see all jobs, run "jobs" command.
it will show you all jobs with it's job ID.
Now if you want to attach the terminal to the 2nd program, run fg %2
If you want to attach the terminal to the 1st program, run fg %1
After attaching the program you can simply press Ctrl+C to terminate the program.

Similary you can use bg %1 to move foreground to background.
run gedit and then Press Ctrl+Z. it will stop the program but will not terminate. then move it to background with bg command.


If you want to kill a program run kill [pid]. example kill 4935 here 4935 is the process id.
You can also use job id to kill a program. example: kill %1


To see process and it's pid use ps [ Check my previous post for details ]

To terminate all instances of a program, run "killall programName". example: killall gedit
it will terminate all gedit instances.

Sunday, June 16, 2013

How to view Linux processes

Today I will discuss about Linux processes.

In linux every process has a uniq process ID (PID).
When Linux boot, some processes run in background.
and /sbin/init this is a process which run when the OS boot before any other process
and then daemons process run. /sbin/init this process ID (PID) is always 1.

If we want to see current running process we can do it with the following command

ps

The above command will show only process from current user.
if you want to see all processes, use following command

ps -A

If you want to see all details of each process, run the followinc command

ps aux

if you want to see process from specific user, like process run by user sadiq

ps -u sadiq

If you want to see real-tim view of running process, you can use top command

top

use q or Esc for exiting top program. you can use htop for more interactive result.
htop is not installed by default. you need to install it. For installing htop, you need to install EPEL repository first.

For Redhat / CentOS 5
32bit:
rpm -Uvh http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
64bit:
rpm -Uvh http://download.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm

for Redhat / CentOS 6
32bit:
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
64bit:
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

And then run the following command to install htop

yum install htop

When htop is installed, use htop command to see top process list.

Follow my next post to learn about controlling processes.

Thursday, June 13, 2013

Installing APC (Alternate PHP Cache) on CentOS / Redhat


First we need the pecl command to install apc.

run the following command

yum install php-pear

And we need the following command for phpize command

yum install php-devel

We also need to run the following command to enable apxs command

yum install httpd-devel

And then run the following command

yum install pcre-devel

For Debian User

aptitude install libpcre3-dev


And then finally run the pecl install command

pecl install apc


Once finishes, we need to enable apc in apache configuration, run the following command to include extension into php.ini

echo "extension=apc.so" > /etc/php.d/apc.ini


Then restart the apache

service httpd restart

That's it. Now enjoy and watch for less execution time compared to before.

Wednesday, June 5, 2013

Freeing up memory cache for Linux OS

The Linux OS has very good RAM management system.
It free up cached memory when it needs.
But if you need to free up memory by force, you
can apply following command

sync; echo 3 > /proc/sys/vm/drop_caches

If you want to do it automatically, run a cron job to do it automatically.

create a file "clearmemory.sh" with the following content

#!/bin/sh
sync; echo 3 > /proc/sys/vm/drop_caches


Then create a cronjob like

0 * * * * /root/clearcache.sh

It will now run every hour and clear memory cache.

Tuesday, May 28, 2013

Adding a website into android home screen

Adding a website into android home screen is very easy.

Bookmark the website.
on your android home screen, long-press in an empty space.
"Add to Home Screen will appear"
Select "Shortcuts"
select "Bookmarks"
select your bookmarked website.



Standard bookmark image of the site’s favicon will be used as the icon.
If the following tag available in the <head>
<link rel="apple-touch-icon-precomposed" href="image-source" />
then source image will be used. Both the iPhone and Android support the apple-touch-icon-precomposed link rel-type.

Another way

Bookmark the website.
Long-press the bookmark.
Select “Add to Home screen”


For web developer, It is best practice to add <link rel="apple-touch-icon-precomposed" href="image-source" /> in the <head> So that reader can add your site into their home screen. iOS and Android both support this "apple-touch-icon-precomposed" rel-tyupe.

Monday, May 27, 2013

Linux Basic - Part 1

If you want to change your current directory, you can use "cd" command.
cd your_destination_directory
changing directory to root directory
cd /
changing directory to your home directory
cd home/your_user_name
OR you can use ~ for changing to your home directory
cd ~
it will change your current directory to your home directory.
if you need to change directory to your "documents" directory /home/you/documents, you can command
cd ~/documents
you can quickly come back to your home directory from any directory
cd ~