The Pragmatic Ball boy

iOSを中心にやってる万年球拾いの老害エンジニアメモ

VagrantとAnsibleでFuelPHP開発環境構築(Ubuntu 14.04, nginx)

事前準備

  • Vagrantのインストール
  • Ansibleのインストール

ここではこれらのインストール方法は割愛

環境構築

とりあえず開発環境作りたい場合は、以下を実行し、しばらくすると環境が構築されます。

$ git clone https://github.com/yanamura3/FuelEnv
$ cd FuelEnv
$ vagrant up

localhost:3334をブラウザで開いてFuelPHPの画面がでればOKです。 FuelPHPでの開発はFuelEnv以下にfuelというフォルダができているのでそれを使えばよいです。



詳細

いじったところなどのメモ

Vagrant

VagrantFile

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty64"

  config.vm.provider "virtualbox" do |vb|
    # Customize the amount of memory on the VM:
    vb.memory = "2048"
  end

  config.vm.network "forwarded_port", guest: 80, host: 3334

  config.vm.synced_folder "./", "/var/www"

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "provisionings/site.yml"
    ansible.limit = "default"
    ansible.groups = {
      "vagrant" => ["default"],
    }
  end
end

config.vm.synced_folderでカレントディレクトリとVM側の/var/www以下を同期するようにしています。 これはあとでFuelPHPを/var/www以下にcloneしてきて、PHPStormで開発しやすくするためにこうしました。

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "provisionings/site.yml"

このように書くことで簡単にVagrantからansibleのplaybookを実行することができます。(これによってinvetoryの設定など面倒なことをVagrant側がやってくれます)

    ansible.limit = "default"
    ansible.groups = {
      "vagrant" => ["default"],
    }

この部分は、VagrantからAnsibleを実行されたときのみで条件をわけるためにこうしています。host名はVagrantではdefaultになっていました。

Ansible

Ansibleというか、Fuelを使う上での設定周りではまったところで、主にnginx周りです。 Ansibleに関してはansible-example(ansible/ansible-examples · GitHub)をベースにやるとやりやすかったです。

Why Ansible

サーバー側に何もいれなくてよいので、knife-soloでやるよりはこっちでいいかなくらいのノリです。

nginx

nginx.confのほうはsites-enableのconfigファイルを参照するように変更

/etc/nginx/nginx.conf

include /etc/nginx/sites-enable/*.conf

virtualhostの設定自体はsites-available以下において、sites-enable以下にそれに対するシンボリックリンクを置くようにしました。(不要なときにシンボリックリンクだけ消せばよいので

/etc/nginx/sites-available/virtual.conf

server {
       listen 80 default_server;
       server_name localhost;

       root /var/www/fuel/public;

       location / {
                try_files $uri /index.php?$uri&$args;
        }

        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

rootを/var/www/fuel/publicとpublicフォルダ以下になるように設定。

とくにlocationのところが重要で、localhost:xxxxでwelcomeページは開けるけど、localhost:xxxx/helloが開けない という場合はここの記述が間違っている可能性が高いです。

fastcgiを動かすために、fastcgi_passを127.0.0.1:9000にして、fpmの設定で9000番ポートをlistenするように修正しています。

/etc/php/fpm/pool.d/www.conf

listen = 9000

また普通にnginxをインストールするとバージョンが1.4とかになるので最新のstableをとってくるように変更しました

- name: get nginx signing key
  apt_key: url=http://nginx.org/keys/nginx_signing.key

- name: update nginx sources1
  apt_repository: repo="deb http://nginx.org/packages/ubuntu/ trusty nginx"

- name: update nginx sources2
  apt_repository: repo="deb-src http://nginx.org/packages/ubuntu/ trusty nginx"

- name: install nginx
  apt: pkg=nginx state=present update_cache=yes