Debian GNU/Linux 安装说明

本节包含了在 » Debian GNU/Linux 下安装 PHP 的说明和提示。

Warning

这里不支持非官方的第三方发行包。任何错误应报告给 Debian 开发组,除非该错误在使用从官方» 下载的最新版时仍能重现。

尽管在 Unix 下编译 PHP 的指示也适用于 Debian,本节包含有一些特定信息,例如使用 apt 或者 aptitude 命令。本节中这两条命令可以互换。

使用 APT

首先,注意其它有关的包可能需要 libapache-mod-php 集成入 Apache 2,以及 PEAR 的 php-pear

其次,在安装一个包之前,最好先确定该包是最新版。通常可以运行命令 apt update

Example #1 Debian 下将 PHP 安装入 Apache 2 的例子

# apt install php-common libapache2-mod-php php-cli

APT 将自动安装 Apache 2 的 PHP 模块以及所有依赖的库并激活之。应重启动 Apache 以使更改生效,例如:

Example #2 安装完 PHP 后停止并启动 Apache

# /etc/init.d/apache2 stop
# /etc/init.d/apache2 start

更好地控制配置

上一节中 PHP 仅安装了核心模块。很可能还需要更多模块,例如 MySQLcURLGD 等。这些模块也可以通过 apt 命令安装。

Example #3 取得 PHP 附加软件包的列表

# apt-cache search php
# apt search php | grep -i mysql
# aptitude search php

以上命令的输出中列出了很多的包,其中有几个针对 PHP 的模块例如 php-cgi,php-cli 以及 php-dev。决定好要安装哪些之后可以用 apt 或者 aptitude 来安装。Debian 会进行倚赖性检查,会给出提示,例如安装 MySQL 和 cURL:

Example #4 安装 PHP 的 MySQL 和 cURL 支持

# apt install php-mysql php-curl

APT 会自动把适当的行添加到不同的 php.ini 相关文件中去,例如 /etc/php/7.4/php.ini/etc/php/7.4/conf.d/*.ini 等,并且根据扩展,还会添加类似 extension=foo.so 的内容。不过还是需要重新启动 web 服务器(例如 Apache)以使这些改动生效。

常见问题

  • 如果 PHP 脚本没有通过 web 服务器被解析,则有可能是 PHP 没有被加入到 web 服务器的配置文件中,在 Debian 中可能是 /etc/apache2/apache2.conf 或类似文件。具体内容参见 Debian 手册。
  • 如果某扩展貌似已经安装,但其函数却又未定义,确保合适的 ini 文件已被加载并且 web 服务器在安装后重新启动过。
  • 在 Debian(以及其它 Linux 变种)下有两个基本命令来安装包:aptaptitude。不过要解释这两个命令的细微区别已超出本手册范围。

User Contributed Notes

kearney dot taaffe at gmail dot com 09-Jan-2018 02:24
Compiling PHP on Ubuntu boxes.

If you would like to compile PHP from source as opposed to relying on package maintainers, here's a list of packages, and commands you can run

STEP 1:
sudo apt-get install autoconf build-essential curl libtool \
  libssl-dev libcurl4-openssl-dev libxml2-dev libreadline7 \
  libreadline-dev libzip-dev libzip4 nginx openssl \
  pkg-config zlib1g-dev

So you don't overwrite any existing PHP installs on your system, install PHP in your home directory. Create a directory for the PHP binaries to live

    mkdir -p ~/bin/php7-latest/

STEP 2:
# download the latest PHP tarball, decompress it, then cd to the new directory.

STEP 3:
Configure PHP. Remove any options you don't need (like MySQL or Postgres (--with-pdo-pgsql))

./configure --prefix=$HOME/bin/php-latest \
    --enable-mysqlnd \
    --with-pdo-mysql \
    --with-pdo-mysql=mysqlnd \
    --with-pdo-pgsql=/usr/bin/pg_config \
    --enable-bcmath \
    --enable-fpm \
    --with-fpm-user=www-data \
    --with-fpm-group=www-data \
    --enable-mbstring \
    --enable-phpdbg \
    --enable-shmop \
    --enable-sockets \
    --enable-sysvmsg \
    --enable-sysvsem \
    --enable-sysvshm \
    --enable-zip \
    --with-libzip=/usr/lib/x86_64-linux-gnu \
    --with-zlib \
    --with-curl \
    --with-pear \
    --with-openssl \
    --enable-pcntl \
    --with-readline

STEP 4:
compile the binaries by typing: make

If no errors, install by typing: make install

STEP 5:
Copy the PHP.ini file to the install directory

    cp php.ini-development ~/bin/php-latest/lib/

STEP 6:

cd ~/bin/php-latest/etc;
mv php-fpm.conf.default php-fpm.conf
mv php-fpm.d/www.conf.default php-fpm.d/www.conf

STEP 7:
create symbolic links for your for your binary files

   cd ~/bin
   ln -s php-latest/bin/php php
   ln -s php-latest/bin/php-cgi php-cgi
   ln -s php-latest/bin/php-config php-config
   ln -s php-latest/bin/phpize phpize
   ln -s php-latest/bin/phar.phar phar
   ln -s php-latest/bin/pear pear
   ln -s php-latest/bin/phpdbg phpdbg
   ln -s php-latest/sbin/php-fpm php-fpm

STEP 8: link your local PHP to the php command. You will need to logout then log back in for php to switch to the local version instead of the installed version

# add this to .bashrc
if [ -d "$HOME/bin" ] ; then
  PATH="$HOME/bin:$PATH"
fi

STEP 9: Start PHP-FPM

    sudo ~/bin/php7/sbin/php-fpm
marin at sagovac dot com 23-Mar-2014 01:10
To install LAMP stack on Ubuntu (+Server) from 10.04 you need first install taskel and then lamp-server for example:

Install taskel, follow terminal guides:
sudo apt-get install tasksel

Install LAMP stack package from Ubuntu repository:
sudo tasksel install lamp-server
thumbs at apache dot org 27-Jun-2013 05:50
To refresh this document, perhaps it would be worth mentioning more modern methods to serve php content under apache httpd.

Specifically, the preferred method is now fastcgi, using either of those recipes:

(mod_fastcgi, httpd 2.2)
http://wiki.apache.org/httpd/php-fastcgi

(mod_fcgid, httpd 2.2)
http://wiki.apache.org/httpd/php-fcgid

(mod_proxy_fcgi, httpd 2.4)
http://wiki.apache.org/httpd/PHP-FPM

While the legacy mod_php approach is still applicable for some older installations, the fastcgi method is much faster, and require much less RAM to operate, based on similar traffic patterns.

Thank you!
juraj at jurajsplayground dot com 08-May-2009 05:59
On Ubuntu (since 7.04), rather do:
sudo tasksel install lamp-server

Details:
https://help.ubuntu.com/community/ApacheMySQLPHP
tranzbit at yahoo dot com 02-May-2009 01:59
On Ubuntu:

sudo apt-get install apache2 php5 mysql-client-5.0 mysql-server-5.0 phpmyadmin libapache2-mod-php5 libapache2-mod-auth-mysql php5-mysql

then restart the computer/start mysql manually
From:
http://ubuntuforums.org/showthread.php?t=186492
John Fisher 08-Feb-2007 04:53
With Apache2 and Php4 under Debian Sarge there is an extra configuration file :  /etc/apache2/sites-available/default
This file is not clearly documented, at least not for noobs, in Apache docs.

It overrides the conf file in the way you expect the /etc/apache2/conf.d/apache2-doc  to do according to the README.

Add ExecCGI to it to get rid of "Options ExecCGI is off in this directory"  errors.
PHP8中文手册 站长在线 整理 版权归PHP文档组所有