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.

參見[編輯 | 編輯原始碼]