PHP是一種廣泛使用的通用腳本語言,特別適合於 Web 開發,可嵌入到 HTML 中。
安裝
如果應用程式還沒有適配最新版本,可以並行安裝 php-legacy包,這個軟體包提供了最老的 維護版本,軟體必須進行必須的配置,請參考 Nextcloud#Migrating_to_php-legacy.
AUR 中也提供了老的版本,php53AUR, php54AUR, php55AUR, php56AUR, php70AUR, php71AUR, php72AUR, php73AUR, php74AUR, php80AUR, php81AUR 和 php82AUR。
運行
PHP通常用於HTTP伺服器,配合其他軟體工作,請參考:
要想像純CGI那樣運行PHP,需要安裝 php-cgi包 。
配置
主要PHP配置位於 /etc/php/php.ini
.
- 建議在
/etc/php/php.ini
中設置所在時區(list of timezones) 。如下:
date.timezone = Europe/Berlin
- 如果你想調試PHP時顯示錯誤,在
/etc/php/php.ini
中將display_errors
設為On
:
display_errors=On
-
open_basedir 限制 PHP 可以訪問的目錄,可以增加安全性,但是會影響程序的正常執行。從 PHP 7.0 開始,和上游一樣默認不再設置,要使用的用戶請手動設置。符號連結會被解析,所以無法通過符號連結跳過限制。某些軟體的 Arch 軟體包,例如
nextcloud
和phpmyadmin
安裝在/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
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 緩存,這兩種緩存都大幅度提升性能,因此最好開啟。
- Zend OPCache僅提供opcode緩存。
- APCu僅提供userland緩存
OPCache
OPCache隨PHP發布,因此在PHP configuration file中開啟或添加此行即可:
/etc/php/php.ini
zend_extension=opcache
你可在官網 找到其他設置以及建議設置。
APCu
通過 php-apcu包 軟體包安裝 APCu, 然後在 /etc/php/conf.d/apcu.ini
中取消下面行的注釋:
extension=apcu.so
作者 建議進行一些設置[失效連結 2021-05-17 ⓘ]:
-
apc.enabled=1
和apc.shm_size=32M
並不是必須的,因為這已經是 默認值[失效連結 2021-05-17 ⓘ]; -
apc.ttl=7200
看上去 很有效[失效連結 2021-05-17 ⓘ]; - 最後,
apc.enable_cli=1
, 雖然手冊上 不建議啟用,有些軟體比如 ownCloud 需要這個選項.
/etc/php/conf.d/apcu.ini
或直接加到住配置文件,只需要注意不要同時加入。開發工具
- Visual Studio Code — 支持 PHP 等多種語言的開發編輯器。
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.