LoginSignup
17
13

More than 5 years have passed since last update.

Ansibleでnginx+php構築

Posted at

Ansibleでnginx+phpを入れてみました

環境

・Ansibleサーバ
・WEBサーバ ←今回これをAnsibleでつくる

どちらもEC2で作成しました。
・バージョンはAmazon Linux AMI release 2016.03

Ansibleのインストール

yumでインストールすると実行時にエラーが発生したのでpipでインストールしました

$ sudo pip install ansible 
$ ansible --version
ansible 2.1.0.0

接続設定

接続先を定義します。

/etc/ansible/hosts

 [webservers]
  server01

[]はグループ名ですので、クライアントが複数ある場合はホスト名を追加します。

sshのconfig設定を行います。

/root/.ssh/config

Host server01
  HostName xxx.xxx.xxx.xxx
  Port 22
  User ec2-user
  IdentityFile ~/.ssh/ansible-key

IdentityFileには接続先の秘密鍵を格納しておきます。

接続確認

pingを通してみます。
※EC2の場合はセキュリティグループでICMPを許可にする必要があります。

$ ansible all -m ping
xxx.xxx.xxx.xxx | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

PlayBookの作成

サーバの構成を記述したplaybookなるものを作ります。
まずは、ベースのyamlを作成します。

$ vi hoge.yml

全ホストに対してec2-userでsudoモードで実行させるようにします。

hoge.yml
- name: install and configure the web
  hosts: all
  remote_user: ec2-user
  become: yes

  roles:
    - nginx

次に、構成ファイルを格納するディレクトリを作成します。

$ mkdir roles
$ cd roles

Roleの作成

続いて、roleの作成
roleとはAnsibleが読み込むモジュールの単位です。構成単位はWEBとしてnginxとphpなどのモジュールを一つとして管理するやり方や、nginxとphpでサービスを分けて管理するやり方などあるようですが、今回はサービス毎に分けて行います。

$ ansible-galaxy init nginx
$ ansible-galaxy init php

ansible-galaxyを実行するとテンプレートが作成されます。

nginx/
 ├ defaults
 │  └ main.yml
 ├ files
 ├ handlers
 │ └ main.yml
 ├ meta
 │ └ main.yml
 ├ README.md
 ├ tasks
 │ └ main.yml
 ├ test
 │ ├ inventory
 │ └ test.yml
 └ vars
  └ test.yml

各ディレクトリの意味はクラスメソッドさんのサイトでまとめられていました。
参考: サイト

nginxの設定

nginxのtaskディレクトリにあるmain.ymlファイルを修正します。

$ vi nginx/tasks/main.yml

yumからnginxをインストールして常時起動に設定してサービスを起動させます。

nginx/tasks/main.yml
# tasks file for nginx
   - name: Install NGINX.
     yum: name=nginx
   - command: chkconfig nginx on
   - command: service nginx restart

※デフォルトのリポジトリではnginxのバージョンが古い場合は↓にする

nginx/tasks/main.yml
   - name: Install NGINX.
     yum: name={{item}}
     with_items:
        - http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

   # enabled=0
   - replace: dest=/etc/yum.repos.d/{{item}} regexp="enabled *= *1" replace="enabled=0"
     with_items:
       - nginx.repo
   - yum: name=nginx
   - command: chkconfig nginx on
   - command: service nginx restart

実行してみる

$ ansible-playbook hoge.yml

PLAY [install and configure the web] *******************************************

TASK [setup] *******************************************************************
ok: [xxx]

TASK [nginx : Install NGINX.] **************************************************
ok: [xxx] => (item=[u'http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm'])

TASK [nginx : replace] *********************************************************
ok: [xxx] => (item=nginx.repo)

PLAY RECAP *********************************************************************
xxx             : ok=3    changed=0    unreachable=0    failed=0

ホスト側でインストールされたか確認

$nginx -v
nginx version: nginx/1.8.1

phpの設定

続いてphpの設定

$ vi php/tasks/main.yml
php/tasks/main.yml
# tasks file for php
  - name: Install PHP
    yum: name={{ item }} state=present enablerepo=epel
    with_items:
        - php
        - php-pgsql
        - php-devel
        - php-mbstring
        - php-gd
        - php-fpm
        - php-pecl-apc
#php setting
  - lineinfile: dest=/etc/php.ini
          regexp=';date.timezone ='
          line='date.timezone = Asia/Tokyo'
  - lineinfile: dest=/etc/php.ini
          regexp='session.gc_maxlifetime ='
          line='session.gc_maxlifetime = 1800'
  - lineinfile: dest=/etc/php.ini
          regexp='memory_limit ='
          line='memory_limit = 256M'
#php-fpm setting
  - lineinfile: "dest=/etc/php-fpm.d/www.conf
          state=present
          regexp='^user = apache'
          line='user = nginx'"
  - lineinfile: "dest=/etc/php-fpm.d/www.conf
          state=present
          regexp='^group = apache'
          line='group = nginx'"
  - command: chkconfig php-fpm on
  - command: service php-fpm restart
  - command: chown nginx /var/lib/php/session
  - command: chgrp nginx /var/lib/php/session
#apc setting
  - lineinfile: dest=/etc/php.d/apc.ini
          regexp='apc.mmap_file_mask='
          line='apc.mmap_file_mask=/dev/zero'
  - lineinfile: dest=/etc/php.d/apc.ini
          regexp='apc.shm_size='
          line='apc.shm_size=128M'

前段でyumからphpのモジュールをインストールしています。
次にphp.ini、php-fpm、apcの設定も連続して行えます。便利!

終わったらまた実行

$ ansible-playbook hoge.yml

ホストでphp.infoを作成して確認できればOK

17
13
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
17
13