さんごー日記。

映画や本やゲームの感想をゆるく記録したり、プログラミングの勉強をゆるく記録するゆるい日記です。

laravelの起動設定とエラー

Laravelを起動する上での設定と、遭遇したエラーと解消方法をメモ。

httpd.confの修正

DocumentRootやmod_rewriteの有効化を行います。
httpd.confに直接書いても良いですが、Laravel用の設定として別のファイルに書いておくのがベターでしょう。

`/etc/httpd/conf.d/`の下にconfファイルを置けば自動的に追加で読み込んでくれます。
Laravelのプロジェクトを`/var/www/html`以下に`sample`という名前で作ったとします。

`/etc/httpd/conf.d/vhost.conf`

<VirtualHost *:80>
        DocumentRoot /var/www/html/sample/public
        ServerName ***
        ServerAlias ***
        RewriteEngine On
        <Directory "/var/www/html/sample/public">
                AllowOverride All
        </Directory>
</VirtualHost>

これでhttpdを再起動すればとりあえずOKなはず。

エラーログで`Fatal error: require(): Failed opening required '***/vendor/autoload.php' `

以下のような感じのエラーがでる場合は、Lavaleのアプリケーションのディレクトリ以下で「composer update」します。

[php7:error] [pid 30783] [client 126.202.52.69:56931] PHP Fatal error:  require(): Failed opening required '/var/www/html/sample/public/../vendor/autoload.php' (include_path='.:/usr/share/pear7:/usr/share/php7') in /var/www/html/sample/public/index.php on line 24

composer updateが上手行かない可能性があるので、エラーメッセージに従って必要なパッケージを追加でインストールしましょう。
私の場合はmbstringとかが無いというエラーになっていました。以下解決方法。

composer install(またはupdate)でエラー - さんごー日記。

エラーログで`failed to open stream: Permission denied in …`

パーミッションエラーがでる。

[php7:error] [pid 31622] [client 126.202.52.69:58464] PHP Fatal error:  Uncaught ErrorException: file_put_contents(/var/www/html/sample/storage/framework/views/d941f1b905cf3a512ba2f0a58bfc7ab7ca0af8b5.php): failed to open stream: Permission denied in /var/www/html/sample/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:122\nStack trace:\n#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'file_put_conten...', '/var/www/html/s...', 122, Array)\n#1 /var/www/html/sample/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php(122): file_put_contents('/var/www/html/s...', '<?php $__env->s...', 0)\n#2 /var/www/html/sample/vendor/laravel/framework/src/Illuminate/View/Compilers/BladeCompiler.php(122): Illuminate\\Filesystem\\Filesystem->put('/var/www/html/s...', '<?php $__env->s...')\n#3 /var/www/html/sample/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(51): Illuminate\\View\\Compilers\\BladeCompiler->compile('/var/www/html/s...')\n#4 /var/www/html/sample/vendor/laravel/framework/src/Illuminate/View/View.php( in /var/www/html/sample/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php on line 122

`/var/www/html/sample/storage/framework/views/`の下にファイルが書き込めない状態になっているらしい。
Laravelはここに静的なファイルを出力するようになっているのかな。
viewのパーミッションは755(rwxr-xr-x)となってい他ので、とりあえず777(rwxrwxrwx)にしておいた。

$ sudo chmod 777 /var/www/html/sample/storage/framework/views/

`Whoops, looks like something went wrong.`と表示される。

動いたーと思ったら期待と違うエラーメッセージが表示されてしまった。
「何かが間違っているように見えます。」だそうです。「何か」が分からないぐらい例外的な状態なのだろうか。
レスポンスコードは500になっている。
エラーログには何も出ていない。

`.env`ファイルが無いとこうなるらしい。
プロジェクトのディレクトリにある`.env.examp`をコピーして`.env`を作ることで解消する。

$ cp .env.example .env

`Whoops! There was an error.` - UnexpectedValueException

画面いっぱいにエラーが表示される。
またパーミッションエラーっぽい。

f:id:thirtyfive:20180607130318p:plain

UnexpectedValueException
The stream or file "/var/www/html/sample/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied

ここも単純にパーミッションを変える。

$ sudo chmod 777 /var/www/html/sample/storage/logs

`Whoops! There was an error.` - RuntimeException

また画面いっぱいのエラー。
エラー箇所のソースコードまで出してくれるのは親切ですね。
f:id:thirtyfive:20180607232050p:plain

RuntimeException
No application encryption key has been specified.

なんかkeyが無いらしい。
`php artisan key:generate`をしないとダメらしい。

$ php artisan key:generate
Application key [base64:****] set successfully.

`Whoops! There was an error.` - ErrorException (E_WARNING) … failed to open stream: Permission denied

またパーミッションエラー。

f:id:thirtyfive:20180607233802p:plain

今度はセッションが書けないらしい。storage以下は全部パーミッション変えたほうが良いのかな。

ErrorException (E_WARNING)
file_put_contents(/var/www/html/sample/storage/framework/sessions/EYYizlcsD0GvrhsyS6bCEjzyAlisgRwGN5tvvzSN): failed to open stream: Permission denied
$ sudo chmod 777 /var/www/html/sample/storage/framework/sessions

やっとできたわ。
f:id:thirtyfive:20180607234044p:plain


Laravel関連書籍など。

PHPフレームワーク Laravel入門

PHPフレームワーク Laravel入門

Laravel 5.5 サンプルプログラム

Laravel 5.5 サンプルプログラム

Laravel リファレンス[Ver.5.1 LTS 対応] Web職人好みの新世代PHPフレームワーク

Laravel リファレンス[Ver.5.1 LTS 対応] Web職人好みの新世代PHPフレームワーク