PHP
PHPは、動的なWebページを生成する汎用のサーバー側スクリプト言語です。
インストール
USE フラグ
PHP パッケージで最重要の USE フラグである、SAPI (サーバ API) USE フラグは、apache2、cgi、そして fpm です。これらのフラグと並んで、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 |
bcmath
|
Add support for libbcmath |
berkdb
|
Add support for sys-libs/db (Berkeley DB for MySQL) |
bzip2
|
Use the bzlib compression library |
calendar
|
Add support for calendars (not using mcal!) |
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 CLI SAPI |
coverage
|
Include gcov symbols for test coverage and lcov reports. Only useful for extension developers, and requires GCC. |
ctype
|
Enable ctype functions |
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
|
Add supports Enchant spelling library. |
exif
|
Add support for reading EXIF headers from JPEG and TIFF images |
ffi
|
Add foreign function interface (ffi extension) support |
fileinfo
|
Add fileinfo extension support |
filter
|
Add filter extension support |
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
|
Adds support for gd (bundled with PHP) |
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
|
Add support for IP version 6 |
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 |
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 gettextGNU 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
|
Add support for process creation functions |
pdo
|
Enable the bundled PDO extensions |
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
|
Add support for shared memory use |
simplexml
|
Support for SimpleXML |
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 sqliteembedded 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
|
Support for System V-compatible inter-process communication |
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 |
webp
|
Enable webp support for GD in php-7.x |
xml
|
Add support for XML files |
xmlreader
|
Enable XMLReader support |
xmlwriter
|
Enable XMLWriter support |
xpm
|
Add support for XPM graphics format |
xslt
|
Enable the XSL extension |
zip
|
Enable support for ZIP archives |
zlib
|
Add support for zlib (de)compression |
この ebuild は、"production" バージョンの php.ini に対し、より厳密なセキュリティ設定と、Gentoo システム用にデフォルトパスを調整する修正を施したものをインストールします。上流の "production" と "development" の php.ini ファイルは、/usr/share/doc/php-* にインストールされます。
拡張
拡張モジュールをインストールするには、まずどのバージョンの PHP に対して拡張モジュールをコンパイルするかを決定してください。これは、PHP_TARGETS 変数を設定することで行えます:
/etc/portage/make.conf
PHP_TARGETS="php5-6"
複数のバージョンを定義することができます; スペースで区切って、追加のバージョンを追加してください。php スロットの名前が "5.6" のとき、対応する PHP_TARGETS の値は php5-6
であることに注意してください。これは現行の USE 名の制限によるものです。
Emerge
上記のUSEフラグ設定を行った後、変更を有効にするためにシステムを更新する必要があります:
root #
emerge --ask --update --changed-use --deep @world
複数のバージョンの PHP を実行する
PHP 開発のために Gentoo を使用する大きな利点のひとつは、バージョンスロットです。複数のバージョンの PHP の間で切り換えたり、複数のバージョンを同時に実行するのは、とても簡単に行えます。これはすべて app-eselect/eselect-php パッケージが提供する eselect コマンドによって行われ、これは PHP をインストールすると自動的に emerge されるはずです。
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.
To list the available versions for the cli SAPI module use the following syntax:
root #
eselect php list cli
[1] php5.5 [2] php5.6 * [3] php7.0
The *
(asterisk) marks current active version for the selected module. To check the other SAPIs simply replace cli
with fpm
,cgi
or apache2
modules.
バージョンを切り替えるには、以下を利用して下さい:
root #
eselect php set cli 3
The number 3
in this example corresponds to the number in the output of the list
sub-command used above. PHP version 7.0 is now used for the command-line:
root #
php -v
PHP 7.0.10-pl0-gentoo (cli) (built: Aug 23 2016 12:38:44) ( NTS )
設定
The PHP configuration is at /etc/php, which contains one subdirectory for each Server API (SAPI) and for each PHP version. For instance, configuration files for the PHP-5.6 apache2 SAPI are installed in /etc/php/apache2-php5.6.
ウェブサーバ
PHPをサーバ側で使用するには、WebサーバをインストールしてPHPを使用するように設定する必要があります。 数多くの一般的なWebサーバーが次に簡単に触れられます。
Nginx
nginx をインストールするには、単に emerge してください:
root #
emerge --ask www-servers/nginx
If custom NGINX_MODULES_HTTP values are set in make.conf, make sure that the
fastcgi
module is enabled.Once Nginx has been installed, modify the server section of /etc/nginx/nginx.conf to look something like this:
/etc/nginx/nginx.conf
Part of the nginx configuration to enable PHP through FastCGIserver { 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$ { try_files $uri $uri/ =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; } }
Now start the related services to have a working site. The php-fpm init script starts the PHP FastCGI Process Manager. FastCGI allows web servers to offload the PHP calculations to this process manager.
サービスマネージャとして OpenRC を実行している場合は:
root #
rc-service nginx start
root #
rc-service php-fpm start
systemd では:
root #
systemctl start nginx
root #
systemctl start php-fpm@7.2
The version number after the
php-fpm
service name will change based on the available version of PHP. Adjust as appropriate.lighttpd
Make sure that lighttpd is built with support for fastcgi
and that the php
USE flag is disabled:
/etc/portage/package.use
www-servers/lighttpd fastcgi -php
Lighttpd ships with a default FastCGI config file, but unfortunately, it is written to work with the old PHP FCGI SAPI only, instead of a general FCGI setup. Since the php
USE flag has been disabled, it will not be included, and rightly so. However it does provide a good foundation for a configuration file that can be used with FPM.
Edit /etc/lighttpd/mod_fastcgi.conf to look something like this:
/etc/lighttpd/mod_fastcgi.conf
Enable FastCGI on lighttpdserver.modules += ("mod_fastcgi") fastcgi.server = ( ".php" => ( "localhost" => ( "host" => "127.0.0.1", "port" => "9000" ) ) )
Note the host
and port
parts.
Since Gentoo ships with a working php-fpm.ini file, located in /etc/php/fpm-php5/php-fpm.ini, and init script, starting the services is all that is needed:
root #
/etc/init.d/php-fpm start
root #
/etc/init.d/lighttpd start
Apache (mod_php)
To configure Apache to load the PHP5 module (mod_php), add -D PHP
to APACHE2_OPTS variable in /etc/conf.d/apache2. Users might remember that previously, the PHP version had to be added as well (like -D PHP5
). However, since app-eselect/eselect-php version 0.8.1, the variable is changed to just -D PHP
to allow future major versions to be easily integrated.
/etc/conf.d/apache2
Configure Apache to load mod_php## (settings for PHP5 and above) APACHE2_OPTS="-D PHP"
Make sure that PHP is built with the apache2
USE flag.
If for some reason the system is missing the PHP module integration file /etc/apache2/modules.d/70_mod_php.conf, currently provided by app-eselect/eselect-php when installed with apache2
USE flag set, manually insert it. Its current content is displayed below.
/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>
Although multiple PHP versions can be installed on a system, Apache can only use a single PHP version with mod_php. Support for multiple PHP versions on Apache is available using fpm. The eselect php command is used to switch between active mod PHP versions.
参考
- Upgrading to PHP 5.6, guide to upgrade older installations to 5.6
- Upgrading to PHP 7.1, guide to upgrade older installations to 7.1
- Enabling PHP support in the Apache article