在 linux 上启动新的 php-laravel 项目时服务器 500

Posted

技术标签:

【中文标题】在 linux 上启动新的 php-laravel 项目时服务器 500【英文标题】:Server 500 while starting up a new php-laravel project on linux 【发布时间】:2021-11-20 18:21:27 【问题描述】:

我的 php 经验为零,但需要启动一个我克隆的现有应用程序(它应该是功能性的),以便快速测试它。我想我已经很接近让它工作了。

如果我运行php artisan serve:

[franck@franck-vee appliweb]$ php artisan serve
Starting Laravel development server: http://127.0.0.1:8000
[Tue Sep 28 13:31:56 2021] PHP 8.0.10 Development Server (http://127.0.0.1:8000) started
[Tue Sep 28 13:32:00 2021] 127.0.0.1:34716 Accepted
[Tue Sep 28 13:32:00 2021] 127.0.0.1:34716 Closing
[Tue Sep 28 13:32:00 2021] 127.0.0.1:34722 Accepted
[Tue Sep 28 13:32:00 2021] 127.0.0.1:34722 [200]: GET /favicon.ico
[Tue Sep 28 13:32:00 2021] 127.0.0.1:34722 Closing

但是,如果我访问 localhost:8000,我会得到 ,这似乎是 Laravel 500 错误页面。所以我正在服务,这很好,但应用程序并没有真正运行。

我已经完成的安装步骤:

将源 coe 和 cd 的 git 存储库克隆到项目根目录 安装 php、composer、npm(用于 javascript)并验证它们在我的系统上工作(都在我的系统上返回有效版本) 从项目根目录运行 npm install、composer install 以安装 composer.lock、package.lock 的内容,它们存在于项目根目录 php artisan serve 来自项目根目录

收到 500 服务器错误后,如果查看日志(在 storage/logs/laravel.logs 中),我会发现以下堆栈跟踪:

[2021-09-28 17:40:49] production.ERROR: No application encryption key has been specified. "exception":"[object] (Illuminate\\Encryption\\MissingAppKeyException(code: 0): No application encryption key has been specified. at /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php:79)
[stacktrace]
#0 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Support/helpers.php(263): Illuminate\\Encryption\\EncryptionServiceProvider->Illuminate\\Encryption\\closure()
#1 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php(81): tap()
#2 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php(60): Illuminate\\Encryption\\EncryptionServiceProvider->key()
#3 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php(32): Illuminate\\Encryption\\EncryptionServiceProvider->parseKey()
#4 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(829): Illuminate\\Encryption\\EncryptionServiceProvider->Illuminate\\Encryption\\closure()
#5 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(714): Illuminate\\Container\\Container->build()
#6 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(841): Illuminate\\Container\\Container->resolve()
#7 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(652): Illuminate\\Foundation\\Application->resolve()
#8 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(826): Illuminate\\Container\\Container->make()
#9 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(987): Illuminate\\Foundation\\Application->make()
#10 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(907): Illuminate\\Container\\Container->resolveClass()
#11 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(868): Illuminate\\Container\\Container->resolveDependencies()
#12 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(714): Illuminate\\Container\\Container->build()
#13 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(841): Illuminate\\Container\\Container->resolve()
#14 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(652): Illuminate\\Foundation\\Application->resolve()
#15 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(826): Illuminate\\Container\\Container->make()
#16 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(156): Illuminate\\Foundation\\Application->make()
#17 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\closure()
#18 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Routing/Router.php(695): Illuminate\\Pipeline\\Pipeline->then()
#19 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Routing/Router.php(670): Illuminate\\Routing\\Router->runRouteWithinStack()
#20 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Routing/Router.php(636): Illuminate\\Routing\\Router->runRoute()
#21 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Routing/Router.php(625): Illuminate\\Routing\\Router->dispatchToRoute()
#22 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): Illuminate\\Routing\\Router->dispatch()
#23 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\closure()
#24 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\closure()
#25 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#26 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\closure()
#27 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#28 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\closure()
#29 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle()
#30 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\closure()
#31 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle()
#32 /home/franck/Documents/PRO/foobar/appliweb/vendor/fruitcake/laravel-cors/src/HandleCors.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\closure()
#33 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\\Cors\\HandleCors->handle()
#34 /home/franck/Documents/PRO/foobar/appliweb/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\closure()
#35 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\\Proxy\\TrustProxies->handle()
#36 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\closure()
#37 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\\Pipeline\\Pipeline->then()
#38 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#39 /home/franck/Documents/PRO/foobar/appliweb/public/index.php(52): Illuminate\\Foundation\\Http\\Kernel->handle()
#40 /home/franck/Documents/PRO/foobar/appliweb/server.php(21): require_once('...')
#41 main
" 
[2021-09-28 17:40:49] production.ERROR: No application encryption key has been specified. "exception":"[object] (Illuminate\\Encryption\\MissingAppKeyException(code: 0): No application encryption key has been specified. at /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php:79)
[stacktrace]
#0 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Support/helpers.php(263): Illuminate\\Encryption\\EncryptionServiceProvider->Illuminate\\Encryption\\closure()
#1 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php(81): tap()
#2 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php(60): Illuminate\\Encryption\\EncryptionServiceProvider->key()
#3 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php(32): Illuminate\\Encryption\\EncryptionServiceProvider->parseKey()
#4 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(829): Illuminate\\Encryption\\EncryptionServiceProvider->Illuminate\\Encryption\\closure()
#5 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(714): Illuminate\\Container\\Container->build()
#6 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(841): Illuminate\\Container\\Container->resolve()
#7 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(652): Illuminate\\Foundation\\Application->resolve()
#8 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(826): Illuminate\\Container\\Container->make()
#9 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(987): Illuminate\\Foundation\\Application->make()
#10 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(907): Illuminate\\Container\\Container->resolveClass()
#11 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(868): Illuminate\\Container\\Container->resolveDependencies()
#12 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(714): Illuminate\\Container\\Container->build()
#13 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(841): Illuminate\\Container\\Container->resolve()
#14 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(652): Illuminate\\Foundation\\Application->resolve()
#15 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(826): Illuminate\\Container\\Container->make()
#16 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(205): Illuminate\\Foundation\\Application->make()
#17 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(179): Illuminate\\Foundation\\Http\\Kernel->terminateMiddleware()
#18 /home/franck/Documents/PRO/foobar/appliweb/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->terminate()
#19 /home/franck/Documents/PRO/foobar/appliweb/server.php(21): require_once('...')
#20 main
" 
[2021-09-28 17:42:09] production.ERROR: file_get_contents(/home/franck/Documents/PRO/foobar/appliweb/.env): Failed to open stream: No such file or directory "exception":"[object] (ErrorException(code: 0): file_get_contents(/home/franck/Documents/PRO/foobar/appliweb/.env): Failed to open stream: No such file or directory at /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Console/KeyGenerateCommand.php:96)
[stacktrace]
#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Console/KeyGenerateCommand.php(96): file_get_contents()
#2 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Console/KeyGenerateCommand.php(80): Illuminate\\Foundation\\Console\\KeyGenerateCommand->writeNewEnvironmentFileWith()
#3 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Console/KeyGenerateCommand.php(45): Illuminate\\Foundation\\Console\\KeyGenerateCommand->setKeyInEnvironmentFile()
#4 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\\Foundation\\Console\\KeyGenerateCommand->handle()
#5 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\closure()
#6 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure()
#7 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod()
#8 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Container/Container.php(611): Illuminate\\Container\\BoundMethod::call()
#9 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Illuminate\\Container\\Container->call()
#10 /home/franck/Documents/PRO/foobar/appliweb/vendor/symfony/console/Command/Command.php(256): Illuminate\\Console\\Command->execute()
#11 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\\Component\\Console\\Command\\Command->run()
#12 /home/franck/Documents/PRO/foobar/appliweb/vendor/symfony/console/Application.php(971): Illuminate\\Console\\Command->run()
#13 /home/franck/Documents/PRO/foobar/appliweb/vendor/symfony/console/Application.php(290): Symfony\\Component\\Console\\Application->doRunCommand()
#14 /home/franck/Documents/PRO/foobar/appliweb/vendor/symfony/console/Application.php(166): Symfony\\Component\\Console\\Application->doRun()
#15 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Console/Application.php(92): Symfony\\Component\\Console\\Application->run()
#16 /home/franck/Documents/PRO/foobar/appliweb/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\\Console\\Application->run()
#17 /home/franck/Documents/PRO/foobar/appliweb/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle()
#18 main
" 

我发现一些参考资料指出php artisan key:generate 应该为我生成这样的密钥并解决错误。但是,如果我运行最后一个命令,则会出现另一个错误:

[franck@franck-vee appli-assurance]$ php artisan key:generate

   ErrorException 

  file_get_contents(/home/franck/Documents/PRO/kbrs/appli-assurance/.env): Failed to open stream: No such file or directory

  at vendor/laravel/framework/src/Illuminate/Foundation/Console/KeyGenerateCommand.php:96
     92▕     
     93▕         file_put_contents($this->laravel->environmentFilePath(), preg_replace(
     94▕             $this->keyReplacementPattern(),
     95▕             'APP_KEY='.$key,
  ➜  96▕             file_get_contents($this->laravel->environmentFilePath())
     97▕         ));
     98▕     
     99▕ 
    100▕     /**

      +16 vendor frames 
  17  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

我开始认为我错过了上游的一些关键。我应该在项目中有某种 .env 文件吗?没有来自 git repo 的数据。可能是某些环境文件/变量不存在吗?或者我是否需要运行其他工匠/作曲家命令才能考虑启动它?

【问题讨论】:

Laravel 是一个框架,它允许您以无限的方式组装您的应用程序并导致 500 错误。不可能说你的应用程序出了什么问题。设置断点并通过代码进行调试。 除非真正阅读问题,否则这是不可能的。我提供了该错误的日志。 Ugur 说得对。 【参考方案1】:

您的根目录中似乎没有.env 文件。您需要创建一个.env 文件,该文件可以从.env.example 复制。

之后您可以运行php artisan key:generate,它应该可以正常工作。

【讨论】:

确实如此。我的意思是我知道 .env 文件通常不会被提交,但它仍然会在某种程度上期待最后的注释(如果应用程序运行需要它)。非常感谢 @logicOnAbstractions “启动一个我克隆的现有应用程序(它应该是功能性的)” - 听起来你克隆这个的人并没有让你有效运行 Laravel 应用程序的说明 ? 这来自文档:“在全新的 Laravel 安装中,应用程序的根目录将包含一个 .env.example 文件,该文件定义了许多常见的环境变量。在 Laravel 安装过程中,此文件将自动复制到 .env。";您的项目错过了这一点。很高兴你让它正常工作! 哈。我实际上是作为一个 zip 文件收到的(不是来自原始开发人员)。所以是的,从技术上讲,我在为它创建了一个 repo 之后克隆了它:| - 与来自世界各地的朋友一起租用编码的乐趣!

以上是关于在 linux 上启动新的 php-laravel 项目时服务器 500的主要内容,如果未能解决你的问题,请参考以下文章

php-laravel4.0框架 简单快速入门

微软启动新的开源项目 将Linux工具eBPF引入Windows

如何在Linux上创建,列出和删除Docker容器

在 Apache 上添加新的虚拟主机而不重新启动服务器?

在 Ubuntu 上创建新的 Rails 项目返回错误

在linux下添加一块新硬盘` 如何对新的硬盘做磁盘配额`