LoginSignup
2
2

More than 5 years have passed since last update.

EC2でphp環境構築 ② gitサーバー構築 + altax導入 + デプロイコマンド作成

Last updated at Posted at 2015-09-28

前編
EC2でphp環境構築 ① Nginx + php56 + php56-fpm + virtualhost

目次

  • gitリモートサーバ構築
  • deployユーザーの作成
  • デプロイコマンドを作成
  • 次にやること
# EC2 IP Adress(仮)
52.111.111.111

gitリモートサーバ構築

1. EC2へログイン

[hoge@local ~]$ ssh -i "XXX.pem" ec2-user@52.111.111.111

2. 【EC2】gituserの作成

[ec2-user@ip-172-31-4-XXX ~]$ sudo useradd -m -s /bin/bash gituser
[ec2-user@ip-172-31-4-XXX ~]$ sudo mkdir /home/gituser/.ssh
[ec2-user@ip-172-31-4-XXX ~]$ sudo touch /home/gituser/.ssh/authorized_keys
[ec2-user@ip-172-31-4-XXX ~]$ sudo chown gituser. /home/gituser/.ssh
[ec2-user@ip-172-31-4-XXX ~]$ sudo chown gituser. /home/gituser/.ssh/authorized_keys
[ec2-user@ip-172-31-4-XXX ~]$ sudo chmod 700 /home/gituser/.ssh
[ec2-user@ip-172-31-4-XXX ~]$ sudo chmod 600 /home/gituser/.ssh/authorized_keys

3. 【ローカル】rsaキーをコピー

[hoge@local ~]$ pbcopy < ~/.ssh/id_rsa.pub

4. 【EC2】gituserのauthorized_keysにペースト

[hoge@local ~]$ ssh -i "XXX.pem" ec2-user@52.111.111.111
[ec2-user@ip-172-31-4-XXX ~]$ sudo su - gituser
[gituser@ip-172-31-4-XXX ~]$ vi ~/.ssh/authorized_keys

5. 【ローカル】ssh接続確認

[hoge@local ~]$ ssh gituser@52.111.111.111 -i ~/.ssh/id_rsa

6. 【ローカル】.ssh/configに設定

[hoge@local ~]$ touch ~/.ssh/config
[hoge@local ~]$ vi ~/.ssh/config
~/.ssh/config
Host gitserver
  User         gituser
  hostname     52.111.111.111
  identityfile ~/.ssh/id_rsa

7. 【ローカル】ssh接続確認

[hoge@local ~]$ ssh gitserver

8. 【EC2】gitインストール

[hoge@local ~]$ ssh -i "XXX.pem" ec2-user@52.111.111.111
[ec2-user@ip-172-31-4-XXX ~]$ sudo yum install git -y
[ec2-user@ip-172-31-4-XXX ~]$ git --version
git version 2.1.0

9. 【EC2】リポジトリディレクトリ作成

[ec2-user@ip-172-31-4-XXX ~]$ sudo su - gituser
[gituser@ip-172-31-4-XXX ~]$ mkdir -p ~/repos
[gituser@ip-172-31-4-XXX ~]$ cd ~/repos
[gituser@ip-172-31-4-XXX repos]$ mkdir -p global.git
[gituser@ip-172-31-4-XXX repos]$ cd global.git
[gituser@ip-172-31-4-XXX global.git]$ git --bare init

10. 【ローカル】リモートリポジトリclone

[hoge@local ~]$ mkdir -p ~/projects
[hoge@local ~]$ cd ~/projects
[hoge@local projects]$ git clone  gitserver:/home/gituser/repos/global.git

11. 【ローカル】リポジトリでファイル作成しコミットしpush

[hoge@local projects]$ cd global
[hoge@local global]$ echo "this is my first file." > README.md
[hoge@local global]$ git add .
[hoge@local global]$ git commit -m "first commit"
[hoge@local global]$ git push origin master

12. 【ローカル】別のディレクトリにpullしてみる

「README.md」が存在していればOK
[hoge@local ~]$ cd ~/projects
[hoge@local projects]$ git clone  gitserver:/home/gituser/repos/global.git global2
[hoge@local projects]$ cd global2
[hoge@local global2]$ cat README.md
this is my first file.

deployユーザーの作成

1. EC2へログイン

[hoge@local ~]$ ssh -i "XXX.pem" ec2-user@52.111.111.111

2. 【EC2】deployユーザーの作成

wheelグループはsudo可能なので、wheelグループに追加する
[ec2-user@ip-172-31-4-XXX ~]$ sudo useradd -m -s /bin/bash deploy
[ec2-user@ip-172-31-4-XXX ~]$ sudo usermod -G wheel deploy

3. 【EC2】deployユーザーにsudo権限を付与する

wheelグループのユーザがpasswdなしでsudoのコマンドが実行できるようにしておく
[ec2-user@ip-172-31-4-XXX ~]$ sudo su -
[root@ip-172-31-4-XXX ~]$ visudo

%wheel ALL=(ALL) NOPASSWD: ALL → コメントアウト除去
%wheel ALL=(ALL) ALL → コメントアウト

/etc/sudoers
# %wheel        ALL=(ALL)       ALL
%wheel  ALL=(ALL)       NOPASSWD: ALL

4. 【EC2】デプロイ先の「/vaw/www」ディレクトリのオーナーをdeployユーザーにする

「/vaw/www」配下のディレクトリを削除しておく
[root@ip-172-31-4-XXX ~]$ chown -Rf deploy. /var/www
[root@ip-172-31-4-XXX ~]$ rm -rf /var/www/*

5. 【EC2】ローカルからdeployユーザーへのsshアクセス権限付与

ローカルの公開鍵をリモートサーバの/home/deploy/.ssh/authorized_keysに登録しておく。
# ローカルの公開鍵をコピー
[hoge@local ~]$ cat ~/.ssh/id_rsa.pub | pbcopy
# EC2へログインしローカルの公開鍵を登録
[hoge@local ~]$ ssh -i "XXX.pem" ec2-user@52.111.111.111
[ec2-user@ip-172-31-4-XXX ~]$ sudo su - deploy
[deploy@ip-172-31-4-XXX ~]$ mkdir .ssh
[deploy@ip-172-31-4-XXX ~]$ chmod 700 .ssh
[deploy@ip-172-31-4-XXX .ssh]$ cd .ssh
[deploy@ip-172-31-4-XXX .ssh]$ touch authorized_keys
[deploy@ip-172-31-4-XXX .ssh]$ chmod 600 authorized_keys
[deploy@ip-172-31-4-XXX .ssh]$ vi authorized_keys
# authorized_keysに貼り付けて保存

6. 【EC2】ローカルからdeployユーザーへのアクセス確認

[hoge@local ~]$ ssh deploy@52.111.111.111
[deploy@ip-172-31-4-XXX ~]$ 

7. 【EC2】deployユーザーからgituserユーザーへのアクセス権限付与

deployユーザーの公開鍵の作成しコピー
[hoge@local ~]$ ssh deploy@52.111.111.111
[deploy@ip-172-31-4-XXX ~]$ ssh-keygen -t rsa
[deploy@ip-172-31-4-XXX ~]$ cat ~/.ssh/id_rsa.pub
deployユーザーからgituserへのsshアクセス許容
[hoge@local ~]$ ssh gituser@52.111.111.111
[gituser@ip-172-31-4-XXX ~]$ vi ~/.ssh/authorized_keys
# deployユーザーの公開鍵を貼り付けて保存

8. 【EC2】deployユーザーからgituserユーザーへのアクセス確認

[hoge@local ~]$ ssh deploy@52.111.111.111
[deploy@ip-172-31-4-XXX ~]$ ssh gituser@52.111.111.111
[gituser@ip-172-31-4-XXX ~]$ 

9. 【EC2】deployユーザーの~/.ssh/configにgituserのHostを登録する

[hoge@local ~]$ ssh deploy@52.111.111.111
[deploy@ip-172-31-4-XXX ~]$ touch ~/.ssh/config
[deploy@ip-172-31-4-XXX ~]$ chmod 600 ~/.ssh/config
[deploy@ip-172-31-4-XXX ~]$ vi ~/.ssh/config
~/.ssh/config
Host gitserver
  User         gituser
  hostname     52.111.111.111
  identityfile ~/.ssh/id_rsa

10. 【EC2】deployユーザーからgitserverへのアクセス確認

[hoge@local ~]$ ssh deploy@52.111.111.111
[deploy@ip-172-31-4-XXX ~]$ ssh gitserver
[gituser@ip-172-31-4-XXX ~]$ 

デプロイコマンドを作成

1. 【ローカル】phpデプロイツール(altax)をインストールする

altaxチュートリアル及びファレンス
http://kohkimakimoto.github.io/altax/ja/

[hoge@local ~]$ curl -L https://raw.github.com/kohkimakimoto/altax/master/installer.sh | bash -s system

2. 【ローカル】altaxを初期化

[hoge@local ~]$ cd ~/projects/global
[hoge@local global]$ altax init
Created file: /Users/hoge/projects/global/.altax/config.php
Created file: /Users/hoge/projects/global/.altax/composer.json
Created file: /Users/hoge/projects/global/.altax/.gitignore

3. 【ローカル】deployコマンド作成

altax deploy デプロイする環境名 適用したいブランチ名

~/projects/global/.altax/config.php
Server::node("deployserver", array(
    "host" => "52.111.111.111",
    "username" => "deploy",
    "port" => 22,
    "key" => "~/.ssh/id_rsa"
));
Server::role("web", array("deployserver"));
Task::register("deploy", function($task) {

  $envirName =  $task->getArgument(0, null);
  $branchName =  $task->getArgument(1, null);
  if (is_null($envirName) || is_null($branchName)) {
    exit();
  }

  // アプリケーションディレクトリ
  $appDir = "/var/www/$envirName";

  // Execute parallel processes for each nodes.
  $task->exec(function($process) use ($appDir, $branchName) {

    // Run a command remotely and get a return code.
    if ($process->run("test -d $appDir")->isFailed()) {
      $process->run("git clone gitserver:~/repos/global.git $appDir");
    } else {
      $process->run(array(
        "cd $appDir",
        "git fetch -q",
        "git co $branchName",
        "git pull origin $branchName",
      ));
    }

  }, array("web"));

});

4. 【ローカル】デプロイ実行してみる

初回
[hoge@local ~]$ cd ~/projects/global
[hoge@local global]$ echo "feature1 deployed" > index.php
[hoge@local global]$ altax deploy feature1 develop
[deployserver:555] Run: test -d /var/www/feature1
[deployserver:555] Run: git clone gitserver:~/repos/global.git /var/www/feature1
Cloning into '/var/www/feature1'...
2回目以降
[hoge@local global]$ altax deploy feature1 master
[deployserver:559] Run: test -d /var/www/feature1
[deployserver:559] Run: cd /var/www/feature1 && git fetch -q && git pull origin master
From gitserver:~/repos/global
 * branch            master     -> FETCH_HEAD
Already up-to-date.

5. 【ローカル】デプロイしたドメインに入って更新されたか確認

http://feature1-develop.com/

FireShot Capture 2 -  - http___feature1-develop.com_.png

これでサーバー構築からデプロイ方法まである程度分かるようになった。

次にやること

今回はリポジトリディレクトリを直接ドキュメントルートにしたが、
下記のように構成を変更しておく。

  • 各環境のリポジトリをrepoディレクトリに移行
  • releaseディレクトリを二つ用意(release/A,release/B)
  • deploy → repo更新 → release切り替え → 該当releaseにrsync → currentディレクトリのシンボリックリンク変更
  • deployロックファイル作成(重複実行防止)
2
2
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
2
2