7
8

More than 5 years have passed since last update.

EC2でnginx+php+Laravelの環境を作ってみる。

Last updated at Posted at 2019-09-03

EC2でPHP・Laravelを動かしてみる

作成したサーバーにアクセスする。

# sshで接続
ssh -i ~/.ssh/admin-key.pem ec2-user@{IPv4 パブリックIP}

# -i 接続に使用する認証キーを指定する場合のオプション
# ~/.ssh/admin-key.pemはEC2インスタンス時に指定したキーペアの場所を指定
# {IPv4 パブリックIP} EC2インスタンスを作成した後に割り振られたIP(インスタンス>選択>下の「説明」の中に記載している。

接続できなかった場合は、EC2インスタンス時に選択(または、新規作成)したセキュリティグループでsshを追加しているか確認する。

接続に成功すると下記のような画面になる。

1.png

必要なものをインストールする。

# とりあえず、yumを最新にする
sudo yum update

nginxをインストール

nginx

# nginxインストール
[ec2-user@ip-10-0-10-10 ~]$ sudo yum install nginx

「Is this ok [y/d/N]」と確認されるので y を入力する。

これでインストールが完了した。
問題なくインストールされているか確認してみる。

# nginxのバージョンを確認
[ec2-user@ip-10-0-10-10 ~]$ nginx -v
nginx version: nginx/1.14.1

これで問題なくインストールはできている。

PHPのインストール

php7.3系をインストールする。

# php関連のインストール
[ec2-user@ip-10-0-10-10 ~]$ sudo yum install php73 php73-fpm php-mbstring

composerをインストール

# composer install
[ec2-user@ip-10-0-10-10 ~]# sudo curl -sS https://getcomposer.org/installer | php

# パスが通る場所に移動
[ec2-user@ip-10-0-10-10 ~]# sudo mv composer.phar /usr/bin/composer

# インストールの確認
[ec2-user@ip-10-0-10-10 ~]$ composer -v
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/

gitをインストールする。

使わないのであればインストールする必要はない。

今回は、gitにサンプルコードをpushし、サーバー上にpullするつもりなのでインストール。

# git install
[ec2-user@ip-10-0-10-10 ~]$ sudo yum install git

# インストールの確認
[ec2-user@ip-10-0-10-10 ~]$ git --version
git version 2.14.5

これで一旦インストールは終了

インストールしたものの設定をしていく

gitの設定〜git cloneまで

githubからソースを持ってこれるように、ssh接続の設定をしていく。

# .sshフォルダに移動
[ec2-user@ip-10-0-10-10 ~]$ cd .ssh

# 公開キーの作成
[ec2-user@ip-10-0-10-10 .ssh]$ ssh-keygen 

# 下記を実行し表示された公開キーをコピーする
[ec2-user@ip-10-0-10-10 ~]$ cat id_rsa.pub

自分のgitHubのアカウントにログインし、SettingsよりSSH and GPG keysに移動し、New SSH Keysをクリック。

keyの欄に先ほどコピーしたものを貼り付け。

# ssh configを作成
[ec2-user@ip-10-0-10-10 ~]$ sudo vi ~/.ssh/config

# 下記を貼り付け
Host github
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa

# sshで接続できるか確認
[ec2-user@ip-10-0-10-10 test]$ ssh -T github
Hi {userName}! You've successfully authenticated, but GitHub does not provide shell access.

# git clone
[ec2-user@ip-10-0-10-10 ~]$ git clone git@github.com:{user/repository}

git cloneができない。
fatal: could not create work tree dir 'repository': 許可がありません
→ ディレクトリ作成の許可がない。
→ chown ec2-user {directory} で権限を与える。
→ これでできるはず。

もし、sshできない場合は、ssh-addしてみる。

# おそらく、下記のエラーが出るはず、、、
[ec2-user@ip-10-0-10-10 home]$ ssh-add ~/.ssh/id_rsa
Could not open a connection to your authentication agent.

# 先にssh-agentを実行しておく。
[ec2-user@ip-10-0-10-10 home]$ eval "ssh-agent"
[ec2-user@ip-10-0-10-10 home]$ ssh-add ~/.ssh/id_rsa

composer install

cloneしたディレクトリでcomposerをinstallする。

[ec2-user@ip-10-0-10-10 test]$ composer install
・
・
・
Package manifest generated successfully.

これで完了。

nginxの設定

php-fpm conf

# fpmの設定を変更する
[ec2-user@ip-10-0-10-10 /]$ sudo vi /etc/php-fpm.d/www.conf

変更箇所は下記の部分

# apacheになっているのでnginxに変更
user = nginx
group = nginx

listen = /var/run/php-fpm/php-fpm.sock
listen.user = nginx
listen.group = nginx

nginx conf

nginx.confを実行したい環境に合わせて変更する。

# nginxの設定を変更する
[ec2-user@ip-10-0-10-10 /]$ sudo vi /etc/nginx/nginx.conf

ファイルの下の方で、下記の部分がコメントアウトされているので、コメントアウトを解除

#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}

一部今回の設定に合わせて変更する。

php-fpmのlistenに指定したものと統一する。

fastcgi_pass   127.0.0.1:9000;
            ↓
fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;

それぞれを稼働させる

# php-fpm
[ec2-user@ip-10-0-10-10 /]$ sudo service php-fpm start

# nginx
[ec2-user@ip-10-0-10-10 /]$ sudo service nginx start

# 一度実行している場合はstart → restartを使う。

laravelのデフォルトの画面を表示する。

グローバルIPにアクセスしてみる。

アクセスしたら、おそらく500 internal server errorになるはず。

なので、laravelの権限などの問題なので、設定を変更していく。

storageに書き込み権限を設定する。

# storageに権限を設定
# ディレクトリに移動し、storageに権限を与える
[ec2-user@ip-10-0-10-10 sample.com]$ sudo chown -R nginx:nginx storage
[ec2-user@ip-10-0-10-10 test]$ sudo chmod -R 775 storage

.envファイルを作成する。

# .env.exampleというファイルが存在するのでコピー
[ec2-user@ip-10-0-10-10 test]$ sudo cp .env.example .env

# keyを発行
[ec2-user@ip-10-0-10-10 test]$ sudo php artisan key:generate
Application key set successfully.

[ec2-user@ip-10-0-10-10 test]$ php artisan config:clear
Configuration cache cleared!

最後に

下記の画面が表示されれば完了
2.png

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8