CakePHP4を試す


環境構築 - Vagrant で LAMP 環境を構築 (3) -

CakePHP 4のインストール

前回はMySQLをセットアップしました。今回はCakePHPをインストールしたいと思います。
まずはcomposerのバージョンを確認します。

$ composer --version ↵
Composer 1.6.3 2018-01-31 16:28:17

composerを使ってCakePHP4をインストールします。

$ cd cd /vagrant_data/ ↵
$ composer create-project --prefer-dist cakephp/app:4.* cms ↵
Installing cakephp/app (4.0.3)
  - Installing cakephp/app (4.0.3): Downloading (100%)         
Created project in cms
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 80 installs, 0 updates, 0 removals
  - Installing cakephp/plugin-installer (1.2.0): Downloading (100%)         
PHP Fatal error:  Uncaught Error: Class 'Cake\Composer\Installer\PluginInstaller' not found in /usr/share/php/Composer/Plugin/PluginManager.php:201
Stack trace:
#0 /usr/share/php/Composer/Installer/PluginInstaller.php(63): Composer\Plugin\PluginManager->registerPackage()
#1 /usr/share/php/Composer/Installer/InstallationManager.php(173): Composer\Installer\PluginInstaller->install()
#2 /usr/share/php/Composer/Installer/InstallationManager.php(160): Composer\Installer\InstallationManager->install()
#3 /usr/share/php/Composer/Installer.php(588): Composer\Installer\InstallationManager->execute()
#4 /usr/share/php/Composer/Installer.php(227): Composer\Installer->doInstall()
#5 /usr/share/php/Composer/Command/CreateProjectCommand.php(193): Composer\Installer->run()
#6 /usr/share/php/Composer/Command/CreateProjectCommand.php(145): Composer\Command\CreateProjectCommand->installProject()
#7 /usr/share/php/Symfony/Component/Console/Command/Command.php(252): Composer\Command\CreateProjectCommand->execute()
#8 /usr/share/php/Symfony/Compo in /usr/share/php/Composer/Plugin/PluginManager.php on line 201

Fatal error: Uncaught Error: Class 'Cake\Composer\Installer\PluginInstaller' not found in /usr/share/php/Composer/Plugin/PluginManager.php:201
Stack trace:
#0 /usr/share/php/Composer/Installer/PluginInstaller.php(63): Composer\Plugin\PluginManager->registerPackage()
#1 /usr/share/php/Composer/Installer/InstallationManager.php(173): Composer\Installer\PluginInstaller->install()
#2 /usr/share/php/Composer/Installer/InstallationManager.php(160): Composer\Installer\InstallationManager->install()
#3 /usr/share/php/Composer/Installer.php(588): Composer\Installer\InstallationManager->execute()
#4 /usr/share/php/Composer/Installer.php(227): Composer\Installer->doInstall()
#5 /usr/share/php/Composer/Command/CreateProjectCommand.php(193): Composer\Installer->run()
#6 /usr/share/php/Composer/Command/CreateProjectCommand.php(145): Composer\Command\CreateProjectCommand->installProject()
#7 /usr/share/php/Symfony/Component/Console/Command/Command.php(252): Composer\Command\CreateProjectCommand->execute()
#8 /usr/share/php/Symfony/Compo in /usr/share/php/Composer/Plugin/PluginManager.php on line 201

エラーが発生しています。
パッケージでインストールしたcomposerのバージョンが古かったのでしょうか?
最新にしてみます。まずはパッケージのcomposerをアンインストールします。

$ sudo apt remove composer ↵
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  jsonlint php-cli-prompt php-composer-ca-bundle php-composer-semver php-composer-spdx-licenses php-json-schema php-psr-log php-symfony-console
  php-symfony-debug php-symfony-filesystem php-symfony-finder php-symfony-polyfill-mbstring php-symfony-process
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  composer
0 upgraded, 0 newly installed, 1 to remove and 9 not upgraded.
After this operation, 1864 kB disk space will be freed.
Do you want to continue? [Y/n] y ↵
(Reading database ... 72237 files and directories currently installed.)
Removing composer (1.6.3-1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

composerの公式ドキュメントに従って作業を進めます。
まずインストーラをダウンロードします。

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" ↵
PHP Warning:  copy(): SSL: Connection reset by peer in Command line code on line 1
PHP Warning:  copy(): Failed to enable crypto in Command line code on line 1
PHP Warning:  copy(https://getcomposer.org/installer): failed to open stream: operation failed in Command line code on line 1

え!
SSLのエラーです。一般的にこの手のエラーはルートCAの証明書が手元にないときに発生します。
Google先生にお伺いを立てても概ね証明書を指定するようにと言われます。
では、そのようにしてみましょう。

$ ls -l /etc/ssl/certs/ca-* ↵
-rw-r--r-- 1 root root 207436 Apr 16 15:54 /etc/ssl/certs/ca-certificates.crt

ca-certificates.crtが既に手元にありましたので、このファイルを明示してみます。

$ cd /etc/php/7.4/cli/ ↵
$ sudo cp -p php.ini php.ini.ORG ↵
$ sudo vi php.ini ↵

openssl.cafileディレクティブを探して、ca-certificates.crtをフルパスで指定してみます。

/etc/php/7.4/cli/php.ini
[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
;curl.cainfo =

[openssl]
; The location of a Certificate Authority (CA) file on the local filesystem
; to use when verifying the identity of SSL/TLS peers. Most users should
; not specify a value for this directive as PHP will attempt to use the
; OS-managed cert stores in its absence. If specified, this value may still
; be overridden on a per-stream basis via the "cafile" SSL stream context
; option.
;openssl.cafile=

; If openssl.cafile is not specified or if the CA file is not found, the
; directory pointed to by openssl.capath is searched for a suitable
; certificate. This value must be a correctly hashed certificate directory.
; Most users should not specify a value for this directive as PHP will
; attempt to use the OS-managed cert stores in its absence. If specified,
; this value may still be overridden on a per-stream basis via the "capath"
; SSL stream context option.
;openssl.capath=

を次のように修正します。

/etc/php/7.4/cli/php.ini
[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
;curl.cainfo =

[openssl]
; The location of a Certificate Authority (CA) file on the local filesystem
; to use when verifying the identity of SSL/TLS peers. Most users should
; not specify a value for this directive as PHP will attempt to use the
; OS-managed cert stores in its absence. If specified, this value may still
; be overridden on a per-stream basis via the "cafile" SSL stream context
; option.
openssl.cafile=/etc/ssl/certs/ca-certificates.crt

; If openssl.cafile is not specified or if the CA file is not found, the
; directory pointed to by openssl.capath is searched for a suitable
; certificate. This value must be a correctly hashed certificate directory.
; Most users should not specify a value for this directive as PHP will
; attempt to use the OS-managed cert stores in its absence. If specified,
; this value may still be overridden on a per-stream basis via the "capath"
; SSL stream context option.
;openssl.capath=

念のため/etc/php/7.4/apache2/php.iniも同様に編集します。
再びダウンロードを試みます。

$ cd ↵
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" ↵
PHP Warning:  copy(): SSL: Connection reset by peer in Command line code on line 1
PHP Warning:  copy(): Failed to enable crypto in Command line code on line 1
PHP Warning:  copy(https://getcomposer.org/installer): failed to open stream: operation failed in Command line code on line 1

変わりませんね。
当然と言えばそうかもしれません。
ComposerからLaravelを導入しようとしてOpenSSL周りのエラーで困った件によると、http://curl.haxx.se/ca/cacert.pemをダウンロードして組み込めばよいらしいです。
それでは早速やってみましょう。

$ wget http://curl.haxx.se/ca/cacert.pem ↵
$ sudo cp -v -f cacert.pem /etc/ssl/certs/ca-bundle-curl.crt ↵

今回は、openssl.cafileディレクティブとcurl.cainfoディレクティブを探して、ca-bundle-curl.crtをフルパスで指定してみます。

$ sudo vi /etc/php/7.4/cli/php.ini ↵
/etc/php/7.4/cli/php.ini
[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
;curl.cainfo =

[openssl]
; The location of a Certificate Authority (CA) file on the local filesystem
; to use when verifying the identity of SSL/TLS peers. Most users should
; not specify a value for this directive as PHP will attempt to use the
; OS-managed cert stores in its absence. If specified, this value may still
; be overridden on a per-stream basis via the "cafile" SSL stream context
; option.
openssl.cafile=/etc/ssl/certs/ca-certificates.crt

; If openssl.cafile is not specified or if the CA file is not found, the
; directory pointed to by openssl.capath is searched for a suitable
; certificate. This value must be a correctly hashed certificate directory.
; Most users should not specify a value for this directive as PHP will
; attempt to use the OS-managed cert stores in its absence. If specified,
; this value may still be overridden on a per-stream basis via the "capath"
; SSL stream context option.
;openssl.capath=

を次のように修正します。

/etc/php/7.4/cli/php.ini
[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
curl.cainfo =/etc/ssl/certs/ca-bundle-curl.crt

[openssl]
; The location of a Certificate Authority (CA) file on the local filesystem
; to use when verifying the identity of SSL/TLS peers. Most users should
; not specify a value for this directive as PHP will attempt to use the
; OS-managed cert stores in its absence. If specified, this value may still
; be overridden on a per-stream basis via the "cafile" SSL stream context
; option.
openssl.cafile=/etc/ssl/certs/ca-bundle-curl.crt

; If openssl.cafile is not specified or if the CA file is not found, the
; directory pointed to by openssl.capath is searched for a suitable
; certificate. This value must be a correctly hashed certificate directory.
; Most users should not specify a value for this directive as PHP will
; attempt to use the OS-managed cert stores in its absence. If specified,
; this value may still be overridden on a per-stream basis via the "capath"
; SSL stream context option.
;openssl.capath=

同様に/etc/php/7.4/apache2/php.iniも編集します。
三度ダウンロードを試みます。

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" ↵
PHP Warning:  copy(): SSL: Connection reset by peer in Command line code on line 1
PHP Warning:  copy(): Failed to enable crypto in Command line code on line 1
PHP Warning:  copy(https://getcomposer.org/installer): failed to open stream: operation failed in Command line code on line 1

う~ん。変わりませんね。
物は試し、リブートしてみます。

$ shutdown -r now ↵
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" ↵
PHP Warning:  copy(): SSL: Connection reset by peer in Command line code on line 1
PHP Warning:  copy(): Failed to enable crypto in Command line code on line 1
PHP Warning:  copy(https://getcomposer.org/installer): failed to open stream: operation failed in Command line code on line 1

やはり変わりませんね。
もとに戻してみましょうか?

$ sudo vi /etc/php/7.4/cli/php.ini ↵
/etc/php/7.4/cli/php.ini
[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
curl.cainfo =/etc/ssl/certs/ca-bundle-curl.crt

[openssl]
; The location of a Certificate Authority (CA) file on the local filesystem
; to use when verifying the identity of SSL/TLS peers. Most users should
; not specify a value for this directive as PHP will attempt to use the
; OS-managed cert stores in its absence. If specified, this value may still
; be overridden on a per-stream basis via the "cafile" SSL stream context
; option.
openssl.cafile=/etc/ssl/certs/ca-certificates.crt

; If openssl.cafile is not specified or if the CA file is not found, the
; directory pointed to by openssl.capath is searched for a suitable
; certificate. This value must be a correctly hashed certificate directory.
; Most users should not specify a value for this directive as PHP will
; attempt to use the OS-managed cert stores in its absence. If specified,
; this value may still be overridden on a per-stream basis via the "capath"
; SSL stream context option.
;openssl.capath=

を次のように修正しました。

/etc/php/7.4/cli/php.ini
[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
;curl.cainfo =/etc/ssl/certs/ca-bundle-curl.crt

[openssl]
; The location of a Certificate Authority (CA) file on the local filesystem
; to use when verifying the identity of SSL/TLS peers. Most users should
; not specify a value for this directive as PHP will attempt to use the
; OS-managed cert stores in its absence. If specified, this value may still
; be overridden on a per-stream basis via the "cafile" SSL stream context
; option.
;openssl.cafile=/etc/ssl/certs/ca-bundle-curl.crt

; If openssl.cafile is not specified or if the CA file is not found, the
; directory pointed to by openssl.capath is searched for a suitable
; certificate. This value must be a correctly hashed certificate directory.
; Most users should not specify a value for this directive as PHP will
; attempt to use the OS-managed cert stores in its absence. If specified,
; this value may still be overridden on a per-stream basis via the "capath"
; SSL stream context option.
;openssl.capath=

念のためリブートもしておきましょうか。

$ shutdown -r now ↵

ダメもとで、ダウンロードを試みます。

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" ↵

あれ?
ダウンロードが成功しました。
腑に落ちませんが、良しとして続きの処理を行います。

$ php -r "if (hash_file('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" ↵
Installer verified
$ php composer-setup.php ↵
All settings correct for using Composer
Downloading...
The "https://getcomposer.org/versions" file could not be downloaded: SSL: Connection reset by peer
Failed to enable crypto
failed to open stream: operation failed
Retrying...

Composer (version 1.10.5) successfully installed to: /home/vagrant/composer.phar
Use it: php composer.phar

やはりエラーが出ました。
原因と対処方法がわかりません。どなたかわかる方がおられたらお教えください。

CakePHPの公式ドキュメントによれば、Composerを使わない方法もあるようです。
次は、Composerを使わない方法で試してみようと思います。

参考サイト

ComposerからLaravelを導入しようとしてOpenSSL周りのエラーで困った件
(現在は解決しました)PHPの外部への接続でSSLのエラーが出てしまう@KUSANAGI PHP7.2