Apache: A Web Server

Apache: A Web Server

Apache is the most popular Web server on the Internet. It's easy to obtain and install, stable and secure, and reasonably easy to configure and modify; it handles both small and large Web sites; and it has a huge number of available options. Several common versions of Unix come with Apache already installedincluding, of course, Mac OS X.

Apache gets its name from its history. Back at the dawn of the Web (circa 1993), the folks at the National Center for Supercomputing Applications (NCSA), at the University of Illinois at Urbana-Champaign, created the NCSA Web-server application, called HTTPD ( Hypertext Transfer Protocol daemon ). It was (and is) an open -source application, and many people contributed code for it, known as patches . So many patches were contributed that it became known as "a patchy server." "A patchy" morphed into "Apache."

Activating Apache

As with AppleShare and the SSH server, you can activate and deactivate the Apache Web server from the Sharing pane of System Preferences, on the Services tab, by clicking the Start/Stop button or checking the box for Personal Web Sharing.

Web servers listen on port 80 by default. If you are using firewall software, make sure that access to port 80 is not blocked. (Activating Apache via System Preferences does this automatically.)

Here's the command-line version. Astute readers will note that these tasks are almost identical to the ones for the SSH server.

Apache Documentation

Mac OS X comes with the complete Apache documentation in HTML format.

If you turn on Personal Web Sharing in the Sharing pane of System preferences, you can browse the documentation with the URL

http://127.0.0.1/manual/

Or, if Web sharing is not on, then with this URL:

file:///Library/WebServer/Documents/manual/index.html.html

(Yes, html appears twice in that filename.)

More documentation, as well as specific how-to documents, is available through the Apache HTTP Server Project (http://httpd.apache.org/docs-project). The site contains a document on performance tuning, for example (http://httpd.apache.org/docs/misc/perf-tuning.html).


To activate Apache from the command line:

1.
Per the task "To change the /etc/hostconfig system-configuration file," become root, back up /etc/hostconfig , and edit the file using a text editor such as vi .

Note that this leaves you in a root shell.

2.
Change the line that says

WEBSERVER=-NO-

to

WEBSERVER=-YES-

3.
Save your changes, and quit the editor.

4.
Start the Apache server:

apachectl start

The Apache Web server is now active.

The apachectl script ( /usr/sbin/apachectl ) is used by the Mac OS X StartupItem for Apache, so you can also do

SystemStarter start Apache

But the apachectl script offers more options. See man apachectl .

5.
Stop being root:

exit

You should be back at your regular shell prompt.

Tips

  • To keep the Apache server from starting at boot time, reverse the change from step 2 above. That is, change WEBSERVER=-YES- back to WEBSERVER=-NO- .

  • Apache logs all connections to /var/log/httpd/access_log , and error-message and startup/shutdown events to /var/log/httpd/error_log .

  • If you have the firewall running, you must open access on port 80 for people to reach your Web server.


More About the Apache Startup Script

The Apple-supplied StartupItem script, /System/Library/StartupItems/Apache/Apache , actually executes the standard Apache startup script, apachectl .

On Mac OS X, apachectl is installed as /usr/sbin/apachectl . It is a Bourne shell scriptyou can look at it with any text editor to see how it works.

On most Unix systems where Apache is installed, the apachectl script itself is used to start (and stop) Apache, because that script handles arguments of start , stop , restart , and others. There is a man page for apachectl showing all of its options.

You can use apachectl directly (ignoring /etc/hostconfig ), if you want.

On Mac OS X, it will be perfectly effective for you to start Apache with

sudo apachectl start

and to stop it with

sudo apachectl stop


To browse your Web server:

  • Enter the URL for your Web server into your favorite Web browser.

  • If you are browsing from the same machine the server is on, you can simply use

    http://localhost/

    Note: That URL will not work in the Microsoft Internet Explorer browser; instead use http://127.0.0.1/

  • If you want to browse your machine from another machine, you need to know your machine's FQDN or IP address: www.mozilla.org or http://207.200.81.215/.

Tips

  • The main HTML directory for your Web server is /Library/WebServer/Documents , which is defined in the Apache-configuration file ( /etc/httpd/httpd.conf ) by the DocumentRoot directive.

  • Mac OS X automatically creates a separate Apache-configuration file for each user you create, through the Accounts pane in System Preferences. The user -configuration files are in /etc/httpd/users and are named after each userfor example:

    /etc/httpd/users/puffball.conf

    The last line of the main Apache-configuration file reads all the files in the /etc/httpd/users directory when Apache starts up. The default is for each user to have his or her own directory of HTML pages, which is the Sites directory in each user's home directory. The URL for a user's personal Web page is

    http:// domainname /~ username /

    For user "puffball," it would be

    http://localhost/~puffball/

    or

    http:// domainname /~puffball/


You can also shut down the Apache server from the command line.

To shut down the Apache server from the command line:

  • sudo apachectl stop

    Enter your password if prompted.

    When the Apache server starts up, it writes its process ID ( pid ) number into the file /var/run/httpd.pid . The apachectl script uses that file to find the pid number and stop the server. See the sidebar "More About the Apache Startup Script."

Adding a CGI script

One of the most importantand excitingthings you can do with Apache is to build your first Common Gateway Interface (CGI) script. CGI is the standard for how Web servers communicate with other software. A CGI program can be written in any language, as long as it adheres to the CGI standard.

Basically, CGI works this way: The Web server is configured to treat certain requests as CGI requests . When the Web server gets a CGI request, it executes a program instead of simply sending back an HTML page. The CGI program then does . . . something. It might contact a database or send some e-mail. (A CGI program can be written to do anything any other program can do.) It then sends a response back to the Web server, which in turn passes that response back to the Web browser. Often that response is an HTML document generated on the fly, perhaps based on a database connection.

Typically, CGI requests begin with /cgi-bin , but they can begin with anything the person doing the configuration decides. In Mac OS X, the Apache server is configured so that any request starting with /cgi-bin is a CGI request, and Apache looks for the CGI program to execute in the directory /Library/WebServer/CGI-Executables .

So if Apache gets a request for /cgi-bin/shopping.pl , it will execute the file

 /Library/WebServer/CGI-Executables/  shopping.pl 

The process of installing your first CGI program involves creating the program (we will use a simple script in this case), and telling the Web server where the CGI programs are and which requests to treat as CGI requests. We'll use a simple script as an example. This Perl script produces an HTML page that shows all the environment variables present at the time the script is run. Apache sets a large number of environment variables when it executes a CGI script. These environment variables contain a great deal of information about the Web server and the request, so a script like this is very useful for troubleshooting and debugging.

To create a CGI script:

1.
cd /Library/WebServer/CGI-Executables

This is where you will create the new script.

2.
Copy the script from Figure 14.12 into a new file called test.pl.

Figure 14.12. Code listing of a simple CGI script that displays its environment variables.
 #!/usr/bin/perl # Simple CGI test script # Displays environment variables in HTML format # Save as /Library/WebServer/CGI-Executables/test.pl ################################################################### # Anything printed out gets sent back to the Web server # and then to the Web browser. # Print everything up until the line that starts with BUNNY print <<"BUNNY"; Content-type: text/html <html> <head> <title>CGI Test Output</title> </head> <body bgcolor="#ffffff"> <h1>CGI Test Output</h1> <table> <tr>        <th>Variable<th>        <th>Value</th> </tr> BUNNY # Iterate over all the environment variable names. foreach $variable ( sort keys %ENV ) {        print <<"BUNNY";        <tr>        <td><font color="blue">$variable</font></td>        <td><font color="red">$ENV{$variable}</font></td>        </tr> BUNNY } print <<"BUNNY"; </table> </html> BUNNY 

The URL for this script will be

http://localhost/cgi-bin/test.pl

You do not need to be root for this step, but you must be in group admin. (Throughout this book we assume you are an admin user.)

3.
Make the script executable:

chmod 755 test.pl

4.
Test the script from the command line:

./test.pl

You should get output similar to that in Figure 14.13 .

Figure 14.13. Abbreviated output from the CGI test script at the command line.
 [g4-cube:/Library/WebServer/CGI-Executables] vanilla%  ./test.pl  Content-type: text/html <html> <head> <title>CGI Test Output</title> </head> <body bgcolor="#ffffff"> <h1>CGI Test Output</h1> <table> <tr>        <th>Variable<th>        <th>Value</th> </tr>             <tr>             <td><font color="blue"> ENV_SET</font></td>             <td><font color="red"> </font></td>             </tr>             <tr>             <td><font color="blue"> GROUP</font></td>             <td><font color="red"> staff</font></td>             </tr>             <tr>             <td><font color="blue"> HOME</font></td>             <td><font color="red"> /Users/matisse</font></td> . . . Output continues... 

5.
Test the script from a Web browser.

To test from the same machine, you can use the URL

http://localhost/cgi-bin/test.pl

You can also use your IP address or hostname instead of "localhost." Of course, if your machine has an FQDN, you can use that too, and it should work from anywhere on the Internet.

Figure 14.14 shows the output from the script in a browser window, so we know it works.

Figure 14.14. A Web-browser window showing output from the CGI test script.

Learning More About CGI

The CGI standard has been very stable, but it is still evolving.

You can read about the effort to formalize the CGI 1.1 specification at the Common Gateway InterfaceRFC Project Page (http://ken.coar.org/cgi/).

An introductory book for creating CGI programs with Perl is Perl and CGI for the World Wide Web: Visual QuickStart Guide , Second Edition, by Elizabeth Castro (Peachpit Press; www.peachpit.com).


6.
Congratulationsyou've created a CGI script and are hosting dynamically generated HTML pages.

If you want to install a CGI script somewhere besides the default location, you will need to make a change to the Apache-configuration file.

To install a CGI program in your personal Sites directory:

1.
Create a cgi-bin directory inside your Sites directory:

mkdir ~/Sites/cgi-bin

2.
Copy the script from Figure 14.12 into the new cgi-bin directory.

  • If you already created a script in the previous task, you can just copy that file.

  • If you are creating the script as a new file, be sure to make its mode executable by all when you are done:

    chmod +x filename

3.
Become root, entering your password if prompted:

sudo -s

4.
Edit the Apache-configuration file.

You will be editing the Apache configuration for just your one user. For example, for the user vanilla:

vi /etc/httpd/users/vanilla.conf

Add the following line to the file:

 ScriptAlias "/~vanilla/cgi-bin/"  "/Users/vanilla/Sites/cgi-bin/" 

Figure 14.15 shows the resulting file.

Figure 14.15. Code listing of user vanilla's Apache-configuration file ( /etc/httpd/users/vanilla.conf ) after adding a line to enable a cgi-bin directory.
 <Directory "/Users/vanilla/Sites/">        Options Indexes MultiViews        AllowOverride None        Order allow,deny        Allow from all </Directory> ScriptAlias "/~vanilla/cgi-bin/" "/Users/ vanilla/Sites/cgi-bin/" 

Be sure to save the changes to the file before you quit the editor.

5.
Test the Apache-configuration file:

apachectl configtest

The apachectl command checks all of the Apache-configuration files for errors (the main configuration file and those for each user); the output looks like that in Figure 14.16 .

Figure 14.16. Using the apachectl command to check the Apache-configuration files for syntax errors.
 [g4-cube:/Users/vanilla/Sites/cgi-bin] root#  apachectl configtest  Processing config directory: /private/etc/httpd/users/*.conf  Processing config file: /private/etc/httpd/users/matisse.conf  Processing config file: /private/etc/httpd/users/puffball.conf  Processing config file: /private/etc/httpd/users/scott.conf  Processing config file: /private/etc/httpd/users/vanilla.conf Syntax OK [g4-cube:/Users/vanilla/Sites/cgi-bin] root# 

6.
Restart the Apache server:

apachectl graceful

The apachectl command has two ways of restarting the server. The argument graceful allows any current connections to finish before the server restarts, while the argument restart kills all current connections before the server restarts.

7.
Stop being root:

exit

You should be back at your normal shell prompt.

8.
Test the CGI through a Web browser.

Assuming you logged in as the user vanilla's account, the URL is http://localhost/~vanilla/cgi-bin/test.pl.

You should get almost exactly the same result as in the previous task and Figure 14.14, only the values of some of the environment variables will be different (have a look at REQUEST_URL , SCRIPT_FILENAME , and SCRIPT_NAME in particular).

Apache Version 1.3.x vs. 2.x

Mac OS X 10.4 comes with version 1.3.26 of Apache. In May 2002, though, Apache version 2.0 was released.

The 2.x family of Apache is a major rewrite of Apache, offering significantly improved speed on some systems (for the geeks in the audience, Apache 2.x supports POSIX threads). The new features in Apache 2 are listed at httpd.apache.org/docs-2.0/new_features_2_0.html.

Apache 2 has been slow to catch on, mostly because Apache 1.3.x is so good, and some of the more popular add-on modules for Apache 1.3 have not yet been rewritten for Apache 2. Still, Apache 2 is the future, and if you are thinking of being heavily involved in managing a Web site, you should look into Apache 2.




Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net