PHP 8.1.20 Released!

Unix 系统下的安装


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

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

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


  • 基础的 Unix 技能(有能力操作“make”和一种 C 语言编译器)
  • 一个 ANSI C 语言编译器
  • 一个 web 服务器
  • 任何模块特需的组件(例如 GDPDF 库等)

直接从 Git 源文件或者自己修改过的包编译时可能需要:

  • autoconf: 2.59+ (for PHP >= 7.0.0), 2.64+ (for PHP >= 7.2.0)
  • automake: 1.4+
  • libtool: 1.4.x+(除了 1.4.2)
  • re2c: 版本 0.13.4+
  • bison:
    • PHP 7.0 - 7.3: 2.4 或更高 (包含 Bison 3.x)
    • PHP 7.4: > 3.0

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

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


某些 Unix 系统(类似 OpenBSD 和 SELinux)出于安全考虑,可能不允许同时设置文件的写和执行的权限,又称为 "PaX MPROTECT" 或 "W^X violation" 保护。但是 PCRE's JIT 又要求不能这么做,所以安装时可以参考 关闭 PCRE's JIT 支持,或者在系统中将相关的二进制文件加入保护白名单。

注意: 目前还不支持 ARM 与 Android 工具链的交叉编译。

add a note

User Contributed Notes 2 notes

cj3 at clifjackson dot net
5 years ago
I recently ran in to a situation where I was building PHP 7.1.13 from source. Configuration & make went fine, however, when I ran make install it hung. It turns out that if you are building PHP with Apache (apxs) the make file calls apxs with the -a flag which tells it to modify the httpd.conf file. If, for whatever reason, the file is not writeable then it fails and you get the hang.

Line 108 in my Makefile looks like this:

INSTALL_IT = $(mkinstalldirs) '$(INSTALL_ROOT)/usr/lib64/httpd/modules' && $(mkinstalldirs) '$(INSTALL_ROOT)/etc/httpd/conf' && /usr/sbin/apxs -S LIBEXECDIR='$(INSTALL_ROOT)/usr/lib64/httpd/modules'      -S SYSCONFDIR='$(INSTALL_ROOT)/etc/httpd/conf' -i -a -n php7

I had to remove the -a flag and then it was fine.
Arjan van Bentem
16 years ago
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 and
To Top