cakephp3.6のwebrootをApacheのドキュメントルートに移動する

この記事は最新更新日から、6年以上経過しています。

概要

いろいろあってcakephp3をインストールしたフォルダ以外で動かすことになりました。

2018-08-08 パーミッションエラー時の対応を追加

作業環境

Windows8.1
Vagrant 2.0.2
VirtualBox 5.2.6
cakephp3.6
centos6.9
apache2.4

手順

/cakephp3/project
というディレクトリにcakephp3をインストールしました。

サーバーのドキュメントルートは下記です
/var/www/html

「/cakephp3/project/webroot」の中身を「/var/www/html」移動します

移動した「/var/www/html/index.php」の中身を下記のように書き換えます
具体的には$cakephp_rootを追加してdirname(__DIR__)を置き換えていきます。

19行目を書きかえ

$cakephp_root = '/cakephp3/project';
// require dirname(__DIR__) . '/config/requirements.php';
require $cakephp_root . '/config/requirements.php';

31行目を書きかえ

// require dirname(__DIR__) . '/vendor/autoload.php';
require $cakephp_root . '/vendor/autoload.php';

37行目を書きかえ

//$server = new Server(new Application(dirname(__DIR__) . '/config'));
$server = new Server(new Application($cakephp_root . '/config'));

404になる

普通これだけで大丈夫らしいんですが、なぜか自作したログインページに移動すると404になりました、なんでだ…。

そういえばapacheを2.2から2.4にバージョンアップして設定してなかった気がするのでmod_rewriteのせいかな…。
なのでapacheの設定ファイル探してhttpd.confを編集します。

# find / -name 'httpd.conf'
/opt/rh/httpd24/root/etc/httpd/conf/httpd.conf
# vi /opt/rh/httpd24/root/etc/httpd/conf/httpd.conf

AllowOverride None

AllowOverride All
に書き換えます、公式によるとDirectory とwebrootの場所だけでいいらしいです。
となると該当箇所は下記のような感じになるはず。(apache2.4の場合)

<Directory />
    AllowOverride All
    Require all denied
</Directory>

<Directory /var/www>
    AllowOverride All
    # Allow open access:
    Require all granted
</Directory>

書き換えたので、サーバーを再起動します

# service httpd24-httpd restart

パーミッションエラー事件

これでアクセスしてファイルのパーミッションエラーが出るようならパーミッションを変更しましょう

# sudo chown -R apache:apache /var/www/html/
# sudo chown -R apache:apache /cakephp3/project

ログイン時にセッション効かない事件

一応ログインページが表示されるようになったのですが、ログイン時すると下記エラーが出ました。
「Could not start the session」

ちなみにウェブルートにおいたindex.phpの2行目にsession_start();を追加しようものなら下記エラーに変化します。なんでだ。
「Session was already started」

エラーをよく見たら「Could not start the session」時に/tmp/sess_hogehoge のセッションファイルがPermission deniedになってエラーが起きているようです。…なんでだ。
よくわからないけどそのうちわかるようになります。インストール時にrootとか使ってたからそのせいかな…。

該当のセッションファイルのオーナーとグループをapacheにしたら無事ログインできるようになりました。

# sudo chown -R apache:apache /tmp/sess_hogehoge

長かった…。