Wednesday, December 28, 2011

Apache vs NginX


Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption

Nginx is one of a handful of servers written to address the C10K problem. Unlike traditional servers, Nginx doesn't rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load.

Even if you don't expect to handle thousands of simultaneous requests, you can still benefit from Nginx's high-performance and small memory footprint. Nginx scales in all directions: from the smallest VPS all the way up to clusters of servers.

Nginx particularly excels at serving static files—like the Tectonicus map tile images. For larger websites, it's often employed as a front-end Web server to quickly dish up unchanging page content, while passing on requests for dynamic stuff to more complex Apache Web servers running elsewhere. However, I was interested in it purely as a fast single Web server.

http://joeandmotorboat.com/2008/02/28/apache-vs-nginx-web-server-performance-deathmatch/

http://www.wikivs.com/wiki/Apache_vs_nginx

Apache

The drawback to doing everything with processes is that Apache prefork can be a bit of a memory hog, especially under load. Another precompiled flavor of Apache can be installed as an alternative: Apache MPM worker. "Worker" differs from "prefork" in that worker's processes are multithreaded, giving them the ability to service more requests with fewer system resources. This can translate into faster pages served with less RAM and CPU. However, because some Apache modules don't necessarily work well when run under multithreaded Apache, you have to specifically select this version to install on Ubuntu and on other GNU/Linux distros with package management.

Alternatively we can use FastCGI with Apache to handle PHP requests. FastCGI can use a single persistent process which handles many requests over its lifetime

http://www.cyberciti.biz/tips/rhel-centos-fedora-apache2-fastcgi-php-configuration.html

Other Surveys for All Web Servers

http://news.netcraft.com/archives/2011/04/06/april-2011-web-server-survey.html

Friday, December 2, 2011

Apache Questions

Q1 How to enable Directory Listing

Ans Place "Options +Indexes" in Directory Tabs

Q2 Allow Apache to use / access symbolic links directory/files

Ans Place "Options +FollowSymLinks" in Directory Tabs

Q3 Allow Apache to use / access .htaccess file

Ans Place "AllowOverride All" in Directory Tabs

Q4 Can we replace .htaccess file to some other for our convenient

Ans Yes, change "AccessFileName .htaccess" to "AccessFileName .xyz"

Q5 Set default number of start processes

Ans Find "StartServers ..." and set as per your requirement

Q6 How to disable Apache signature/version

Ans ServerSignature Off

Q7 How to block/disable TRACE

Ans Use "TraceEnable Off" globally so that TRACE will be disabled for all VHOSTS

Q8 Best tools for monitoring Apache Requests

Ans use Apache Handler "server-status" and APACHETOP tool (its like TOP command in linux)

Q9 How can we protect our site to avoid DOS/DDOS attacks

Ans Use mod_evasive apache module or this can also be done through firewall

Q10 Redirect abc.com to www.abc.com

Ans Use following in abc.com VHOST : - "Redirect / http://www.abc.com/"

Q11 Which Apache processes are consuming higher memory

Ans Use ApacheTop or "ps -ylC httpd --sort rss" this command will show all apache processes in ascending order according to \memory size used by per process (RSS field)

Q12 What are different MPM's available in Apache for Linux and how they work

Ans There are 2 MPMs available 1) Worker 2) Prefork

     

        Worker :- This Multi-Processing Module (MPM) implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with fewer system resources than a process-based server. However, it retains much of the stability of a process-based server by keeping multiple processes available, each with many threads.

The most important directives used to control this MPM are ThreadsPerChild, which controls the number of threads deployed by each child process and MaxClients, which controls the maximum total number of threads that may be launched

      Prefork :- This Multi-Processing Module (MPM) implements a non-threaded, pre-forking web server that handles requests in a manner similar to Apache 1.3. It is appropriate for sites that need to avoid threading for compatibility with non-thread-safe libraries. It is also the best MPM for isolating each request, so that a problem with a single request will not affect any other.

This MPM is very self-regulating, so it is rarely necessary to adjust its configuration directives. Most important is that MaxClients be big enough to handle as many simultaneous requests as you expect to receive, but small enough to assure that there is enough physical RAM for all processes.


Q13 Write a Rewrite rule to redirect (www).abc.com/request.php to www.abc.com/request1.php

Ans RewriteEngine On
       RewriteCond %{HTTP_HOST} ^abc.com$ [NC,OR]
       RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]
       RewriteRule ^/request.php$ http://www.abc.com/request1.php [L]

Q14 How many connections apache server can handle

Ans This completely depends on our server configuration mainly on RAM

        Max no. of connections = (Total Memory - Memory Used By System For Its use) / average single apache process size*

        *Average Single Apache Process Size : Can be find using "top" command or "ps -ylC httpd --sort rss"


To be continued....

Tuesday, March 29, 2011

Install Apache module without re-installation with APXS

In case we have installed Apache using source code and we would like to install one of its modules called mod_rewrite or whatever. Then we need to do the following to install module without re-installing Apache:




cd apache-source-code
cd modules/mappers

/path/to/apache2/bin/apxs -iac mod_rewrite.c


Add following to httpd.conf file:


LoadModule rewrite_module modules/mod_rewrite.so




Then Restart Apache, thats it!!!

Tuesday, March 15, 2011

Apache Virtual Hosting


What is Apache Virtual Hosting?

When setting up more then One Host/Site over Single Server, this is called Virtual Hosting.

Virtual Hosting can be of 2 type's:

1) Name Based Virtual Hosting
2) IP Based Virtual Hosting

1) Name Based Virtual Hosting : Running multiple virtual hosts over a Single IP is called Name Based Virtual Hosting.

2) IP Based Virtual Hosting : When a Server is having multiple IP's and per IP is having it's own Virtual Host is called IP Based Virtual Hosting.


How to Setup Name Based Virtual Hosting and IP Based Virtual Hosting?

Name Based Virtual Host :

edit /etc/httpd/conf/httpd.conf

Listen 192.168.1.1:80

NameVirtualHost 192.168..1.1:80

VirtualHost 192.168..1.1:80
ServerName localhost
ServerAlias localhost.localdomain
DocumentRoot /var/www/html

Directory /var/www/html
AllowOverride None
Options -Indexes -FollowSymLinks
Order allow, deny
Allow from All
/Directory

CustomLog logs/localhost_access.log common
ErrorLog logs/localhost_error.log
/VirtualHost


VirtualHost 192.168..1.1:80

ServerName fistsite.com
ServerAlias www.firstsite.com
DocumentRoot /var/www/html/firstsite

Directory /var/www/html/firstsite
AllowOverride None
Options -Indexes -FollowSymLinks
Order allow, deny
Allow from All
/Directory
CustomLog logs/firstsite_access.log common
ErrorLog logs/firstsite_error.log
/VirtualHost
------------------------------------------------------------------------------------------------

IP Based Virtual Host:


edit /etc/httpd/conf/httpd.conf

Listen 192.168.1.1:80
Listen 192.168.1.2:80


VirtualHost 192.168..1.1:80
ServerName localhost
ServerAlias localhost.localdomain
DocumentRoot /var/www/html

Directory /var/www/html
AllowOverride None
Options -Indexes -FollowSymLinks
Order allow, deny
Allow from All
/Directory

CustomLog logs/localhost_access.log common
ErrorLog logs/localhost_error.log
/VirtualHost


VirtualHost 192.168..1.2:80
ServerName fistsite.com
ServerAlias www.firstsite.com
DocumentRoot /var/www/html/firstsite

Directory /var/www/html/firstsite
AllowOverride None
Options -Indexes -FollowSymLinks
Order allow, deny
Allow from All
/Directory
CustomLog logs/firstsite_access.log common
ErrorLog logs/firstsite_error.log
/VirtualHost

VirtualHost 192.168..1.3:80
ServerName second.com
ServerAlias www.second.com

DocumentRoot /var/www/html/second

Directory /var/www/html/second
AllowOverride None
Options -Indexes -FollowSymLinks
Order allow, deny
Allow from All
/Directory

CustomLog logs/second_access.log common
ErrorLog logs/second_error.log
/VirtualHost

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

Wednesday, March 9, 2011