Skip to content

Instantly share code, notes, and snippets.

@catatsuy
Last active August 24, 2023 03:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catatsuy/7e8a5bf5d5874a02f40832eb2777c549 to your computer and use it in GitHub Desktop.
Save catatsuy/7e8a5bf5d5874a02f40832eb2777c549 to your computer and use it in GitHub Desktop.

AWSにインスタンスを立ててみよう

  • 質問:AWSにEC2インスタンスを立てたことがある人?
  • Ubuntu 22.04のインスタンスを起動してsshでログインしてみよう
    • ubuntuユーザーでログインしてみよう
.ssh/config
Host infra-training
  User ubuntu
  Port 22
  IdentityFile 鍵ファイル
  HostName パブリックIPアドレス
  ServerAliveInterval 5
  ServerAliveCountMax 12

サーバーの状態を見てみよう

  • ps aux を打ってみよう
    • 質問:プロセスは何個ぐらい起動してる?
      • 自分のMacでも打ってみて数えてみよう
  • top を打ってみよう
    • 質問:このサーバーのメモリはどのぐらい?そのうちどのぐらい使われてる?
  • df -h を打ってみよう
    • 質問:このサーバーのディスク容量はどのぐらい?そのうちどのぐらい使われてる?
  • 質問:このサーバーに入ってるテキストエディタは何がある?
    • vimとかemacsとか入れたければaptで入れてみよう
      • どれも得意じゃなければnanoを使おう
    • ~/.bashrc を開いてみよう

PHPでウェブアプリケーションを起動してみよう

  • sudo apt install php してみよう
    • その前にsudo apt updateをしてみよう
  • ps aux を打ってみよう
    • 質問:Apacheのプロセスはいくつ起動してる?
  • less /etc/apache2/sites-enabled/000-default.conf してみよう
    • 質問:どこにphpファイルを置けばブラウザから見られる?
  • <?php var_dump($_SERVER); と書いたindex.phpというファイルを置いてブラウザでアクセスしてみよう
    • 質問:SERVER_SOFTWAREの値は?

PHPをphp-fpmで動かしてみよう

  • Apacheを止めてみよう
    • systemctlを使ってApacheを止めてみよう
systemctl
# apache2の状態を見る
sudo systemctl status apache2

# apache2を再起動
sudo systemctl restart apache2

# apache2の設定を再読み込み
sudo systemctl reload apache2

# apache2を止める
sudo systemctl stop apache2

aptでphp8.1-fpmをインストールしてみよう

  • sudo systemctl status php8.1-fpm と打ってみよう
    • 質問:php-fpmのプロセスはいくつ起動してる?
    • 質問:php-fpm の設定ファイルはどこにある?
  • 設定ファイルを開いてみよう
    • 質問:php-fpmがlistenしているソケットファイルはどこにある?
    • (トリッキーなので誰も手が上がらなければすぐに教える)

nginxを使ってみよう

  • aptでnginxをインストールしてみよう
  • nginxでphp-fpmにリバースプロキシしてみよう
    • 手元のマシンで /etc/hosts を書いてブラウザでアクセスしてみよう
    • 質問:SERVER_SOFTWAREの値は?
nginx
# /etc/nginx/sites-enabled/php.conf
server {
  listen 80;
  server_name php.aws;
  root /var/www/html;

  location / {
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

nginxの設定を反映するときは

sudo nginx -t

してから

sudo systemctl reload nginx

再起動しても良い

sudo systemctl restart nginx

負荷をかけてみよう

  • nginxのアクセスログにレスポンスタイムを出すようにしてみよう
  • htopコマンドを使ってみよう
    • メモリ順に並べてみよう
    • CPU順に並べてみよう
    • ウェブブラウザで負荷をかけてみよう
  • dstatコマンドをインストールしてみよう
    • dstat -tlamp --top-cpu-adv --top-io-adv と打ってみよう
    • ウェブブラウザで負荷をかけてみよう
nginx
log_format ltsv "time:$time_local"
    "\thost:$remote_addr"
    "\tforwardedfor:$http_x_forwarded_for"
    "\treq:$request"
    "\tstatus:$status"
    "\tmethod:$request_method"
    "\turi:$request_uri"
    "\tsize:$body_bytes_sent"
    "\treferer:$http_referer"
    "\tua:$http_user_agent"
    "\treqtime:$request_time"
    "\tcache:$upstream_http_x_cache"
    "\truntime:$upstream_http_x_runtime"
    "\tapptime:$upstream_response_time"
    "\tvhost:$host";

  # 既にあるこの行の一番後ろにltsvと追加
  access_log /var/log/nginx/access.log ltsv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment