Linux

Apache Server

Apache is probably the most popular Linux-based Web server application in use. The Hypertext Transfer Protocol (HTTP) is, of course, a significant portion of the foundation for the World Wide Web, and Apache is the server implementation of the HTTP protocol. Browsers such as Netscape Navigator, Mozilla, Opera, and Microsoft Internet Explorer are client implementations of HTTP.
The default port for HTTP requests is port 80
In this page we will study how to configure Apache in a number of commonly encountered scenarios for small web sites.

RPM of Apache server

LINUX Apache rpm
LINUX Apache rpm

[root@localhost~]# rpm -qa | grep http
Result would be like this if the rpm have already been installed.

Starting or stopping httpd service

[root@ server ~]# service httpd start
[root@ server ~]# service httpd stop
[root@ server ~]# service httpd restart

Or
[root@ server ~]# /etc/init.d/httpd start
[root@ server ~]# /etc/init.d/httpd stop
[root@ server ~]# /etc/init.d/httpd restart

If you want service remain in effect after rebooting,
use command
[root@ server ~]# chkconfig httpd on


General Configuration Steps of Apache

The configuration file used by Apache is /etc/httpd/conf/httpd.conf in Redhat Starting from simple configuration. Open file /etc/httpd/conf/httpd.conf Write at the last as shown in figure below.
LINUX Apache Virtual host
LINUX Apache Virtual host

  1. Virtual Host Sections =To serve more than one website from the same Apache HTTP server, you need to configure virtual hosts. There are two types of virtual hosts: name-based and IP-based.
    • Namebased virtual host means that multiple names are running on one IP address.
    • IPbased virtual host means that a different IP address exists for one website. Most configurations are named-based because it only requires one IP address, Virtual hosts are configured one at a time usually at the end of the httpd.conf file.
  2. ServerAdmin=Add an administrative e-mail address where someone can contact you in case an error isencountered with your server.
  3. ServerName = name of hosting website
  4. DocumentRoot = This is the path of directory where the document of website raj.com are stored.
  5. ServerAlias = Enables the virtual server to respond to one or more alternate host
  6. LOGs= we have two type of logs which stored in /var/log/httpd with the name as shown in figure
    • Logging errors (ErrorLog) The error log contains messages generated by the server that describe various error conditions.
    • LINUX Apache Error Logs
      LINUX Apache Error Logs

    • Logging hits (CustomLog) Every incoming HTTP request generates an entry in the transfer log.
    • LINUX Apache Custom Logs
      LINUX Apache Custom Logs

To make the Web server available to your LAN, you can use your IP address instead of www.raj.com (for example, ServerName 192.168.0.3).
For checking purpose
Use command
[root@localhost ~]# netstat -a | grep http
LINUX Apache service is running
LINUX Apache service is running

In order to display your page, make a webpage named index.html. Put this into the directory as specified in DocumentRoot means /var/www/raj For e.g. make a webpage
LINUX Apache index page
LINUX Apache index page

Now open the web browser of connected client
Type ip address http://192.168.0.3 (DNS is not ready so we can’t give the domain name)
Here is the result.
Wow!!!! Local website is ready
LINUX Apache displaying index page
LINUX Apache displaying index page

Every times you make changes in the http server you will have to restart its service
[root@localhost~]# service httpd restart


Named Virtual Hosting or Named based virtual hosting

You can make your Web server to host more than one site per IP address by using Apache's named virtual hosting feature. You use the NameVirtualHost directive in the /etc/httpd/conf/httpd.conf file to tell Apache which IP addresses will participate in this feature. Now this time we would host two website on one ip.
Open file /etc/httpd/conf/httpd.conf
Make a new entry
Put a line in file
NameVirtualHost 192.168.0.3
Modify the file as shown in figure
LINUX Apache /etc/httpd/conf/httpd.conf
LINUX Apache /etc/httpd/conf/httpd.conf

Don't confuse to see the :80 it is optional Restart the httpd service I think this clear everything. www.indya.com is also being hosted on same ip To Host this websites a DNS server will be needed. Because this time Website is being accessed by their name not ip. For testing purpose we can do one thing if client computer have Windows OS then make some changes in hosts file which is located in Windows/system32/drivers/etc
Write at the last ( in WINDOWS/system32/drivers/etc/hosts)
192.168.0.3 www.raj.com
192.168.0.3 www.indya.com

Now open client's web browser
Type www.raj.com
Result would be
LINUX Apache Named based virtual hosting
LINUX Apache Named based virtual hosting

Now type www.indya.com
LINUX Apache Named based virtual hosting 2nd website
LINUX Apache Named based virtual hosting


IP-based virtual hosting

The server has two IP addresses (192.168.0.3 and 192.168.0.4) which resolve to the names www.raj.com and www.raj.org respectively. Two ip means may have two Ethernet card How to give two ip? Use setup command make new Ethernet device give new ip and then restart the service
Server configuration

ServerAdmin root@raj.com
DocumentRoot /var/www/raj
ServerName www.raj.com


ServerAdmin root@raj.org
DocumentRoot /var/www/rajorg
ServerName www.raj.com

Serving the same content on different IP addresses

The server machine has two IP addresses (192.168.0.3 and 172.20.30.40). The machine is sitting between an internal (intranet) network and an external (internet) network. Outside of the network, the name www.raj.com resolves to the external address (172.20.30.40), but inside the network, that same name resolves to the internal address (192.168.0.3). The server can be made to respond to internal and external requests with the same content, with just one VirtualHost section. Server configuration

NameVirtualHost 192.168.0.3
NameVirtualHost 172.20.30.40

ServerAdmin root@raj.com
DocumentRoot /var/www/raj
ServerName www.raj.com

Linux Apache Password protection

If you have a directory that you do not want the general public to have access to, this simple procedure will produce a username and password prompt anytime someone tries to access the directory. First we will create which is known as the .htaccess authentication file The .htaccess file needs to be placed inside the directory you would like protected. Open you favourite editor write as give in figure.
LINUX Apache .htaccess
LINUX Apache .htaccess

.htpasswd with full path, which contain the username and password.
My Private Directory is the name you would like for the authentication window.
Configuration of the .htpasswd file
  • We must create the .htpasswd file. This file contains the usernames and passwords of those individuals who we authorize access to our directory, and subdirectories.
  • Go to the directory, using the cd command, you specified your AuthUserFile (in this example, we specified /var/www/raj).
  • Type htpasswd -c .htpasswd username to create the .htpasswd file and add "username" to list of authorized users. The program will initially prompt you for a password and then ask you to verify it. As shown in figure
LINUX Apache .htaccess password protection
LINUX Apache .htaccess password protection

Now test the server open the web browser of client computer
LINUX Apache .htaccess password prompt
LINUX Apache .htaccess password prompt

If your server does not reply Then make sure the AllowOverride option is set to All. In /etc/httpd/conf/httpd.conf file. If still it doesn’t work then Change your configuration in /etc/httpd/conf/httpd.conf file as shown in figure.
LINUX Apache .htaccess password configuration
LINUX Apache .htaccess password configuration

To delete users, open the .htpasswd file, using your favorite unix editor, like vi, and delete the row(s) associated with the specific user(s) that you want to remove. Till this is about hosting a website, Now we can do some more experiments with /etc/httpd/conf/httpd.conf using GLOBAL CONFIGURATION DIRECTIVES.
GLOBAL CONFIGURATION DIRECTIVES.
Timeout 60 bThe Timeout directive determines the number of seconds that Apache will hold a connection open between the receipt of packets, between the receipt of acknowledgments on sent responses, or while receiving an incoming request.
KeepAlive OnPermits multiple requests on the same connection, speeding up delivery of HTML documents. Specifying KeepAlive On results in significant performance improvements because it eliminates the overhead involved in initiating new HTTP connections between clients and the Web server.
MaxKeepAliveRequests 100 Sets the number of requests permitted per connection.
KeepAliveTimeout 15 Sets the number of seconds permitted to elapse Between requests from the same client on the same connection when KeepAlive is On. This directive specifies the number of seconds to hold the connection while awaiting another request.
StartServers 8How many child server processes should be started by the parent httpd server.
MinSpareServers 5The minimum number of idle server processes kept Spare.
MaxSpareServers 20The maximum number of idle server processes kept spare.
MaxClients 150Sets the maximum number of simultaneous Connections (child servers) supported.
ServerLimitThe maximum value MaxClients can be over the life of the server.
MaxRequestsPerChildThe maximum number of requests a process can serve.
ServerLimitThe maximum value MaxClients can be over the life of the server.
MaxRequestsPerChildThe maximum number of requests a process can serve.


admin