PHP

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page PHP and the translation is 55% complete.
Outdated translations are marked like this.
Other languages:
Resources

PHP è un linguaggio di scripting general-purpose lato server per produrre pagine web.

Installazione

USE flags

Le USE flags più importanti del pacchetto PHP, cioè le USE flags SAPI (Server API), sono apache2, cgi, e fpm. Unitamente a queste flags, c'è anche una elaborata lista di flags utili per abilitare varie caratteristiche dell'interprete PHP:

USE flags for dev-lang/php The PHP language runtime engine

acl Add support for Access Control Lists
apache2 Add Apache2 support
apparmor Support FPM application confinement through sys-libs/libapparmor
argon2 Enable password hashing algorithm from app-crypt/argon2
avif Add AV1 Image Format (AVIF) support
bcmath Enable the BCMath Arbitrary Precision Mathematics extension
berkdb Add support for sys-libs/db (Berkeley DB for MySQL)
bzip2 Enable bzip2 compression support
calendar Add support for calendars (not using mcal!)
capstone Support opcache JIT disassembly through dev-libs/capstone
cdb Add support for the CDB database engine from the author of qmail
cgi Add CGI script support
cjk Add support for Multi-byte character languages (Chinese, Japanese, Korean)
cli Enable the Command-Line Interface (CLI) SAPI
coverage Include gcov symbols for test coverage and lcov reports. Only useful for extension developers, and requires GCC.
ctype Enable the Character type checking (ctype) extension
curl Add support for client-side URL transfer library
debug Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see https://wiki.gentoo.org/wiki/Project:Quality_Assurance/Backtraces
embed Enable embed SAPI
enchant Enable the Enchant spelling library extension using app-text/enchant
exif Add support for reading EXIF headers from JPEG and TIFF images
ffi Enable the Foreign Function Interface (FFI) extension using dev-libs/libffi
fileinfo Enable the File Information extension
filter Enable the Data Filtering extension
firebird Add support for the Firebird relational database
flatfile Add dbm support for flat files
fpm Enable the FastCGI Process Manager SAPI
ftp Add FTP (File Transfer Protocol) support
gd Enable the Image Processing and GD extension (requires media-libs/libjpeg-turbo and media-libs/libpng)
gdbm Add support for sys-libs/gdbm (GNU database libraries)
gmp Add support for dev-libs/gmp (GNU MP library)
iconv Enable support for the iconv character set conversion library
imap Add support for IMAP (Internet Mail Application Protocol)
inifile Add dbm support for .ini files
intl Enables the intl extension for extended internalization support
iodbc Add support for iODBC library
ipv6 Support IPv6 connectivity in fopen and friends, and v6 address conversions in functions like inet_pton and inet_ntop.
jit Enable PCRE JIT support
kerberos Add kerberos support
ldap Add LDAP support (Lightweight Directory Access Protocol)
ldap-sasl Add SASL support for the PHP LDAP extension using dev-libs/cyrus-sasl
libedit Use the libedit library (replacement for readline)
lmdb Enable support for dev-db/lmdb db backend
mhash Add support for the mhash library
mssql Add support for Microsoft SQL Server database
mysql Add mySQL Database support
mysqli Add support for the improved mySQL libraries
nls Add Native Language Support (using gettext - GNU locale utilities)
oci8-instant-client Use dev-db/oracle-instantclient-basic as Oracle provider instead of requiring a full Oracle server install
odbc Add ODBC Support (Open DataBase Connectivity)
opcache Enables built-in opcode cache, replacing pecl-apc et al.
pcntl Enable the Process Control extension
pdo Enable the PHP Data Objects extension
phar Enables the phar extension to provide phar archive support
phpdbg Enable the PHP Debug Command Line SAPI (like gdb for php)
posix Add support for POSIX-compatible functions
postgres Add support for the postgresql database
qdbm Add support for the qdbm (Quick Database Manager) library
readline Enable support for libreadline, a GNU line-editing library that almost everyone wants
selinux !!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur
session Add persistent session support
session-mm Include dev-libs/mm support for session storage
sharedmem Enable the shmop extension
simplexml Enable the SimpleXML extension
snmp Add support for the Simple Network Management Protocol if available
soap Add support for SOAP (Simple Object Access Protocol)
sockets Add support for tcp/ip sockets
sodium Enable support for crypto through dev-libs/libsodium
spell Add dictionary support
sqlite Add support for sqlite - embedded sql database
ssl Add support for SSL/TLS connections (Secure Socket Layer / Transport Layer Security)
systemd Enable use of systemd-specific libraries and features like socket activation or session tracking
sysvipc Enable the PHP System-V semaphore, shared memory and IPC extension
test Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently)
threads Add threads support for various packages. Usually pthreads
tidy Add support for HTML Tidy
tokenizer Add support for the PHP file parser
tokyocabinet Enable support for dev-db/tokyocabinet db backend
truetype Add support for FreeType and/or FreeType2 fonts
unicode Add support for Unicode
valgrind Enable annotations for accuracy. May slow down runtime slightly. Safe to use even if not currently using dev-debug/valgrind
webp Add support for the WebP image format
xml Add support for XML files
xmlreader Enable the XMLReader extension
xmlwriter Enable the XMLWriter extension
xpm Add support for XPM graphics format
xslt Build the XSL extension
zip Enable support for ZIP archives
zlib Add support for zlib compression

Di default l'ebuild installa la versione di sviluppo php.ini. Per installare la versione production, che ha impostazioni di sicurezza maggiori ma potrebbe essere più difficile eseguirgli il debug, aggiungere al file make.conf la seguente riga:

Estensioni

Per installare le estensioni, per prima cosa decidere per quale versione di PHP bisogna compilare tali estensioni. Questo va fatto impostando la variabile PHP_TARGETS:

FILE /etc/portage/make.conf
PHP_TARGETS="php5-6"

Possono essere impostate più di una versione; basta aggiungere un'altra versione separata da spazio. Notare che lo slot php è chiamato "5.6" e il corrispondente valore di PHP_TARGETS è php5-6. Questo è dovuto alla corrente restrizione sul nome USE.

Emerge

Dopo aver effettuato la configurazione sopra descritta della USE flag è necessario aggiornare il sistema così che le variazioni abbiano effetto:

root #emerge --ask --update --changed-use --deep @world

Eseguire versioni multiple di PHP

Uno dei maggiori vantaggi per utilizzare Gentoo per lo sviluppo di PHP è l'utilizzo delle versioni slot. E' veramente facile muoversi tra le versioni multiple di PHP, è come eseguirle simultaneamente. Tutto ciò è fatto con il comando eselect fornito dal pacchetto app-eselect/eselect-php, il quale dovrebbe essere installato automaticamente insieme al pacchetto PHP. Per esempio, selezionando differenti versioni di PHP permette al sistema di eseguire PHP 7.0 per il cli SAPI ma PHP 5.6 per il server web di sistema. Esso permette anche di testare rapidamente un'applicazione su versioni differenti di PHP.

For example, selecting different versions of PHP can allow a system to run PHP 7.0 for the cli SAPI but PHP 5.6 for the system's web server. It also allows system administrators or application developers to quickly test an application on different versions of PHP.

Per elencare le versioni disponibili per il cli SAPI, utilizzare il seguente comando:

root #eselect php list cli
 [1]   php5.5 
 [2]   php5.6 *   
 [3]   php7.0

La versione segnata con * è la versione attualmente attiva, e per verificare le altre sostituire cli con fpm,cgi oppure apache2.

Per scambiare versioni utilizzare il seguente comando:

root #eselect php set cli 3

Il numero 3 in questo esempio corrisponde al numero dell'output del sotto-comando list utilizzato sopra. La versione 7.0 di PHP viene così utilizzata con il seguente comando:

root #php -v
PHP 7.0.10-pl0-gentoo (cli) (built: Aug 23 2016 12:38:44) ( NTS )

Configurazione

La configurazione di PHP si trova nel file /etc/php, il quale contiene una sottodirectory per ogni SAPI e per ogni versione di PHP. Per esempio, i files di configurazione per il SAPI apache2 PHP-5.6 sono installati in /etc/php/apache2-php5.6.

Web servers

Per utilizzare PHP dal lato server, deve essere installato e configurato per utilizzare PHP un web server. Qualche web server popolare è brevemente menzionato successivamente.

Nginx

Per installare nginx basta semplicemente emergerlo:

root #emerge --ask www-servers/nginx
Importante
Se i valori NGINX_MODULES_HTTP sono lasciati di default in make.conf, assicurarsi che il modulo fastcgi sia abilitato.

Una volta che Nginx è installato, modificare la sezione server in /etc/nginx/nginx.conf come di seguito:

FILE /etc/nginx/nginx.confParte della configurazione di nginx per abilitare PHP tramite FastCGI
server {
                listen 127.0.0.1;
                server_name localhost;
 
                access_log /var/log/nginx/localhost.access_log main;
                error_log /var/log/nginx/localhost.error_log info;
 
                root /var/www/localhost/htdocs;
 
                location ~ .php$ {
                        fastcgi_pass 127.0.0.1:9000;
                        include fastcgi.conf;
                }
        }

Ora avviare i relativi servizi per avere un sito funzionante. Lo script init php-fpm avvia il Process Manager PHP FastCGI. FastCGI permette ai web servers di scaricare i calcoli PHP per questo process manager.

When running OpenRC as the service manager:

root #service nginx start
root #service php-fpm start

For systemd:

root #systemctl start nginx
root #systemctl start php-fpm@7.2
Nota
The version number after the php-fpm service name will change based on the available version of PHP. Adjust as appropriate.

lighttpd

Attenzione
Non abilitare la USE flag php per il pacchetto www-servers/lighttpd. Attualmente potrebbe bloccare la compilazione.

Assicurarsi che lighttpd venga compilato con il supporto per fastcgi e che la USE flag php sia disabilitata:

FILE /etc/portage/package.use
www-servers/lighttpd fastcgi -php

Lighttpd lavora con il file di configurazione FastCGI di default, ma sfortunatamente questo file è scritto per lavorare solo con il PHP FCGI SAPI vecchio invece che con l'impostazione FCGI generale. Dopo che la USE flag php è stata disabilitata tale impostazione non è più inclusa, ed è giusto così. Tuttavia è fornita una buona base per il file di configurazione che può essere utilizzato con FPM.

Editare /etc/lighttpd/mod_fastcgi.conf simile al seguente:

FILE /etc/lighttpd/mod_fastcgi.confAbilitare FastCGI su lighttpd
server.modules += ("mod_fastcgi")
fastcgi.server = ( ".php" =>
  ( "localhost" =>
    (
      "host" => "127.0.0.1",
      "port" => "9000"
    )
  )
)

Notare la parti host e port.

Da quando Gentoo lavora con un file php-fpm.ini funzionante situato in /etc/php/fpm-php5/php-fpm.ini e lo script init, tutto quello che è necessario è l'avvio dei servizi:

root #/etc/init.d/php-fpm start
root #/etc/init.d/lighttpd start

Apache (mod_php)

Per configurare Apache in modo che sia in grado di caricare il modulo PHP5 (mod_php), aggiungere -D PHP sulla variabile APACHE2_OPTS nel file /etc/conf.d/apache2. Gli utenti si dovrebbero ricordare che anche in precedenza la versione PHP doveva essere aggiunta (tipo -D PHP5). Tuttavia, dalla versione 0.8.1 del pacchetto app-eselect/eselect-php la variabile è cambiata in -D PHP, per permettere che eventuali future nuove versioni maggiori possano essere facilmente integrate.

FILE /etc/conf.d/apache2Configurare Apache per caricare mod_php
## (settings for PHP5 and above)
APACHE2_OPTS="-D PHP"

Assicurarsi che PHP sia compilato con la USE flag apache2.

Se per qualche ragione sul sistema manca il file di integrazione del modulo PHP /etc/apache2/modules.d/70_mod_php.conf, file attualmente fornito dal pacchetto app-eselect/eselect-php quando questo viene compilato con la USE flag apache2 impostata, inserirlo manualmente. Il suo contenuto è quello mostrato qui sotto.

FILE /etc/apache2/modules.d/70_mod_php.conf
<IfDefine PHP>
	# The mod_php.so symlink is controlled by
	# eselect-php. However, the module name changed from
	# php5_module to php7_module so we can't blindly load whatever
	# is there. Instead we let eselect-php manage a small
	# configuration file that loads the appropriate module.
	#
	# This is relative to ServerRoot (see httpd.conf).
	Include ../../../var/lib/eselect-php/mod_php.conf
 
	# Tell apache that mod_php should handle PHP files.
	#
	# NOTE: Avoiding AddHandler/AddType for security (bug
	# #538822). Please read the related news item!
	<FilesMatch "\.(php|php[57]|phtml)$">
		SetHandler application/x-httpd-php
	</FilesMatch>
 
	# PHP source files which are meant to be displayed as
	# syntax-highlighted source code.
	<FilesMatch "\.phps$">
		SetHandler application/x-httpd-php-source
	</FilesMatch>
 
	DirectoryIndex index.php index.phtml
</IfDefine>

Sebbene sul sistema possono essere installate multiple versioni di PHP, Apache ne può utilizzare soltanto una. Con il comando eselect php si sceglie la versione di PHP da utilizzare.

To change the version of PHP handled by Apache, first list the available versions for the apache2 Server Application Programming Interface (SAPI):

root #eselect php list apache2
 [1]   php5.3
 [2]   php5.4 *
 [3]   php5.5

Change it to the version of choice:

root #eselect php set apache2 N

Substitute N in the example above to the requested number in the output of eselect php list apache2 as displayed earlier on.

To verify the PHP module works, create a test page:

FILE /var/www/localhost/htdocs/index.phpPHP test page
<html>
 <body>
  <?php phpinfo(); ?>
 </body>
</html>

Now, suppress or rename /var/www/localhost/htdocs/index.html and open the test page: http://localhost/

A table describing the PHP settings should be visible.

Apache (fcgid)

See enabling PHP through fcgid in the Apache article.

Apache (mod_proxy_fcgi)

See enabling PHP-FPM through mod_proxy_fcgi in Apache 2.4.

Vedere anche

External resources