INLS 183 Project 2: Apache_1.3.12 and PHP-4.0.2

Introduction to Apache and PHP

One of the first webservers was designed at the National Center for Supercomputing Applications (NCSA), not coincidentally the same place that Marc Andreessen wrote one of the first web browsers, Mosaic, the precursor to Netscape Navigator. As this was a government installation, the source code of the software was deemed public domain. By early 1995, after work on NCSA’s http daemon had stalled since mid-1994, a loose group of webmasters and programmers, connected by email, decided to update and improve the server software, initially in the form of  'patches' hence "a-patchy project." Eventually this group overhauled most of the code and released version 1 on December 1, 1995. Apache is now credited as the most popular webserver on the Internet.

It makes sense then that at the same time the popularity of the web was exploding, people were looking for ways to generate or 'script' HTML just before the page is sent to the requesting client. In 1995 Rasmus Lerdorf created a Perl CGI script to log the people that were visiting his web-based resume. Thus the content of his page was automatically updated or 'dynamic,' and people wanted to know how he did it. Thus he began giving away his “Personal Home Page” tools. In 1996 after a job in Toronto, he released the second version, called PHP/FI which he created to access a library database from a web interface. The immensely popular version 3 was released with the assistance of Zeev Suraski and Andi Gutmans, and version 4 came out in May of this year. PHP continues to be a popular and open source method for dynamically generating web content on the server as well as interacting with various database formats. It has also grown popular do to its incorporation with the Apache web server. As a result, the webserver today processes and generates HTML as much it responds to and serves requests for pages.

I knew the Apache install would be pretty straightforward, and I’ve been interested in working with some server-side scripting languages, like PHP and Perl, so I decided to combine the installation of Apache and PHP into one project.

Apache and PHP Installation

My first installation of Apache_1.3.12 (from the source) was very easy, however when I read the PHP-4.0.2 installation instructions, I realized I’d have to redo the Apache install. And needless to say, even after I started from scratch, my first two attempts at installing Apache-PHP from these instructions failed. The method documented in those instructions (from the PHP Manual) dealt with compiling PHP into Apache, which would allow PHP to parse documents faster. The next best alternative would be to install PHP as a Dynamic Shared Object (DSO) of Apache. I found the best instructions for that process in the INSTALL file in the php-4.0.2/ directory (after untarring). However this file pointed to the INSTALL file in the apache_1.3.12/ directory which had to be installed first. After numerous efforts, I worked out all of the kinks, and what follows is a simplified set of instructions for installing both Apache and PHP---with the some of the pitfalls I experienced noted along the way.

Step 1) Install flex and bison. This will avoid PHP ./configure problems that I encountered in Step 3 below. I installed:

# rpm –ivh bison-1.28-2.i386.rpm
# rpm –ivh flex-2.5.4a-9-i386.rpm

Step 2) Install Apache with mod.so, which allows for Dynamic Shared Objects (DSO’s):

# ./configure --enable-module=so
# make
# make install

At this point, Apache is installed in /usr/local/apache/ and can be started. However, before we do that we need to install PHP.

Step 3) Install PHP with MySQL and APXS. MySQL is relational database management system (RDBMS). By including it in the PHP install, it allows for web-database connectivity in the future. APXS stands for APache eXtenSion tool, used for building Apache modules.

The PHP INSTALL file suggests using the following line to configure PHP for compilation:

# ./configure --with-mysql --with-apxs

However, after getting the following error:

checking for Apache module support via DSO through APXS... no
checking for Apache module support... yes - Apache 1.3.x
configure: error: Please run Apache\'s configure or src/Configure program once and try again

I read back over the PHP INSTALL instructions and realized that I’d need to give the path to APXS---which I found using the locate command:

# locate apxs
/home/jwatt/apache_1.3.12/src/support/apxs.8
/home/jwatt/apache_1.3.12/src/support/apxs.pl
/home/jwatt/apache_1.3.12/src/support/apxs
/usr/local/apache/bin/apxs
/usr/local/apache/man/man8/apxs.8

First I tried this:

# ./configure --with-mysql --with-apxs=/usr/local/apache/bin/

Which didn’t work so I tried this:

# ./configure --with-mysql --with-apxs=/usr/local/apache/bin/apxs

which did work. [./configure went on to give me an error because I didn’t have flex installed, and it gave me a warning because I didn’t have bison installed---hence Step 1]

I finished the PHP install successfully with:

# make
# make install

The final task to complete the PHP installation involves copying the PHP configuration file from php-4.0.2/ to it’s permanent home:

# cp php.ini-dist /usr/local/lib/php.ini

Apache and PHP Configuration

The hearts of Apache and PHP are their respective configuration files. Apache’s is /usr/local/apache/conf/httpd.conf and PHP’s is /usr/local/lib/php.ini. Though there are numerous changes and adjustments that can be made in each to tweak and customize performance, I was mostly interested in getting PHP to parse a PHP document. For simplicity and space’s sake, I am only going to list the lines of each configuration file that were pertinent to this installation.

In httpd.conf:
# Dynamic Shared Object (DSO) Support
LoadModule php4_module libexec/libphp4.so  # critically important for PHP to work

### Section 2: 'Main' server configuration
Port 80
ServerName http://xxx.xxx.xxx.xxx/         # http://152.2.38.210/ in my case

# And for PHP 4.x, use:
AddType application/x-httpd-php .php       # critically important for PHP to work

The LoadModule directive is necessary to tell Apache where to find PHP module or DSO. The AddType line is necessary to tell Apache what type of files should be parsed by PHP, in this case, .php files.

In php.ini:
; Language Options ;
engine          =       On      ; Enable the PHP scripting language engine under Apache
short_open_tag  =       On      ; allow the <? tag.  otherwise, only <?php and <script> tags are recognized.

The above two directives are the defaults after installation, however, if PHP does appear to be working, it pays to make sure they are both set to "On". The first enables the PHP engine, the second allows for the ubiquitous <? ?> tags.

PHP Demonstration

The only way to determine if the installation process of Apache and PHP was a success is to write a simple PHP document and to save it as filename.php.

PHP example 1:
<!-- The PHP tag below generates a lot of diagnostic PHP information in HTML format -->
<?phpinfo()?>

If PHP isn’t working, a web browser might just present the .php file as text, exposing the PHP tag. Or perhaps the web browser might prompt you to download the file, not knowing how to display it. In either case this is a sign that PHP is not parsing the PHP tag. Assuming that all the steps above have been followed, and that there were no unexpected complications along the way, the first solution to take would be to restart the Apache webserver in order for those crucial additions to the httpd.conf file to be activated. You can do this by typing

# /usr/local/apache/bin/apachectl restart

Then go back and make sure all of the changes in the httpd.conf file were made correctly and are uncommented. If all else fails, start over and reinstall Apache and PHP, making sure to pay close attention to the messages that scroll by.

On the other hand, if PHP is parsing .php files, then the PHP document above, should look like this: (screen shot)

Ok, so PHP is working, now what can it do? I culled the following example below from a tutorial on the Zend website. Notice that the entire file is enclosed in <? ?> tags. $html is a string variable that PHP concatenates onto using the .= operator. The for loop generates the majority of the content, in this case the squares of the numbers 0 to 9--with the surrounding HTML tags--that create a table in which to hold the values. The final line before the ?> end tag echoes the $html variable out, which is what the browser uses to construct the output for the user.

PHP example 2:
<?
$html = '<html><body><table>';
$html .= '<tr><td>number</td><td>square</td></tr>';
for ( $i=0 ; $i<10 ; $i++) {
     $square = $i * $i;
     $html .= '<tr><td>' . $i . '</td><td>' . $square . '</td></tr>';
}
$html .= '</table></body></html>';
echo $html
?>

Produces this HTML output (line breaks added for readability):
<html><body><table><tr><td>number</td><td>square</td></tr>
<tr><td>0</td><td>0</td></tr><tr><td>1</td><td>1</td></tr>
<tr><td>2</td><td>4</td></tr><tr><td>3</td><td>9</td></tr>
<tr><td>4</td><td>16</td></tr><tr><td>5</td><td>25</td></tr>
<tr><td>6</td><td>36</td></tr><tr><td>7</td><td>49</td></tr>
<tr><td>8</td><td>64</td></tr><tr><td>9</td><td>81</td></tr>
</table></body></html>

Which the browser renders as:
number square
 0       0
 1       1
 2       4
 3       9
 4       16
 5       25
 6       36
 7       49
 8       64
 9       81

Though this example is elementary, it hints at PHP's potential power when it comes to reading through records in a database and outputting the results in HTML for the browser to render.

Final Thoughts

Though my write-up may have obscured the fact that I installed Apache and PHP at least 5 times before I got everything right, the final process (described above) was still rather straightforward. However, there was one small issue in this process that seemed almost impossible to solve. The situation was simple: I wanted PHP to parse files with an .html extension. Nothing indicated that this might be a problem. The documentation mentioned that the following line in the httpd.conf file:

AddType application/x-httpd-php .php

would only have to be changed to this:

AddType application/x-httpd-php .php .html

However, this simple adjustment caused me unknown frustration. When I made this change, PHP continued to parse the .php version of my document, but when I pointed my browser to the .html version of the same exact file, the browser didn't know what to do. Both Netscape and Internet Explorer would attempt to download the file, which as I discovered, was unparsed. I spent several hours searching for a solution via www.google.com, www.deja.com, www.phpbuilder.com, etc. etc. Several other mentioned this problem, but no solutions ever came of their postings. After several hours of researching this infinitesimally small issue, I had an epiphany looking at the lines in the httpd.conf file for server-side includes (SSI):

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

I noticed that it took both lines to tell Apache what to do with .shtml files. The first defined .shtml as type text/html and the second told Apache what to do with .shtml files. It occurred to me that perhaps the PHP AddType line above didn’t know what an .html file was, and maybe I should define .html just like .shtml is defined. Thus I added these lines to my httpd.conf file:

AddType text/html .html
AddType application/x-httpd-php .php .html

I restarted the server and it worked!

INLS 183 Project 2: Apache_1.3.12 and PHP-4.0.2 script file

Resources

Apache: The Definitive Guide, 2nd Edition, Ben Laurie & Peter Laurie, O'Reilly, 1999
www.php.net: Home of PHP: Hypertext Preprocessor
www.apache.org/httpd.html: Apache HTTP Server Project
www.zend.com: Where PHP meets eBusiness
www.phpbuilder.com
www.google.com: Search Engine