質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

0回答

865閲覧

EC2にデプロイしているLaravel Sanctumが500エラーになってしまう。

wai_yamato

総合スコア2

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2023/03/04 12:26

編集2023/03/04 13:38

EC2にデプロイしているLaravel Sanctumを実行できるようにしたい

現在Laravel Sanctum、Angular、Nginx,EC2でSPAのアプリケーションをデプロイしようと考えております。
local環境で動作確認できたので、
EC2にLaravelをデプロイしようとしました。
ですが、Laravel APIを実行しようとするとエラーが発生してしまいます。
何かヒントを頂けると幸いです。
よろしくお願いいたします。

  • Laravel Sanctumを実行できるようにしたい。

前提

環境

  • Laravel 9.31.0
  • Angular 14
  • nginx 1.22.1
  • RDS MYSQL 8.0

発生している問題・エラーメッセージ

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

が発生しております。

access.log

1[04/Mar/2023:11:25:32 +0000] "GET /api/healthcheck HTTP/1.1" 500 3708 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" "-" 2[04/Mar/2023:11:25:32 +0000] "GET /nginx-logo.png HTTP/1.1" 404 3665 "http://[IPAddress]/api/healthcheck" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" "-" 3[04/Mar/2023:11:25:32 +0000] "GET /poweredby.png HTTP/1.1" 404 3665 "http://[IPAddress]/api/healthcheck" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" "-" 4[04/Mar/2023:11:25:51 +0000] "GET /api/healthcheck HTTP/1.1" 500 3708 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" "-" 5[04/Mar/2023:11:25:51 +0000] "GET /nginx-logo.png HTTP/1.1" 404 3665 "http://[IPAddress]/api/healthcheck" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" "-" 6[04/Mar/2023:11:25:51 +0000] "GET /poweredby.png HTTP/1.1" 404 3665 "http://[IPAddress]/api/healthcheck" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" "-" 7[ec2-user@

etc/nginx/error.log

1 2023/03/04 11:18:42 [error] 3133#3133: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 45.56.108.128, server: _, request: "GET /systembc/password.php HTTP/1.0", upstream: "

※しかし、Laravelを新しく作成して、URLを実行するとLaravelが表示されていました。
なので、Laravel Sanctum周りの設定ができていないのかと思います。

該当のソースコード

api.php

1<?php 2 3use App\Http\Controllers\API\Auth\FollowUserController; 4use App\Http\Controllers\API\Auth\PasswordResetController; 5use App\Http\Controllers\API\Auth\LoginController; 6use App\Http\Controllers\API\Auth\RegisterController; 7use App\Http\Controllers\API\Auth\UserInfoEditController; 8use App\Http\Controllers\API\Auth\UserPageController; 9use App\Http\Controllers\API\drink\BoardController; 10use Illuminate\Support\Facades\Route; 11 12/* 13|-------------------------------------------------------------------------- 14| API Routes 15|-------------------------------------------------------------------------- 16| 17| Here is where you can register API routes for your application. These 18| routes are loaded by the RouteServiceProvider within a group which 19| is assigned the "api" middleware group. Enjoy building your API! 20| 21*/ 22 23Route::post('/login', [LoginController::class, 'authenticate']); 24 25 26Route::controller(PasswordResetController::class)->group(function () { 27 Route::post('/password/email', 'checkEmail'); 28 Route::post('/password/reset/{token}', 'passwordReset'); 29 Route::post('/password/complete', 'changePasswordComplete'); 30}); 31 32Route::controller(RegisterController::class)->group(function () { 33 Route::post('/user/regist', 'registTmpUser'); 34 Route::post('/user/regist/complete/{token}', 'registUserComplete'); 35}); 36 37Route::post('/user/update/complete/{token}', [UserInfoEditController::class, 'completedEmailCertification']); 38 39Route::middleware(['auth:sanctum'])->group(function () { 40 41 Route::controller(BoardController::class)->group(function () { 42 Route::get('/drink/show/{numOfDisplaiedPosts}', 'show'); 43 Route::post('/drink/add', 'add'); 44 Route::get('/drink/create', 'create'); 45 Route::get('/drink/detail/{postId}', 'detail'); 46 Route::get('/drink/search/{tagId}/{numOfDisplaiedPosts}', 'searchPostsByTag'); 47 }); 48 49 50 Route::controller(FollowUserController::class)->group(function () { 51 Route::put('/user/follow/{followId}', 'followUser'); 52 Route::delete('/user/unfollow/{unfollowId}', 'unfollowUser'); 53 }); 54 55 Route::controller(LoginController::class)->group(function () { 56 Route::post('/logout', 'logout'); 57 Route::post('/user/check', 'checkLogin'); 58 }); 59 60 Route::get('/user/page/{userId}', [UserPageController::class, 'showUserPage']); 61 62 Route::controller(UserInfoEditController::class)->group(function () { 63 Route::get('/user/edit/{userId}', 'userInfoEdit'); 64 Route::post('/user/update', 'updateUserInfo'); 65 }); 66}); 67 68Route::get('/healthcheck', function () { 69 return response()->json(200); 70});

RouteServiceProvider.php

1<?php 2 3namespace App\Providers; 4 5use Illuminate\Cache\RateLimiting\Limit; 6use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; 7use Illuminate\Http\Request; 8use Illuminate\Support\Facades\RateLimiter; 9use Illuminate\Support\Facades\Route; 10 11class RouteServiceProvider extends ServiceProvider 12{ 13 /** 14 * The path to the "home" route for your application. 15 * 16 * Typically, users are redirected here after authentication. 17 * 18 * @var string 19 */ 20 public const HOME = '/home'; 21 22 /** 23 * Define your route model bindings, pattern filters, and other route configuration. 24 * 25 * @return void 26 */ 27 public function boot() 28 { 29 $this->configureRateLimiting(); 30 31 $this->routes(function () { 32 Route::middleware('api') 33 ->prefix('api') 34 ->group(base_path('routes/api.php')); 35 Route::middleware('web') 36 ->prefix('web') 37 ->group(base_path('routes/web.php')); 38 }); 39 } 40 41 /** 42 * Configure the rate limiters for the application. 43 * 44 * @return void 45 */ 46 protected function configureRateLimiting() 47 { 48 RateLimiter::for('api', function (Request $request) { 49 return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip()); 50 }); 51 } 52}

nginx.conf

1# For more information on configuration, see: 2# * Official English Documentation: http://nginx.org/en/docs/ 3# * Official Russian Documentation: http://nginx.org/ru/docs/ 4 5user nginx; 6worker_processes auto; 7error_log /var/log/nginx/error.log; 8pid /run/nginx.pid; 9 10# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. 11include /usr/share/nginx/modules/*.conf; 12 13events { 14 worker_connections 1024; 15} 16 17http { 18 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 19 '$status $body_bytes_sent "$http_referer" ' 20 '"$http_user_agent" "$http_x_forwarded_for"'; 21 22 access_log /var/log/nginx/access.log main; 23 24 sendfile on; 25 tcp_nopush on; 26 tcp_nodelay on; 27 keepalive_timeout 65; 28 types_hash_max_size 4096; 29 30 include /etc/nginx/mime.types; 31 default_type application/octet-stream; 32 33 # Load modular configuration files from the /etc/nginx/conf.d directory. 34 # See http://nginx.org/en/docs/ngx_core_module.html#include 35 # for more information. 36 include /etc/nginx/conf.d/*.conf; 37 38 server { 39 listen 80; 40 listen [::]:80; 41 server_name _; 42 root /var/www/src/public; 43 charset utf-8; 44 45 # Load configuration files for the default server block. 46 include /etc/nginx/default.d/*.conf; 47 48 location / { 49 index index.php; 50 try_files $uri $uri/ /index.php?$query_string; 51 } 52 53 54 location /index.html { 55 root /usr/share/nginx/html; 56 index index.html; 57 } 58 59 location /phpinfo.php { 60 index phpinfo.php; 61 } 62 63 location /test.html { 64 index test.html; 65 } 66 67 location ~ \.php$ { 68 fastcgi_pass unix:/etc/run/php-fpm/www.sock; 69 # root /var/www/src/public; 70 fastcgi_split_path_info ^(.+\.php)(/.+)$; 71 fastcgi_index index.php; 72 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 73 include fastcgi_params; 74 } 75 76 error_page 404 /404.html; 77 location = /404.html { 78 root /usr/share/nginx/html; 79 } 80 81 error_page 500 502 503 504 /50x.html; 82 location = /50x.html { 83 root /usr/share/nginx/html; 84 } 85 86 location ~ /\.(?!well-known).* { 87 deny all; 88 } 89 } 90 91} 92

Kernel.php

1<?php 2 3namespace App\Http; 4 5use Illuminate\Foundation\Http\Kernel as HttpKernel; 6 7class Kernel extends HttpKernel 8{ 9 'api' => [ 10 \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, 11 'throttle:api', 12 \Illuminate\Routing\Middleware\SubstituteBindings::class, 13 \Illuminate\Session\Middleware\StartSession::class 14 ], 15 ]; 16}

試したこと

  • Laravel プロジェクトを新しく作成し、画面を表示

→ 成功

  • Laravelのpublicフォルダにphpinfo.phpを配置しnginx.confでlocationを表示できるか確認

→ 成功

  • local環境でAngular側からLaravel API実行

→ 成功

最後に

足りていない情報などがありましたら、コメントしていただければ、早めに添付させていただきたいと思います。
また、言葉として足りない箇所やわかりにくい箇所がありましたら、コメントいただければ、修正したいと思います。
何か、ヒントや参考になりますURLなどがありましたら、教えていただけれると幸いです。
よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yuma.inaura

2023/03/04 13:32

400じゃなくて500じゃないでしょうか?
wai_yamato

2023/03/04 13:38

ご指摘いただき、ありがとうございます。 修正いたしました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問