章 4. Unix 系统下的安装

本节将指导如何在 Unix 系统下安装和配置 PHP。在开始安装之前,请务必研究自己使用的系统和 web 服务器的相关章节。

安装前需要考虑的事项一节提到,在本节主要以 web 为中心介绍 PHP 的设置。不过本节也会覆盖一些 PHP 命令行用法的设置方法。

在 Unix 平台下安装 PHP 有几种方法:使用配置和编译过程,或是使用各种预编译的包。本文主要针对配置和编译 PHP 的过程。很多 Unix 类系统都有包安装系统,可以用它来设置一个有着标准配置的 PHP。但是若需要与标准配置不同的功能(例如一个安全服务器,或者不同的数据库驱动扩展模块),可能需要编译 PHP 和/或 web 服务器。如果不熟悉编译软件,可以考虑搜索一下是否有人已经编译了包含所需要功能的预编译包。

编译所需的知识和软件:

  • 基础的 Unix 技能(有能力操作“make”和一种 C 语言编译器)

  • 一个 ANSI C 语言编译器

  • flex:版本 2.5.4

  • bison:版本 1.28(推荐)、1.35 或 1.75

  • 一个 web 服务器

  • 模块特别需要的组件(例如 gd、pdf 库等)

PHP 初始的配置和安装过程被 configure 脚本中一系列命令行选项控制。可以通过 ./configure --help 命令了解 PHP 所有可用的编译选项及简短解释。本手册是分开对这些选项编写文档的。可在附录中找到核心配置选项,而扩展模块特定的配置选项分别在其函数参考页面中描述。

配置好 PHP 后,便可以开始编译模块和/或可执行文件。make 命令用来做这一工作。如果该命令执行失败而找不到原因,请参考安装问题一节。

Unix 系统下的 Apache 1.3.x

本节包括在 Unix 平台的 Apache 下安装 PHP 的说明和提示。我们在另外的页面也有 Apache 2 的安装和说明

可以从核心配置选项列表以及位于手册对应部分的特定扩展配置选项中选择参数并在安装步骤第 10 步将它们添加到 configure 命令中。版本号在这里被省略了以保证此说明的正确性。需要将这里的“xxx”替换为自己使用的文件的正确值。

例子 4-1. PHP 的 Apache 共享模块版本安装说明

1.  gunzip apache_xxx.tar.gz
2.  tar -xvf apache_xxx.tar
3.  gunzip php-xxx.tar.gz
4.  tar -xvf php-xxx.tar
5.  cd apache_xxx
6.  ./configure --prefix=/www --enable-module=so
7.  make
8.  make install
9.  cd ../php-xxx
10. 现在,配置 PHP。这是定制 PHP 的不同选项的时候,例如要加载哪些扩展库。使用
      ./configure --help
    来列出可用的选项。在下面的示例中只是简单地配置 Apache 1 和 MySQL 支持。
    用户的 apxs 的路径可能和此示例中的不同。
      ./configure --with-mysql --with-apxs=/www/bin/apxs
11. make
12. make install
    如果在安装之后决定修改配置选项,那么只需重复以上最后三步。只须重新启动
    Apache 就可以使新模块生效。不需要重新编译 Apache。注意除非特别指出,
    “make install”总是会安装 PEAR,各种 PHP 工具例如 phpize,PHP CLI 以及其它。
13. 建立 php.ini 文件。
      cp php.ini-dist /usr/local/lib/php.ini
    可以编辑 php.ini 来设置 PHP 选项。如果想把 php.ini 放在其它目录,在第
    10 步加上以下选项:
      --with-config-file-path=/path
    如果选择了 php.ini-recommended,确保阅读一下其中的变更说明,因为这些会
    影响到 PHP 的行为。
14. 编辑 httpd.conf 来加载 PHP 模块。在 LoadModule 语句右边的路径必须指向系统中
    PHP 模块所在的路径。上面的 make install 步骤可能已经添加了,但还是检查确认一下。
    对 PHP 4:
      LoadModule php4_module        libexec/libphp4.so
    对 PHP 5:
      LoadModule php5_module        libexec/libphp5.so
15. 在 httpd.conf 中加入 AddModule 部分,在 ClearModuleList 下面的某处,加上这一句:
    对 PHP 4:
      AddModule mod_php4.c
    对 PHP 5:
      AddModule mod_php5.c
16. 告诉 Apache 将哪些后缀作为 PHP 解析。例如,让 Apache 把 .php 后缀的文件解析为
    PHP。可以将任何后缀的文件解析为 PHP,只要在以下语句中加入并用空格分开。这里以
    添加一个 .phtml 来示例。
      AddType application/x-httpd-php .php .phtml
    为了将 .phps 作为 PHP 的源文件进行语法高亮显示,还可以加上:
      AddType application/x-httpd-php-source .phps
17. 用通常的过程启动 Apache(必须完全停止 Apache 再重新启动,而不是用 HUP 或者
    USR1 信号使 Apache 重新加载)。

也可以将 PHP 作为静态对象来安装:

例子 4-2. PHP 的 Apache 静态模块版本安装说明

1.  gunzip -c apache_1.3.x.tar.gz | tar xf -
2.  cd apache_1.3.x
3.  ./configure
4.  cd ..

5.  gunzip -c php-4.x.y.tar.gz | tar xf -
6.  cd php-4.x.y
7.  ./configure --with-mysql --with-apache=../apache_1.3.x
8.  make
9.  make install

10. cd ../apache_1.3.x

11. ./configure --prefix=/www --activate-module=src/modules/php4/libphp4.a
    (上面一行是正确的!是的,我们知道 libphp4.a 在目前不存在。它是不应该存在的,
    它将被创建。)

12. make
    (现在应该有一个 httpd 二进制文件,可以将它复制到 Apache bin 目录。如果这是
    第一次安装,还要“make install”。)

13. cd ../php-4.x.y
14. cp php.ini-dist /usr/local/lib/php.ini

15. 可以编辑 /usr/local/lib/php.ini 文件以设置 PHP 选项。编辑 httpd.conf 或
    srm.conf 文件,添加:
    AddType application/x-httpd-php .php

根据 Unix 系统和 Apache 安装方法的不同,有很多方法停止和重启动 Apache。以下是一些不同的 Apache/UNIX 下重启动 Apache 的典型命令。需要把 /path/to/ 替换成自己系统上的确切路径。

例子 4-3. 重启动 Apache 的示例命令

1. 在一些 Linux 和 SysV 的变种下:
/etc/rc.d/init.d/httpd restart

2. 使用 apachectl 脚本:
/path/to/apachectl stop
/path/to/apachectl start

3. httpdctl 和 httpsdctl(使用了 OpenSSL),类似 apachectl:
/path/to/httpsdctl stop
/path/to/httpsdctl start

4. 使用了 mod_ssl,或其他 SSL 服务器,可能需要手工重启动:
/path/to/apachectl stop
/path/to/apachectl startssl

apachectl 和 http(s)dctl 程序所在的路径在不同系统中通常不一样。如果系统中有 locate 或者 whereis 或者 which 命令,那么可以帮助找到这些控制程序。

编译 PHP 和 Apache 的不同例子还有:

./configure --with-apxs --with-pgsql

此配置将生成在 Apache 的 httpd.conf 文件中用 LoadModule 加载的 libphp4.so 共享库。而 PostgreSQL 支持将嵌入到 libphp4.so 库中。

./configure --with-apxs --with-pgsql=shared

此配置将生成 Apache 的 libphp4.so 共享库,并且还生成 pgsql.so 共享库,可以在 php.ini 文件中用 extension 指令加载,或者在 PHP 脚本中用 dl() 函数明确地加载。

./configure --with-apache=/path/to/apache_source --with-pgsql

此配置将生成 libmodphp4.a 库,mod_php4.c 和一些相关的文件并且拷贝到 Apache 源程序目录中的 src/modules/php4 目录下。然后用 --activate-module=src/modules/php4/libphp4.a 编译 Apache,Apache 编译系统会生成 libphp4.a 并且将其静态地连接到 httpd 程序中。PostgreSQL 支持也直接包括在这个 httpd 程序中了,因此最终结果是单一的一个包括了所有 Apache 和 PHP 支持的 httpd 可执行文件。

./configure --with-apache=/path/to/apache_source --with-pgsql=shared

此配置和上面一样――除了没有在最后的 httpd 可执行文件中包括 PostgreSQL 的支持以及生成了一个 pgsql.so 共享库以外。该共享库可以在 php.ini 文件中或者用 dl() 函数加载。

当选择不同的方法编译 PHP 时,需要考虑每种方法的优势和缺点。用共享对象方式编译 PHP 意味着可以单独编译 Apache,并且不用在添加或修改了 PHP 的时候重新编译所有程序。用内置方法编译 PHP(静态方式)意味着 PHP 可以加载和运行得更快。更多信息见 Apache 的 DSO 支持页面

注: Apache 默认的 httpd.conf 文件中目前包括类似如下的内容:

User nobody
Group "#-1"

除非把它修改成“Group nogroup”或者其它类似的(“Group daemon”也很通用),PHP 将不能打开文件。

注: 确认在使用 --with-apxs=/path/to/apxs 时指向 Apache 安装后的目录中的 apxs。绝对不能用 Apache 源程序中的 apxs 而要用安装后的 apxs。


add a note add a note User Contributed Notes
squeegee
01-Oct-2006 12:24
regarding the note by phptard at gmail dot com:

This was a good starting point to get php built against the Intel-compiled mysql, but in my case, there were still errors. It wasn't until I also added LIBS="-lirc" to the beginning of the configure line that it would configure without errors. It also compiled fine after that. So the start of my configure line looked like this:

LDFLAGS="-lirc" LIBS="-lirc" EXTRA_LIBS="-lirc" ./configure
Arjan van Bentem
13-Jul-2006 03:29
When using Red Hat Fedora, beware of Security Enhanced Linux, SELinux.

Quoted from Red Hat: "The security goal is to make sure that Apache HTTP is only reading the static Web content, and not doing anything else such as writing to the content, connecting to database sockets, reading user home directories, etc."

These limitations include, among many other things, using mkdir to create directories, using fopen to access files, using fopen or get_headers to read URLs, or using exec to run external applications that happen to use sockets (or maybe access some files, but which will run fine when executed from the command line as Unix user apache or httpd -- such as HylaFAX "faxstat" as invoked from nweb2fax recvq.php and sendq.php).

See /var/log/messages for any denials due to the SELinux policy. To disable it:

- System, Administration, Security Level and Firewall
- open the SELinux tab
- click the Transition tree
- check Disable SELinux protection for Apache HTTP
- execute /etc/init.d/httpd restart

See also http://fedora.redhat.com/docs/selinux-faq/ and http://php.net/results.php?q=selinux&p=wholesite
flconseil at yahoo dot fr
07-Mar-2006 10:15
Building Apache 2 and PHP 5.1.2 :

On AIX 5.2 : http://flaupretre.free.fr/redir.php?key=build_apa_aix

On HP-UX 11i (11.11) : http://flaupretre.free.fr/redir.php?key=build_apa_hpux

These documents are complete step-by-step howtos, and describe how to buid a self-sufficient package, including every software it depends on (zlib, SSL, LDAP, iconv, expat, xml, xslt, gd, png, Xpm, jpeg, freetype, bzip2, curl, MySQL, PostgreSQL, Oracle, AdoDB).
dpresley4 at yahoo dot com
07-Nov-2005 07:42
Hi,

PROBLEM:  ./configure PHP --with-oci8

fails with unresolved references such as __rpc_thread_destroy@GLIBC_2_2_3_...

ONE SOLUTOIN

FOR SOLVING PHP ./configure RESULTING IN  __rcp_thread_destroy@GLIBC_2_2_3_... AND
UNRESOLVED REFERENCES WITH ORACLE OCI8

KEYWORDS: PHP OCI OCI8 NET8 ./configure __rpc_thred_destroy UNRESOLVED REFERENCES

For building php-4.4.1 or later with oci8, make sure your LD_LIBARRY_PATH has at a minimum the following directories in its path for Oracle8i 8.1.5 or later, Oracle9i 9.0.2 or later, and Oracle9i Release 2: 9.2.0.4 or later, do the following:

Note:  We are not using the Oracle Instant Client here.  This assumes you have an actual Oracle Installation.

1. Set ORACLE_HOME

Example using Oracle 9i Relase 2 -- 9.2.0.5:

ORACLE_HOME=/opt/app/oracle/product/9iR2

2. Set LD_LIBRARY_PATH with:

LD_LIBRARY_PATH=$ORACLE_HOME/lib: \
   $ORACLE_HOME/rdbms/lib:\
   $LD_LIBRARY_PATH

3. On Unix / Linux, don't forget to export these environment variables:

export ORACLE_HOME LD_LIBRARY_PATH

4. Now, build PHP with the following:

./configure --with-apxs2=<path to Apache 2.0/bin/apxs> --with-oci8=$ORACLE_HOME --enable-sigchild

It should now build correctly. The key with Oracle is to ensure that you pick up the libclntX.so (client librariess) where X is the Oracle version associated with the version your using for instance, in the above example, libclnt9.so

Also note that if your using Oracle 9iAS Release 2 v9.0.2, Oracle 10g iAS Release 1 v9.0.4.1, the above steps will work because ORACLE_HOME will containe all of the libraries necessary.  Simply point ORACLE_HOME to the top level directory of these installations and set LD_LIBRARY_PATH as described above.

Hope this helps.
phptard at gmail dot com
23-Mar-2005 08:17
after a long night of wrestling with mysql4.0 under linux compiled with the intel compiler, i've gotten php5.0.3 to compile with mysql libraries for this flavor of mysql:

1: download the mysql for linux/intel compiler and install
2: download the rpm for the intel compiler libraries and install
3: configure php with LDFLAGS="-lirc -lgcc_s" and EXTRA_LIBS="-lirc -lgcc_s"

Example:

LDFLAGS="-lirc -lgcc_s" LD_LIBRARY_PATH="-L/usr/lib64" LD_PATH="-L/usr/lib64" LDPATH="-L/usr/lib64" EXTRA_LIBS="-lirc -lgcc_s" ./configure --with-apxs2=/usr/local/apache/bin/apxs --with-ssl=/usr/local/ssl --without-sqlite --with-zlib-dir=/usr --with-mysql=/usr/local/mysql

of course this is on a xeon system that has half of its modules in the /usr/lib64 directory, so on a normal system, without the other kruft, it would look something more like this:

LDFLAGS="-lirc -lgcc_s" EXTRA_LIBS="-lirc -lgcc_s" ./configure --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql

Hopefully this will save someone the 6 hour headache it caused me..
diemuzi at gmail dot com
14-Jan-2005 02:11
In reference to van [at] webfreshener [dot] com to fix the krb5 problems. An easier fix is to do the following:

ln -s /usr/kerberos/include/krb5.h /usr/include/krb5.h
ln -s /usr/kerberos/include/profile.h /usr/include/profile.h
ln -s /usr/kerberos/include/com_err.h /usr/include/com_err.h

This will help solve some deps. in the future incase a situation with another compilation occurs.
alexander justadot henry at acm dot org
23-Dec-2004 04:48
The system at my workplace has a need for apache/php with all static compilation.  In order to save time adminning our systems, I decided to make my own RPM of php/apache with mod_ssl support.  I had always installed by hand with the instructions on this page, but when buiding the RPM way, came upon the following error when apache was compiling:

===> src/modules/php4
make[4]: *** No rule to make target `all'. Stop.

Ordinarily this is because one did not do a 'make install' in php before the second apache configure, or somehow the make install failed.  But the way rpm's work, the make install must be in the %install portion of the spec file, after all makes are completed.

make install-sapi

This line will copy relevant files to the directory specified in --with-apache
samael99 at web dot de
24-Jun-2004 09:51
Quick hint for people using RH8:

if make gives you this error
FT_ENCODING_MS_SYMBOL undeclared

change on line in this file
/usr/include/freetype2/freetype/freetype.h

Search for ft_encoding_symbol - change it to
ft_encoding_ms_symbol

Now this problem is dealt with, go ahead with make.

Good Luck !
robert_sgi at yahoo dot com
09-May-2004 02:57
If you install php 4 on SGI IRIX 6.5 (in my case it was php 4.3.6 on Silicon Graphics O2 IRIX 6.5.22 machine) and you're building it with:
--with-gettext=/usr/freeware
then you need to manually edit the file named "configure" (from the php source directory) and change the line# 36739
from:
GETTEXT_LIBDIR=$GETTEXT_DIR/lib
to:
GETTEXT_LIBDIR=$GETTEXT_DIR/lib32
If you have problems in locating the line, search the text for "bindtextdomain", and look several (4) lines above.
karthik (dot) k (at) extremix (dot) net
19-Jan-2004 12:28
This is regarding the post down below about the problem with openssl on RH9. Openssl on RH9 is built with kerberos. To get PHP to build correctly you need the output of these commands when you make.

[root@graf-spee local]# pkg-config --cflags openssl
-I/usr/kerberos/include 

[root@graf-spee local]# pkg-config --libs openssl
-L/usr/kerberos/lib -lssl -lcrypto -lresolv -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -ldl -lz 

These could be added to your make command thus. I have not tested it out, but should work with some tweaking

make EXTRA_LDFLAGS=`pkg-config --libs openssl` EXTRA_CFLAGS=`pkg-config --cflags openssl`
thansen at terra dot com dot br
31-Dec-2003 12:36
The configure directives --with-apxs2 and --with-apxs2filter are not compatible one with other, even though the configure script will not complain about that. Each one affect the way Apache will call the php parser: If you choose the first one, you must use the traditional include:

AddType application/x-httpd-php php

at httpd.conf, to call the parser. If you use the --with-apxs2filter, the include will be:

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

, and php will be called as a filter to .php files.

If you use both together, you will get compilation errors (duplicate symbols while linking libphp4).
aaronmorris at mindspring dot com
06-Dec-2003 04:47
If you have the libphp4.a instead of libphp4.so on AIX, you can extract the .so file from the .a file by running "ar -x libphp4.a".
jazee_at_bigfoot.com
27-Mar-2003 04:52
http://dan.drydog.com/apache2php.html has a nice set of instructions for Apache2 + php
doug at NOSPAM dot techie dot net
04-Feb-2003 09:16
Users compiling under some versions of Solaris/SunOS may encounter the following error.
   symbol ap_block_alarms: referenced symbol not found

To address this problem, add the following additional flag to the Apache build configure line:
   --enable-rule=SHARED_CORE

So, adding this to the original instructions, you'd configure your Apache build like so:
   ./configure --prefix=/www --enable-module=so --enable-rule=SHARED_CORE

Doug
mbabcock-php at fibrespeed dot net
21-Jul-2001 12:32
The best configuration guide I've found for Apache with PHP (and PERL, mod_ssl, etc.) is Apacompile.  Its home site is
http://www.delouw.ch/linux/apache.phtml
dimaberastau at hotmail dot com
10-Jun-2001 12:33
when installing with mysql support (--with-mysql=<path/to/your/mysql>) via Apache APXS you'll probably get something like 'can't load libmysqlclient.so' when you try to start up apache. There are 2 solutions to this problem. First, (as documented in INSTALL file of the php4 distribution) you can modify /etc/ld.so.conf to contain the directory name where libmysqlclient.so is (so if your mysql is installed in /usr/local, you want to add something like /usr/local/lib/mysql into /etc/ld.so.conf), else (and particularly if you haven't got the super-user on the system) you can modify (or create if it isn't defined already) LD_LIBRARY_PATH shell variable to reflect the changes you would have otherwise made to /etc/ld.so.conf (again if mysql is /usr/local LD_LIBRARY_PATH=/usr/local/lib/mysql). Either one of these methods will get the problem sorted. Just remember to run ldconfig (so that /etc/ld.so.cache is updated) if you chose to modify /etc/ld.so.conf
marshalm at ebrd dot com
18-May-2001 01:43
HP-UX 11.X PA-RISC installation with oracle (oci8). You need to install the HP-UX patch PHSS_22514 patch (updated libdld.sl), otherwise you will get errors with dlopen() and dlclose() not found during the apache integration stage.
philip at c()rnad() dot c()m
25-Mar-2001 08:22
HOWTO: Installation on Cobalt RaQ  (RaQ3 or RaQ4) :
http://marc.theaimsgroup.com/?l=php-general&m=98039640119670&w=2