<<

. 50
( 69 .)



>>

Return, then type print
/sw/apache/cgi-bin/mycgi.pl
‹ Type ° Type print h1("$greet");
and press Return. header; and press Return
/sw/apache/conf/httpd.conf and again. and press Return, type print
press Return. hr; and press Return twice,
and then type print
242
end_html;.
14
SERVE WEB PAGES WITH APACHE

To make use of the Perl CGI module, add the command
line use CGI. In the following CGI example, this
command is included with standard options.

Example:
#!/usr/bin/perl -w

use CGI qw(:standard);
print header;
print start_html("this is a CGI");
$greet="Thanks for visiting";
print h1("$greet");
print hr;
print end_html;

Most of the print lines in this simple CGI script are
actually calls to the CGI module. When you type print
header, for example, a routine in the module composes
a proper HTML header. When you type print h1 and
include a text message or a string variable inside
parentheses, another module composes HTML code
for a level-1 HTML heading.




– Your new CGI runs and
· Save your file, and exit ‚ Type sudo chmod a+x ± Open Internet Explorer in
Pico. the Dock. displays the output.
/sw/apache/cgi-bin/mycgi.pl
and press Return.
– Pico saves your CGI. ¡ Type http://localhost/cgi-
” If a prompt appears, type bin/mycgi2.pl into the URL
your password. field, and press Return.



243
UNIX FOR MAC



INSTALL APACHE MODULES
After you compile a module into a DSO, you can use the

Y
ou can download and install additional Apache
LoadModule command that is part of the mod_so module
modules to expand the capabilities of your Apache
in your httpd.conf file. This causes the module to load when
server. While the default Apache installation sets up a
you start or restart Apache. The exact line that you enter
very adequate and responsive Web server, the default set of
depends on the module that you are installing. The
modules that are included may not meet all of your needs.
instructions that you obtain with the module, often found in
You can obtain a list of the modules that your installation of
a file called README or INSTALL, tell you which line to enter.
Apache already supports by typing httpd “l.
To build a module into your Apache binary, you need to
To upgrade your Apache server to include additional
download the module, install the files into the modules
modules, you must download the new modules. If your
directory for your Apache distribution, and recompile.
Apache installation includes the mod_so module, you can
Again, the exact process depends on the module that you
install additional modules without having to recompile
are installing. Read the README and INSTALL files for
Apache. This is normal with recent installations of Apache.
Apache and the module that you are installing.
The mod_so module provides Apache with support for
dynamic shared objects (DSO). This means that you can use
Fortunately, the Apache Web site offers a list of modules
an external module without changing Apache itself ” that
along with a description of each one. This information helps
is, without having to recompile. DSO modules load when
you to find a module that can add the functionality that you
Apache starts up.
require.



INSTALL APACHE MODULES




– The browser takes you to
„ Open Internet Explorer ‹ Click search for modules. › Type your search term.
from the Dock. the Apache site to search for
modules.
¤ Type http://modules.
apache.org/ in your URL
field, and press Return.


244
14
SERVE WEB PAGES WITH APACHE




Most Apache modules contain fairly detailed
Most, if not all, Apache modules arrive as
instructions that tell you the commands you must
gzipped TAR files. This means that you must use
type to install your new module.
the gunzip and tar “xf commands to extract
your source files. You must then type cd into the
If you install modules using the dynamic method,
target directory and look for a README or
you must confirm that your Apache daemon
INSTALL file, which should contain detailed
supports mod_so; to do this, type the command
instructions for installing the module.
httpd “l and look for mod_so in the output. If you
prefer the static method, whereby the new module
To install a module using DSO, you follow
is built into your apache binary, or if your Apache
directions similar to these:
daemon does not support DSO, you must first
Example: compile the new module, then follow instructions
$ ./configure ”with-apxs to move the compiled files into your Apache source
$ make tree, and then recompile Apache.
$ make install




– The download page
ˇ Scroll through the list of Á Click the module you ‡ Click the link provided to
modules containing your want to download. appears. download your module.
search string.




245
UNIX FOR MAC


WRITE PHP APPLICATIONS
You must always enclose PHP scripts between two PHP

Y
ou can develop PHP applications for use in your Web
tags. The tag pairs can be in one of several forms, such as
site. PHP is a relatively new language that integrates
<? and ?> or <?php and php?>, although some
easily with the Apache Web server. When you can
programmers use <?php and ?>.
code in PHP, you can dramatically reduce the time that it
takes for you to develop Web applications. In order to use
PHP uses many commands, including printing, looping, if
PHP, your Apache binary must support it.
commands, arrays, and forms, but if you are already familiar
with HTML, you should grasp the fundamentals of PHP
To understand the similarities between PHP and HTML, you
quite easily.
have only to look at some sample code. For example, to
print a variable on a Web page, PHP mimics the style of
PHP is a server-side scripting language. This means that the
HTML commands while using special tags that only belong
interpreter has to be installed and configured on the server
to PHP:
before it can be used. No special client-side setup is
required. The language combines features from Perl, Java,
<html>
and C, so most people who have programmed in any of
<head><title>Latest Counts</title></head>
these languages will feel at home with PHP.
<body><?php echo $grp1cnt<br>; ?></body>
</html>
All text to be written to the screen must be enclosed in
double-quotes and almost all commands will end in a
PHP files do not need to be executable, and the output
semicolon. Pages containing PHP commands should be
appears indistinguishable from HTML.
saved with the extension .php.

WRITE PHP APPLICATIONS




¤ Type /sw/apache/htdocs ‹ Type <html> and ˇ Type </body> and press
TEST PHP
/testme.php and press Return. press Return, then type Return, then type </html>.
„ Type sudo pico followed
<title>testing</title> and
Á Save your text, and exit
by a space.
press Return again.
Pico.
› Type <body> and press
Return, then type <?php
phpinfo(); ?> and press
Return again.
246
14
SERVE WEB PAGES WITH APACHE




Basic comparison operators include all
You can mix PHP and HTML easily in a single file.
those listed in the table below.
When you do so, you enclose each of your PHP
statements in a pair of PHP tags, as in the sample
OPERATOR DESCRIPTION
below:
== Equal
Example:
!= Not equal
<?php $season="summer"; ?>
= Assignment
We are now accepting reservations for our
<?php print $season season; ?> * Multiplication
Call 1-800-FUN-VACS today.
/ Division
+ Addition
This code segment assigns a value to $season
and then uses this value to customize the - Subtraction
content of the remainder of the display. In a . Concatenation
similar manner to Perl, PHP allows you to create
&& Logical AND
simple data types, like $season, and arrays in
|| Logical OR
which you can use both a variable name and an
index, such as $season[1].




– The browser displays a
‡ Open Internet Explorer in ‹ Type while ($x < $times) {
CREATE A PHP
the Dock. page of information about and press Return, type echo
„ Start Pico to create a file
PHP if your PHP installation "Hello, World"; and press
° Type http://localhost/ name hello.php.
is active. Return again, and then type
testme.php in your URL
¤ Type <? php and press ++$x; and press Return.
field, and press Return. Return, type $times=11; and
› Type } and press Return,
press Return again, and then
then type ?>.
type $x=0; and press Return
ˇ Save your file.
twice.
247
UNIX FOR MAC



<<

. 50
( 69 .)



>>