初めてubuntuのaws環境でapache2設定中、時間使いすぎてしまったところについて
初めてubuntuのaws環境でapache2設定中、時間使いすぎてしまったところについて
目的:一人遊びのためapi作って、aws上に久々にあげようとしていた。
特にPJのURL変更したくてapache2設定操作中・・出会ったウザいオプションについて記録。
まず、Projectのdefaultをサーバに入れた後
apache2のDirectory設定をこのコードの下に書こうとしてたのに
ブラウザ上で、URLの認識されてなかった
# /etc/apache2/apache2.conf <Directory /> Options FollowSymLinks AllowOverride none Require all granted </Directory> # この下に新しいDirectory書いたのに・・・できると思った(..)
でも、、基本画面がでてくるはずなのにでてこなかったし、めっちゃくっちゃ時間使わせてしまった。
今日の午前中の5時間も飛んじゃんって悲しぃ・・T_T
原因は、
<Directory /> Options FollowSymLinks # ここが noneになっていたのが原因だった・・こいつ・・ AllowOverride All Require all granted </Directory>
でも、今までのプロジェっくとは私が
なぜだろう。。?
会社はサーバチームが絶対100%設定したはずだろうと思いつつ
今度からApache2を使うことにしたので(初めて)
もしかしたら、Apacheのバージョンごとに異なるのかなぁと思って調べてみようとしたのに出かけることになったので 調査は次回に(・・・) ただ、急ぎでlocalのapache2は確かにnoneになっていた。ApacheはAllになっている様子。(私が変えたのかな・・覚えてないのでpass)
Apache2の公式文書を検索してみると
流石に出ていた・・
AllowOverrideのオプションのやつ・・
https://httpd.apache.org/docs/2.4/ja/mod/core.html#allowoverride
# # Deny access to the entirety of your server's filesystem. You must # explicitly permit access to web content directories in other # <Directory> blocks below. # <Directory /> AllowOverride none Require all denied </Directory>
- 個人の感想
確かにnoneなのに
私が今まで問題なくいけたので、いけると思っていた。
なんか、新しいApacheなのに、文書もよく読まずにww
めんどくさく適当に書いたのでこういうふうになったと思った。
小さなオプションも見逃さないように・・w
PHPで文字列の演算子で計算したい時の変換方法!
**string to operatorは「安全」にswitchを使いましょ! eval()などの方法もありそうですが、
こっちが安全らしいです!**
<?php // Your code here! $en = '>='; $left = (int)3; $right = (int)7; $result = 0; switch($en){ case ">=": $result = $left >= $right; break; case "+"; $result = $left + $right; break; // etc... } if ($result) { echo 'true'; } else { echo 'false'; } ?>
LaravelでAuth::user()がredirectしたら消えちゃう問題・・T_T
環境:
Laravel8
php8.1
LaravelをAPIだけでしか触った経験しかなくて・・
一人遊びでWebもしてみよーかぁとしたら
ログイン画面から変な感じが・・・www恥ずかしいけどぉ
if (Auth::attempt(['email' => $request->email, 'password' => $request->password], true)) {
とにかく、これでログインしたのに
ログイン後、redirectしたら
Auth::user();
がnullだった・・TT
それで
ネットで調べたみたら
.envのAPP_URL設定を間違えてできなかったようだった・・・
🙄
// 間違ってたやつ APP_URL=localhost:8888/testProject SESSION_DOMAIN=localhost:8888/testProject //これが定番 APP_URL=localhost SESSION_DOMAIN=localhost
なんでやねん・・?
とにかく、localhostに変えたらいけた。
stackoverflowでは
In short, I needed to remove the http://
protocol from SESSION_DOMAIN
in .env
and set it to match APP_URL
だと書いていた
理由は
When you change the SESSION_DOMAIN env variable, Laravel will issue new cookies but old ones are still in the browsers memory. When the request comes from browser to Laravel app the old cookie has priority and there for you are not able to login.
The solution to this is delete old cookies from the browser (those registered without any domain so to actual host of app).
This error typically bites those who wants to add multi subdomain persistent login. And what is even worse, when you have your application in production already, all your clients will suffer from this issue as well. Nobody will able to login (not only that, but mainly) to your application until they completely clear out their cookies.
I solved this by adding middleware, which does it for me:
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Cookie; class ForgotOldSessionCookies { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * * @return mixed */ public function handle($request, Closure $next) { Cookie::queue(new \Symfony\Component\HttpFoundation\Cookie('laravel_session', null, now()->subYear(1)->timestamp, '/')); Cookie::queue(new \Symfony\Component\HttpFoundation\Cookie('XSRF-TOKEN', null, now()->subYear(1)->timestamp, '/')); return $next($request); } }
Be aware, when you use only this:
Cookie::queue(Cookie::make('laravel_session', null, -6000, '/', 'yourdomain.com'))
or
Cookie::queue(Cookie::forget('laravel_session', '/', 'yourdomain.com'))
Laravel will send Cookie header with domain so browser will create the cookie with domain .yourdomain.com
- that's why nothing will be deleted.
vscode利用し、Xdebug インストールする方法
環境
1.Mac m1 pro
2.MAMP利用
3.PHP 8.0.23
順番
1.
xdebugインストール
pecl install xdebug
- このコマンドで無事にインストールされてるのか確認
➜ ~ php -v PHP 8.0.23 (cli) (built: Sep 3 2022 12:20:56) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.23, Copyright (c) Zend Technologies with Xdebug v3.1.5, Copyright (c) 2002-2022, by Derick Rethans with Zend OPcache v8.0.23, Copyright (c), by Zend Technologies
私の場合には、MAMPを利用し、Laravelのデバッグをする予定なので
まず、こっちに追加しました。
vi /Applications/MAMP/bin/php/php8.0.8/conf/php.ini
一番上に追加しました。
zend_extensionのパスはpeclコマンドでインストールする時
パスが出るのでそこのパスをコピペーして書きます。
#pecl インストール中にパスが書いているところw extension_dir => /opt/homebrew/lib/php/pecl/20200930 => /opt/homebrew/lib/php/pecl/20200930
#php.ini zend_extension="/opt/homebrew/Cellar/php@8.0/8.0.23_1/pecl/20200930/xdebug.so" xdebug.mode=debug xdebug.client_host=localhost xdebug.client_port=9003 //(port9003になっているのに注意!) xdebug.start_with_request=yes [PHP] ;;;;;;;;;;;;;;;;;;; ; About php.ini ; ;;;;;;;;;;;;;;;;;;;
詳細設定は launch.jsonに書きます。php.iniに書いといたportを合わせます。
#launch.json { "configurations": [ { "name": "Listen for XDebug", "type": "php", "request": "launch", "port": 9003 }, { "name": "Launch currently open script", "type": "php", "request": "launch", "program": "${file}", "cwd": "${fileDirname}", "port": 9003 } ] }
6.debugを実行しますw
→launch.jsonで設定したport番号に入るのではなく
普通のapacheのport番号で接続することです!
赤いポイントをつけたら
該当時点にアクセスすると
デバッグが実行されます。
・苦労したところ
→
①port設定を9003にしたので
9003にずっと接続したが、
普通のapacheのアクセスでやることでした
②最初には
ローカルのphp.iniだけに追加したので
当たり前だが、Laravelが利用するphpと違うphpだったため
できなかったです・・
本当は簡単な設定やインストールっぽいのようですが
まあ、PHPに関していろいろ勉強になりました。
mountコマンド
mount
→new media diskをvitual diskの中におくこと。
ex)CD, MOUSE, KEYBOARD
Linuxのfile systemとは
msのosとは多くの違いがある。
windows
複数のdirectory 構造に保存することができる。
物理disk driveに名前を与えられる。 (c drive c:// , d drive d://)
各driveに各自の構造の持てる。。
→c:// d://のように名前を見て、どの物理disk partitionにあるのかわかることができる。
Linux (single directory structure)
→ただ1つのdirectory 構造しか存在してない。
つまり、drive文字を使わず。(c drive c:// , d drive d://なし)1つのみ。
実は物理的には複数のdiskで分けられているが、virtual directoryのため、
1つのdirectory 構造で存在できる。
virtual directoryは実際に存在してないので、複数のdiskを1つのdirectoryにまとめられる。
command: mount
mountコマンドは現在、mountされた装置を全部リストで見せてくれる。
route : /etc/mtab このルートからmountされた情報を取ってくるらしい。
見せてくれる情報:)
1)mediaのfile name
→usbのnameとか
2)mount point
→EX) USB PORTを指すと、OSが認識できるようなpointer。
3)file system type
-> アクセスできているのか等の知らせ
最近のOSは自動的に装置をmountしてくれるが
手動ですることもあるので。
手動でmountするやり方を知っておくのもいいと思う。。
この作業(mount)は必ず「root」権限が必須で。
そのため、sudo と一緒に使う。
command : sudo mount (option)
sudo mount のoption
1) -t
-> type
ex) (file system typeでosで必ず必要なシステム。)
OS最初インストールする時file systemを設定する。
2)-d
-> device
実際のmedia装置に位置している。
->Linuxはhard driveに名付ける時の標準がある。
hard driveのタイプ(IDE昔/SATA最新!)によって変わるらしい。
ex1) 昔 /dev/hdx (hda, hdb, hdc ・・・でいく)
これはもう使わない。
ex2) 現在 /dev/sdx (sda, sdb, sdc・・・)
3)自分のLinuxのmount point
ex) mount -t vfat /dev/sdb1 /media/disk
vfat:file system type
/dev/sdb1 : 実際のmedia装置に位置しているところ
ー>sdb1:sdの後にくるalphabetがdiskの番号, その後の数字がpartition番号
/media/disk : usbがあるところ
4) -o
色々なオプションを追加できる。
ex) ro : read only
rw : read write
unmount
:mountの解除
コマンド: umount
mountする時与えた4番目のパラメータ(mount point)を渡すと
mountの解除ができる。
teeコマンドを利用して、画面とファイルに同時出力しましょう
TEEコマンド : stdinをもらい、stdoutと1つ以上のファイルに出力する
オプション
[ -a ]: 上書きではなく、ファイルに追加して入力
[ -i ]: interrupt無視
[ File ... ]: ファイル名
例
$ echo test | tee-test.txt
test
$ cat tee-test.txt
test
Linux-topコマンドでサーバー負荷確認ができる
Linuxの「top」コマンドで
サーバー負荷確認ができます!!
現在、運営中のサービスがあったら
コメント
top
でサーバー負荷の色々なステータスを見られます。
例えば、topコマンドを実行してからの状態で
画面上部の色々な文が書いているところがあります。
ex) ①load average: 0.05(now), 0.03(3m), 0.05(5mか?)
このような感じで、3つの基準で分かれていて時間ごとの サーバー負荷の平均値を見せています。
次はcpuのパーセントです。
ex) ②%Cpu(s): 0.3/0.3
サーバーの状況によるのはもちろんのことですが、 cpuの場合は最大10未満になったらいいかも?と思います。
ex)③df -h
diskでどれぐらいの容量を占めているのか?を
オプション -h: human readable
人間が読めるように表示する
これでも確認可能です。
危険値の判断基準(…)は実は決まっておらず、
サーバーの状況や条件によるので
自分のサーバーの判断基準を決めておいて
確認するのがいいと思います!