Windows10にVagrantとVirtualBoxでCentOSの仮想環境を構築⑦Laravelページをブラウザから閲覧する
■Laravelページを表示する
①sudo systemctl start httpdして192.168.33.10をブラウザから見ても何も表示されない。
②今度はLaravelフォルダに移動してphp artisan serveしてみると、読み込めないファイルがあると。
→そういえばgit.ignoreの対象ファイルは無いままだった!
③git.ignore対象ファイルをrsyncフォルダに追加。
④もう一回、今度はLaravelフォルダに移動してphp artisan serve --host=192.168.33.10 --port=8000を試みる。
※オプションをつけずにやると、hostが127.0.0.1で立ち上がってしまうらしい。
→問題なく起動はできた。
→でも「このサイトにアクセスできません192.168.33.10 で接続が拒否されました。」になってしまう。
⑤apacheを起動してからphp artisan serveする
→ブラウザからアクセスすると、Apacheサーバの画面にはなるけどページは表示されない。
⑥http://192.168.33.10:8000でもう一度アクセス
→CSSが適用されていないページは表示できた!
多分secure_assetにしてるからCSSが適用されない?
⑦https://192.168.33.10:8000でもう一度アクセス
→「このサイトにアクセスできません192.168.33.10 により途中で接続が切断されました。」みたいにつながらなくなった。
コマンドラインは下記を繰り返している。
Closing
Invalid request (Unsupported SSL request)
一回だけ Invalid request (Connection reset by peer)が入った。
←php artisan serveはPHPの組込み開発サーバをアプリケーションサーバとして使いたい場合に使用するらしい。
↓
書いてあった!!!
Laravelをインストールできたら、Webサーバのドキュメント/Webルートがpublicディレクトリになるように設定してください。このディレクトリのindex.phpは、アプリケーションへ送信された、全HTTPリクエストを始めに処理するフロントコントローラとして動作します
⑧下記の2ページを参考に、下記にしてみた。
https://www.rem-system.com/centos-httpd-inst/#6_httpd
https://qiita.com/tosite0345/items/b9ec1594f359f03b0403
※本当はcp -p httpd.conf httpd.conf.orgをして変更前ファイルをバックアップとして保存すべきだった!(やらなかった)
/etc/httpd/conf
DocumentRoot "/var/www/html/public"
# Relax access to content within /var/www.
<Directory "/var/www/html">
AllowOverride All
# Allow open access:
Require all granted
</Directory>
# Further relax access to the default document root:
<Directory "/var/www/html/public">
AllowOverride All
↓
表示できた!!!^^
⑨やっぱりCSS、JSが読み込めてないので、ビューの読み込み部分のsecure_asset関数をasset関数に変更。
↓
ブラウザでアクセスすると今度はこんなエラー。
"The stream or file "/vagrant/html/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied"
⑩いろいろやってみて、エラーが止まらなくて、抜本的に変えてみる過程↓
(1)$ sudo chown apache:apache /var/www/html/storage/logs/
https://qiita.com/isao_e_dev/items/a78ddef30cdf6fea712a
ちなみにフォルダ内のファイルの権限を見るにはこれ。
$ ls -latr /vagrant/html/storage/l
↓
今度はこれ。
"The /vagrant/html/bootstrap/cache directory must be present and writable."
↓
(2)sudo chown apache:apache /vagrant/html/bootstrap/cache/
↓
今度はこれ。止まらん・・・
ErrorException
file_put_contents(/vagrant/html/storage/framework/views/e36941e2921b67f0bd695392295a9e39e6a8f8cb.php): failed to open stream: Permission denied
↓
(3)このまま対処療法的にやっても新しいことするたびにエラーが出そうなので、全体初期設定的な情報を探し、↓に行きつく。全部この通りに変更。
https://qiita.com/Todate/items/d0d5c38f62621711a79b
(直そうとして直してみたら、もうapacheユーザで新しいファイルができていた。下記が結果。
ls -latr /vagrant/html/storage/logs/
drwxr-xr-x 5 vagrant vagrant ..
-rw-r--r-- 1 vagrant vagrant .gitignore
drwxr-xr-x 2 vagrant vagrant .
-rw-r--r-- 1 apache apache laravel.log
ls -latr /vagrant/html/bootstrap/cache
drwxr-xr-x 3 vagrant vagrant ..
-rw-r--r-- 1 vagrant vagrant .gitignore
-rwxr-xr-x 1 apache apache packages.php
-rwxr-xr-x 1 apache apache services.php
)
[vagrant@localhost ~]$ sudo groupadd laravel
[vagrant@localhost ~]$ sudo cat /etc/group | grep laravel
laravel:x:1001:
[vagrant@localhost ~]$ sudo gpasswd -a vagrant laravel
Adding user vagrant to group laravel
[vagrant@localhost ~]$ sudo gpasswd -a apache laravel
Adding user apache to group laravel
[vagrant@localhost html]$ sudo find ./ -type d -exec chmod 755 {} \;
[vagrant@localhost html]$ sudo find ./ -type f -exec chmod 644 {} \;
[vagrant@localhost html]$ sudo chown -R :laravel ./storage
[vagrant@localhost html]$ sudo chown -R :laravel ./bootstrap/cache
[vagrant@localhost html]$ sudo find ./storage -type d -exec chmod 775 {} \;
[vagrant@localhost html]$ sudo find ./storage -type f -exec chmod 664 {} \;
[vagrant@localhost html]$ sudo find ./bootstrap/cache -type d -exec chmod 775 {} \;
[vagrant@localhost html]$ sudo find ./bootstrap/cache -type f -exec chmod 664 {} \;
[vagrant@localhost html]$ sudo find ./storage -type d -exec chmod g+s {} \;
[vagrant@localhost html]$ sudo find ./bootstrap/cache -type d -exec chmod g+s {} \;
[vagrant@localhost html]$ sudo setfacl -R -d -m g::rwx ./storage
[vagrant@localhost html]$ sudo setfacl -R -d -m g::rwx ./bootstrap/cache
↓
閲覧成功!^^
※血迷った過程でやったこと。
下記を参考にmod_rewriteが有効になっているかを確認
-Apacheモジュールのmod_rewriteが有効になっているかどうか
--.htaccessでmod_rewriteの機能を利用している為
httpd -t -D DUMP_MODULES | grep rewrite
→有効にはなっている。