Project93.Configure Apache More


Project 93. Configure Apache More

"How do I configure Apache to enable PHP support?"

This project shows you how to configure the Apache Web server that is part of a standard Mac OS X install. It shows how to enable features such as PHP support, server-side includes (SSI), and CGI (Common Gateway Interface) scripts. The project continues Project 92, which describes how to start and stop Apache and how to change the document root. It doesn't teach you how to write Web sites but assumes that if, for example, you want to enable CGI scripts, you know what a CGI script is.

Configure Apache

If you're not familiar with basic Apache configuration, read Project 92 first.

All the Apache configuration directives you'll want to change reside in a single file called /etc/httpd/httpd.conf. We'll be editing this file throughout the project as the root user. For the remainder of this project, we'll assume the status of the root user by issuing the command

$ sudo -s Password: #


Enable PHP Support

To enable PHP support, uncomment the following lines by removing the leading hash symbols.

#LoadModule php4_module   libexec/httpd/libphp4.so #AddModule mod_php4.c


Restart Apache by typing

# apachectl restart


Tip

If you maintain a live Web site and do not want to disrupt the experience of clients who may already have connections, restart Apache by typing

# apachectl graceful



Write a test file called index.php, and put it in the directory ~/Sites (or whatever directory is your document root). This simple script tells PHP to generate a test page that reports on the PHP configuration (Figure 10.2).

Figure 10.2. You'll see this test page if you have successfully enabled PHP support in Apache.


$ cat ~/Sites/index.php <?php   phpinfo() ?>


Load the test page from the URL http://localhost/index.php or any of the equivalent URLs.

For older versions of Mac OS X, it may be necessary to add the following lines at the end of the configuration file but before the Include directive. Check first to see whether you already have them.

<IfModule mod_php4.c>     AddType application/x-httpd-php .php     AddType application/x-httpd-php-source .phps     DirectoryIndex index.html index.php </IfModule>


Search for Index.html

How does Apache know which filenames to search for when none is specifiedfor example, in a URL such as http://localhost/? To answer this question, search for the DirectoryIndex directive in Apache's configuration file. There probably are two of them, and you'll want the second, which looks like this.

DirectoryIndex index.html index.php


The directive tells Apache to check for a file named index.html and serve it if found; otherwise, serve index.php. If neither is found, Apache lists the contents of the document root directory.

Tip

To tell Apache to load the file index.php in preference to index.html where they both exist, swap the order in which the two files are listed in the DirectoryIndex directive. If two DirectoryIndex directives are present, remove one of them.


Enable Server-Side Includes

Server-side includes (SSI) are commands interpreted by the Apache HTTP server as it serves a page. The Apache manual has a short tutorial on SSIs. We'll simply enable them and present a test page to write the current date and time to a Web page as it is served.

Tip

Read the Apache manual at the URL http://localhost/manual/. Don't leave out the trailing forward slash when entering the URL.


Uncomment the following lines to make them active. They tell Apache to treat files whose names end with .shtml as SSI scripts.

AddType text/html .shtml AddHandler server-parsed .shtml


Next, change the options on the directory that serves as the document root. If you followed the steps in Project 92, you'll be looking for the following line (except that it names your home directory).

<Directory "/Users/saruman/Sites">


Otherwise, the directory root will be defined by the line

<Directory "/Library/WebServer/Documents">


Shortly after this line, you'll see an Options directive.

Options Indexes FollowSymLinks MultiViews


Add the option Includes to enable SSI.

Options Includes Indexes FollowSymLinks MultiViews


Restart Apache by typing

# apachectl restart


Tip

Check the syntax of a configuration file by typing

# apachectl configtest



Finally, we must write a test file, which we'll call index.shtml, and place it in the directory ~/Sites (or whatever you have chosen as your root directory). This simple script tells Apache to generate a page that displays the current date and time (Figure 10.3).

Figure 10.3. You'll see this test page if you've successfully enabled SSI support in Apache.


$ cat index.shtml   <p> SSI Test: <br />   <!--#config timefmt="It is now %k:%M on %A %e %B" -->   <!--#echo var="DATE_LOCAL" -->   </p>


Load the test page from the URL http://localhost/index.shtm or any of the equivalent URLs.

If you encounter problems in getting this to work, check the Apache log files and review "Remove Clashing Sites" in Project 92.

Disable Personal Web Sites

To disable personal Web sites that answer to URLs that look like http://computer-name.local/~myusername/, comment out the following lines in /etc/httpd/httpd.conf and then restart Apache.

LoadModule userdir_module libexec/httpd/mod_userdir.so AddModule mod_userdir.c Include /private/etc/httpd/users/*.conf


Control Bonjour (Rendezvous)

To control the way that the main and personal Web sites are advertised over Bonjour, search for the following directive.

<IfModule mod_bonjour.c>


In Mac OS X versions before 10.4 (Tiger), search for

<IfModule mod_rendezvous_apple.c>


Switch on and off the advertising of all user sites, customized user sites, and the main site by activating or commenting out the relevant directives. The options available in versions of Mac OS X before Tiger are more limited.

Enable CGI

The default Apache configuration serves CGI (Common Gateway Interface) scripts from the directory /Library/WebServer/CGI-Executables/. CGI scripts placed elsewhere are not executed but simply displayed as text in the generated Web page. CGIs are enabled by the ScriptAlias directive, which in the default configuration looks like this.

ScriptAlias /cgi-bin/ "/Library/WebServer/CGI-Executables/"


The ScriptAlias directive tells Apache two things. First, when a URL mentions any directory starting with /cgi-bin/..., Apache should actually look in /Library/WebServer/CGI-Executables/... no matter what the document root may be. Second, it says that files in the named directory (and its subdirectories) should be interpreted as executable scripts. In response to the URL http://localhost/cgi-bin/test.pl, for example, Apache will grab and execute a script called /Library/WebServer/CGI-Executables/test.pl.

Tip

The ScriptAlias directive may map any alias to any target directory. The target directory does not even have to be in the document root (but must exist).


Serve CGI from Elsewhere

To serve CGI scripts from elsewhere, you must add more ScriptAlias directives. Assuming that ~/Sites is our new document root, let's serve CGIs from the directory ~/Sites/cgi. First, we'll create the necessary directory. Issue the commands as your own user, not the root user, because you are working in your home directory. Type

$ mkdir ~/Sites/cgi


Tip

Check out the Alias directive. It maps a directory mentioned in a URL to any other directory. The difference between ScriptAlias and Alias is that ScriptAlias additionally enables CGI execution on the target directory.


We'll borrow a simple Perl test CGI from the default document root, typing

$ cp /Library/WebServer/CGI-Executables/test.pl ¬     ~/Sites/cgi/


Edit this test file to work with directories that do actually exist in Mac OS X! The Apple-supplied file we just copied is somewhat flawed. For example, change the line

my $path = "/usr/local/ncbi/";


to

my $path = "/usr/local/";


Next, add a new ScriptAlias directive, and optionally remove the existing directive. Remember to replace the document root given in the example with your own.

ScriptAlias /cgi/ "/Users/saruman/Sites/cgi/"


As ever, restart Apache, and load the URL http://localhost/cgi/test.pl. Apache will load the file /cgi/test.pl from /Users/saruman/Sites/cgi/test.pl and interpret it as a script that should be run by the Perl interpreter.

CGI from Anywhere

You can execute a CGI script from an arbitrary directory and are not limited to those directories named by ScriptAlias directives. To do so, you must add the appropriate the handlers and an option to the document root. Search for the following line in Apache's configuration file.

#AddHandler cgi-script .cgi


Note

Enabling CGIs to run from anywhere in the document root is a security risk compared with limiting them to specific directories.


To run a simple Perl test CGI, named with an extension of .pl, add the following AddHandler directive after the one we just searched for.

AddHandler cgi-script .pl


Next, add the option ExecCGI to the Options directive of the root directory. (We added Includes to the same line when we enabled SSI earlier.)

Options ExecCGI Includes Indexes FollowSymLinks MultiViews


After restarting Apache, copy the test Perl script we used earlier to anywhere in the document root; then browse for it with the appropriate URL. If we copy the script to ~/Sites by typing

$ cp ~/Sites/cgi/test.pl ~/Sites/


we would use the URL http://localhost/test.pl.




Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

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