大企業営業から未経験ITエンジニアを目指すブログ

アラサー大企業営業から未経験ITエンジニアを目指すブログ。日々学んだことを忘備録的に記します。

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で立ち上がってしまうらしい。

http://hpscript.com/blog/vagrant%E3%81%A7php-artisan-serve%E3%81%A7%E5%8B%95%E3%81%8B%E3%81%AA%E3%81%84%E3%81%A8%E3%81%8D/

 

→問題なく起動はできた。

→でも「このサイトにアクセスできません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リクエストを始めに処理するフロントコントローラとして動作します

https://readouble.com/laravel/6.x/ja/installation.html

 

⑧下記の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

https://error-search.com/error-post/detail/175/Laravel%25E3%2581%25A7%25E3%2582%25A8%25E3%2583%25A9%25E3%2583%25BC%25E3%2580%2580The%2Bstream%2Bor%2Bfile%2B%2522%252Fvar%252Fwww%252Fhtml%252Flaravel_pj%252Fstorage%252Flogs%252Flaravel-2018-04-06.log%2522%2Bcould%2Bnot%2Bbe%2Bopened%253A%2Bfailed%2Bto%2Bopen%2Bstream%253A%2BPermission%2Bdenied

ちなみにフォルダ内のファイルの権限を見るにはこれ。

$ 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が有効になっているかどうか
--.htaccessmod_rewriteの機能を利用している為

https://teratail.com/questions/5920

 参考:https://ja.stackoverflow.com/questions/56942/web%E3%82%B5%E3%83%BC%E3%83%90%E3%83%BCapache2-4%E3%81%A7mod-rewrite-%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB%E3%82%92%E8%AA%AD%E3%81%BF%E8%BE%BC%E3%82%80%E6%96%B9%E6%B3%95

httpd -t -D DUMP_MODULES | grep rewrite

→有効にはなっている。