Apache 2 and PHP Installation

The following notes are how I got Apache 2 and PHP 5 (or PHP 4) working together on Linux. These instructions also apply, mostly, for any UNIX-like system, especially other Linux distributions. If you have a recent Linux distribution (say since 2002), you already have Apache 2 and PHP, so you don’t need to do this unless you want the latest Apache 2 or PHP release or need to customize the Apache or PHP software. Don’t forget to remove (or at least disable) the Apache rpm package if you install your own custom Apache.

Apache 2 Version Tip: Beginning with Apache 2.0.42 the API will be kept stable (yeah!). That means you will NOT have to recompile modules (and possibly upgrade/fix source) every time you install a new Apache release. This assumes you stay in the same Apache release series. For example, upgrading from 2.2.0 to 2.2.2 should work. This will not apply to upgrading to the next series (e.g., “development” 2.3.x or “stable” 2.4.x).

I was only able to get PHP working with Apache 2 as a *.so DSO (”dynamic shared object”), as opposed to compiled into Apache (with a static *.a library). I think DSO is the only way PHP is supported now on Apache 2.

I first used httpd-2.0.43 and php-4.3.0 with RedHat 7.3. I am now using httpd-2.2.3 and php-5.2.0 with SUSE 10.1.

Note: If you have problems with PHP and think it’s a recent bug, you may want to consider using the latest http://snaps.php.net/ snapshot. Beware that snapshots frequently have regression and are not for production use. Usually problems are because of mis-configuration, not bugs, so snapshots will probably hurt more than help.

1. Download/unpack Apache2 source from the Apache httpd server website, http://httpd.apache.org/

2. In the Apache 2 source directory, create a Makefile by typing:

./configure –prefix=/usr/local/apache \
–enable-so \
–enable-cgi \
–enable-info \
–enable-rewrite \
–enable-speling \
–enable-usertrack \
–enable-deflate \
–enable-ssl \
–enable-mime-magic

You only need the enable-so line above. For information on other options, type ./configure –help and see “Compiling and Installing” in the Apache 2 Documentation, http://httpd.apache.org/

3. Make Apache from the just-created Makefile:

make

4. If make is successful, install Apache as root:

make install

5. Download/unpack PHP source from the PHP website, http://www.php.net/
Pick the latest from the 4.x series or 5.x series.

6. In the PHP source directory, create a Makefile by typing:

./configure \
–with-apxs2=/usr/local/apache/bin/apxs \
–with-mysql \
–prefix=/usr/local/apache/php \
–with-config-file-path=/usr/local/apache/php \
–enable-force-cgi-redirect \
–disable-cgi \
–with-zlib \
–with-gettext \
–with-gdbm

You only need the –with-apxs2, and prefix lines. –with-mysql adds MySql (you need to specify the directory if it’s in a unusual location (e.g., –with-mysql=/usr/local ), –with-config-file moves the php.ini file location, disable-cgi disables the CGI version, which is not needed if you use Apache modules. It also enables and installs the command line interface (CLI) version. –with-zlib allows use of gzip-type compression, –with-gettext is for internationalization, and –with-gdbm allows access to GDBM databases. For more information, type ./configure –help and see the “Installation” chapter in the PHP Manual, http://ww.php.net/docs.php

7. Make PHP from the just-created Makefile:

make

8. If make is successful, type this as root to install PHP:

make install

If you are not root (I do not perform makes while root, for security and safety reasons), become root and type the following:

make install-su

9. If file /usr/local/apache/modules/libphp5.so does not exist or is an older version, type this (change this to libphp4.so for PHP 4):

cp -p .libs/libphp5.so /usr/local/apache/modules

10. Install the php.ini file:

cp -p php.ini-recommended /usr/local/apache/php/php.ini

11. Add these directives are in /usr/local/apache/conf/httpd.conf (if already there, verify they are correct):

# Make sure there’s only **1** line for each of these 2 directives:
# Use for PHP 4.x:
#LoadModule php4_module modules/libphp4.so
#AddHandler php-script php

# Use for PHP 5.x:
LoadModule php5_module modules/libphp5.so
AddHandler php5-script php

# Add index.php to your DirectoryIndex line:
DirectoryIndex index.html index.php

AddType text/html php

# PHP Syntax Coloring
# (optional but useful for reading PHP source for debugging):
AddType application/x-httpd-php-source phps

Note 1: The php documentation recommends AddType application/x-httpd-php php instead of the above. However, it causes problems with the “MultiViews” feature of HTTP. That is, if the .php extension is left off a URL, and with certain browser Accept headers, Apache will not know .php (application/x-httpd-php) is HTML and will return a 406 Not Acceptable error. Using the AddType and AddHandler as shown above fixes this problem. For details see Mark Tranchant’s webpage, “Using Apache’s MultiViews with PHP whilst avoid 406 errors,” and PHP bug 28023.

Note 2: PHP Syntax coloring isn’t required, but it’s very nice for looking at your php source while debugging. Here’s an example.

Note 3: just for completeness I’ll mention that you should be able to use SetOutputFilter / SetInputFilter instead of AddType, but you can’t use both. However, SetOutputFilter / SetInputFilter no longer works for me. It used to work with an earlier PHP 4.x or Apache 2 version, but not with Apache 2.0.47/PHP 4.3.3. I understand this (PHP as an Apache 2 filter) is experimental, so I don’t use it anymore:

SetOutputFilter PHP
SetInputFilter PHP

12. You’re now ready to try it out. Start Apache (httpd) as root:

/usr/local/apache/bin/apachectl start

Read Here

Posted in News. 2 Comments »

2 Responses to “Apache 2 and PHP Installation”

  1. pzby kqdjvlmxr Says:

    rkpshvxj gqwloyrua reigajwmf mpyfasi rlykbqgdc hwrb gabmpflhz

  2. John992 Says:

    Very nice site!


Leave a Reply

You must be logged in to post a comment.