发新话题
打印

Apache 2 and PHP (mod_php) on Linux

Apache 2 and PHP (mod_php) on Linux

The following is how I got Apache 2 and PHP working together on Linux (RedHat 7.3). These instructions also apply, mostly, for any UNIX, especially Linux.

Version Tip: Beginning with Apache 2.0.42 the API will be kept stable. That means you will NOT have to recompile modules (and possibly upgrade/fix source) everytime a new Apache is released. This assumes you stay in the same Apache release series. For example, upgrading from 2.0.43 to 2.0.45. This will not apply to upgrading to the next series (e.g., 2.1.x).

I was only able to get PHP working with Apache 2 as a DSO (as opposed to compiled into Apache). I think DSO is the only way PHP is supported now on Apache 2.

I used httpd-2.0.43 and php-4.3.0. I also used http-2.0.44 with php-4.3.0. The older php-4.2.3 did not work with Apache 2.0.43. You should use php-4.3.1 or higher, as 4.3.0 has a security problem.

If you have problems and think it's a recent bug, you may want to consider using the latest snapshot at http://snaps.php.net/php4-latest.tar.gz Beware that snapshots frequently have regression and are not for production use.


Download/unpack Apache2 source from the Apache httpd server website.

In the Apache 2 source directory, type:
./configure --prefix=/usr/local/apache \
        --enable-so        \
        --enable-cgi        \
        --enable-info        \
        --enable-rewrite        \
        --enable-speling        \
        --enable-usertrack        \
        --enable-deflate \
        --enable-ssl        \
        --enable-mime-magic

make



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.

If successful, install, as root:
make install




Download/unpack PHP source from the PHP website Pick one from the 4.3.x series (see note above).

In the php source directory, type:
./configure \
        --with-apxs2=/usr/local/apache/bin/apxs \
        --with-mysql \
        --prefix=/usr/local/apache/php \
        --with-config-file-path=/usr/local/apache/php \
        --enable-track-vars \
        --enable-force-cgi-redirect \
        --disable-cgi \
        --with-zlib \
        --with-gettext \
        --with-gdbm
make



You only need the with-apxs2, and prefix lines. with-mysql adds MySql, with-config-file moves the php.ini file location, enable-track-vars makes APHP track GET/POST/cookie variables, 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.

If successful, type this as root to install the PHP files:
make install



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

make install-su




If file /usr/local/apache/modules/libphp4.so does not exist or is an older version, type this:
cp -p .libs/libphp4.so /usr/local/apache/modules




Install the php.ini file:
cp -p php.ini-recommended /usr/local/apache/php/php.ini




Verify (and add if missing) these directives in /usr/local/apache/conf/httpd.conf:
# Make sure there's only **1** line with this directive:
LoadModule php4_module        modules/libphp4.so

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

AddType application/x-httpd-php php

# PHP Syntax Coloring (recommended):
AddType application/x-httpd-php-source phps



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

Note: just for completeness I'll mention that you can use the following instead of AddType, but you can't use both AddType and SetOutputFilter / SetInputFilter:

<Files *.php>
        SetOutputFilter PHP
        SetInputFilter  PHP
</Files>




Start httpd:
/usr/local/apache/bin/apachectl start




Sanity checks:
$ /usr/local/apache/bin/httpd -t
Syntax OK

$ /usr/local/apache/bin/httpd -v
Server version: Apache/2.0.43
Server built:   Oct 21 2002 16:20:01

$ /usr/local/apache/bin/httpd -l
Compiled in modules:
. . .
  mod_so.c

$ /usr/local/apache/bin/httpd -V
Server version: Apache/2.0.43
Server built:   Oct 21 2002 16:20:01
Server's Module Magic Number: 20020903:0
Architecture:   32-bit
Server compiled with....
-D APACHE_MPM_DIR="server/mpm/prefork"
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D HTTPD_ROOT="/usr/local/apache"
-D SUEXEC_BIN="/usr/local/apache/bin/suexec"
-D DEFAULT_PIDLOG="logs/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_LOCKFILE="logs/accept.lock"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"

$ ps -ef |grep httpd
root     24069     1  0 09:17 ?        00:00:08 /usr/local/apache/bin/httpd -k s
apache   29917 24069  0 15:30 ?        00:00:00 /usr/local/apache/bin/httpd -k s
. . .






Access your webserver with telnet. Type HEAD / HTTP/1.0 followwed by a blank line:
$ telnet localhost 80
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
Escape character is '^]'.
HEAD / HTTP/1.0

HTTP/1.1 200 OK
Date: Thu, 02 Jan 2003 19:29:27 GMT
Server: Apache/2.0.43 (Unix) mod_ssl/2.0.43 OpenSSL/0.9.6b PHP/4.3.0
Accept-Ranges: bytes
X-Powered-By: PHP/4.3.0
Connection: close
Content-Type: text/html; charset=ISO-8859-1
Content-Language: en
X-Pad: avoid browser bug

Connection closed by foreign host.




Access your webserver with your favorite browser. The following is a good test page to use for PHP. Only the line in bold is needed to display PHP configuration information. Name the file anything you want such as phpinfo.php and put it in your web server content directory:
<html>
<head>
        <title>PHP Test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
        <h1>PHP Test</h1>
        <p>
        <b>An Example of PHP in Action</b><br />
                <?php echo "The Current Date and Time is: <br>";
                        echo date("g:i A l, F j Y.";?>
        </p>

        <h2>PHP Information</h2>
        <p>
                <?php phpinfo(); ?>
        </p>
</body>
</html>




Tips and Notes
• Redhat 8.0
Note that RedHat 8.0 has Apache 2 and PHP built-in (packages httpd, 2.0.40, and php, 4.2.2). Apache is mostly under /usr/lib/httpd and /usr/sbin/httpd. PHP is at /usr/lib/httpd/modules/libphp4.so For a complete file listing, type: rpm -ql httpd php

TOP

发新话题