【PHP】Laravel ドキュメントルートの設定
Laravelでは、「public」ディレクトリが公開領域となっており、ドキュメントルートにpublicディレクトリを設定するように推奨されています。
実際には、他のアプリケーションがあるなどドキュメントルートを合わせることが出来ない場合が多くpublicディレクトリをドキュメントルートに合わせて移動することになります。その際にした2つの内容をまとめています。
動作環境
- Ubuntu 18.04.2 LTS (Bionic Beaver)
 - Laravel 5.8.3
 
ディレクトリ構成
ディレクトリの構成は、以下の構成を例として記載しています
「html」ディレクトリがドキュメントルート、「sampleapp」ディレクトリがLaravelのプロジェクトディレクトリになります
var
└─ www
     |- html (ドキュメントルート)
     └─ sampleapp (Laravelプロジェクトディレクトリ)
         |- app
         |- bootstrap
         |- config
         |- database
         |- public
         |- resources
         |- routes
         |- storage
         |- tests
         └─ vender
シンボリックリンクを貼る
1つ目の方法は、publicディレクトリにシンボリックリンクを設定します
Laravelのプロジェクトディレクトリの「sampleapp」内にある「public」ディレクトリをリンク元とし、ドキュメントルート(htmlディレクトリ)以下の「laravel_public」をリンクとして設定します
リンク作成後のディレクトリ構造は以下の様な構成です
www
 |- html
 |   └─ laravel_public (リンク名)
 └─ sampleapp
        |- app
        |- bootstrap
        |- config
        |- database
        |- public (リンク元)
        |- resources
        |- routes
        |- storage
        |- tests
        └─ vender
シンボリックリンクの作成は、lnコマンドにオプション「s」を付けて実行します
ln -s [リンク元(実体)] [リンク名]
上記の条件に合わせて「リンク元(実体)」と「リンク名」を記述します
[root@hostname /]#
ln -s /var/www/sampleapp/public /var/www/html/laravel_public
リンクが設定されているかどうか確認します
[root@hostname /]#
ls -la /var/www/c /var/www/html
lrwxrwxrwx 1 root root  25 May  6 15:36 laravel_public -> /var/www/sampleapp/public
LaravelのWelcomeページが表示されれば完了です

publicディレクトリを移動する
2つ目の方法は、publicディレクトリをドキュメントルート(htmlディレクトリ)直下に移動します
移動後のディレクトリ構成は以下になります
www
 |- html
 |   └─ laravel_public (publicをリネーム)
 └─ sampleapp
        |- app
        |- bootstrap
        |- config
        |- database
        |- resources
        |- routes
        |- storage
        |- tests
        └─ vender
mvコマンドを使って、「laravel_public」に名前を変えてドキュメントルート以下に移動します
[root@hostname /]#
mv /var/www/sampleapp/public /var/www/html/laravel_public
移動したディレクトリ内にあるindex.phpを変更します
変更前のindex.php
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/
require __DIR__.'/../vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../bootstrap/app.php';
変更後のindex.php
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/
//require __DIR__.'/../vendor/autoload.php';
require __DIR__.'/../../sampleapp/vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
//$app = require_once __DIR__.'/../bootstrap/app.php';
$app = require_once __DIR__.'/../../sampleapp/bootstrap/app.php';
autoload.phpとapp.phpのパスの記述を移動したディレクトリに合わせて変更しています
path.publicの変更
変更後、artisanコマンドでサーバーを起動させようとすると以下のエラーが発生します
[root@hostname /]#
php artisan serve
   ErrorException  : chdir(): No such file or directory (errno 2)
  at /var/www/sampleapp/vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php:42
    38|      * @throws \Exception
    39|      */
    40|     public function handle()
    41|     {
  > 42|         chdir(public_path());
    43|
    44|         $this->line("Laravel development server started: <http://{$this->host()}:{$this->port()}>");
    45|
    46|         passthru($this->serverCommand(), $status);
  Exception trace:
  1   chdir("/var/www/sampleapp/public")
      /var/www/sampleapp/vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php:42
  2   Illuminate\Foundation\Console\ServeCommand::handle()
      /var/www/sampleapp/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32
  Please use the argument -v to see more details.
Laravelで設定しているパスと実際のパスが異なっているために起こるエラーです
Laravelが設定しているpublicディレクトリのパスは「vendor/laravel/framework/src/Illuminate/Foundation/Application.php」の「publicPath」メソッドに以下のように記載されています
/**
 * Get the path to the public / web directory.
 *
 * @return string
 */
 public function publicPath()
 {
     return $this->basePath.DIRECTORY_SEPARATOR.'public';
 }
「app/Providers/AppServiceProvider.php」の「register」メソッドに次の記述を追加してパスの設定を変更します
 /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
            //
            $this->app->bind('path.public', function() {
                    return realpath(base_path() . '/../html/laravel_public');
            });
    }
server.phpの変更
エミュレート用に用意されている「server.php」に記載されているパスを変更します
変更前のserver.php
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}
require_once __DIR__.'/public/index.php';
変更後のserver.php
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
/*
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}
*/
if ($uri !== '/' && file_exists(__DIR__.'/../html/laravel_public'.$uri)) {
    return false;
}
//require_once __DIR__.'/public/index.php';
require_once __DIR__.'/../html/laravel_public/index.php';
コマンドが動作するか確認します
[root@hostname /]#
php artisan serve
Laravel development server started: <http://127.0.0.1:8000>
上記に記載されているURLでLaravelのWelcomeページが表示されれば完了です

Laravelに同梱されているwebpackを使用する場合
「webpack.mix.js」に記載されているパスを変更します
変更前のwebpack.mix.js
mix.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css');
変更後のwebpack.mix.js
mix.setPublicPath('../html/laravel_public/');
mix.js('resources/js/app.js', 'js')
mix.sass('resources/sass/app.scss', 'css');



ディスカッション
コメント一覧
まだ、コメントがありません