Developer

10 tips for PHP scripts: Installing PHP as an Apache DSO

Learn how to install a fresh version of Apache and PHP as a dynamic module (DSO).

By Julie Meloni
(2/6/01)

PHP is most often paired with the Apache Web server on a Linux/Unix platform. When installing PHP with Apache, you have three installation options: static module, dynamic module (DSO), and CGI binary.

I recommend the DSO installation option as it's extremely easy to maintain and upgrade. For example, suppose you do a simple installation of PHP with just database support. A few days later, you decide that you want to install encryption support. All you have to do is type make clean, add the new configuration option, and followed by make and make install. A new PHP module will be dumped in the proper location for Apache, and all you have to do is restart Apache, not recompile it.

These basic steps will install a fresh version of Apache and PHP as a DSO:

1. Get the latest version of the Apache source code from the Apache Software Foundation.
2. Put this file somewhere logical, such as /usr/local/ or /opt/ or anywhere else you want.
3. Gunzip or uncompress the file, so that you're left with the *.tar file.
4. Type the following to un-tar the file into a directory called apache_[version]:

tar -xvf apache_[version].tar

5. cd into /usr/local/apache_[version] (or wherever you un-tar'd the compressed source file). 6. Type the following to prepare for building, replacing [path] with your own path, such as /usr/local/apache[version] (no trailing slash!). You are now enabling mod_so, which will allow Apache to use DSOs.

./configure —prefix=[path] —enable-module=so

7. When you're back at the prompt, type make, and wait for the prompt to return again.
8. Type make install.

At this point, the compilation will create the final set of directories and files and return you to the prompt.

Next, install PHP:

1. Go to the Downloads area of the PHP home page and select the link for the latest source files.
2. Put this file somewhere logical, such as /usr/local/ or /opt/ or anywhere else you want.
3. Gunzip or uncompress the file, so that you're left with the *.tar file.
4. Type the following to un-tar the file into a directory called php-[version]:

tar -xvf php-[version]

5. cd into /usr/local/php-[version] (or wherever you put it)

You're now ready to build the PHP DSO. Only one configuration option is necessary—with-apxs (a file in the Apache bin directory)—but I'll throw MySQL support in for good measure.

./configure —with-mysql=/[path to mysql] —with-apxs=/[path to apxs]

6. When you're back at the prompt, type make, and wait for the prompt to return again.
7. Type make install.

At this point, the compilation will create the final DSO, plop it in the Apache modules directory, modify some parts of the Apache httpd.conf file for you, and return you to the prompt. When you're back at the prompt, you'll need to go back to the Apache httpd.conf and make a few modifications:

1. Find the line for ServerAdmin, and add your e-mail address, in other words:

ServerAdmin you@yourdomain.com

2. Find the line starting with ServerName, and change it to real values, such as:

ServerName localhost

3. Find a section like the following:

# And for PHP 4.x, use:
#
#AddType application/x-httpd-php .php
#AddType application/x-httpd-php-source .phps

Modify these lines so that the AddType for PHP 4.0 is uncommented, and add any more file extensions you want to use with PHP, so that the block looks like this:

# And for PHP 4.x, use:
#
AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps

Save the file, cd up a directory, and start Apache by typing:

./bin/apachectl start

If there are no errors on startup, you can test your installation of Apache and PHP by creating a file called phpinfo.php, containing just this line:

<? phpinfo() ?>

Save this file and put it in Apache's document root (htdocs), then fire up your Web browser and go to http://localhost/phpinfo.php, where you should see a long page of variables and their values.

If you want to reconfigure PHP, all you need to do is run make clean, then a new ./configure command with a new set of options, then make and make install. A new module will appear in the Apache modules directory, and just restart Apache to load this new module. Many headaches are now relieved.

Julie Meloni is the technical director at i2i Interactive and is an avowed proponent of Linux and the open source community. A regular contribtor to CNET Builder.com, she has written a few books on PHP and other technologies.

Editor's Picks