laravel5:chdir():没有这样的文件或目录(errno 2)

问题描述 投票:0回答:13

我在将 Laravel 5 上的网站部署到 VPS 服务器时遇到问题,但在本地计算机上工作正常。

我的页面是 http://easyway.vn/ 当前页面显示空白并出现错误

无法加载资源:服务器响应状态为 500(内部服务器错误)

当我跑步时

php工匠服务--host=0.0.0.0

我有一个错误

[错误异常]
chdir(): 没有这样的文件或目录 (errno 2)



服务器信息

操作系统:Linux 2.6
服务器版本:Apache/2.2.29 (Unix)
PHP 5.4.41 (cli)(构建时间:2015 年 6 月 4 日 13:27:02) 版权所有 (c) 1997-2014 PHP 集团 Zend Engine v2.4.0,版权所有 (c) 1998-2014 Zend Technologies

请帮助我!

laravel-5 vps
13个回答
47
投票

这意味着你的 Laravel 应用程序找不到 public 文件夹。

我让它工作了

改变:

chdir($this->laravel->publicPath());

在:

vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php

致:

chdir('/');

16
投票

重命名公用文件夹名称会导致此问题。

触摸供应商目录下的文件是你可能永远不会做的事情。

这是我在我的活动项目中测试和使用的一种可行的替代方法。

创建 app/Console/Commands/Serve.php 文件并将内容设置为:

<?php

namespace App\Console\Commands;

use Exception;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\ProcessUtils;

class Serve extends Command {
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'serve';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Serve the application on the PHP development server';

    /**
     * Execute the console command.
     *
     * @return void
     *
     * @throws \Exception
     */
    public function fire() {
        chdir('/');

        $host = $this->input->getOption('host');

        $port = $this->input->getOption('port');

        $base = ProcessUtils::escapeArgument($this->laravel->basePath());

        $binary = ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false));

        $this->info("Laravel development server started on http://{$host}:{$port}/");

        if (defined('HHVM_VERSION')) {
            if (version_compare(HHVM_VERSION, '3.8.0') >= 0) {
                passthru("{$binary} -m server -v Server.Type=proxygen -v Server.SourceRoot={$base}/ -v Server.IP={$host} -v Server.Port={$port} -v Server.DefaultDocument=server.php -v Server.ErrorDocument404=server.php");
            } else {
                throw new Exception("HHVM's built-in server requires HHVM >= 3.8.0.");
            }
        } else {
            passthru("{$binary} -S {$host}:{$port} {$base}/server.php");
        }
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions() {
        return [
            ['host', null, InputOption::VALUE_OPTIONAL, 'The host address to serve the application on.', 'localhost'],

            ['port', null, InputOption::VALUE_OPTIONAL, 'The port to serve the application on.', 8000],
        ];
    }
}

使用以下内容更新 app/Console/Kernel.php 文件:

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel {
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        // Commands\Inspire::class,
        Commands\Serve::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule) {
        // $schedule->command('inspire')
        //  ->hourly();
    }
}

仅此而已!

现在运行您的服务命令:

$ php artisan serve

服务器启动:

Laravel development server started on http://localhost:8000/

9
投票

我知道你很久以前就问过这个问题,但对于那些寻求最简单有效的方法来解决这个问题的人来说:

这个错误意味着laravel找不到public文件夹。你必须在Providers/AppServiceProvider.phpregister方法中添加这段代码:

public function register()
{
    $this->app->bind('path.public', function() {
       return base_path('PATH TO PUBLIC FOLDER');
    });
}

public_html 示例

在您的情况下,公共 HTML 的路径将是:

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

这样 Laravel 就知道去哪里寻找公共文件夹路径。


5
投票

改变

chdir(public_path());

chdir('/');

来自:vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php


5
投票

只需在根目录中创建一个名为

public
的文件夹。如果您从 GitHub 下载项目或现有的旧项目。

如果您有兴趣,请查看更多文章

  1. 如何逐步使用 Laravel 创建动态滑块:- https://codewithronny.com/how-to-create-dynamic-slider-using-laravel-step-by-step/

  2. 对共享主机 cPanel 上的 Laravel HTTP 错误 500 进行故障排除:- https://codewithronny.com/troubleshooting-laravel-http-error-500-on-shared-hosting-cpanel/


4
投票
  1. 第一步 在vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php中
`chdir($this->laravel->publicPath());`
change to 

chdir('/');

2.第二步更改your_public_folder/index.php中的这两个路径

require __DIR__.'/../your_app/bootstrap/autoload.php';

$app = require_once __DIR__.'/../your_app/bootstrap/app.php';

3.第三步更改your_app/server.php中的公共路径

    if ($uri !== '/' && file_exists(__DIR__.'/../public_html/'.$uri)) {
    return false;
}

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

注意:根据您的应用程序的新文件夹结构更改这些路径。


3
投票

当您重命名

public
文件夹时,会出现此错误。

永远不要编辑供应商文件夹中的文件。 composer 更新全新安装后,您将丢失所做的更改。

更好的解决方案是编辑 bootstrap/app.php 并将此代码片段放在 return 语句之前。

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

1
投票

转到根文件夹中的 server.php 并将任何

public
重命名为
any name you have


1
投票

通常,当出现此类错误时,意味着 Laravel 正在寻找公共文件夹,但找不到。这取决于您如何配置应用程序,并且在这种情况下使用 php artisanserve

--host=0.0.0.0
将不起作用。处理这个问题的最佳方法是将您的应用程序放置在您的服务器(htdocs)中并在您的网址中访问它。

供应商目录包含您的 Composer 依赖项。请访问 Laravel 文档 了解详细信息。切勿在供应商中编辑任何内容,因为当您更新作曲家时可能会导致问题。


0
投票

请记住,当您在生产环境中运行时,将 public 路径更改为 public_html,以便在本地主机中再次运行,我将其更改为 app/Providers/AppServiceProvider.php

这个:

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

对此:

public function register()
    {
       // commented this line
    }

也在更改 .env 环境以及 server.php 和 index.php 后


0
投票

确定“public”文件夹是否存在


0
投票

您可能已经覆盖了

public_path
方法,可能在
AppServiceProvider
index.php
中。


0
投票

切勿编辑供应商文件夹中的文件。一个简单的解决方案就是在根目录中创建一个名为“public”的文件夹,并将可用项目的“索引”复制到该文件夹中。

索引

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <[email protected]>
 */

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| 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';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

© www.soinside.com 2019 - 2024. All rights reserved.