Docsity

Lecture Notes on APACHE Web Server | CIS 238, Study notes of Computer Science

Toggle sidebar
bg1
APACHE Web Server
Installation
Apache is the most popular Linux-based Web server application in use. It also has a port
to Windows replacing IIS. Most RedHat and Fedora Linux software products are
available in the RPM format with a filename usually starting with the word httpd
followed by a version number, as in httpd-2.0.48-1.2.rpm. It is best to use the latest
version of Apache when installing.
After installation, use the chkconfig command to configure Apache to start at boot:
chkconfig httpd on
Use the httpd init script in the /etc/init.d directory to start,stop, and restart Apache after
booting:
service httpd start
service httpd stop
service httpd restart
You can test whether the Apache process is running with
pgrep httpd
you should get a response of plain old process ID numbers.
It is not recommended to run Apache services as an XINETD application for
performance reasons.
DNS
To make your Apache server known, you must add the server name and IP address to the
DNS domain it is to be published in. By default, this is ususally a www A entry in the
DNS zone. Additional address entries may be required for any virtual hosts serviced by
the Apache server.
Configuring Apache
The configuration file used by Apache is /etc/httpd/conf/httpd.conf. As for most Linux
applications, you must restart Apache before changes to this configuration file take effect
- service httpd restart.
Files in the /etc/httpd/conf.d directory are read and automatically appended to the
configuration in the httpd.conf file every time Apache is restarted. In complicated
configurations, in which a Web server has to host many Web sites (see below), you can
create one configuration file per Web site each with its own set of <VirtualHost> and
<Directory> containers. This can make Web site management much simpler. To do this
correctly:
1. Backup your httpd.conf file, in case you make a mistake.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lecture Notes on APACHE Web Server | CIS 238 and more Study notes Computer Science in PDF only on Docsity!

APACHE Web Server

Installation

Apache is the most popular Linux-based Web server application in use. It also has a port to Windows replacing IIS. Most RedHat and Fedora Linux software products are available in the RPM format with a filename usually starting with the word httpd followed by a version number, as in httpd-2.0.48-1.2.rpm. It is best to use the latest version of Apache when installing. After installation, use the chkconfig command to configure Apache to start at boot: chkconfig httpd on Use the httpd init script in the /etc/init.d directory to start,stop, and restart Apache after booting: service httpd start service httpd stop service httpd restart You can test whether the Apache process is running with pgrep httpd you should get a response of plain old process ID numbers. It is not recommended to run Apache services as an XINETD application for performance reasons. DNS To make your Apache server known, you must add the server name and IP address to the DNS domain it is to be published in. By default, this is ususally a www A entry in the DNS zone. Additional address entries may be required for any virtual hosts serviced by the Apache server.

Configuring Apache

The configuration file used by Apache is /etc/httpd/conf/httpd.conf. As for most Linux applications, you must restart Apache before changes to this configuration file take effect

  • service httpd restart. Files in the /etc/httpd/conf.d directory are read and automatically appended to the configuration in the httpd.conf file every time Apache is restarted. In complicated configurations, in which a Web server has to host many Web sites (see below), you can create one configuration file per Web site each with its own set of and containers. This can make Web site management much simpler. To do this correctly:
    1. Backup your httpd.conf file, in case you make a mistake.
  1. Create the files located in this directory that contain the Apache required and containers and directives.
  2. If each site has a dedicated IP address, then place the NameVirtualHost statements in the corresponding /etc/httpd/conf.d directory file. If it is shared, it'll need to remain in the main httpd.conf file.
  3. Remove the corresponding directives from the httpd.conf file.
  4. Restart Apache, and test. The files located in the /etc/httpd/conf.d directory don't have to have any special names, and you don't have to refer to them in the httpd.conf file. Where To Put Your Web Pages All the statements that define the features of each web site are grouped together inside their own section, or container, in the httpd.conf file. The most commonly used statements, or directives, inside a container are:  servername : Defines the name of the website managed by the 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.my-site.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. This example creates a symbolic link to index.html in the /home/www/site1 directory. cd /home/www/site ln -s index.htm index.html 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. Apache will display Web page files as long as they are world readable (444). 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.

httpd_sys_script_rw_t Files with this type may be changed by a CGI script in any way, including deletion. httpd_sys_script_exec_t The type required for the execution of CGI scripts In the example case, the PERL script test.cgi was created to display the word "Success" on the screen of your Web browser. #!/usr/bin/perl

CGI Script "test.cgi"

print qq(

Linux Home Networking

Success!

); The ScriptAlias directive has been set to point to /home/www/cgi-bin/ instead of /var/www/cgi-bin/: ScriptAlias /cgi-bin/ "/home/www/cgi-bin/" User root creates the /home/www/cgi-bin/ directory, changes the directory's security context label to httpd_sys_script_exec_t, and then creates the script /home/www/cgi-bin/test/test.cgi mentioned previously with the correct executable file permissions. If you find security contexts too restrictive, you can turn them off system wide by editing your /etc/selinux/config file, modifying the SELINUX parameter to disabled. SELinux will be disabled after your next reboot. Named Virtual Hosting and DNS You can make your Web server host more than one site per IP address by using Apache's named virtual hosting feature – implemented in HTML 1.1. 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 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 container applies.

In each 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, , 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 in the list that matches the target IP address of the request. This is why the first statement contains an asterisk: to indicate it should be used for all other Web queries. A with a specific IP address always gets higher priority than a 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 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. Example: a server is configured to provide content on 97.158.253.26. NameVirtualHost 97.158.253.

Default Directives. (In other words, not site #1 or site #2)

servername www.my-site.com Directives for site #

servername www.another-site.com Directives for site #

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 (HTML 1.0). In this case, you will not have a NameVirtualHost directive for the IP address, and you must only have a single container per IP address. Also, because there is only one Web site per IP address, the ServerName directive isn't needed in each container, unlike in named virtual hosting.

Directives for other sites

Directives for site that also run on SSL

Compressing Web Pages Apache has the ability to dynamically compress static Web pages into gzip format and then send the result to the remote Web surfers' Web browser. Most current Web browsers support this format, transparently uncompressing the data and presenting it on the screen. This can significantly reduce bandwidth utilization on slow or heavily used Intenet links. The trade-off is that compression can be very CPU instensive for active websites. Some weberservers have SSL encryption and compression performed by built-in hardware modules or using outboard network devices to take the CPU load off of a web server. To implement compression, load Apache version 2's deflate module in your httpd.conf file, use Location directives to specify which type of files to compress akd and restart Apache. You can verify compression from statistics in the from your /var/log/httpd/access_log file. Fedora's version of httpd.conf loads the compression module mod_deflate by default. Compression Example Insert the following statemnts before the virtual hosting section of httpd.conf: loadModule deflate_module modules/mod_deflate.so

Insert filter

SetOutputFilter DEFLATE

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

Don't compress images

SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary

Make sure proxies don't deliver the wrong content

Header append Vary User-Agent env=!dont-vary

Apache Website Security

Apache security is a complex subject. In addition to having the latest Apache code, it is always worthwhile to review Internet info on the latest Apache security patches and procedures.Website security is only half the battle, proper application coding technniques and code isolation (SELINUX CGI above) are just as important. We’ll only cover website security here. Disable Directory Listings Include an index.html pages in each subdirectory under your DocumentRoot directory. If one isn't found, Apache will default to providing a directory listing of all the files in that subdirectory. You can disable the directory listing by using a -Indexes option in the directive for the DocumentRoot like this:

Options MultiViews -Indexes SymLinksIfOwnerMatch IncludesNoExec Users attempting to access the nonexistent index page will now get a "403 Access denied" message. Apache Server Behind A NAT Firewall The most common network configuration is running a web server on a privately address demilitarized zone (DMZ) behind a network address translating (NAT) firewall. If your web server sits behind a NAT firewall, you may want to have the server respond on both public and private IP addresses. Example: Linux Web server bigboy has an internal IP address of 192.168.1.100, and an external IP address of 97.158.253.26 via NAT. If you are on the inside, 192.168.1.X network, internal DNS may ersolve to the private address, public DNS to the public address. You want the server to respond to both. Here’s how you do it.: Configure Apache to serve the correct content when accessing www.mysite.com or www.another-site.com from the outside, and also when accessing the specific IP address 192.168.1.100 from the inside. Fortunately Apache allows you to specify multiple IP addresses in the statements to help you overcome this problem. : NameVirtualHost 192.168.1. NameVirtualHost 97.158.253.

DocumentRoot /www/server ServerName www.my-site.com

DocumentRoot /home/www

Remember to restart Apache after any configuration file changes.

Troubleshooting Apache

Testing Basic HTTP Connectivity TELNET to port 80 (HTTP) at the desired URL. Failure to do so indicates either a firewall/Nat issue or the service is not started. Basic HTTP Status Codes These are also found in the main httpd.conf file. HTTP_Cod e Description 200 Successful request 304 Successful request, but the web page requested hasn't been modified since the current version in the remote web browser's cache. This means the web page will not be sent to the remote browser, it will just use its cached version instead. Frequently occurs when a surfer is browsing back and forth on a site. 401 Unauthorized access. Someone entered an incorrect username / password on a password protected page. 403 Forbidden. File permissions prevents Apache from reading the file. Often occurs when the web page file is owned by user "root" even though it has universal read access. 404 Not found. Page requested doesn't exist. 500 Internal server error. Frequently generated by CGI scripts that fail due to bad syntax. Check your error_log file for further details on the script's error message. Missing Web Pages – 404 Mesages Default action in Apache for missign web pages is to display of a generic "404 file Not Found" message. You can configure tell Apache to display a predefined HTML file whenever a web surfer attempts to access a non-index page that doesn't exist by You placing this statement in the httpd.conf file:

ErrorDocument 404 /missing.htm Then put a file with the name missing.htm in each DocumentRoot directory. Browser 403 Forbidden Messages Browser 403 Forbidden messages are usually caused by file permissions or security context issues as in SELINUX notes in the above section.. A sure sign of problems related to security context are "avc: denied" messages in your /var/log/messages log file. Nov 21 20:41:23 bigboy kernel: audit(1101098483.897:0): avc: denied { getattr } for pid=1377 exe=/usr/sbin/httpd path=/home/www/index.html dev=hda5 ino= scontext=root:system_r:httpd_t tcontext=root:object_r:home_root_t tclass=file Only The Default Apache Page Appears When only the default Apache page appears, there are two main causes. The first is the lack of an index.html file in your Web site's DocumentRoot directory. The second cause is usually related to an incorrect security context for the Web page's file. Deprecated /etc/httpd/conf/http.conf file format after upgrading Apache Old Apache configuration files may be incompatible when upgrading Apache (especially when jumping versions). The new version 2.X default configuration file is stored in /etc/httpd/conf/httpd.conf.rpmnew. You may have to maunally convert host secetions based on version level requirements. Server Name Errors All ServerName directives must list a domain that is resolvable in DNS, or else you'll get an error similar to these when starting httpd. Starting httpd: httpd: Could not determine the server's fully qualified domain name, using 127.0.0.1 for ServerName Starting httpd: [Wed Feb 04 21:18:16 2004] [error] (EAI 2)Name or service not known: Failed to resolve server name for 192.16.1.100 (check DNS) -- or specify an explicit ServerName You can avoid this by adding a default generic ServerName directive at the top of the httpd.conf file that references localhost instead of the default new.host.name:80. #ServerName new.host.name: ServerName localhost The Apache Status Log Files