Tuesday, July 26, 2011

.htaccess in detail

The Apache web server has a number of configuration options that are available to the server administrator. In a shared hosting environment, you don’t have access to the main Apache configuration so you’re stuck with the default configuration. However, it is possible to override some of the default settings by creating (or editing) a file named “.htaccess”.

The .htaccess is a simple ASCII text file placed in your www directory or in a subdirectory of your www directory. You can create or edit this file in any text editor (such as NotePad) and then upload it to the directory for which you want to modify the settings. Be sure that the file is uploaded in ASCII (not BINARY) format, and be sure that the file permissions for the file are set to 644 (rw-r–r–). This allows the server to access the file, but prevents visitors from accessing the file through their web browser (a security risk.)
Commands in the .htaccess file affect the directory that it’s placed in and all subdirectories. If you place the .htaccess file in your www directory, it will affect your entire web site. If you place it in a subdirectory of your www directory, it will affect only that directory plus and subdirectories of that directory.

Most .htaccess commands are designed to be placed on one line. If your text editor wraps lines automatically, you should disable that function before saving and uploading your file. Also, note that .htaccess commands are case-sensitive.

The information presented here may work and it may not, or it may work today and not tomorrow. Use it at your own risk.
Some of the things you can do with .htaccess include:

Customize Error Messages

If you want to override the server’s error pages, you can use .htaccess to define your own messages. An example of the syntax is:

ErrorDocument 500 /error.html

Override SSI Settings

By default, only pages ending in the .shtml extension will parse server-side includes (SSI). You can override this restriction in your .htaccess file:
If you want to override the default server configuration so that SSI will work with .html documents, you can create a file named .htaccess and upload it (in ASCII mode) to your main www directory. Add the following lines to your .htaccess file:
 
AddType text/html .html
 
ddHandler server-parsed .html

If you want both .html and .htm documents to parse SSI, create your .htaccess file with these lines:
 
AddType text/html .html
 
AddHandler server-parsed .html
 
AddHandler server-parsed .htm

Change Your Default Home Page

In order to browse your site by specifying the domain name only (e.g., http://www.yourdomain.com) instead of having to specify an exact page filename (e.g., http://www.yourdomain.com/filename.html), you must have an index page in your www directory. Default acceptable file names for index pages include index.htm, index.html, index.cgi, index.shtml, index.php, etc. Note that they’re all named index.*.

There is also a default order of precedence for these names. So if you have both a file named index.cgi and a file named index.html in your directory, the server will display index.cgi because that name takes a higher precedence than index.html.

Using .htaccess, you can define additional index filenames and/or change the order of precedence. To define your index page as custom.html add the following line to your .htaccess file:

DirectoryIndex custom.html
 
This will cause the server to look for a file named custom.html. If it finds that file, it will display it. If it does not find that file, it will return a 404 Missing Page error.
 
To change the order of precedence, enter a DirectoryIndex command with multiple file names on the same line. The order in which the file names are listed (from left to right) determines the order of precedence. For example,

DirectoryIndex custom.html index.cgi index.php index.html

Enable Directory Browsing

This is the option that allows the contents of a directory to be displayed in the browser when the directory does not contain an index page.
For example, if you make an http call to a directory such as http://yourdomain.com/images/, it would list all the images in that directory without the need for an html page with links.

If you require this option on specific directories it is still available. You can reactivate it by adding the following line to your .htaccess file:

Options +Indexes
 
Once this is added, the directory will fully index again. (Note: Coversely “Options -Indexes” will prevent directory browsing.)

Preventing Directory Listing

Do you have a directory full of images or zips that you do not want people to be able to browse through? Typically a server is setup to prevent directory listing, but sometimes they are not. If not, become self-sufficient and fix it yourself:

IndexIgnore *
 
The * is a wildcard that matches all files, so if you stick that line into an htaccess file in your images directory, nothing in that directory will be allowed to be listed.

On the other hand, what if you did want the directory contents to be listed, but only if they were HTML pages and not images? Simple says I:

IndexIgnore *.gif *.jpg
 
This would return a list of all files not ending in .jpg or .gif, but would still list .txt, .html, etc.

Block Users from Accessing Your Web Site

If you want to deny access to a particular individual, and you know the IP address or domain name that the individual uses to connect to the Internet, you can use .htaccess to block that individual from your web site.

<Limit GET>
 
order deny,allow
 
deny from 123.456.789.000
 
deny from 456.78.90.
 
deny from .aol.com
allow from all
 
</Limit>

In the example above, a user from the exact IP number 123.456.789.000 would be blocked; all users within a range of IP numbers from 456.78.90.000 to 456.78.90.999 would be blocked; and all users connecting from America Online (aol.com) would be blocked. When they attempted to browse your web site, they would be presented with the 403 Forbidden (“You do not have permission to access this site”) error.

Redirect Visitors to a New Page or Directory

Let’s say you re-do your entire web site, renaming pages and directories. Visitors to the old pages will receive the 404 File Not Found error. You can solve this problem by redirecting calls to an old page to the new page. For example, if your old page was named oldpage.html and that page has been replaced by newpage.html, add this line to your .htaccess file:
 
Redirect permanent /oldpage.html http://www.mydomain.com/newpage.html

Of course, you want to replace mydomain.com with your actual domain name. Now, when the visitor types in http://www.mydomain.com/myoldpage.html, they will be automatically redirected to http://www.mydomain.com/mynewpage.html.

If you’ve renamed a directory, you can use one redirect line to affect all pages within the directory:
 
Redirect permanent /olddirectory http://www.mydomain.com/newdirectory/

Note that the old page or directory is specified using the system path relative to your www directory, while the new page or directory is specified by the absolute URL.

Prevent Hot Linking and Bandwidth Leeching

What if another web site owner is stealing your images and your bandwidth by linking directly to your image files from his/her web site? You can prevent this by adding this to your .htaccess file:
 
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ - [F]

Replace mydomain.com with your actual domain name. With this code in place, your images will only display when the visitor is browsing http://mydomain.com. Images linked from other domains will appear as broken images.

If you’re feeling particularly nasty, you can even provide an alternative image to display on the hot linked pages — for example, an image that says “Stealing is Bad … visit http://mydomain.com to see the real picture that belongs here.” Use this code to accomplish that:
 
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ http://www.mydomain.com/dontsteal.gif [R,L]

This time, replace mydomain.com with your domain name, and replace dontsteal.gif with the file name of the image you’ve created to discourage hot linking.

Prevent viewing of .htaccess or other files

To prevent visitors from seeing the contents of your .htaccess file, place the following code in the file:
 
<Files .htaccess>
order allow,deny
deny from all
</Files>

If you want to prevent visitors from seeing another file, just substitute that file’s name for .htaccess in the Files specification.

Eliminate Code Red and NIMDA Virus Attacks from your Access Log

Placing the below redirects in .htacess eliminates the logging problem without affecting your personalized error redirecting scripts.
 
redirect /scripts http://www.stoptheviruscold.invalid
redirect /MSADC http://www.stoptheviruscold.invalid
redirect /c http://www.stoptheviruscold.invalid
redirect /d http://www.stoptheviruscold.invalid
redirect /_mem_bin http://stoptheviruscold.invalid
redirect /msadc http://stoptheviruscold.invalid
RedirectMatch (.*)\cmd.exe$ http://stoptheviruscold.invalid$1

Access Control to your web files via .htaccess

Setting up access control using HTACCESS

There is an advantage to controlling access to certain parts of your domain. If, for instance, you wanted to make general information public, but only wanted to make specific information available to your customers you could use a feature of NCSA-based httpd servers commonly reffered to as HTACCESS.

Using this access control method you can limit access to certain branches of the directory tree. If you want to really understand how this works, nothing is better than reading the manual.

Basic Access Control

You can control access to your webpage two different ways, by host filtering or user authentication. But keep in mind that neither method is fullproof. This should be considered as secure as a courtesy lock on a restroom door; nice, but ultimately ineffective.

The default name of the access control file is .htaccess but that is not written in stone. In the server configuration overview we looked at a file called httpd.conf. This file had the following entry:
 
AccessFileName .htaccess

This is the default value, but any specified filename can be used. For the purposes of this tutorial I will refer to the .htaccess file by name, but your server may use a different file name.

The method of control is very simple. Place a correctly formated file called .htaccess in a directory and you can restrict access via the web to that directory. Here is a simple example of an .htacess file:
 
AuthUserFile /dev/null 
 
AuthGroupFile /dev/null
AuthName "This is NOT a restricted directory"
AuthType Basic

<Limit GET>


order allow,deny


allow from all


</Limit>

The first two lines refer to files that contain lists of users and groups. I will cover the specific format of the files and their use later. The AuthName entry is displayed in the message box if the browser needs to request a username / password. AuthType is always Basic because the advanced authorization methods based on Kerberos or MD5 are detailed enough for books themselves.

The important parts for now are contained in the familiar looking
tag. GET is the only widely supported method. PUT was under developement to allow uploading and while POST is partially supported, its use is too complex for this document. Basically, to retrieve ANY document from this directory via the web, the web server will evaluate the .htaccess file and allow or deny access based on the outcome. The above example file is wide open and will allow anyone access. Let’s look at a more restrictive <Limit> rule.
 
<Limit GET>
order deny,allow
deny from all
allow from linuxweblog.com
</Limit>

This rule will cause everyone to be denied EXCEPT hosts from linuxweblog.com. The server processes the rules in order and the first exception case is returned. Here is another way to look at it.
 
<Limit GET>
order allow,deny
deny from linuxweblog.com
</Limit>

By changing the order to allow,deny and changing the allow entry to deny we have created a ban list. Everyone EXCEPT linuxweb.com hosts can get documents from the directory.
 
<Limit GET>
order deny,allow
allow from all
deny from linuxweblog.com 192.168.10.
</Limit>

This rule set is evaluated the same as the one above it, but includes an additional deny rule for the 192.168.10. domain. The drawback to using a DNS name can be illustrated if the web server can not resolve an IP address to a domain name. If you rely completely on DNS names and DNS ever fails, you may find yourself locked out of your own site!

Host access control is the simplest way to control access, but what if you have a different ip address every time you log in and you don’t want to allow everyone from your domain access to the directory tree? I’m glad I asked that.

User Based Access Control

The most effective method of access restriction is the use of a username and password. By using two additional files, people can be granted access either by username or group membership. These two files are conventionally called .htpasswd and .htgroup but they can be any name specified in the .htaccess file. I will refer to the conventional names, but feel free to change them on your site.

The .htpasswd file is a file that contains a list of usernames and encrypted passwords seperated by colons. Here is an example:
 
Bob:ZUvJgtVp77Vik
Ted:rBW8u1RJUr6eU
Carol:HwrJPys5u7NcM
Alice:UORwkyVbeWc6M

This is a list of a 4 user .htpasswd file. The format is similar to a standard Un*x /etc/passwd file and in fact the encryption method is compatible. So if you want, you can base the .htpasswd off of an actual modified Un*x /etc/passwd file. Here is an example of the .htgroup file:
 
Admin: Carol
Managers: Ted Carol
Staff: Bob Ted Carol Alice

The names of the groups are not special except as they are used. Using these files as examples, lets look at some new rule sets.
 
AuthUserFile /usr/local/etc/httpd/private/.htpasswd
AuthGroupFile /usr/local/etc/httpd/private/.htgroup
AuthName "This is a restricted directory"
AuthType Basic

<Limit GET>


order allow,deny


allow from all


require user Alice


require group Managers


satisfy any


</Limit>

In this case we have specified authorization user and group files and given a title to the message box. The rule will deny everyone EXCEPT Alice OR the group Managers. The satifisy element handles whether the rule is evalutated as a logical AND or OR. By default it is a logical AND. That means that without the “satisfy any” line it would assume “satisfy all” and require both user Alice and group Managers to access the directory. Since Alice is not a part of the Managers group NO ONE would have access to the directory. Let’s look at another one.
 
AuthUserFile /usr/local/etc/httpd/private/.htpasswd
AuthGroupFile /usr/local/etc/httpd/private/.htgroup
AuthName "This is a restricted directory"
AuthType Basic

<Limit GET>


order deny,allow


deny from all


allow from linuxweblog.com


require group Managers


satisfy all


</Limit>

This example combines both user and host validation. You have to supply a username that is in the Managers group AND be connecting from the linuxweblog.com domain.

Redirect browser to use SSL

You can redirect browser to use SSL secure port using .htaccess file with Rewrite Rules.
Create a .htaccess file with the below Rewrite rule.
 
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://secure.yourdomain.com%{REQUEST_URI} [NS,R,L]

Redirecting Dynamic URL using mod_rewrite

What do you do when you need to move servers or web-files to a different domain or directory, especially if you need to be moving dynamic content. How would you prevent down-time? This is not an end all solution, but Apaches’ module mod_rewrite comes to the rescue of redirecting URLs.
Below are the steps that was taken to move web-files to a different servers.

1. Create a temporary unused sub-domain to point to the new servers IP address.

2. Allow for a day before you migrate your content to let the subdomain resolve.

3. Setup rewrite rule to redirect your current domain to the temporary domain after migrating content.

4. Change the Primary and Secondary NameServers for the domain to point to the new location.

5. Keep the redirection up for a while until the NameServers are fully resolved.

Below is an example of what was used:
 
# this tells the web server to allow rewriting for this directory
RewriteEngine On                                                                             

# check the hostname to apply the redirection to


RewriteCond %{HTTP_HOST} domain.com [OR]


RewriteCond %{HTTP_HOST} www.domain.com
 
# describe the pattern to look for, and how to rewrite it


RewriteRule ^(.*)$ http://temp.domain.com/$1 [R]

All rewrite rules are contained in the .htaccess file. The rewrite rules cover all the files in the directory that contains the .htaccess file.

In general, each RewriteRule line specifies a pattern to look for, and a replacement text. The patterns can be very complicated — the rules have the full power of Unix Regular Expressions (ie. grep), but the example shown above will serve most people.

The “[R]” in the rewrite rule shown above tells the web server to redirect the user’s browser to the new URL. This is useful because the browser will show the new URL, and saving a bookmark will always lead to the new location.
Leaving the [R] off the line will also display the new URL, but a bookmark saved from the resulting page will continue to use the original (non-rewritten) URL. This would be useful if you want to preserve an easy-to-remember URL, but also want the ability to change it in the future.

Watermark images with mod_rewrite

Below is how I have watermarked images excluding thumbnails with the text “.thumb.” and “.highlight.” in the name of the image files.
Contents of “.htaccess” :
 
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !\.thumb\.|\.highlight\.
RewriteRule ^.*[Jj][Pp][Gg]$|.*[Gg][Ii][Ff]$|.*[Pp][Nn][Gg]$ watermark.php?%{REQUEST_FILENAME}

Contents of “watermark.php” :
 
<?php

// watermark.gif should have a transparent background.


$watermark = “watermark.gif”;


$image = $QUERY_STRING;
 
if (empty($image)) die();
 
if (!file_exists($image)) {


   header(“404 Not Found”);


   echo “File Not Found.”; die();


}
 
$outputType = getFileType($image);
 
watermark($image, $watermark, $outputType);
 
/**


   Outputs the image $source with $watermark in the lower right corner.


   @param $source the source image


   @param $watermark the watermark to apply


   @param $outputType the type to output as (png, jpg, gif, etc.)


                      defaults to the image type of $source if left blank


*/


function watermark($source, $watermark, $outputType=”") {


   $sourceType = getFileType($source);


   $watermarkType = getFileType($watermark);
 
   if (empty($outputType)) $outputType = $sourceType;


   if ($outputType == “gif”) $outputType = “png”; // Okay to remove


   header(“Content-type:image/$outputType”);
 
   // Derive function names


   $createSource = “ImageCreateFrom”.strtoupper($sourceType);


   $showImage = “Image”.strtoupper($outputType);


   $createWatermark = “ImageCreateFrom”.strtoupper($watermarkType);
 
   // Load original and watermark to memory


   $output = $createSource($source);


   $logo = $createWatermark($watermark);


   ImageAlphaBlending($output, true);
 
   // Find proper coordinates so watermark will be in the lower right corner


   $x = ImageSX($output) – ImageSX($logo);


   $y = ImageSY($output) – ImageSY($logo);
 
   // Display


   ImageCopy($output, $logo, $x, $y, 0, 0, ImageSX($logo), ImageSY($logo));


   $showImage($output);
 
   // Purge


   ImageDestroy($output);


   ImageDestroy($logo);


}
 
function getFileType($string) {


   $type = strtolower(eregi_replace(“^(.*)\.”,”",$string));


   if ($type == “jpg”) $type = “jpeg”;


   return $type;


}


?>

Reference by : http://rhcelinuxguide.wordpress.com/category/tuning-linux/

Monday, July 25, 2011

MySQL Optimization


Find max connection using the formula:- memory = keybuffer + (readbuffer + sort buffer ) max connections


Open /etc/my.cnf file in your favorite editor (eg: vi, pico etc)

max_connections=400
max_user_connections=30
key_buffer=256M (128MB for every 1GB of RAM)
myisam_sort_buffer_size=64M
join_buffer_size=1M
read_buffer_size=1M (1MB for every 1GB of RAM)
sort_buffer_size=1M (1MB for every 1GB of RAM)
table_cache=1500
thread_concurrency=2 (Number of CPUs x 2)
thread_cache_size=128M
wait_timeout=10
connect_timeout=5
max_allowed_packet=16M
max_connect_errors=1082.165.248.54190.212.44.109
query_cache_limit=1M
query_cache_size=32M (32MB for every 1GB of RAM)
query_cache_type=1

and restart mysql service by /etc/init.d/mysqld restart
Below are notes on some of the important variables in the my.cnf file , that is to be changed inorder to tweak mysql performance

1. query_cache_size:
*********************
*MySQL provides one feature that can prove very handy – a query cache. In a situation where the database has to repeatedly run the same queries on the same data set, returning the same results each time, MySQL can cache the result set, avoiding the overhead of running through the data over and over and is extremely helpful on busy servers.

2. key_buffer_size:
*******************
* The value of key_buffer_size is the size of the buffer used with indexes. The larger the buffer, the faster the SQL command will finish and a result will be returned. The rule-of-thumb is to set the key_buffer_size to at least a quarter, but no more than half, of the total amount of memory on the server. Ideally, it will be large enough to contain all the indexes (the total size of all .MYI files on the server).
* A simple way to check the actual performance of the buffer is to examine four additional variables: key_read_requests, key_reads, key_write_requests, and key_writes.
* If you divide the value of key_read by the value of key_reads_requests, the result should be less than 0.01. Also, if you divide the value of key_write by the value of key_writes_requests, the result should be less than 1.

3. table_cache:
*****************
* The default is 64. Each time MySQL accesses a table, it places it in the cache. If the system accesses many tables, it is faster to have these in the cache. MySQL, being multi-threaded, may be running many queries on the table at one time, and each of these will open a table. Examine the value of open_tables at peak times. If you find it stays at the same value as your table_cache value, and then the number of opened_tables starts rapidly increasing, you should increase the table_cache if you have enough memory.

4. sort_buffer:
***************
* The sort_buffer is very useful for speeding up myisamchk operations (which is why it is set much higher for that purpose in the default configuration files), but it can also be useful everyday when performing large numbers of sorts.

5. read_rnd_buffer_size:
**************************
* The read_rnd_buffer_size is used after a sort, when reading rows in sorted order. If you use many queries with ORDER BY, upping this can improve performance. Remember that, unlike key_buffer_size and table_cache, this buffer is allocated for each thread. This variable was renamed from record_rnd_buffer in MySQL 4.0.3. It defaults to the same size as the read_buffer_size. A rule-of-thumb is to allocate 1KB for each 1MB of memory on the server, for example 1MB on a machine with 1GB memory.

6. thread_cache:
******************
* If you have a busy server that’s getting a lot of quick connections, set your thread cache high enough that the Threads_created value in SHOW STATUS stops increasing. This should take some of the load off of the CPU.

7. tmp_table_size:
*******************
* “Created_tmp_disk_tables” are the number of implicit temporary tables on disk created while executing statements and “created_tmp_tables” are memory-based. Obviously it is bad if you have to go to disk instead of memory all the time.

8. query_cache_size
********************
Query caching has been introduced from MySQL 4 onwards. If your application executes a particular query again and again, MySQL can cache the result set, thereby avoiding the overhead of running through the data over and over and thereby increase the execution time.

You can enable query caching by setting the server variable query_cache_type=1 and setting the cache size in the variable query_cache_size. If either of the above is set to 0, query caching will not be enabled.
There are three status for query caching;

1. Disabled – query_cache_type = 0
2. Enabled – query_cache_type = 1
3. On Demand – query_cache_type = 2

Reference : http://linux.techzinformatica.in/?p=80

Apache Optimization


All the important configuration options are stored by Apache in a config file called httpd.conf that is located at /usr/local/apache/conf/httpd.conf. We will start by opening this file in your favorite text editor. For example:

vi /usr/local/apache/conf/httpd.conf

MaxClients 

Total number of concurrent connections.
Locate it in the configuration file. This should be set to a reasonable value. I suggest using this formula to determine the right value for your server.

MaxClients = 150 x RAM (GB)

So for example if you have 2 GB or RAM set this value to 300.
There is no reason for you to set it any higher unless you have a specific problem with this value. A high value can lead to a complete server hang in case of a DOS attack. A value too low can create timeout problems for your clients if the limit is reached.

ServerLimit

This value should be same as MaxClients

ServerLimit = 150 x RAM (GB)

MinSpareServers and MaxSpareServers 
 
MaxSpareServers and MinSpareServers control how many spare (unused) child-processes Apache will keep alive while waiting for more requests to put them to use. Each child-process consumes resources, so having MaxSpareServers set too high can cause resource problems. On the other hand, if the number of unused servers drops below MinSpareServers, Apache will fork (an expensive operation) new child-processes until MinSpareServers is satisfied.
Leave those values to:

MinSpareServers 5
MaxSpareServers 10

If you have more them 2 GB of RAM and you run a resource intensive website consider increasing MaxSpareServers.

MaxRequestsPerChild
 
Controls the number of request the a child serves before the child is killed. This should not be set too low as it will put an unnecessary load on the apache server to recreate the child. I suggest setting it to:

MaxRequestsPerChild 1000 for 1 GB RAM
10,000 for 2 GB and 0 for more than 2 GB RAM

KeepAlive and MaxKeepAliveRequests
 
KeepAlive provides long-lived HTTP sessions which allow multiple requests to be sent over the same TCP connection. In some cases this has been shown to result in an almost 50% speedup in latency times for HTML documents with many images, but having keepalive on is also a resource intensive setting.
Here comes the big question: To KeepAlive or not to KeepAlive?
Well the opinions are mixed here, some say to KeepAlive some say not to.

KeepAlive off

If you want to hear my option I would say NOT to KeepAlive if you are running a shared hosting business or if you want to get the most out of your hardware. You should KeepAlive only if the loading time of your pages is the most important factor in your business and you have the money to invest in a more powerful hardware. If you decide to KeepAlive I suggest you set MaxKeepAliveRequest low to something like 2 seconds.

StartServers 

Sets the number of child server processes created on startup. This setting depends greatly on the type of webserver you run. If you run low traffic websites on that server set it low to something like 5. If you have resource intensive websites on that server you should set it close to MaxClients.

StartServers 5

Timeout
 
The amount of time Apache will wait for three things: the total amount of time it takes to receive a GET request, The amount of time between receipt of TCP packets on a POST or PUT request, the amount of time between ACKs on transmissions of TCP packets in responses.

The default value is 300. You should set time to something a bit lower. A setting of 150 is probably ok. This will also help in case of small DOS attacks like to ones targeting some phpBB forums. Do NOT set it any lower then 10 as your users will start having timeout problems.

Timeout 150

After you have done all the necessary changes you can go ahead and restart Apache.

There is an extra step that you have to do so that the changes that you done to httpd.conf aren’t lost when a recompile is done.

To also save the changes in the database you will have to run:

/usr/local/cpanel/bin/apache_conf_distiller –update

You can check to see if the changes were accepted and will not be discarded at the next apache recompile by running

/usr/local/cpanel/bin/build_apache_conf

Sample values:

MinSpareServers 5
MaxSpareServers 10
ServerLimit 600
MaxClients 600
MaxRequestsPerChild 0
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 3

Timeout 30

Reference : http://linux.techzinformatica.in/?p=81

Install Fantastico on cPanel/WHM


You don’t need to download any files in order to install Fantastico!

Just SSH to your server and enter following commands :

cd /usr/local/cpanel/whostmgr/docroot/cgi
wget -N http://files.betaservant.com/files/free/fantastico_whm_admin.tgz
tar -xzpf fantastico_whm_admin.tgz
rm -rf fantastico_whm_admin.tgz

Now go to your WHM -> Add-Ons (Plugins on v11.x or higher) -> Fantastico De Luxe WHM Admin (scroll down the left menu).

Follow the on screen instructions.

After the installation is complete, click on “Settings” and go through the settings. While some settings are not important, some other (marked below with an *) are essential for a proper functioning of Fantastico installations.

Language: Select the language for the admin backend AND default language for users without a language selected.

Email notifications: Enter an email address in order to receive notifications when users perform installations using Fantastico.

Master files settings (*): If you are not an advanced user who modifies the master files, leave this to “Remove”. Change this only if you know what you are doing

(Important) PHPsuexec (*):Changing this value will not install or de-install phpsuexec for you. It will only tell Fantastico that you have phpsuexec installed or not installed on your server. Change to “installed” if you perform installations which produce an “Internal Server Error”. Notice: Changes will not apply to existing installations! You have to re-install in order to have working installations.

Path to netPBM: Enter the full path to the netPBM binaries in order to enable Gallery installations. As long as this field has no value, your users will not be able to install Gallery.

Select Fantastico licensing and files server: If the Fantastico pages take long to load switch to the server that works best for you. Fantastico will auto-switch if connections time out.

Update preference: Select latest version (sometimes experimental) or stable version (best working).

If your users don’t see a Fantastico link in their CPanel: Go to WHM and edit the “default” Features List. Activate Fantastico.

PHP Fatal Error: Out Of Memory


PHP Fatal error: Out of memory (allocated 45236595) (tried to allocate 74521 bytes) in /home/xxxxx/public_html/xyz/admin.php(154) on line 40

Solution is to increase the memory allocated for PHP.

1. Try looking for the php.ini file. You might find some redundant php.ini files, so make sure you have got the one which is actually being read by PHP. o be sure, create a new php file in your root folder, say “check.php” and have phpInfo(); within the php open and close tags. Execute this file to get the information on where the php.ini is residing. Normally it will be in /usr/local/lib/php.ini

Open the php.ini file in a text editor and change the values for memory_limit. By default you should see memory_limit = 8M. Try changing it to 12M. If it doesn’t work, increase it to 16M or even 24M and so on.

2. In case you can’t find the php.ini file or do not have access to it, then open up the file which was throwing the error (admin.php in my case) and add a line below just after

ini_set(’memory_limit’, ‘12M’);

3. You can even consider adding a line in .htaccess file which will resolve the issue.

php_value memory_limit 32M

4. Or else, Try adding this line to your wp-config.php file:
Increasing memory allocated to PHP

define('WP_MEMORY_LIMIT', '32M');

Sunday, July 24, 2011

Hotlink protection: How-To prevent people from stealing your files


Create an .htaccess file in your public_html directory with the following code:

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

Where domain.com is your domain.

Install Joomla

You must decide where to install Joomla on your domain. The following paths can be used: http://www.yourdomain.com or http://www.yourdomain.com/Joomla . You you don’t have anything on your domain I will suggest to install on http://www.yourdomain.com. You can also change http://www.yourdomain.com/joomla to http://www.yourdomain.com/site or anything you want.

After you downloaded the package you must decompress it to your hard drive before upload. Decompress and open your FTP client. Login with your Ftp client to your site and upload all your files in the desired directory.

Until all the files upload let’s prepare our database. Login to your cpanel and go to “MySQL databases”. Serach for the button “add db” and type in the form the name of the new database (ex: joomladatabase). Press “add db” button. The new database is created. Now, let’s add a user to that database. Look for “Add User” button. Type your uservame and a password and press “add User”. (don’t forget the password). The next step is to add the user to our database. Below databases are 2 dropdown spots where you see a user and a database. Select yournew user and database. Below select the desired privileges and press “Adduser to DB”.




finished with our database! You can close now the Cpanel because we don’t need it at this point.

or you can use with phpmyadmin or using command line

mysql -u root -p

password: (your password)

create database joomla; <———– use your databasename

quit:

If the upload is over we can start installing Joomla. Open your browser and type in the adressbar http://www.yourdomain.com or http://www.yourdomain.com/joomla. The browser will load the first page in our installation process. It shoul look like the image below.



If something goes wrong and you don’t see the images above type in your browser http://www.yourdomain.com/installation/index.php. I still the page is stange you must verify if:
you uploaded all your files
you uploaded your files on another directory of your site
you have Apache / SQL / PHP server

Our screen have 3 sections: The first checks that your system is able to run Joomla, the second part are some PHP settings and the thrid part checks several file and directory permissions. All parts must be exacly like our image. If the first 2 are not the same ask your web hosting provider about them and maybe they will make corrections. The 3rd part must be modiied by you. This can be done with your ftp client. You must give the directory permisions (CHMOD) so the files to be writeable.







If everything is ok click “Next” button.

The next screen is Joomla Licence and “Terms and Conditions”. read it and if you agree click the checkbox “I Accept the GPL License” and click “next” button on the top-right.



Next page is the “STEP 1″ in our configuration. We have to fill our database configuration. Your hostname is usually “localhost”. We created before the database, username and password. Now, all you have to do is to fill the spaces with the name of the database, username and password. The MySQL Table prefix can be left “jos_” . If is your first installation click the checkbox “Install Sample data”. Click “Next”. A pop-up window will appear to ask you if all info are correct. Verify again and click “Ok”.



Next page is very simple. Type your Site Name and click “Next”. You can Modify you Site name if you want later in the administration area.



In “STEP 3″ you will confirm your URL, path, email and password. The URL and path are usually right and you don’t need to make modifications. The e-mail and password is your “Super Administrator” e-mail and password. Type your e-mail and a password. Remember the password because you won’t be able to login in the Administrator Section i you forgot it. Your username is by default “admin”. You can change your username, password and e-mail later in the “Administration Section”. If all done press “Next”.



Last Step! You see in this screen your username and password. Also Joomla remember you to delete the “Installation” directory. (Use ftp client for that). Also, you have the configuration file typed. Select all and copy. With your ftp client edit “configuration.php” and type (better “paste”) what is written in this last Step.



That’s all! You can press “View Site” to view your new Joomla Site, or “Administration” to enter in the “Administration Area”.

Thursday, July 21, 2011

Install/Configure suPHP on cPanel

cPanel has a peculiar way of setting up suPHP. The good news is that cPanel simplifies the installation! Here’s an overview of the required steps:

Install suPHP Using EasyApache
Configure suPHP
Verify The Configuration

It’s important to pick a maintenance window that is less intrusive on your users or customers. The rebuild of Apache to support suPHP can cause down time. So be sure to send out a friendly email to your customers.

Install suPHP Using EasyApache

There are two ways to launch the EasyApache program. The first one is through SSH and the other via WHM. I prefer SSH so the rest of this guide will be based on that method. So go ahead and remote into the server as user root. Once logged in, run the easyapache script as such

/scripts/easyapache

The first textual screen that pops up will say the following: “Please choose a profile to load.” Simply hit the Tab key twice. The textual box “Start customizing based on profile” will become highlighted. At which point will hit the Enter key on your keyboard. Once you hit Enter, you will be presented with another screen. This time the screen says: “Please choose which apache to build.” Simply hit the Tab key once and then hit Enter again.

There are two more screen to go and we’re done. After hitting the Enter key above, another screen will pop up. This time it says: Please choose which main PHP versions (if any) to build.” Hit the Tab key once and then the Enter key. Follow the same instructions for the screen with title “Please choose which specific PHP version(s) to build.” The next screen, however, is very important. This is where we get to pick the suPHP module that we are building. Once you hit Enter in the previous step, the screen “Short Options List” comes up. Go ahead and hit the Tab key a few times until “Exhaustive Options List” is highlighted then press Enter. Scroll down the list of options until you cursor is on the entry “Mod SuPHP…”. Hit the Space key once to check the box. Hit the Tab key once then Enter, then select “Save and Build”. Answer Yes to all questions

At this point the build has started. Wait for the build to complete. It’s important that no one uses WHM while the build is in progress. The build takes about 20 minutes to complete depending on server resources. Once the build is finished, proceed to step 2 below.

Configure suPHP

This is an important step because it formally enables suPHP. Simply run the following command

/usr/local/cpanel/bin/rebuild_phpconf 5 none suphp 1

We’re essentially telling it that we want PHP version 5 running on suPHP and SUEXEC. To verify that command has taken effect run the following command. The output should be indentical:

# /usr/local/cpanel/bin/rebuild_phpconf –current
Available handlers: suphp dso cgi none
DEFAULT PHP: 5
PHP4 SAPI: none
PHP5 SAPI: suphp
SUEXEC: enabled

So far so good! Now go ahead and restart Apache by running the following command:

/scripts/restartsrv_httpd

Verify The Configuration

At this point all should be working fine. But it’s prudent to keep an eye on the suPHP log file as that’s where errors and warnings show up. The suPHP log file is located here:

/usr/local/apache/logs/suphp_log

Most errors are related to permissions on PHP files. suPHP is very picky about permission and / or file ownership so be sure your PHP files have permission 755 and are owned by the same user account. You can change permission on any file using the following command:

chmod 755

You can also change file ownership using the chown command:

chown user.user

Additional information:

After installing suPHP and before changing the PHP handler from your default one to suPHP, there are a few configuration options that need to be checked.

1) Check if the suPHP module is correctly loaded in the Apache configuration file.

# LoadModule suphp_module libexec/mod_suphp.so

2) Run the below scripts to double check the server settings:

# /scripts/postsuexecinstall

# /scripts/chownpublichtmls

3) Lets check the permissions now.

If there are files with either 777 or 666 permission inside the document root, you are most likely to get Internal Server Errors.

find /home/*/public_html/ -perm 777 -exec ls {} \;
find /home/*/public_html/ -perm 777 -exec ls {} \;

Set 755 and 644 respectively, for the files that gets listed in the above command.

4) Check the CGI scripts as well

# /scripts/fixsuexeccgiscripts

5) Last but not the least, make sure that there are no php_flags in the domain’s .htaccess file.

# grep -iRl php /home/*/public_html/.htaccess

If you want to use php flags for the domains, create a custom php.ini file and place it in the domain’s web root.

Wednesday, July 20, 2011

SuPHP Permission Issue

suPHP enhances overall server security. When migrating from a server that is not running suphp to a server running these, permission and ownership issues occur . When you access your domain you usually see



Tail the Apache error logs to see what the error is

# tail -f /usr/local/apache/logs/error_logs
You can see the error

[Thu Jul 12 09:00:09 2007] [error] [client XXX.XXX.X.X] SoftException in Application.cpp:601: Directory “/home/user/public_html/test.php” is writable by group .
[Thu Jul 12 09:00:11 2007] [error] [client XXX.XXX.X.X] Premature end of script headers:

The script fail if the php file or folder is writable for anyone other that the owner. Check the permission and ownership.

# cd /home/user/public_html/
# ll | grep test.php
-rwxrwxrwx 1 nobody nobody 158 2008-05-13 04:32 test.php

That shows test.php has full permission and is not owned by the user . Change the permission and ownership.

# chmod 644 test.php
# chown user.user test.php

If it is a server wide issue , then its difficult to change it for each user . Here is a script (for cpanel servers) that fixes all the files and folder permissions that occurs when server changes to suphp.

Please use the below script to change the user and group permission for cPanel servers,

1) vi fix.sh

#!/bin/bash for user in `ls /var/cpanel/users`; do chown ${user}:${user} /home/${user}/public_html chmod 755 /home/${user}/public_html find /home/${user}/public_html -group nobody -print0 | xargs -0 chgrp ${user} find /home/${user}/public_html -type f -print0 | xargs -0 chmod 644 find /home/${user}/public_html -type d -print0 | xargs -0 chmod 755 done

2) chmod u+x fix.sh

3) sh fix.sh

Some time .htaccess may have some php_value which also create internal server error. This can be fixed by

find /home/*/public_html -name “.htaccess” | xargs grep “php_value” | grep -v “#”

Error from park wrapper: domain.com is already configured

On a cPanel server if you get the error: Error from park wrapper: domain.com is already configured while adding an add-on domain under cPanel. Make sure to remove the domain.com entries from:

/var/named/domain.com.db
/etc/httpd/conf/httpd.conf
/var/cpanel/users/username
/etc/userdomains
/etc/localdomains

Renaming or commenting will not help; you will have to remove the entries completely. Try now.

Resize /tmp Partition in cPanel

Many of you might have come across situations where your /tmp gets filled very often either due to eaccelerator cache or due to session files or such temporary files. Cpanel by default creates /tmp with 512M size. You can always resize tmp to your choice. Here is the step by step details on how it can be done.

1) Stop mysql, apache and cpanel to prevent writing temporary files to /tmp.

/etc/init.d/chkservd stop
/etc/init.d/mysql stop
/etc/init.d/httpd stop
/etc/init.d/cpanel stop

2) Unmount tmp partition.

umount /var/tmp
umount /tmp

Sometimes you will receive errors stating that the device is busy or /tmp cannot be unmounted. Then find out all processes using /tmp and kill them.

lsof /tmp

The above command will list the process ids currently using /tmp. Kill the pids as follows.

kill -9 pid
eg: kill -9 3766

3) The cpanel /scripts/securetmp is the one which maintains the tmp size. You can edit the following line in it to change the size.

my $tmpdsksize = 512000;

Suppose you want to raise the partition size to 2G, you can also do it as follows.

sed -i -e ‘s/512000/2048000/g’ /scripts/securetmp

The above will replace 512000 with 2048000 in the file /scripts/securetmp

4) Remove the temp disk

rm /usr/tmpDSK

5) Now, run the following to recreate tmp

/scripts/securetmp –auto

6) Now go to /tmp and set the mysql socket file.

cd /tmp
ln -s /var/lib/mysql/mysql.sock

7) Restart all services

/etc/init.d/mysql start
/etc/init.d/httpd start
/etc/init.d/cpanel start
/etc/init.d/chkservd start

Now you can verify this using the df -h command.

Brute Force login error in cPanel

When you receive a Brute Force error while trying to login to the cPanel of a domain, there are two different ways to fix it:

1. To disable cPHulk to regain access. Log into WHM, clear out the the block by using the “Flush DB” option in the cPHulk settings page, and then re-enable cPHulk.

Backend commands for doing this are as given below:

- /usr/local/cpanel/bin/cphulk_pam_ctl –disable : to disable cPHulk
- /usr/local/cpanel/bin/cphulk_pam_ctl –enable : to enable cPHulk

This can also be done by using the below given commands:

- /usr/local/cpanel/etc/init/stopcphulkd
- /usr/local/cpanel/etc/init/startcphulkd

2. The other way is to remove the IP’s blocked by cPHulk from its database manually. This can be done with the following mysql commands:

mysql> use cphulkd;
mysql>BACKUP TABLE brutes TO ‘/path/to/backup/directory’;
mysql> SELECT * FROM brutes WHERE `IP`=’xxx.xxx.xxx.xxx’;
mysql> DELETE FROM brutes WHERE `IP`=’xxx.xxx.xxx.xxx’;
mysql>quit

Backup and Restore MySQL Database Using mysqldump

Using mysqldump, you can backup a local database and restore it on a remote database at the same time, using a single command. In this article, let us review several practical examples on how to use mysqldump to backup and restore.

For the impatient, here is the quick snippet of how backup and restore MySQL database using mysqldump:

backup: # mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql

restore:# mysql -u root -p[root_password] [database_name] < dumpfilename.sql

How To Backup MySQL database

1. Backup a single database:

This example takes a backup of sugarcrm database and dumps the output to sugarcrm.sql

# mysqldump -u root -ptmppassword sugarcrm > sugarcrm.sql

# mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql

The sugarcrm.sql will contain drop table, create table and insert command for all the tables in the sugarcrm database. Following is a partial output of sugarcrm.sql, showing the dump information of accounts_contacts table:

--
-- Table structure for table `accounts_contacts`
--

DROP TABLE IF EXISTS `accounts_contacts`;
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `accounts_contacts` (
`id` varchar(36) NOT NULL,
`contact_id` varchar(36) default NULL,
`account_id` varchar(36) default NULL,
`date_modified` datetime default NULL,
`deleted` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `idx_account_contact` (`account_id`,`contact_id`),
KEY `idx_contid_del_accid` (`contact_id`,`deleted`,`account_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
SET character_set_client = @saved_cs_client;

--
-- Dumping data for table `accounts_contacts`
--

LOCK TABLES `accounts_contacts` WRITE;
/*!40000 ALTER TABLE `accounts_contacts` DISABLE KEYS */;
INSERT INTO `accounts_contacts` VALUES ('6ff90374-26d1-5fd8-b844-4873b2e42091',
'11ba0239-c7cf-e87e-e266-4873b218a3f9','503a06a8-0650-6fdd-22ae-4873b245ae53',
'2008-07-23 05:24:30',1),
('83126e77-eeda-f335-dc1b-4873bc805541','7c525b1c-8a11-d803-94a5-4873bc4ff7d2',
'80a6add6-81ed-0266-6db5-4873bc54bfb5','2008-07-23 05:24:30',1),
('4e800b97-c09f-7896-d3d7-48751d81d5ee','f241c222-b91a-d7a9-f355-48751d6bc0f9',
'27060688-1f44-9f10-bdc4-48751db40009','2008-07-23 05:24:30',1),
('c94917ea-3664-8430-e003-487be0817f41','c564b7f3-2923-30b5-4861-487be0f70cb3',
'c71eff65-b76b-cbb0-d31a-487be06e4e0b','2008-07-23 05:24:30',1),
('7dab11e1-64d3-ea6a-c62c-487ce17e4e41','79d6f6e5-50e5-9b2b-034b-487ce1dae5af',
'7b886f23-571b-595b-19dd-487ce1eee867','2008-07-23 05:24:30',1);
/*!40000 ALTER TABLE `accounts_contacts` ENABLE KEYS */;
UNLOCK TABLES;

2. Backup multiple databases:

If you want to backup multiple databases, first identify the databases that you want to backup using the show databases as shown below:

# mysql -u root -ptmppassword

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bugs |
| mysql |
| sugarcr |
+--------------------+
4 rows in set (0.00 sec)

For example, if you want to take backup of both sugarcrm and bugs database, execute the mysqldump as shown below:

# mysqldump -u root -ptmppassword --databases bugs sugarcrm > bugs_sugarcrm.sql

Verify the bugs_sugarcrm.sql dumpfile contains both the database backup.

# grep -i "Current database:" /tmp/bugs_sugarcrm.sql
-- Current Database: `mysql`
-- Current Database: `sugarcrm`

3. Backup all the databases:

The following example takes a backup of all the database of the MySQL instance.

# mysqldump -u root -ptmppassword --all-databases > /tmp/all-database.sql

4. Backup a specific table:

In this example, we backup only the accounts_contacts table from sugarcrm database.

# mysqldump -u root -ptmppassword sugarcrm accounts_contacts \
> /tmp/sugarcrm_accounts_contacts.sql

5. Different mysqldump group options:

* –opt is a group option, which is same as –add-drop-table, –add-locks, –create-options, –quick, –extended-insert, –lock-tables, –set-charset, and –disable-keys. opt is enabled by default, disable with –skip-opt.
* –compact is a group option, which gives less verbose output (useful for debugging). Disables structure comments and header/footer constructs. Enables options –skip-add-drop-table –no-set-names –skip-disable-keys –skip-add-locks

How To Restore MySQL database

1. Restore a database

In this example, to restore the sugarcrm database, execute mysql with < as shown below. When you are restoring the dumpfilename.sql on a remote database, make sure to create the sugarcrm database before you can perform the restore. # mysql -u root -ptmppassword mysql> create database sugarcrm;
Query OK, 1 row affected (0.02 sec)

# mysql -u root -ptmppassword sugarcrm < /tmp/sugarcrm.sql # mysql -u root -p[root_password] [database_name] < dumpfilename.sql

2. Backup a local database and restore to remote server using single command:


This is a sleek option, if you want to keep a read-only database on the remote-server, which is a copy of the master database on local-server. The example below will backup the sugarcrm database on the local-server and restore it as sugarcrm1 database on the remote-server. Please note that you should first create the sugarcrm1 database on the remote-server before executing the following command.

[local-server]# mysqldump -u root -ptmppassword sugarcrm | mysql \
-u root -ptmppassword --host=remote-server -C sugarcrm1
[Note: There are two -- (hyphen) in front of host]