PHP

来自 Arch Linux 中文维基

PHP是一种广泛使用的通用脚本语言,特别适合于 Web 开发,可嵌入到 HTML 中。

安装[编辑 | 编辑源代码]

安装 软件包 php

如果应用程序还没有适配最新版本,可以并行安装 php-legacy,这个软件包提供了最老的 维护版本,软件必须进行必须的配置,请参考 Nextcloud#Migrating_to_php-legacy.


AUR 中也提供了老的版本,php53AUR, php54AUR, php55AUR, php56AUR, php70AUR, php71AUR, php72AUR, php73AUR, php74AUR, php80AUR, php81AURphp82AUR

运行[编辑 | 编辑源代码]

PHP通常用于HTTP服务器,配合其他软件工作,请参考:

要想像纯CGI那样运行PHP,需要安装 php-cgi

配置[编辑 | 编辑源代码]

主要PHP配置位于 /etc/php/php.ini.

date.timezone = Europe/Berlin
  • 如果你想调试PHP时显示错误,在/etc/php/php.ini中将display_errors 设为 On
display_errors=On
  • open_basedir 限制 PHP 可以访问的目录,可以增加安全性,但是会影响程序的正常执行。从 PHP 7.0 开始,和上游一样默认不再设置,要使用的用户请手动设置。符号链接会被解析,所以无法通过符号链接跳过限制。某些软件的 Arch 软件包,例如 nextcloudphpmyadmin 安装在 /usr/share/webapps,然后在 /etc/webapps 中创建了配置文件的符号链接。设置 open_basedir 时请加入这两个目录。例如:
open_basedir = /srv/http/:/var/www/:/home/:/tmp/:/var/tmp/:/var/cache/:/usr/share/pear/:/usr/share/webapps/:/etc/webapps/

扩展[编辑 | 编辑源代码]

一些常用的PHP扩展也可以在官方库发现:

$ pacman -Ss php-
提示:不要编辑/etc/php/php.ini,扩展的启停可在 /etc/php/conf.d 中设置,如: (e.g. /etc/php/conf.d/gd.ini)

已经安装的扩展位于 /usr/lib/php/modules 目录。

例如要启用 ext-iconv 扩展,在 /etc/php/conf.d/extensions.ini 中配置:

extension=iconv

要安装 PHP 的扩展,可以在 AUR 中搜索 php-* 或 php56-*, 例如 php-imagick, php-redis php56-mcryptAUR

gd[编辑 | 编辑源代码]

欲使用 php-gd/etc/php/php.ini中取消下列内容的注释:

extension=gd

imagemagick[编辑 | 编辑源代码]

安装imagemagick。这个软件包会创建 /etc/php/conf.d/imagick.ini 配置文件。

要让 imagemagick 支持 SVG,需要安装 librsvg}。

多线程[编辑 | 编辑源代码]

要使用 POSIX 多线程,需要 pthreads 扩展 。用 pecl 安装 pthreads (https://pecl.php.net/package/pthreads) 扩展,需要 PHP 在编译时启用线程安全选项--enable-maintainer-zts. 当前最简单的方式是用需要的选项重新编译.

可在 PHP pthreads extension 页面找到指令介绍。

PCNTL[编辑 | 编辑源代码]

利用 PCNTL 可以在服务器上直接创建进程。虽然这可能是你想要的,但是这样也会让 PHP 有能力把机器搞的一团糟。所以 PHP 不能和其他扩展一样加载,要启用此扩展,需要重新编译PHP。ArchLinux 的 PHP 已经加入 "--enable-pcntl"选项,默认已经启用。

MySQL/MariaDB[编辑 | 编辑源代码]

根据 MariaDB 页面安装并配置 MySQL/MariaDB.

取消 /etc/php/php.ini下面行前面的注释 :

extension=pdo_mysql.so
extension=mysqli.so
警告: PHP 7.0 中 删除了 mysql.so

可以给网络脚本最低的 MySQL 用户权限,可以编辑 /etc/my.cnf.d/server.cnf,在 'mysqld 段落添加 skip-networking,这样 MyQSL 服务器就仅允许通过 localhost 本地访问。请参考 MariaDB#Enable access locally only via Unix sockets。设置之后需要重启 MySQL。

Redis[编辑 | 编辑源代码]

安装并配置 Redis,然后安装 phpredis-gitAUR.

/etc/php/conf.d/redis.ini 中取消 redis 扩展的注释。同时在 /etc/php/conf.d/igbinary.ini 中启用(取消注释) igbinary 扩展。

PostgreSQL[编辑 | 编辑源代码]

安装并配置 PostgreSQL,然后安装 php-pgsql 软件包并取消 /etc/php/php.ini 中下面几行的注释:

extension=pdo_pgsql
extension=pgsql

Sqlite[编辑 | 编辑源代码]

安装并配置 SQLite,然后安装 php-sqlite 软件包并取消 /etc/php/php.ini 中下面几行的注释:

extension=pdo_sqlite
extension=sqlite3

XDebug[编辑 | 编辑源代码]

用 XDebug 可以很容易的通过修改的 var_dump() 函数进行调试、评测、追踪。

安装 xdebug 并取消 /etc/php/conf.d/xdebug.ini 中如下行前面的注释:

zend_extension=xdebug.so

You can configure what XDebug does by adding a xdebug.mode line to the same file. By default, it is set to xdebug.mode=develop.

For Xdebug 3 the default port is 9003, to change it set xdebug.remote_port=9000}}

Snuffleupagus[编辑 | 编辑源代码]

Install php-snuffleupagus, uncomment the two lines in /etc/php/conf.d/snuffleupagus.ini, and put the path to the snuffleupagus.rules file in the second line:

extension=snuffleupagus.so
sp.configuration_file=/etc/php/conf.d/snuffleupagus.rules

缓存[编辑 | 编辑源代码]

PHP有两种缓存: opcode/bytecode 缓存和userland/user data 缓存,这两种缓存都大幅度提升性能,因此最好开启。

OPCache[编辑 | 编辑源代码]

OPCache随PHP发布,因此在PHP configuration file中开启或添加此行即可:

/etc/php/php.ini
zend_extension=opcache

你可在官网 找到其他设置以及建议设置。

警告: 如果你使用推荐设置,要确保你一仔细看过说明,某些情况下可能导致如下错误:zend_mm_heap corrupted

APCu[编辑 | 编辑源代码]

通过 php-apcu 软件包安装 APCu, 然后在 /etc/php/conf.d/apcu.ini 中取消下面行的注释:

extension=apcu.so

作者 建议进行一些设置[失效链接 2021-05-17 ⓘ]:

提示:可以将设置加入 APCu 自己的 /etc/php/conf.d/apcu.ini 或直接加到住配置文件,只需要注意不要同时加入。

开发工具[编辑 | 编辑源代码]

  • Visual Studio Code — 支持 PHP 等多种语言的开发编辑器。
https://code.visualstudio.com/ || visual-studio-code-binAUR

Aptana Studio[编辑 | 编辑源代码]

Aptana Studio 是一个用于PHP和网页开发的IDE. 它能通过 aptana-studioAUR 包来安装. 没有3.2.2版本的PHP调试器.

Eclipse PDT[编辑 | 编辑源代码]

Eclipse PDT 是eclipse的PHP变种. 它能通过 eclipse-phpAUR 包来安装. 可阅览 Eclipse 获取更多信息.

你可能需要其他插件来获取JavaScript支持和数据库查询.

Komodo[编辑 | 编辑源代码]

Komodo 是一个集成了PHP+HTML+JavaScript的非常好的IDE. Komodo Edit 是免费的只支持编辑的变种. 可以通过 komodo-editAUR 安装。

Netbeans[编辑 | 编辑源代码]

NetBeans IDE 是一个用于很多语言的IDE,包括PHP. 它的特性包括调试、重构、代码模板、自动补全、XML特性、网页设计和其他开发功能(包括很棒的CSS自动补全功能和PHP/JavaScript代码建议)。 可以通过 netbeans 来安装.

PhpStorm[编辑 | 编辑源代码]

JetBrains PhpStorm 是一个商业的、跨平台PHP IDE,它基于jetbrains的intellij IDEA平台。它可以通过phpstormAUR 来安装, 或者通过 phpstorm-eapAUR 来进行30天免费试用 version. 你可以从jetbrains获取免费的教育许可.[1]

Zend Studio[编辑 | 编辑源代码]

Zend Studio 是官方的基于Eclipse的PHP IDE. 这个IDE有自动补全、高级代码格式化、WYSIWYG HTML编辑器、重构和所有的Eclipse特性比如数据库访问和版本控制集成和你能从Eclipse获取的所有其他插件。你可以通过zendstudioAUR 来安装.

Commandline tools[编辑 | 编辑源代码]

Composer[编辑 | 编辑源代码]

Composer is a dependency manager for PHP. It can be installed with the composer package.

Box[编辑 | 编辑源代码]

Box is an application for building and managing Phars. It can be installed with the php-boxAUR package.

PDepend[编辑 | 编辑源代码]

PHP Depend (pdepend) is software metrics tool for php. It can be installed with the pdependAUR package.

PHP Coding Standards Fixer[编辑 | 编辑源代码]

PHP Coding Standards Fixer a is PSR-1 and PSR-2 Coding Standards fixer for your code. It can be installed with the php-cs-fixerAUR package.

PHP-CodeSniffer[编辑 | 编辑源代码]

PHP CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards. It can be installed with the php-codesnifferAUR package.

phpcov[编辑 | 编辑源代码]

phpcov is a command-line frontend for the PHP_CodeCoverage library. It can be installed with the phpcovAUR package.

phpDox[编辑 | 编辑源代码]

phpDox is the documentation generator for PHP projects. This includes, but is not limited to, API documentation. It can be installed with the phpdoxAUR package.

PHPLoc[编辑 | 编辑源代码]

PHPLoc is a tool for quickly measuring the size of a PHP project. It can be installed with the phplocAUR package.

PhpMetrics[编辑 | 编辑源代码]

PhpMetrics provides various metrics about PHP projects. It can be installed with the phpmetricsAUR package.

phptok[编辑 | 编辑源代码]

phptok is a tool for quickly dumping the tokens of a PHP sourcecode file. It can be installed with the phptokAUR package.

PHPUnit[编辑 | 编辑源代码]

PHPUnit is a programmer-oriented testing framework for PHP. It can be installed with the phpunitAUR package.

PHPUnit Skeleton Generator[编辑 | 编辑源代码]

PHPUnit Skeleton Generator is a tool that can generate skeleton test classes from production code classes and vice versa. It can be installed with the phpunit-skeleton-generatorAUR package.

Producer[编辑 | 编辑源代码]

Producer is a command-line quality-assurance tool to validate, and then release, your PHP library package.

It can be installed with the producerAUR package.

故障排除[编辑 | 编辑源代码]

PHP Fatal error: Class 'ZipArchive' not found[编辑 | 编辑源代码]

Ensure the zip extension is enabled.

/etc/php/php.ini
extension=zip

/etc/php/php.ini not parsed[编辑 | 编辑源代码]

If your php.ini is not parsed, the ini file is named after the sapi it is using. For instance, if you are using uwsgi, the file would be called /etc/php/php-uwsgi.ini. If you are using cli, it is /etc/php/php-cli.ini.

PHP Warning: PHP Startup: <module>: Unable to initialize module[编辑 | 编辑源代码]

When running php, this error indicates that the aforementioned module is out of date. This will rarely happen in Arch Linux, since maintainers make sure core PHP and all modules be only available in compatible versions.

This might happen in conjunction with a module compiled from the AUR. You usually could confirm this by looking at the dates of the files /usr/lib/php/modules/.

To fix, find a compatible update for your module, probably by looking up the AUR using its common name.

If it applies, flag the outdated AUR package as outdated.

参见[编辑 | 编辑源代码]