Difference between revisions of "PHP"

From ArchWiki
Jump to navigation Jump to search
(→‎Running: added Hiawatha)
Line 26: Line 26:
 
== Running ==
 
== Running ==
  
While PHP can be run standalone, it is typically used with http servers such as [[Apache HTTP Server]], [[nginx]] and [[lighttpd]].
+
While PHP can be run standalone, it is typically used with http servers such as [[Apache HTTP Server]], [[nginx]], [[lighttpd]] and [[Hiawatha]].
  
 
To run PHP standalone issue the {{ic|php -S localhost:8000 -t public_html/}} command. See [https://secure.php.net/manual/en/features.commandline.webserver.php documentation].
 
To run PHP standalone issue the {{ic|php -S localhost:8000 -t public_html/}} command. See [https://secure.php.net/manual/en/features.commandline.webserver.php documentation].

Revision as of 17:30, 20 January 2016

zh-CN:PHP

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.

Installation

Install the php package from the official repositories.

Note that to run PHP scripts as plain CGI, you need the php-cgi package.

Running

While PHP can be run standalone, it is typically used with http servers such as Apache HTTP Server, nginx, lighttpd and Hiawatha.

To run PHP standalone issue the php -S localhost:8000 -t public_html/ command. See documentation.

Configuration

The main PHP configuration file is well-documented and located at /etc/php/php.ini.

  • It is recommended to set your timezone (list of timezones) in /etc/php/php.ini like so:
date.timezone = Europe/Berlin
  • If you want to display errors to debug your PHP code, change display_errors to On in /etc/php/php.ini:
display_errors=On
Tip: Prior to 22 November 2015, php-composer kept its settings in a separate file in /usr/share/php-composer/php.ini

Extensions

A number of commonly used PHP extensions can also be found in the official repositories:

$ pacman -Ss php-
Tip: Instead of editing /etc/php/php.ini, a extension may be enabled/configured in the /etc/php/conf.d directory instead (e.g. /etc/php/conf.d/gd.ini)

gd

For php-gd uncomment the line in /etc/php/php.ini:

extension=gd.so

imagemagick

For imagemagick run # pecl install imagick. The pecl binary is included in the php-pearAUR package. Then add

extension=imagick.so

to /etc/php/php.ini.

pthreads

If you wish to have POSIX multi-threading you will need the pthreads extension. To install the pthreads (http://pecl.php.net/package/pthreads) extension using pecl you are required to use a compiled version of PHP with the the thread safety support flag --enable-maintainer-zts. Currently the most clean way to do this would be to rebuild the original package with the flag.

Instruction can be found on the PHP pthreads extension page.

mcrypt

If you want the mcrypt module, install php-mcrypt and uncomment the line in /etc/php/php.ini:

extension=mcrypt.so

PCNTL

PCNTL allows you to create process directly into the server side machine. While this may seen as something you would want, it also gives the power to PHP to mess things up really bad. So it is a PHP extension that cannot be loaded like other more convenient extension. This is because of the great power it gives to PHP. To enable it (by default it is disabled) PCNTL has to be compiled into 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

Caching

There are two kinds of caching in PHP: opcode/bytecode caching and userland/user data caching. Both allow for substantial gains in applications speed, and therefore should be enabled wherever possible.

For optimal caching, you should enable both. To do this, follow both #OPCache and #APCu.

OPCache

OPCache comes bundled with the standard PHP distribution, therefore to enable it you simply have to add or uncomment the following line in your PHP configuration file:

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

A list of its options and suggested settings can be found in its official entry on the PHP website.

Warning: If you choose to apply the suggested settings its manual offers, be sure to read carefully the first comment below those instructions as well. In some configurations those settings result in errors such as zend_mm_heap corrupted being produced.

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.

Development tools

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

A complete IDE for many languages including PHP. Includes features like debugging, refactoring, code tempalting, 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.

Troubleshooting

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.

See also