Tuesday, March 6, 2012

Mod_Deflate

Mod_Deflate :-

mod_deflate is an module of an Apache HTTP Web Server. The basic purpose of mod_deflate is to speedup download web page access time. It provides DEFLATE output filter that allows output from webserver to webbrowser in the form of compression format.

It results in faster website access and decreases the amount of time in data transmission from web server to web browser.

mod_gzip or mod_gz modules are similar to mod_deflate which compresses the contents using different implementations like gzip implementations and external zlib library respectively.

You can check the gzip compression for website using below link :-

http://aruljohn.com/gziptest.php

How to Enable Mod_Deflate :-

1) # /scripts/easyapache


2) Adding the code under .htaccess file 

<IfModule mod_deflate.c>
    AddOutPutFilterByType DEFLATE text/html text/plain text/xml
    <IfModule mod_setenvif.c>
        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
        # MSIE masquerades as Netscape, but it is fine
        # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
        # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
        # the above regex won't work. You can use the following
        # workaround to get the desired effect:
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
        # Don't compress images
        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    </IfModule>
    <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</IfModule>

3) Append below code under httpd.conf

LoadModule deflate_module modules/mod_deflate.so

Append following configuration <Location /> directive:

<Location />
AddOutputFilterByType DEFLATE text/html text/plain text/xml
....
...
<Location>

Above line only compress html and xml files. Here is the configuration from one of my production box:
<Location />
...
...
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE application/x-httpd-eruby
AddOutputFilterByType DEFLATE text/html
...
...
<Location>

Close and save the file. Next restart apache web server. All of the above extension file should compressed by mod_deflate:

# /etc/init.d/httpd restart

More Information 



Zip files corrupt when downloaded via IE but not via FF.

  Firefox would download the zip and it was fine, but the file was coming through corrupt when downloading via Internet Explorer. This is a result of IE not handling gzip encoded files properly.

PHP Handler

PHP Handlers :-

In order to run a PHP site, the server must interpret the PHP code and generate a page most probably in HTML format that should be easily readable by the browser, when visitors access the website.

A PHP handler actually loads the libraries so that they can be used for interpretation of PHP code.

There are multiple different handlers that can be used for loading PHP :-

- CGI
- DSO
- suPHP
-FastCGI.

Each handler delivers the libraries through different files and implementations. Each file and implementation affects Apache’s performance, because it determines how Apache serves PHP.
It is essential for your server’s performance that you select the handler that fits your situation. Selecting the right handler is just as important as the PHP version itself. One handler is not necessarily always better than another; it depends on your unique setup.

How to change the handler

Changing the handler on cPanel is very easy to do and only takes seconds. Log into WHM and navigate to:

Main >> Service Configuration >> Configure PHP and SuExec

You simply select your PHP handler choice from the drop-down menu. Then hit “Save New Configuration”.

Most of the time, changing settings through WHM panel doesn't work. The only solution for this situation is :

# /scripts/easyapache

PHP handlers :-

* DSO (mod_php)

Apache 1.3 introduced a method to load the modules dynamically called as Dynamic Sharing Object (DSO ) running in an Apache web server. DSO allows to load additional modules to apache webserver anytime without recompiling an entire server.
PHP as an Apache module is DSO.
It is  also know as mod_php.

It runs PHP as an Apache module. This means that PHP scripts will run as the Apache user, which is the user: ‘nobody’.

Merits :-

- DSO is an older configuration
- Actually considered as the fastest PHP handler.
- Optimal utilization of resource usage.
-Additional modules can be added without recompilation of server-

De-Merits :-


- Files created by PHP scripts will have ownership as "NOBODY" - the situation is more prone to website compromise by an attacker. If an hacker finds an exploit in PHP script. This will give them the ability to modify files outside of that user’s account. This is really worst.
-Websites that need to upload files through PHP may face permission issues, due to conflicts settings of permissions.
- OS Platform restriction, since dynamic loading is not supported by some OS.

More Information

CGI

CGI stands for: Common Gateway Interface. CGI handler will run PHP as a CGI module.  CGI still runs PHP processes as the Apache ‘nobody’ user. However, if you have suEXEC enabled, it will allow you to see the user that made the request.

CGI is considered as Neither Secure Nor Fast. CGI is never recommended.

http://docs.cpanel.net/twiki/bin/view/AllDocumentation/WHMDocs/MorePhphandlers

suPHP

suPHP stands for Single user PHP. suPHP also runs PHP as a CGI module instead of an Apache module. The main difference, and the advantage of having suPHP, is that with suEXEC enabled it runs the PHP scripts as the user calling them, rather than as the 'nobody' user. 

suPHP is typically the default handler and is recommended by cPanel for serving PHP because you will be able to see which user owns the account that is running the PHP script.

suPHP is beneficial in that if you are using a file upload tool on your site (such as an automatic updater or theme/plug-in installer for WordPress), the files will already have the right ownership & permissions. Uploading and other WordPress functions will not work without suPHP or FastCGI.

Merits :-

- The security difference between suPHP and DSO is that suPHP confines an intruder to the particular user that he/she has affected. The exploit can't cross accounts, however it can affect every single file the user owns as opposed to just the files writable by the webserver.
- suPHP also offers a security advantage that any php script that is not owned by the particular user (such as another account or root) will not be executable.

De-Merits :-

- suPHP generally runs a much higher CPU load. In addition, you CANNOT use an Opcode Cache (such as eAccelerator, Xcache or APC) with suPHP. It is strongly recommend that you install a caching plug-into supplement. If you find that your server is still continually struggling with CPU usage, you will want to consider switching to DSO or FastCGI.


FastCGI

FastCGI also called as FCGI or mod_fcgid it is similar to suPHP and/or is a high performance variation of CGI. It has the security/ownership benefits of suPHP in that PHP scripts will run as the actual cPanel user as opposed to ‘nobody’.

The difference with FastCGI is that it can drastically save on CPU performance and give speeds close to that of DSO. It can also be used with an opcode cacher like eAccelerator or APC, which can help further speed the loading of pages.

De-Merits :-

- FastCGI has a high memory usage. ( because rather than creating the PHP process each time it is called, like suPHP, it keeps a persistent session open in the background).


Go through below Image that will depict the PHP Handler :-



How to check current PHP Handler on server :-

 # /usr/local/apache/bin/rebuild_phpconf --current



 How to enable mod_fcgid on Shared/Reseller Server :-
Simply add
"AddHandler fcgid-script" and the extension you want into your .htaccess file
AddHandler fcgid-script .php
 

 The apache MPM must be tuned properly first. Prefork should not be used with FastCGI.


Standard Config

Be sure to modify these settings as applicable for the environment you are installing fastcgi in. These directives go in the pre_virtualhost_global.conf

<IfModule mod_fcgid.c>  
   FcgidIdleTimeout 3600
   FcgidIdleScanInterval 480
   FcgidBusyTimeout 1800
   FcgidBusyScanInterval 480
   FcgidZombieScanInterval 12
   FcgidErrorScanInterval 12
   FcgidProcessLifeTime 0

   # Change the rate at which new FastCGI processes are spawned under load. Higher=faster
   FcgidSpawnScoreUpLimit 10

   # Higher number = spawning more FastCGI processes decreases the spawn rate (controls runaway
   FcgidSpawnScore 1

   # Higher number = terminating FastCGI processes decreases the spawn rate (controls runaway)
   FcgidTerminationScore 2

   # Increase the FastCGI max request length for large file uploads (needed for some sites)
   FcgidMaxRequestLen 1073741824

   FcgidMaxRequestsPerProcess 100000
   #FcgidMinProcessesPerClass 3
   FcgidMaxProcessesPerClass 16
   FcgidMaxProcesses 64
   FcgidIOTimeout 1800
</IfModule>
The min and max process settings may need to be tuned. The per class limits can be uncommented if they are needed.


APACHE :-

Apache is an open source web server which guarantees availability of active websites online . The original version of apache was especially designed for unix-like operating system Nowadays, it can be implemented on variety of O.S. platforms. According to Apache Software Foundation and Net craft web server survey, 65% of websites on the internet uses Apache server.

 Features of Apache :-

1) Modularity

2)Uniformity

3)Portability

4)Powerful and Flexible webserver

5)Easily operable on wide variety of OS Platforms.





which made possible technologies like apache modules an API for apache module enabling features such as PHP, Mysql integration, URL Rewrite, PHP Handlers, Global variables and other features.

Apache Web Server Versions :-

Apache 1.3 :-

-Useful configuration files.
-Windows and Netware support
-DSO Support
-APXS tool (Basically APXS is located at /usr/local/apache/bin/apxs  and the APXS is a tool used for building and installing extension modules for Apache HTTP Server).

Apache 2.0 :-

-Most comprehensive configuration files.
-Efficiency
-Supports IPV6 version.
-Unix Threading
-Introduced new compilation system and multi-langauge error messaging.

Apache 2.2 :-

-Offers more and new flexible modules.
-User Authentication and Proxy caching features.
-SQL Support
-Support files exceeding 2Gb.


--------------------------------------------------------------------------------------------------------------------------

You may felt boredom while reading below concept,  But its the core and most important functional body of Apache.


 MPM  :-

MPM stands for Multiple Processing Modules.  The main purpose of MPM  is binding network ports on machine, accepting request and dispatching children to handle the requets.

Apache implements specialized MPM. MPM must be choosen while configuring and must be compiled in the server. MPM behaves like Apache module, the basic difference is only one MPM must be loaded into the server at any time.

List of Apache MPM modules are available. Refer the link for the same :-

http://httpd.apache.org/docs/2.0/mod/

Some of the well know and widely implemented MPM's :-

1) Prefork :-

"The prefork MPM runs multiple processes with each child process handling one connection".

Merits :-

-Stable and Secure
-Compatible with all implementations (cgi, fcgi, suPHP, DSO).
-Failure of one process wont affect other connections.

De-Merits :-

-Simultaneous execution of multiple processes >> indicates more memory usage.

A typical configuration of the process controls in the Preform MPM could look as follows:

StartServers 8
MinSpareServers 5
MaxSpareServers 20
MaxClients 256
MaxRequestsPerChild 1000

More Information

2) Worker :-

"The worker MPM has one control process that launches multi-threaded child processes which handles one connection per thread".

Merits :-

-Multi-threaded design utilizes less memory usage irrespective of high connections.
-Fast performance.

De-Merits :-

-DSO Module Incompatible.

A typical configuration of the process-thread controls in the Worker MPM could look as follows:

ServerLimit 16
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25

More Information

3) Event :-

"The Event MPM is similar to worker MPM but allows high performance by passing some of the listener thread work  to each work processes. Each process can act as worker or listener depending on need and activity in CPU for respective time".

Merits :-

-Better optimization.

De-Merits :-

-DSO Module Incompatible.

 A typical configuration of the process-thread controls in the Event MPM could look as follows:

KeepAlive On
KeepAliveTimeout 2
MaxKeepAliveRequests 20


More Information


Distinguish between Perform, Worker, Event MPM's



Configurations Definitions :-


* StartServers :- The number of  child processes created  on startup.

*MinSpareServers : Minimum number of idle child processes. apache will continue to create new processes. This should be high enough, idle processes should be on hand.

*MaxSpaceServers : Max number of idle child process, parent process will kill idle process in excess of limit.

*MaxClients : Max number of simultaneous request i.e Max number of Apache requests.

*MaxRequestsPerChild : Max number of request a child process will handle before it dies. if set to 0 ; process will never die

*ServerLimit : Max number of child process, must be greater on equal to maxclient divided by thread per child.

*ThreadsPerChild : No. of threads per child process.

*MaxSpareThread : Min no.of idle thread.

*KeepAlive ; allows for persistent connection to improve latency for connections that require multiple requests.

*KeepAliveTimeout :  Length of time connection will be kept open for further request before closing.

*MaxKeepAliveRequests : Max no. of requests through a single keep alive connection.