apache (web sever)



Webserver   to host the websites. We can host multiple websites on same webserver. Apache is probably the most popular Linux-based Web server application in use. Once you have DNS correctly setup and your server has access to the Internet, you'll need to configure Apache to accept surfers wanting to access your Web site.
  ex: apache, tomcat, nginx, lighttpd and IIS

Apache is the default webserver in RHEL.

http (apache):
 package : httpd
service : httpd
config file : /etc/httpd/httpd.conf
logs: /var/log/httpd
port no: 80
Default Document Root :  /var/www/html    ( dir where the website content available)
Default DirectoryIndex : index.html (home page for that website from that documentroot)

Install apache
 yum  install  httpd

Verification
rpm  -qa   httpd

Service on
chkconfig  httpd  on
service httpd   restart
          or
/etc/init.d/httpd restart

General Configuration Steps
The configuration file used by Apache is /etc/httpd/conf/httpd.conf in Redhat / Fedora distributions and /etc/apache*/httpd.conf in Debian / Ubuntu distributions. As for most Linux applications, you must restart Apache before changes to this configuration file take effect. 

Where To Put Your Web Pages
All the statements that define the features of each web site are grouped together inside their own <VirtualHost> section, or container, in the httpd.conf file. The most commonly used statements, or directives, inside a <VirtualHost> container are:
  • servername: Defines the name of the website managed by the <VirtualHost> container. This is needed in named virtual hosting only, as I'll explain soon.
  • DocumentRoot: Defines the directory in which the web pages for the site can be found.
By default, Apache searches the DocumentRoot directory for an index, or home, page named index.html. So for example, if you have a servername of www.my-site.com with a DocumentRoot directory of /home/www/site1/, Apache displays the contents of the file /home/www/site1/index.html when you enter http://www.site1.com in your browser. 

Some editors, such as Microsoft FrontPage, create files with an .htm extension, not .html. This isn't usually a problem if all your HTML files have hyperlinks pointing to files ending in .htm as FrontPage does. The problem occurs with Apache not recognizing the topmost index.htm page. The easiest solution is to create a symbolic link (known as a shortcut to Windows users) called index.html pointing to the file index.htm. This then enables you to edit or copy the file index.htm with index.html being updated automatically. You'll almost never have to worry about index.html and Apache again! 

This example creates a symbolic link to index.html in the /home/www/site1 directory.
[root@linux tmp]# cd /home/www/site1
[root@linux site1]# ln -s index.htm index.html
[root@linux site1]# ll index.*
-rw-rw-r--    1 root     root        48590 Jun 18 23:43 index.htm
lrwxrwxrwx    1 root     root            9 Jun 21 18:05 index.html -> index.htm
[root@linux site1]#

The l at the very beginning of the index.html entry signifies a link and the -> the link target. 

The Default File Location
By default, Apache expects to find all its web page files in the /var/www/html/ directory with a generic DocumentRoot statement at the beginning of httpd.conf. The examples in this chapter use the /home/www directory to illustrate how you can place them in other locations successfully. 

File Permissions And Apache
Apache will display Web page files as long as they are world readable. You have to make sure you make all the files and subdirectories in your DocumentRoot have the correct permissions.
It is a good idea to have the files owned by a nonprivileged user so that Web developers can update the files using FTP or SCP without requiring the root password.
To do this:
1.     Create a user with a home directory of /home/www.
2.     Recursively change the file ownership permissions of the /home/www directory and all its subdirectories.
3.     Change the permissions on the /home/www directory to 755, which allows all users, including the Apache's httpd daemon, to read the files inside. 

[root@linux tmp]# useradd -g users www
[root@linux tmp]# chown -R www:users /home/www
[root@linux tmp]# chmod 755 /home/www
Now we test for the new ownership with the ll command.
[root@linux tmp]# ll /home/www/site1/index.*
-rw-rw-r--    1 www     users       48590 Jun 25 23:43 index.htm
lrwxrwxrwx    1 www     users           9 Jun 25 18:05 index.html -> index.htm
[root@linux tmp]#

Note: Be sure to FTP or SCP new files to your web server as this new user. This will make all the transferred files automatically have the correct ownership.
If you browse your Web site after configuring Apache and get a "403 Forbidden" permissions-related error on your screen, then your files or directories under your DocumentRoot most likely have incorrect permissions. Appendix II, "Codes, Scripts, and Configurations," has a short script that you can use to recursively set the file permissions in a directory to match those expected by Apache. You may also have to use the Directory directive to make Apache serve the pages once the file permissions have been correctly set. If you have your files in the default /home/www directory then this second step becomes unnecessary.
we can host the websites in three ways,

1. Ipbased ( we can resolve the website with both name and ip )
2. Name based ( we need to resolve with name only )
3. port based  for name based hosting in the config file,   NameVirtualHost ip:80 ( check line number 990)

Named Virtual Hosting
You can make your Web server 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.
The <VirtualHost> containers in the file then tell Apache where it should look for the Web pages used on each Web site. You must specify the IP address for which each <VirtualHost> container applies. 

Named Virtual Hosting Example
Consider an example in which the server is configured to provide content on 172.16.15.20. In the code that follows, notice that within each <VirtualHost> container you specify the primary Web site domain name for that IP address with the ServerName directive. The DocumentRoot directive defines the directory that contains the index page for that site.

You can also list secondary domain names that will serve the same content as the primary ServerName using the ServerAlias directive. 

Apache searches for a perfect match of NameVirtualHost, <VirtualHost>, and ServerName when making a decision as to which content to send to the remote user's Web browser. If there is no match, then Apache uses the first <VirtualHost> in the list that matches the target IP address of the request. 

This is why the first <VirtualHost> statement contains an asterisk: to indicate it should be used for all other Web queries.

NameVirtualHost 172.16.15.20:80

<VirtualHost ip:80>
 
   DocumentRoot   <dir for wesite content>
   ServerName   <website name>
   DirectoryIndex  <homepage>
 
</VirtualHost>

<VirtualHost 172.16.15.20:80>
   DocumentRoot  /var/www/html/site1
   servername www.site1.com
      DirectoryIndex index.hmtl
</VirtualHost>


<VirtualHost 172.16.15.20:80>
      DocumentRoot  /var/www/html/site2
   servername www.site2.com
   DirectoryIndex index.hmtl

</VirtualHost>

Be careful with using the asterisk in other containers. A <VirtualHost> with a specific IP address always gets higher priority than a <VirtualHost> statement with an * intended to cover the same IP address, even if the ServerName directive doesn't match. To get consistent results, try to limit the use of your <VirtualHost *> statements to the beginning of the list to cover any other IP addresses your server may have.
You can also have multiple NameVirtualHost directives, each with a single IP address, in cases where your Web server has more than one IP address.

IP-Based Virtual Hosting

The other virtual hosting option is to have one IP address per Web site, which is also known as IP-based virtual hosting. In this case, you will not have a NameVirtualHost directive for the IP address, and you must only have a single <VirtualHost> container per IP address.Also, because there is only one Web site per IP address, the ServerName directive isn't needed in each <VirtualHost> container, unlike in named virtual hosting. 

IP Virtual Hosting Example: Single Wild Card

In this example, Apache listens on all interfaces, but gives the same content. Apache displays the content in the first <VirtualHost *> directive even if you add another right after it. Apache also seems to enforce the single <VirtualHost> container per IP address requirement by ignoring any ServerName directives you may use inside it.
<VirtualHost *>
   DocumentRoot /home/www/site1
</VirtualHost>

IP Virtual Hosting Example: Wild Card and IP addresses

In this example, Apache listens on all interfaces, but gives different content for addresses 172.16.15.26 and 172.16.15.27. Web surfers get the site1 content if they try to access the web server on any of its other IP addresses.
<VirtualHost *>
   DocumentRoot /home/www/site1
</VirtualHost>

<VirtualHost 97.158.253.26>
   DocumentRoot /home/www/site2
</VirtualHost>

<VirtualHost 97.158.253.27>
   DocumentRoot /home/www/site3
</VirtualHost>

Configuration - Multiple Sites And IP Addresses
To help you better understand the edits needed to configure the /etc/httpd/conf/httpd.conf file, I'll walk you through an example scenario.

Example 

ServerName localhost
NameVirtualHost 172.16.15.26
NameVirtualHost 172.16.15.27

#
# Match a webpage directory with each website
#
<VirtualHost *>
    DocumentRoot /home/www/site1
 </VirtualHost>

<VirtualHost 172.16.15.26>
    DocumentRoot /home/www/site2
   ServerName www.my-site.com
    ServerAlias my-site.com, www.my-cool-site.com
</VirtualHost>

<VirtualHost 172.16.15.27>
    DocumentRoot /home/www/site3
   ServerName www.test-site.com
</VirtualHost>

port based hosting

  Listen  8080    ( configuration file line no 990 )

<VirtualHost  ip:8080>  
   DocumentRoot   ....
   ServerName   ....
   DirectoryIndex  ....
</VirtualHost>

Troubleshooting Apache

Troubleshooting a basic Apache configuration is fairly straightforward; you'll find errors in the /var/log/httpd/error_log file during normal operation or displayed on the screen when Apache starts up. Most of the errors you'll encounter will probably be related to incompatible syntax in the <VirtualHosts> statement caused by typing errors.

Testing Basic HTTP Connectivity

The very first step is to determine whether your web server is accessible on TCP port 80 (HTTP).Lack of connectivity could be caused by a firewall with incorrect permit, NAT, or port forwarding rules to your Web server. Other sources of failure include Apache not being started at all, the server being down, or network-related failures.
If you can connect on port 80 but no pages are being served, then the problem is usually due to a bad Web application, not the Web server software itself.
It is best to test this from both inside your network and from the Internet.

No comments:

Post a Comment