FreeBSD 12:PHP 7

PHP言語をインストールしてみましょう。これはwebアプリケーションに必要な言語なので、最新版の7.4を入れておきます。

PHPのインストール

以下のようにportsを使ってインストールします。

root@cafe:~ # cd /usr/ports/lang/php74
root@cafe:/usr/ports/lang/php74 # make install clean
===> Building/installing dialog4ports as it is required for the config dialog
===>  Cleaning for dialog4ports-0.1.6
===>  License BSD2CLAUSE accepted by the user
===>   dialog4ports-0.1.6 depends on file: /usr/local/sbin/pkg - not found
===> Skipping 'config' as NO_DIALOG is defined
===>  License BSD2CLAUSE accepted by the user
=> freebsd-pkg-1.12.0_GH0.tar.gz doesn't seem to exist in /usr/ports/distfiles/.
=> Attempting to fetch https://codeload.github.com/freebsd/pkg/tar.gz/1.12.0?dummy=/freebsd-pkg-1.12.0_GH0.tar.gz
fetch: https://codeload.github.com/freebsd/pkg/tar.gz/1.12.0?dummy=/freebsd-pkg-1.12.0_GH0.tar.gz: size unknown
fetch: https://codeload.github.com/freebsd/pkg/tar.gz/1.12.0?dummy=/freebsd-pkg-1.12.0_GH0.tar.gz: size of remote file is not known
freebsd-pkg-1.12.0_GH0.tar.gz                         3776 kB 3053 kBps    01s
===> Fetching all distfiles required by pkg-1.12.0 for building
===>  Extracting for pkg-1.12.0
===>  License BSD2CLAUSE accepted by the user
===> Fetching all distfiles required by pkg-1.12.0 for building
=> SHA256 Checksum OK for freebsd-pkg-1.12.0_GH0.tar.gz.
===>  Patching for pkg-1.12.0
===>  Applying FreeBSD patches for pkg-1.12.0
===>  Configuring for pkg-1.12.0
No installed jimsh or tclsh, building local bootstrap jimsh0
:
省略
:

cd コマンドで、portsのphp 7.4のディレクトリに移動した後、make コマンドを実行します。
すると、ソースプログラムをダウンロードし、コンパイルを実行します。

コンパイルオプションの選択画面では、初期値のまま「OK」を押しておきます。
依存関係のあるパッケージもportsによって自動的にインストールされます。
そのコンパイルオプションの選択画面が表示されるので、適当なものを選びます。

pkgconfも初期値のままにしておきます。

m4もそのまま。

texinfoもそのまま。

help2manもそのまま。

gettext-toolsもそのまま。

libtextstyleもそのまま。

gettext-runtimeもそのまま。

perl5もそのまま。

gmakeもそのまま。

p5-Locale-libintlもそのまま。

libiconvもそのまま。

automakeもそのまま。

pcre2もそのまま。

libxml2もそのまま。

:
省略
:
install  -m 0644 /usr/ports/lang/php74/work/php-7.4.0RC3/php.ini-development /usr/ports/lang/php74/work/php-7.4.0RC3/php.ini-production  /usr/ports/lang/php74/work/php.conf /usr/ports/lang/php74/work/stage//usr/local/etc
====> Compressing man pages (compress-man)
===> Staging rc.d startup script(s)
===>  Installing for php74-7.4.0.r3
===>  Checking if php74 is already installed
===>   Registering installation for php74-7.4.0.r3
Installing php74-7.4.0.r3...
===> SECURITY REPORT:
      This port has installed the following files which may act as network
      servers and may therefore pose a remote security risk to the system.
/usr/local/bin/php
/usr/local/bin/php-cgi
/usr/local/sbin/php-fpm
/usr/local/lib/libphp7.so

      This port has installed the following startup scripts which may cause
      these network services to be started at boot time.
/usr/local/etc/rc.d/php-fpm

      If there are vulnerabilities in these programs there may be a security
      risk to the system. FreeBSD makes no guarantee about the security of
      ports included in the Ports Collection. Please type 'make deinstall'
      to deinstall the port if this is a concern.

      For more information, and contact details about the security
      status of this software, see the following webpage:
https://www.php.net/
===>  Cleaning for pkgconf-1.6.3,1
===>  Cleaning for autoconf-2.69_3
===>  Cleaning for m4-1.4.18_1,1
===>  Cleaning for texinfo-6.6_4,1
===>  Cleaning for help2man-1.47.11
===>  Cleaning for p5-Locale-gettext-1.07
===>  Cleaning for gettext-tools-0.20.1_1
===>  Cleaning for libtextstyle-0.20.1
===>  Cleaning for libiconv-1.14_11
===>  Cleaning for indexinfo-0.3.1
===>  Cleaning for gettext-runtime-0.20.1
===>  Cleaning for perl5-5.30.0
===>  Cleaning for gmake-4.2.1_3
===>  Cleaning for p5-Locale-libintl-1.31
===>  Cleaning for p5-Text-Unidecode-1.30
===>  Cleaning for p5-Unicode-EastAsianWidth-12.0
===>  Cleaning for autoconf-wrapper-20131203
===>  Cleaning for automake-1.16.1_2
===>  Cleaning for pcre2-10.33
===>  Cleaning for libtool-2.4.6_1
===>  Cleaning for libargon2-20190702
===>  Cleaning for libxml2-2.9.9
===>  Cleaning for php74-7.4.0.r3
root@cafe:/usr/ports/lang/php74 #

しょっぱななので、コンパイル・インストール用のツールも一緒にインストールされましたね。
これで、php 7.4のインストールができました。

PHPの設定

root@cafe:/usr/ports/lang/php74 # cd /usr/local/etc/
root@cafe:/usr/local/etc # ls -l php*
-rw-r--r--  1 root  wheel   5336 Sep 20 09:31 php-fpm.conf
-rw-r--r--  1 root  wheel   5336 Sep 20 09:31 php-fpm.conf.default
-rw-r--r--  1 root  wheel    106 Sep 20 09:31 php.conf
-rw-r--r--  1 root  wheel  72379 Sep 20 09:31 php.ini-development
-rw-r--r--  1 root  wheel  72683 Sep 20 09:31 php.ini-production

php-fpm.d:
total 40
-rw-r--r--  1 root  wheel  19865 Sep 20 09:31 www.conf
-rw-r--r--  1 root  wheel  19865 Sep 20 09:31 www.conf.default
root@cafe:/usr/local/etc #

PHPの設定ファイルは、/usr/loca/etcにあります。
そこに移動してみると、php.iniファイルのサンプルがあります。

  • php.ini-development
    開発用php.iniファイル
  • php.ini-production
    実行用php.iniファイル
    今回は、PHPによる開発は行わないので、php.ini-productionをそのままコピーしておきます。
    root@cafe:/usr/local/etc # cp -p php.ini-production php.ini
    root@cafe:/usr/local/etc #

    php.iniを編集して日本対応にしておきます。

    root@cafe:/usr/local/etc # vi php.ini
    root@cafe:/usr/local/etc # diff php.ini-production  php.ini
    961c961
    < ;date.timezone =
    ---
    > date.timezone = Asia/Tokyo
    1627c1627
    < ;mbstring.language = Japanese
    ---
    > mbstring.language = Japanese
    1634c1634
    < ;mbstring.internal_encoding =
    ---
    > mbstring.internal_encoding = UTF-8
    root@cafe:/usr/local/etc #
  • date.timezoneにAsia/Tokyoを指定して、タイムゾーンを日本に。
  • mbstring.languageにJapaneseを指定して、日本語に。
  • mbstring.internal_encodingにUTF-8を指定して、文字コードをUTF-8に。

動作確認を行います。

root@cafe:/usr/local/etc # php -i
phpinfo()
PHP Version => 7.4.0RC3

System => FreeBSD cafe.lo.zeke.ne.jp 12.1-RELEASE FreeBSD 12.1-RELEASE r354233 GENERIC amd64
Build Date => Sep 20 2020 09:28:25
Configure Command =>  './configure'  '--with-layout=GNU' '--with-config-file-scan-dir=/usr/local/etc/php' '--disable-all' '--with-libxml' '--with-password-argon2=/usr/local' '--program-prefix=' '--enable-mysqlnd' '--enable-fpm' '--with-fpm-user=www' '--with-fpm-group=www' '--enable-embed' '--enable-dtrace' '--prefix=/usr/local' '--localstatedir=/var' '--mandir=/usr/local/man' '--infodir=/usr/local/share/info/' '--build=amd64-portbld-freebsd12.1' 'build_alias=amd64-portbld-freebsd12.1' 'PKG_CONFIG=pkgconf' 'CFLAGS=-O2 -pipe -fstack-protector-strong -fno-strict-aliasing ' 'CPP=cpp' 'CXXFLAGS=-O2 -pipe -fstack-protector-strong -fno-strict-aliasing '
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /usr/local/etc
Loaded Configuration File => /usr/local/etc/php.ini
Scan this dir for additional .ini files => /usr/local/etc/php
Additional .ini files parsed => (none)
PHP API => 20190902
PHP Extension => 20190902
Zend Extension => 320190902
Zend Extension Build => API320190902,NTS
PHP Extension Build => API20190902,NTS
Debug Build => no
Thread Safety => disabled
Zend Signal Handling => enabled
Zend Memory Manager => enabled
Zend Multibyte Support => disabled
IPv6 Support => enabled
DTrace Support => available, disabled

Registered PHP Streams => php, file, glob, data, http, ftp
Registered Stream Socket Transports => tcp, udp, unix, udg
Registered Stream Filters => string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk

This program makes use of the Zend Scripting Language Engine:
Zend Engine v3.4.0-dev, Copyright (c) Zend Technologies

 _______________________________________________________________________

Configuration

Core

PHP Version => 7.4.0RC3

Directive => Local Value => Master Value
allow_url_fopen => On => On
allow_url_include => Off => Off
arg_separator.input => & => &
arg_separator.output => & => &
auto_append_file => no value => no value
auto_globals_jit => On => On
auto_prepend_file => no value => no value
browscap => no value => no value
default_charset => UTF-8 => UTF-8
default_mimetype => text/html => text/html
disable_classes => no value => no value
disable_functions => no value => no value
display_errors => Off => Off
display_startup_errors => Off => Off
doc_root => no value => no value
docref_ext => no value => no value
docref_root => no value => no value
enable_dl => Off => Off
enable_post_data_reading => On => On
error_append_string => no value => no value
error_log => no value => no value
error_prepend_string => no value => no value
error_reporting => 22527 => 22527
expose_php => On => On
extension_dir => /usr/local/lib/php/20190902 => /usr/local/lib/php/20190902
file_uploads => On => On
hard_timeout => 2 => 2
highlight.comment => <font style="color: #FF8000">#FF8000</font> => <font style="color: #FF8000">#FF8000</font>
highlight.default => <font style="color: #0000BB">#0000BB</font> => <font style="color: #0000BB">#0000BB</font>
highlight.html => <font style="color: #000000">#000000</font> => <font style="color: #000000">#000000</font>
highlight.keyword => <font style="color: #007700">#007700</font> => <font style="color: #007700">#007700</font>
highlight.string => <font style="color: #DD0000">#DD0000</font> => <font style="color: #DD0000">#DD0000</font>
html_errors => Off => Off
ignore_repeated_errors => Off => Off
ignore_repeated_source => Off => Off
ignore_user_abort => Off => Off
implicit_flush => On => On
include_path => .:/usr/local/share/pear => .:/usr/local/share/pear
input_encoding => no value => no value
internal_encoding => no value => no value
log_errors => On => On
log_errors_max_len => 1024 => 1024
mail.add_x_header => Off => Off
mail.force_extra_parameters => no value => no value
mail.log => no value => no value
max_execution_time => 0 => 0
max_file_uploads => 20 => 20
max_input_nesting_level => 64 => 64
max_input_time => -1 => -1
max_input_vars => 1000 => 1000
memory_limit => 128M => 128M
open_basedir => no value => no value
output_buffering => 0 => 0
output_encoding => no value => no value
output_handler => no value => no value
post_max_size => 8M => 8M
precision => 14 => 14
realpath_cache_size => 4096K => 4096K
realpath_cache_ttl => 120 => 120
register_argc_argv => On => On
report_memleaks => On => On
report_zend_debug => Off => Off
request_order => GP => GP
sendmail_from => no value => no value
sendmail_path => /usr/sbin/sendmail -t -i  => /usr/sbin/sendmail -t -i
serialize_precision => -1 => -1
short_open_tag => Off => Off
SMTP => localhost => localhost
smtp_port => 25 => 25
sys_temp_dir => no value => no value
syslog.facility => LOG_USER => LOG_USER
syslog.filter => no-ctrl => no-ctrl
syslog.ident => php => php
track_errors => Off => Off
unserialize_callback_func => no value => no value
upload_max_filesize => 2M => 2M
upload_tmp_dir => no value => no value
user_dir => no value => no value
user_ini.cache_ttl => 300 => 300
user_ini.filename => .user.ini => .user.ini
variables_order => GPCS => GPCS
xmlrpc_error_number => 0 => 0
xmlrpc_errors => Off => Off
zend.assertions => -1 => -1
zend.detect_unicode => On => On
zend.enable_gc => On => On
zend.exception_ignore_args => On => On
zend.multibyte => Off => Off
zend.script_encoding => no value => no value
zend.signal_check => Off => Off

date

date/time support => enabled
timelib version => 2018.02
"Olson" Timezone Database Version => 2019.2
Timezone Database => internal
Default timezone => Asia/Tokyo

Directive => Local Value => Master Value
date.default_latitude => 31.7667 => 31.7667
date.default_longitude => 35.2333 => 35.2333
date.sunrise_zenith => 90.583333 => 90.583333
date.sunset_zenith => 90.583333 => 90.583333
date.timezone => Asia/Tokyo => Asia/Tokyo

hash

hash support => enabled
Hashing Engines => md2 md4 md5 sha1 sha224 sha256 sha384 sha512/224 sha512/256 sha512 sha3-224 sha3-256 sha3-384 sha3-512 ripemd128 ripemd160 ripemd256 ripemd320 whirlpool tiger128,3 tiger160,3 tiger192,3 tiger128,4 tiger160,4 tiger192,4 snefru snefru256 gost gost-crypto adler32 crc32 crc32b crc32c fnv132 fnv1a32 fnv164 fnv1a64 joaat haval128,3 haval160,3 haval192,3 haval224,3 haval256,3 haval128,4 haval160,4 haval192,4 haval224,4 haval256,4 haval128,5 haval160,5 haval192,5 haval224,5 haval256,5

libxml

libXML support => active
libXML Compiled Version => 2.9.9
libXML Loaded Version => 20909
libXML streams => enabled

mysqlnd

mysqlnd => enabled
Version => mysqlnd 7.4.0RC3
Compression => not supported
core SSL => supported
extended SSL => not supported
Command buffer size => 4096
Read buffer size => 32768
Read timeout => 86400
Collecting statistics => Yes
Collecting memory statistics => No
Tracing => n/a
Loaded plugins => mysqlnd,debug_trace,auth_plugin_mysql_native_password,auth_plugin_mysql_clear_password
API Extensions =>

pcre

PCRE (Perl Compatible Regular Expressions) Support => enabled
PCRE Library Version => 10.33 2019-04-16
PCRE Unicode Version => 11.0.0
PCRE JIT Support => enabled
PCRE JIT Target => x86 64bit (little endian + unaligned)

Directive => Local Value => Master Value
pcre.backtrack_limit => 1000000 => 1000000
pcre.jit => 1 => 1
pcre.recursion_limit => 100000 => 100000

Reflection

Reflection => enabled

SPL

SPL support => enabled
Interfaces => OuterIterator, RecursiveIterator, SeekableIterator, SplObserver, SplSubject
Classes => AppendIterator, ArrayIterator, ArrayObject, BadFunctionCallException, BadMethodCallException, CachingIterator, CallbackFilterIterator, DirectoryIterator, DomainException, EmptyIterator, FilesystemIterator, FilterIterator, GlobIterator, InfiniteIterator, InvalidArgumentException, IteratorIterator, LengthException, LimitIterator, LogicException, MultipleIterator, NoRewindIterator, OutOfBoundsException, OutOfRangeException, OverflowException, ParentIterator, RangeException, RecursiveArrayIterator, RecursiveCachingIterator, RecursiveCallbackFilterIterator, RecursiveDirectoryIterator, RecursiveFilterIterator, RecursiveIteratorIterator, RecursiveRegexIterator, RecursiveTreeIterator, RegexIterator, RuntimeException, SplDoublyLinkedList, SplFileInfo, SplFileObject, SplFixedArray, SplHeap, SplMinHeap, SplMaxHeap, SplObjectStorage, SplPriorityQueue, SplQueue, SplStack, SplTempFileObject, UnderflowException, UnexpectedValueException

standard

Dynamic Library Support => enabled
Path to sendmail => /usr/sbin/sendmail -t -i

Directive => Local Value => Master Value
assert.active => 1 => 1
assert.bail => 0 => 0
assert.callback => no value => no value
assert.exception => 0 => 0
assert.quiet_eval => 0 => 0
assert.warning => 1 => 1
auto_detect_line_endings => 0 => 0
default_socket_timeout => 60 => 60
from => no value => no value
session.trans_sid_hosts => no value => no value
session.trans_sid_tags => a=href,area=href,frame=src,form= => a=href,area=href,frame=src,form=
unserialize_max_depth => 4096 => 4096
url_rewriter.hosts => no value => no value
url_rewriter.tags => form= => form=
user_agent => no value => no value

Additional Modules

Module Name

Environment

Variable => Value
USER => root
LOGNAME => root
HOME => /root
MAIL => /var/mail/root
PATH => /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin
TERM => xterm
BLOCKSIZE => K
SHELL => /bin/csh
SSH_CLIENT => 192.168.1.69 50219 22
SSH_CONNECTION => 192.168.1.69 50219 192.168.1.7 22
SSH_TTY => /dev/pts/0
HOSTTYPE => FreeBSD
VENDOR => amd
OSTYPE => FreeBSD
MACHTYPE => x86_64
SHLVL => 1
PWD => /usr/local/etc
GROUP => wheel
HOST => cafe.lo.zeke.ne.jp
REMOTEHOST => 192.168.1.69
EDITOR => vi
PAGER => less

PHP Variables

Variable => Value
$_SERVER['USER'] => root
$_SERVER['LOGNAME'] => root
$_SERVER['HOME'] => /root
$_SERVER['MAIL'] => /var/mail/root
$_SERVER['PATH'] => /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin
$_SERVER['TERM'] => xterm
$_SERVER['BLOCKSIZE'] => K
$_SERVER['SHELL'] => /bin/csh
$_SERVER['SSH_CLIENT'] => 192.168.1.69 50219 22
$_SERVER['SSH_CONNECTION'] => 192.168.1.69 50219 192.168.1.7 22
$_SERVER['SSH_TTY'] => /dev/pts/0
$_SERVER['HOSTTYPE'] => FreeBSD
$_SERVER['VENDOR'] => amd
$_SERVER['OSTYPE'] => FreeBSD
$_SERVER['MACHTYPE'] => x86_64
$_SERVER['SHLVL'] => 1
$_SERVER['PWD'] => /usr/local/etc
$_SERVER['GROUP'] => wheel
$_SERVER['HOST'] => cafe.lo.zeke.ne.jp
$_SERVER['REMOTEHOST'] => 192.168.1.69
$_SERVER['EDITOR'] => vi
$_SERVER['PAGER'] => less
$_SERVER['PHP_SELF'] =>
$_SERVER['SCRIPT_NAME'] =>
$_SERVER['SCRIPT_FILENAME'] =>
$_SERVER['PATH_TRANSLATED'] =>
$_SERVER['DOCUMENT_ROOT'] =>
$_SERVER['REQUEST_TIME_FLOAT'] => 1600570469.4568
$_SERVER['REQUEST_TIME'] => 1600570469
$_SERVER['argv'] => Array
(
)

$_SERVER['argc'] => 0

PHP License
This program is free software; you can redistribute it and/or modify
it under the terms of the PHP License as published by the PHP Group
and included in the distribution in the file:  LICENSE

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

If you did not receive a copy of the PHP license, or have any
questions about PHP licensing, please contact license@php.net.
root@cafe:/usr/local/etc #

php -iコマンドで、PHPが実行できること、カスタマイズしたphp.iniの内容が反映されていることを確認します。

php-fpmの設定

次に、php-fpm(FastCGI Process Manager)の設定をしておきます。

root@cafe:/usr/local/etc # cat /usr/local/etc/php-fpm.conf | grep -v ^\; | grep -v ^$
[global]
pid = run/php-fpm.pid
include=/usr/local/etc/php-fpm.d/*.conf
root@cafe:/usr/local/etc # cat /usr/local/etc/php-fpm.d/www.conf | grep -v ^\; | grep -v ^$
[www]
user = www
group = www
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
root@cafe:/usr/local/etc #

設定ファイル/usr/local/etc/php-fpm.confおよびそこから呼ばれている/usr/local/etc/php-fpm.d/www.confの内容を確認します。
ローカルホストの9000番ポートで待ち受けてますね。
とりあえずこのままで、自動起動の設定だけいれておきましょう。

bsdconfigコマンドを実行して、「C Startup」を選択します。

「1 Toggle Startup Services」を選択します。

「php_fpm_enable」でリターンキーを押し、チェックをいれ、「X Exit」を選択します。

root@cafe:/usr/local/etc # cat /etc/rc.conf
hostname="cafe.lo.zeke.ne.jp"
keymap="jp.106"
ifconfig_vtnet0="inet 192.168.1.7 netmask 255.255.255.0"
defaultrouter="192.168.1.254"
ifconfig_vtnet0_ipv6="inet6 2001:2c0:cd03:ca00::cafe/64"
ipv6_defaultrouter="2001:2c0:cd03:ca00::254"
local_unbound_enable="YES"
sshd_enable="YES"
ntpdate_enable="YES"
ntpd_enable="YES"
# Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
dumpdev="NO"
php_fpm_enable="YES"
root@cafe:/usr/local/etc #

/etc/rc.confに設定内容が反映されていることを確認します。

root@cafe:/usr/local/etc # service php-fpm status
php_fpm is not running.
root@cafe:/usr/local/etc # service php-fpm start
Performing sanity check on php-fpm configuration:
[20-Sep-2020 12:35:29] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Starting php_fpm.
root@cafe:/usr/local/etc # service php-fpm status
php_fpm is running as pid 55110.
root@cafe:/usr/local/etc #

php-fpmサービスの状態を確認、サービス起動、起動後の状態を確認します。
正常に起動できているようですね。

PHPのモジュールの追加

インストールするアプリによってはPHPのモジュールが必要なものがあります。
そのとき、以下の手順で、モジュールを追加します。

root@cafe:~ # cd /usr/ports/lang/php74-extensions
root@cafe:/usr/ports/lang/php74-extensions # make install
===>  Staging for php74-extensions-1.0
===>   php74-extensions-1.0 depends on file: /usr/local/include/php/main/php.h - found

のように/usr/ports/lang/php74-extensionsでmake installです。

設定画面がでるので、必要なモジュールを追加してください。
モジュールの追加が終わったら

root@cafe:/usr/ports/lang/php74-extensions # service php-fpm restart
Performing sanity check on php-fpm configuration:
[28-Sep-2020 00:08:25] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Stopping php_fpm.
Waiting for PIDS: 640.
Performing sanity check on php-fpm configuration:
[28-Sep-2020 00:08:25] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Starting php_fpm.
root@cafe:/usr/ports/lang/php74-extensions #

service php-fpm restartコマンドでphp-fpmを再起動します。

以上で、PHPの設定は完了です。