1
1

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.

Laravel6でpublicフォルダをpublic_htmlへ変更する方法

Last updated at Posted at 2020-02-05

こんにちは、ITエンジニアの田中です!
この記事を読むと、Laravel6で、publicフォルダをpublic_htmlにすることができます。

変更する理由

レンタルサーバーによっては、public_htmlが公開フォルダになっており、Laravelで構築したプログラムを配備して公開するためには、publicフォルダではなくpublic_htmlフォルダを公開する必要があるためです。

検証環境

下記の記事を参照して開発環境を自分のローカルマシン上にdockerコンテナとして持っていると仮定します!
https://qiita.com/ucan-lab/items/17c806973e69792ada99

  • PHP7.3
  • Laravel6.0
  • MySQL8.0
  • nginx1.17
    ※ Laravelは最新版使いたかったので、6にしています。

server.php

下記のファイルの最後の行を編集します。
src/server.php

Before

require_once __DIR__.'/public/index.php';

After

require_once __DIR__.'/public_html/index.php';

AppServiceProvider.php

下記のファイルのregister関数を下記のように変更します。
src/app/Providers/AppServiceProvider.php

    public function register()
    {
        //
        $this->app->bind('path.public', function() {
            return base_path().'/public_html';
        });
    }

filesystems.php

下記のファイルのstorage_pathを変更します。

src/config/filesystems.php

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public_html'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

webpack.mix.js

src/webpack.mix.js

Before

mix.js('resources/assets/js/app.js', 'public/js')
  .sass('resources/assets/sass/app.scss', 'public/css');

After

mix.setPublicPath('public_html/');
mix.js('resources/js/app.js', 'js')
mix.sass('resources/sass/app.scss', 'css');

CSSとJSの再作成

下記のコマンドを打つことによりapp.cssとapp.jsをpublic_htmlの下に作り直します。

[mac] $ docker-compose exec node ash
[node] $ npm install
[node] $ npm run dev 

dockerコンテナーの再起動

[mac] docker-compose down
[mac] dpcker-compose up

上記で再起動して、下記のURLにアクセスできれば完了です!
http://localhost:10080/

みなさんのお役に立てたら、是非この記事にいいねをしてください。
今後の記事作成のモチベーションに繋がります!

お願いします。

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?