Thursday, August 11, 2011

Apache Module mod_cache

The following content will help you guys on how to enable caching on specific folders on your server.

There is module called cache_module in apache modules, first we need to enable it.

mod_cache implements an RFC 2616 compliant HTTP content cache that can be used to cache either local or proxied content. mod_cache requires the services of one or more storage management modules. Two storage management modules are included in the base Apache distribution:

mod_disk_cache
implements a disk based storage manager.
mod_mem_cache
implements a memory based storage manager. mod_mem_cache can be configured to operate in two modes: caching open file descriptors or caching objects in heap storage. mod_mem_cache can be used to cache locally generated content or to cache backend server content for mod_proxy when configured using ProxyPass (aka reverse proxy)

Sample httpd.conf

#
# Sample Cache Configuration
#
LoadModule cache_module modules/mod_cache.so


#LoadModule disk_cache_module modules/mod_disk_cache.so
# If you want to use mod_disk_cache instead of mod_mem_cache,
# uncomment the line above and comment out the LoadModule line below.

CacheRoot c:/cacheroot
CacheEnable disk /
CacheDirLevels 5
CacheDirLength 3


LoadModule mem_cache_module modules/mod_mem_cache.so

CacheEnable mem /
MCacheSize 4096
MCacheMaxObjectCount 100
MCacheMinObjectSize 1
MCacheMaxObjectSize 2048


# When acting as a proxy, don't cache the list of security updates
CacheDisable http://security.update.server/update-list/


Tuesday, July 19, 2011

How to write Robots.txt file

In a text editor, open a file named robots.txt. Note that the name must be all lower case, even if your Web pages are hosted on a Windows Web server. You'll need to save this file to the root of your Web server. For example:

http://www.mydomain.com/robots.txt

The format of the robots.txt file is

User-agent: robot

Disallow: directories/folders or files #Google will not crawl the named files / folders here

You can use wildcards to indicate all robots, or all robots of a certain type. For example:
To specify all robots:

User-agent: *

To specify all robots that start with the letter A:

User-agent: A*

The disallow lines can specify files or directories:
Don't allow robots to view any files on the site:

Disallow: /

Don't allow robots to view the index.html file

Disallow: /index.html

If you leave the Disallow blank, that means that all files can be retrieved, for example, you might want the Googlebot to see everything on your site:

User-agent: Googlebot

Disallow:

If you disallow a directory, then all files below it will be disallowed as well.

Disallow: /norobots/

You can also use multiple Disallows for one User-agent, to deny access to multiple areas:

User-agent: *

Disallow: /cgi-bin/

Disallow: /images/

You can include comments in your robots.txt file, by putting a pound-sign (#) at the front of the line to be commented:

# Allow Googlebot anywhere

User-agent: Googlebot

Disallow:

Robots follow the rules in order. For example, if you set googlebot specifically in one of your first directives, it will then ignore a directive lower down that is set to a wildcard.

# Allow Googlebot anywhere

User-agent: Googlebot

Disallow:

# Allow no other bots on the site

User-agent: *

Disallow: /

Tuesday, July 5, 2011

Create virtual hosts in PHP with apache

Virtual hosts are mainly used to run different applications with in a single Apache server by creating different port numbers in Apache configuration.

The default port number that Apache will run is 80.

Now we will see the steps to create virtual hosts in both Windows and Linux.
Both the cases assume that we have installed wamp on D:/wamp
Creating virtual hosts Windows box

1)Go to the directory: D:\wamp\bin\apache\Apache2.2.17\conf
2) There you will find apache.conf file, open it in any text editor.
3) In that file you will be having 2 lines
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

Now you need to include the virtual hosts file as like below.
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

4) Now Open file conf/extra/httpd-vhosts.conf in any text editor.
In that file you will be having lines like below
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

So name your virtual host below these line like below
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
NameVirtualHost *:8001

5) Then you need to set up directory of your new site. By default for virtual host 80 the following code will work, the code block follows below.

ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/httpd-2.2-x64/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common


Then we need to copy and paste the same block and make changes like below.

ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/httpd-2.2-x64/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common


ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "D:/wamp/www/"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/mydomain.com-error.log"
CustomLog "logs/mydomain.com-access.log" common


This process will finishes the creating of your first virtual host.

6) Restart your Apache server and try it in your browser like http://localhost:8001/

Creating virtual hosts Linux box

Sunday, June 12, 2011

Writing a .htaccess file

In this post i am going to give examples of commonly used examples of .htaccess basics.

1) HTTP Authentication for Password protected directories
AuthType Basic
AuthName "Password Required"
AuthUserFile /www/passwords/password.file
AuthGroupFile /www/passwords/group.file
Require Group admins

2) Change default directory page ( index page ) using htaccess
This directory indexing is useful when ever you wish to change the order of parsing file. example if you want to change the default parsing orders to index.php not index.html,, you can keep the following code in your .htaccess file.

DirectoryIndex index.php index.html index.shtml index.txt

If tou browser your site http://www.yourdomain.com/, then it will search for index.php, if index.php not exists then it will searches for index.html.

Note: If no files(index.php index.html index.shtml index.txt) are existed then it will display the directory and its contents. In order to prevent this we can write a code as below.

DirectoryIndex index.html index.txt /cgi-bin/index.pl

Would cause the CGI script /cgi-bin/index.pl to be executed if neither index.html or index.txt existed in a directory.

3) Allow/Deny Directory Browsing

When directory browsing is on, people accessing a URL from your site with no index page or no pages at all, will see a list of files and folders. To prevent such directory access, just place the following line in your .htaccess file.

IndexIgnore */*
OR
Options -Indexes

Many hosting companies, by default deny directory browsing and having said that, just in case you need to enable directory browsing, place the following line in your .htaccess file.

Options +Indexes

4) Redirect visitors from one page or directory to another

It’s quite simple. Look at the example lines below and place similar lines in your .htaccess file of the root folder and it will do the rest. [Remember to use permanent keyword in the line to tell the search engines that the old link has moved to the new link]. You can also setup multiple redirects using htaccess.

Syntax: Redirect permanent [old directory/file name][space][new directory/file name]

Redirect permanent /olddirectory /newdirectory
Redirect permanent /olddirectory /somedirectory/newdirectory
Redirect permanent /oldhtmlfile.htm /newhtmlfile.htm
Redirect permanent /oldhtmlfile.htm http://your-domain.com/newhtmlfile.htm

All the above lines are valid. Just remember to replace the file/directory names with actual ones.

301 Redirection
This example will redirects you from non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteRule (.*) http://www.mydomain/$1 [R=301,L]

5) Change the default index page of a directory or site

Almost every hosting company will have index.htm, index.html, index.php, index.asp, default.asp, default.html as the default index page names in their web server settings. So, in case your site or directory does not has a file name which matches a name from the list above, chances are that your visitors will either see a list of all the files and folders [through directory browsing] or will not see anything at all. To change the default index page’s name for a directory or the site, place the following line in the .htaccess file of the root folder or the particular directory for which you want to change the index page’s name.

DirectoryIndex homepage.htm
DirectoryIndex somepage.htm

To have more names, put a space between file names and it will take into considerations all those file names as possible index page names. Which means, if it finds a filename matching a list of names you supplied [in the given order] in .htaccess, then it will open that page as the index page for the directory. The below line, with multiple names, is also a valid usage:

DirectoryIndex homapage.html somepage.html myindexpage.html anything.html

6) Preventing hot linking of images from your website

If your website contains images which people from other websites are linking to and you get charged for the extra bandwidth, then placing the following lines will prevent any such image hot linking. Most of the hosting companies provide this feature in their control panel itself, such as CPanel. This trick requires mod_rewrite engine to be on in Apache on your web server.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?your-domain.com/.*$ [NC]
RewriteRule .(gif|jpg)$ – [F]

In the above code, replace [your-domain] with your actual domain name [without www], and instead of (www.\), use your actual subdomain name (sub-domain.\)

7) Prevent access to your .htaccess file (.htaccess security)

To prevent visitors from viewing your .htaccess file, place the following lines in your file. Of course, by default most Apache installations will not show .htaccess file but just in case.

<files .htaccess>
order allow,deny
deny from all
</Files>

8) Custom Error Pages

The .htaccess file will cover custom error pages. These will allow you to have your own, personal error pages (for example when a file is not found) instead of using your host's error pages or having no page. This will make your site seem much more professional in the unlikely event of an error. It will also allow you to create scripts to notify you if there is an error (for example I use a PHP script on Free Webmaster Help to automatically e-mail me when a page is not found).

You can use custom error pages for any error as long as you know its number (like 404 for page not found) by adding the following to your .htaccess file:

ErrorDocument errornumber /file.html

For example if I had the file notfound.html in the root direct
ory of my site and I wanted to use it for a 404 error I would use:

ErrorDocument 404 /notfound.html

If the file is not in the root directory of your site, you just need to put the path to it:

ErrorDocument 500 /errorpages/500.html

These are some of the most common errors:

401 - Authorization Required
400 - Bad request
403 - Forbidden
500 - Internal Server Error
404 - Wrong page

Then, all you need to do is to create a file to display when the error happens and upload it and the .htaccess file.

9) Deny/Allow Certian IP Addresses

In some situations, you may want to only allow people with specific IP addresses to access your site (for example, only allowing people using a particular ISP to get into a certian directory) or you may want to ban certian IP addresses (for example, keeping disruptive memembers out of your message boards). Of course, this will only work if you know the IP addresses you want to ban and, as most people on the internet now have a dynamic IP address, so this is not always the best way to limit usage.

You can block an IP address by using:

deny from 000.000.000.000

where 000.000.000.000 is the IP address. If you only specify 1 or 2 of the groups of numbers, you will block a whole range.

You can allow an IP address by using:

allow from 000.000.000.000

where 000.000.000.000 is the IP address. If you only specify 1 or 2 of the groups of numbers, you will allow a whole range.

If you want to deny everyone from accessing a directory, you can use:

deny from all

but this will still allow scripts to use the files in the directory.





------ Contineous............

Saturday, June 11, 2011

Creating and Editing crontab Files

cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs in unix , solaris. Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.

The following example shows how to determine if an editor has been defined, and how to set up vi as the default.

$ which $EDITOR
$ EDITOR=vi
$ export EDITOR


When you create a crontab file, it is automatically placed in the /var/spool/cron/crontabs directory and is given your user name. You can create or edit a crontab file for another user, or root, if you have superuser privileges.

This document covers following aspects of Unix cron jobs
1. Crontab Restrictions
2. Crontab Commands
3. Crontab file – syntax
4. Crontab Example
5. Crontab Environment
6. Disable Email
7. Generate log file for crontab activity

1. Crontab Restrictions
You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use
crontab if your name does not appear in the file /usr/lib/cron/cron.deny.
If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.

2. Crontab Commands

export EDITOR=vi ;to specify a editor to open crontab file.

crontab -e Edit your crontab file, or create one if it doesn’t already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)

3. Crontab file
Crontab syntax :
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)

* in the value field above means all legal values as in braces for that column.
The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).
Notes
A. ) Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.

B.) The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .

4. Crontab Example
A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.

30 18 * * * rm /home/someuser/tmp/*

Changing the parameter values as below will cause this command to run at different time schedule below :
min hour day/month month day/week Execution time
30 0 1 1,6,12 * – 00:30 Hrs on 1st of Jan, June & Dec.
0 20 * 10 1-5 –8.00 PM every weekday (Mon-Fri) only in Oct.
0 0 1,10,15 * * – midnight on 1st ,10th & 15th of month
5,10 0 10 * 1 – At 12.05,12.10 every Monday & on 10th of every month
:

Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.

5. Crontab Environment
cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh).
cron supplies a default environment for every shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh

Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry.

6. Disable Email
By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .

>/dev/null 2>&1

7. Generate log file
To collect the cron execution execution log in a file :

30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log

Sunday, May 1, 2011

How to enable mod_rewrite in apache web server

How to enable mod_rewrite in apache web server

In Order to enable the clean urls and .htaccess, you need to enable the mod_rewrite in Apache server.

Before enabling clean urls, the Druapl Admin will look like below.
http://www.anji.com/?q=admin

After Enabling, the url looks like below
http://www.anji.com/admin
In Linux
If you want to enable mod_rewrite, then open your terminal and type the following command

sudo a2enmod rewrite


After execution of above command, then restart your apache server.

Use the following command to restart your apache web server.

sudo /etc/init.d/apache2 restart


You can see what modules are loaded by using the following command
sudo apache2ctl -l / sudo apache2ctl -M

In Windows
1) Click on WAMP server which is in system tray.
2) In that drag your mouse to Apache-> Apache module
3) It will give you list of modules of Apache, click on rewrite_module and restart your Apache server.

Saturday, February 19, 2011

How to take MySQL database dumps in Windows

If we want to take mysqldump in windows, then use the following syntax.

path to mysqldump execute file -h hostname -u username -ppassword > path to .sql file
For example if you installed WAMP/XAAMP server in E: drive then the mysqldump syntax will follows like below.

E:\wamp\bin\mysql\mysql5.1.36\bin>mysqldump -u root news_admin > E:/anji.sql

To restore mysql dump in the windows then use the following syntax.
Example:
E:\wamp\bin\mysql\mysql5.1.36\bin>mysql -u root newsd < E:/anji.sql

Monday, February 14, 2011

How to add Indexing to a Table in MySQL

Indexing a MySQL table is needed for fast search.
The below syntax is used to add index to a mysql table. Here i am assuming the table name is `product` and the `product_id` is a primary key in that mysql table.

alter table `product` add index `product_id`

How to change the autoincrement value in a mysql table

Initially when we create a new table with auto increment filed, the values start from 1. After some time if we want to set auto increment value to a different value, then we will use the following command in mysql.

alter table members  Engine=MyISAM checksum=1 auto_increment=1000 comment='' delay_key_write=1 row_format=dynamic charset=latin1 collate=latin1_swedish_ci

Saturday, February 12, 2011

How to take MySQL database dumps in Terminal.

Use the following command to do MySQL dump of your database:
 mysqldump -h <hostname> -u <database user name> -p<Database Password> <database name> > <path><filename.sql>

How to restore MySQL dump into our current Database:
mysql -h <hostname> -u<database  user name> -p<Database password> <Database Name> < <path/filename.sql>

How to take backup of only the table structure of MySQL Database:
 mysqldump -h <hostname> -u <database user name> -p<Database Password> <database name> --no-data > <path><filename.sql>

How to take backup of selected MySQL tables: mysqldump -h <hostname> -u <database user name> -p<Database Password> <database name> <tablename1> <tablename2> ..... <tablenameN>  > <path><filename.sql>