PHP (简体中文)

翻译状态: 本文是英文页面 PHP翻译,最后翻译时间:2016-02-27,点击这里可以查看翻译后英文页面的改动。

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

安装

安装官方源安装php

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

运行

虽然PHP可以独立运行,它通常用于HTTP服务器如: Apache HTTP Server, nginx, lighttpdHiawatha.

使用命令:“php -S localhost:8000 -t public_html/ ”可以独立运行PHP。 见 documentation.

配置

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

date.timezone = Europe/Berlin
  • 如果你想调试PHP时显示错误,在/etc/php/php.ini中将display_errors 设为 On
display_errors=On
Tip: 2015-11-22 前,php-composer 的配置文件在/usr/share/php-composer/php.ini
  • open_basedir 限制 PHP 可以访问的目录,可以增加安全性。从 PHP 7.0 开始,和上游一样默认不再设置,要使用的用户请手动设置,例如:
open_basedir = /srv/http/:/home/:/tmp/:/usr/share/pear/:/usr/share/webapps/

扩展

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

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

gd

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

extension=gd.so

imagemagick

运行# pecl install imagick安装imagemagick . pecl 包含于php-pearAUR 包. 在 /etc/php/php.ini中加入

extension=imagick.so

pthreads

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

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

mcrypt

如果想用 mcrypt 模块, 安装 php-mcrypt 以及在/etc/php/php.ini中取消下面这行的注释:

extension=mcrypt.so

PCNTL

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

MySQL/MariaDB

Install and configure MySQL/MariaDB as described in MariaDB.

Uncomment the following lines in /etc/php/php.ini:

extension=pdo_mysql.so
extension=mysqli.so
Warning: mysql.so was removed in PHP 7.0.

You can add minor privileged MySQL users for your web scripts. You might also want to edit /etc/mysql/my.cnf and uncomment the skip-networking line so the MySQL server is only accessible by the localhost. You have to restart MySQL for changes to take effect.

Tip: You may want to install a tool like phpMyAdmin, Adminer or mysql-workbench to work with your databases.

PostgreSQL

Install and configure PostgreSQL, then install the php-pgsql package and uncomment the following lines in /etc/php/php.ini:

extension=pdo_pgsql.so
extension=pgsql.so

Sqlite

Install and configure SQLite, then install the php-sqlite package and uncomment the following lines in /etc/php/php.ini:

extension=pdo_sqlite.so
extension=sqlite3.so

XDebug

XDebug allows you to easily debug php code using modified var_dump() function. Install xdebug and add the line at /etc/php/php.ini:

extension=xdebug.so

缓存

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

要获得最佳性能,应当开启两种缓存。按照下面#OPCache#APCu的步骤操作即可。

OPCache

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

/etc/php/php.ini
zend_extension=opcache.so

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

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

APCu

APCu can be installed with the php-apcu package. You can then enable it by uncommenting the following line in /etc/php/conf.d/apcu.ini, or adding it to your PHP configuration file:

extension=apcu.so

Its author recommends a few suggested settings, among which:

  • apc.enabled=1 and apc.shm_size=32M are not really required as they represent the default values;
  • apc.ttl=7200 on the other hand seems rather beneficial;
  • finally, apc.enable_cli=1, which although not recommended by the manual may be required by some software such as ownCloud.
Tip: You can add those settings either to APCu's own /etc/php/conf.d/apcu.ini or directly to your PHP configuration file. Just make sure not to enable the extension twice as it will result in errors being diplayed in the system logs.

开发工具

Tango-preferences-desktop-locale.png本页面或部分需要翻译,部分内容可能已经与英文文章脱节。如果您希望贡献翻译,请访问简体中文翻译组Tango-preferences-desktop-locale.png

附注: please use the first argument of the template to provide more detailed indications.

Aptana Studio

Aptana Studio is an IDE for programming in PHP and web development. It can be installed with the aptana-studioAUR package. Does not have a PHP debugger as of version 3.2.2.

Eclipse PDT

Eclipse PDT is the PHP variant of Eclipse. It can be installed with the eclipse-php package. See Eclipse for more information.

You would need other plugins for JavaScript support and DB query.

Komodo

Komodo is an IDE with good integration for PHP+HTML+JavaScript. Komodo Edit is a free editor-only variant.

Netbeans

NetBeans IDE is a complete IDE for many languages including PHP. Includes features like debugging, refactoring, code templating, autocomplete, XML features, and web design and development functionalities (very good CSS autocomplete functionality and PHP/JavaScript code notifications/tips). Install it with the netbeans package.

PhpStorm

JetBrains PhpStorm is a commercial, cross-platform IDE for PHP built on JetBrains' IntelliJ IDEA platform. It can be installed with the phpstormAUR package, or with phpstorm-eapAUR for the 30-day trial version. You can get a free license for education from Jetbrains.[1]

Zend Studio

Zend Studio is the official PHP IDE, based on eclipse. The IDE has autocomplete, advanced code formatting, WYSIWYG html editor, refactoring, and all the eclipse features such as db access and version control integration and whatever you can get from other eclipse plugins. You can install it with the zendstudioAUR package.

Commandline tools

Box

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

Composer

Composer is a dependency manager for PHP. It can be installed with the php-composer 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.

故障排除

PHP Fatal error: Class 'ZipArchive' not found

Ensure the zip extension is enabled.

$ grep zip /etc/php/php.ini
extension=zip.so

/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.

参见