1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VirtualBoxを使った仮想開発環境にCakePHPを導入するまで

Posted at

以前Node.jsやPythonの勉強のために立ち上げたVirtualBox+Vagrant上のUbuntuに、CakePHPで開発をできる環境を構築したいと思い色々と調べながら取り組んだ作業のログとなっています。

CakePHPのインストールガイドだけ見ても環境構築弱者の自分にはちんぷんかんぷんでしたので様々なページを参考にさせていただきました。追って参照として示させていただきます。

今回やりたいこと

すでにあるもの

  • Vagrant(Ubuntu)

Vagrantを使ってUbuntuの仮想環境を構築する事に関しては様々な記事がインターネット上にあります。
参照 [https://qiita.com/w2-yamaguchi/items/191830191f8af05ac4dd]

作りたいもの

CakePHPインストールのためのシステム要件
* HTTP サーバー。例: Apache。mod_rewrite が推奨されますが、必須ではありません。
* PHP 5.6.0 以上 (PHP 7.2 も含む)
* mbstring PHP 拡張
* intl PHP 拡張
* simplexml PHP 拡張
参照 [ https://book.cakephp.org/3.0/ja/installation.html ]

CakePHPのプロジェクト作成まで

環境を綺麗にする

違ったバージョンの混在などによってうまく動かないのが怖いので一旦PHP関係を取り除きます。

$ sudo apt remove php php-cli php-common

webサーバーのインストール(Apache2)

$ sudo apt install apache2
$ curl localhost:80 && echo success || echo failed
.
.
.
success

apache2の最初のページのhtmlとともに"success"の文字が出てくればオッケーです。

ifconfigを使ってIPアドレスを一応確認します。

$ ifconfig | grep "inet addr:"
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0

もちろんこのままだとホスト側のブラウザからはVagrant上のlocalhostを見ることはできないのでport forwardingをしてあげましょう。

Vagrantfileに以下の一行を足します。

config.vm.network "forwarded_port", guest: 80, host: 80

これによってVagrant上のlocalhost:80とホストPC側のlocalhost:80が繋がります。
vagrantをリロードしてあげると以下のようにForwarding portsの情報が出てくるはず。

$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: You are trying to forward to privileged ports (ports <= 1024). Most
==> default: operating systems restrict this to only privileged process (typically
==> default: processes running as an administrative user). This is a warning in case
==> default: the port forwarding doesn't work. If any problems occur, please try a
==> default: port higher than 1024.
==> default: Forwarding ports...
    default: 80 (guest) => 80 (host) (adapter 1)

PHPの導入

PHP関係のインストール

$ sudo apt install libapache2-mod-php7.0 php7.0 php7.0-cli php7.0-intl php7.0-json php7.0-mbstring php7.0-sqlite3

PHP,Apache2の動作確認

デフォルトのDocumentRootを変えてあげます。

$ sudo vi /etc/apache2/sites-available/000-default.conf
000-default.conf
#DocumentRoot /var/www/html
 DocumentRoot /vagrant/hoge
 <Directory /vagrant/hoge>
 Options Indexes FollowSymLinks
 AllowOverride All
 Require all granted
 </Directory>

そうしたら新しく指定したrootにindex.phpを作成してみましょう。

$ mkdir /vagrant/hoge
$ vi /vagrant/hoge/index.php
index.php
<?php
phpinfo();
?>

以上の設定が終わったらホストマシンのブラウザでlocalhost:80を叩いてあげれば...

スクリーンショット 2019-02-11 22.35.40.png
スクリーンショット 2019-02-11 22.36.13.png

なんとかApacheとPHPがうまく動きました...

DOCUMENT_ROOTが/vagrant/scheduleとなっていますね。これは私がconfファイルのhogescheduleとしていたからです。

CakePHPのインストール

ApacheとPHPのインストールがうまくいったので次にCakePHPをインストールしましょう
今回はscheduleという名前でプロジェクトを作りました。

参照 [ https://zaki0929.github.io/page14.html ]

$ sudo apt install composer
$ composer create-project --prefer-dist cakephp/app schedule
Installing cakephp/app (3.7.1)
    Failed to download cakephp/app from dist: The zip extension and unzip command are both missing, skipping.
The php.ini used by your command-line PHP is: /etc/php/7.0/cli/php.ini
    Now trying to download from source
  - Installing cakephp/app (3.7.1)
    Cloning 87163adbfb3bde222ab60ef9ea982ae2e0f5f266

Created project in schedule
Loading composer repositories with package information
Updating dependencies (including require-dev)
    Failed to download cakephp/plugin-installer from dist: The zip extension and unzip command are both missing, skipping.
The php.ini used by your command-line PHP is: /etc/php/7.0/cli/php.ini
    Now trying to download from source
  - Installing cakephp/plugin-installer (1.1.0)
    Cloning 41373d0678490502f45adc7be88aa22d24ac1843

    Failed to download jdorn/sql-formatter from dist: The zip extension and unzip command are both missing, skipping.
The php.ini used by your command-line PHP is: /etc/php/7.0/cli/php.ini
    Now trying to download from source
.
.
.

うわ〜Warningがたくさん出てくるぅ〜

冷静によくみてみると
The zip extension and unzip command are both missing, skipping.とのこと
zip,unzipコマンドがないとのこと。apt installしましょう。

$ sudo apt install zip unzip

もう一度
composerコマンドを打つと、長いダウンロードが始まりますが、無事にCakePHPが導入されました。

あとはcakephpのインストールガイド通りにパーミッションやApache2のDocumentRootが作成したプロジェクトのwebrootを参照するように設定してあげれば完了です。

参照[ https://book.cakephp.org/3.0/ja/installation.html ]

$cd schedule(⬅︎各自設定したプロジェクトの名前)
$chmod +x bin/cake
$ HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1`
$ setfacl -R -m u:${HTTPDUSER}:rwx tmp
$ setfacl -R -d -m u:${HTTPDUSER}:rwx tmp
$ setfacl -R -m u:${HTTPDUSER}:rwx logs
$ setfacl -R -d -m u:${HTTPDUSER}:rwx logs
$ sudo vi /etc/apache2/sites-available/000-default.conf
000-default.conf
#DocumentRoot /var/www/html
 DocumentRoot /home/vagrant/workspace/schedule/webroot
 <Directory /home/vagrant/workspace/schedule/webroot>
 Options Indexes FollowSymLinks
 AllowOverride All
 Require all granted
 </Directory>

スクリーンショット 2019-02-12 0.03.12.png

DBとの接続をまだ行なっていないので不穏なページですが、これでCakePHPプロジェクト内のindex.phpがホストPCのブラウザと繋がりました。

最後に

Qiita&プログラミング初心者ですので不要な部分、間違えている部分などいくつもあると思います。皆様からのコメント、指摘、大歓迎でございます。

いずれDockerでの開発環境の構築もやりたいなあ。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?